Preliminary, but functional, autotoolsification
[proto.git] / src / shared / Trackball.cpp
blobc9de5f69ad74ba51ee528b31b2dc95c1873f6b4f
1 /* Interface between the trackball classes and Proto simulator
2 Copyright (C) 2005-2008, Jonathan Bachrach, Jacob Beal, and contributors
3 listed in the AUTHORS file in the MIT Proto distribution's top directory.
5 This file is part of MIT Proto, and is distributed under the terms of
6 the GNU General Public License, with a linking exception, as described
7 in the file LICENSE in the MIT Proto distribution's top directory. */
9 #include "config.h"
10 #include <cmath>
11 #include "Trackball.h"
13 double Radius = 0.8;
14 double Xcenter = 0.0;
15 double Ycenter = 0.0;
16 double Zcenter = 0.0;
17 float zoom_factor = 1.0;
19 GenericTrackball* TB = new Trackball(Radius);
21 #define NONE 0
22 #define ZOOM 1
23 #define ROTATE 2
25 typedef struct { float x; float y; float z; } Vec3f;
27 static Vec3f last_point = { 0.0, 0.0, 0.0 };
28 static int movement = NONE;
30 static const float m_ROTSCALE = 90.0;
31 static const float m_ZOOMSCALE = 0.008;
32 //static const float m_MOVESCALE = 0.0001; // No longer used
34 static Vec3f vec3f(float xx, float yy, float zz) {
35 Vec3f v; v.x=xx; v.y=yy; v.z=zz; return v;
38 float view_width, view_height;
40 void NormalizeCoordinates(double& x, double& y) {
41 x = 2.0 * x / view_width - 1.0;
42 if (x < -1.0) x = -1.0;
43 if (x > 1.0) x = 1.0;
45 y = -(2.0 * y / view_height - 1.0);
46 if (y < -1.0) y = -1.0;
47 if (y > 1.0) y = 1.0;
50 void load_trackball_transformation( void ) {
51 const matrix3x3_type& Tmp = TB->get_current_rotation();
52 int glindex = 0;
53 GLdouble glmatrix[16];
55 glLoadIdentity();
57 for (unsigned int col = 0; col < 4; ++col) {
58 for (unsigned int row = 0; row < 4; ++row) {
59 glmatrix[glindex++] = Tmp[row][col];
62 glLoadMatrixd(glmatrix);
66 // Loads the non-rotational parts of the trackball positioning
67 // Added by jsmb 5/13/06
68 void load_trackball_zoom_translate( void ) {
69 glScalef( zoom_factor, zoom_factor, zoom_factor );
70 glTranslatef( Xcenter, Ycenter, 0.0 );
73 void on_left_button_down (int x, int y) {
74 double Xnorm = x; double Ynorm = y;
75 NormalizeCoordinates(Xnorm, Ynorm);
76 movement = ROTATE;
77 TB->begin_drag(Xnorm, Ynorm);
80 void on_left_button_up (int x, int y) {
81 double Xnorm = x; double Ynorm = y;
82 NormalizeCoordinates(Xnorm, Ynorm);
83 TB->end_drag(Xnorm, Ynorm);
84 movement = NONE;
87 void on_right_button_down (int x, int y) {
88 movement = ZOOM;
89 last_point = vec3f(x, y, 0);
92 void on_right_button_up (int x, int y) {
93 movement = NONE;
96 void on_mouse_move (int x, int y) {
97 float pixel_diff;
98 Vec3f cur_point;
100 switch (movement) {
101 case ROTATE : { // Left-mouse button is being held down
102 double Xnorm = x;
103 double Ynorm = y;
104 NormalizeCoordinates(Xnorm, Ynorm);
105 TB->drag(Xnorm, Ynorm);
106 break;
108 case ZOOM : { // Right-mouse button is being held down
110 // Zoom into or away from the scene based upon how far the mouse moved in the x-direction.
111 // This implementation does this by scaling the eye-space.
112 // This should be the first operation performed by the GL_PROJECTION matrix.
113 // 1. Calculate the signed distance
114 // a. movement to the left is negative (zoom out).
115 // b. movement to the right is positive (zoom in).
116 // 2. Calculate a scale factor for the scene s = 1 + a*dx
117 // 3. Call glScalef to have the scale be the first transformation.
119 // post("ZOOM %d %f %f\n", x, last_point.x, zoom_factor);
120 pixel_diff = x - last_point.x;
121 zoom_factor *= 1.0 + pixel_diff * m_ZOOMSCALE;
123 // Set the current point, so the lastPoint will be saved properly below.
125 //glMatrixMode( GL_PROJECTION );
126 //glScalef( zoom_factor, zoom_factor, zoom_factor );
127 //glMatrixMode( GL_MODELVIEW );
128 cur_point.x = (float) x; cur_point.y = (float) y; cur_point.z = (float)0;
129 break;
131 default: return;
134 // Save the location of the current point for the next movement.
136 last_point = cur_point;
140 void reset_view ( void ) {
141 TB->reset();
142 Xcenter=Ycenter=Zcenter=0.0; // rezoom and recenter as well
143 zoom_factor=1.0;
146 static Vec3f last_joy_point = { 0.0, 0.0, 0.0 };
148 void on_joy_move (float x, float y) {
149 float pixel_diff_x, move_amount_x;
150 float pixel_diff_y, move_amount_y;
151 Vec3f cur_joy_point;
153 move_amount_x = x / zoom_factor;
154 move_amount_y = -y / zoom_factor;
155 //glMatrixMode( GL_PROJECTION );
156 //glTranslatef( move_amount_x, move_amount_y, 0.0 );
157 Xcenter += move_amount_x; Ycenter += move_amount_y;
158 //glMatrixMode( GL_MODELVIEW );
159 last_joy_point = cur_joy_point;
162 void on_key_zoom (float amt) {
163 zoom_factor *= 1.0 + amt * m_ZOOMSCALE;