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. */
11 #include "Trackball.h"
17 float zoom_factor
= 1.0;
19 GenericTrackball
* TB
= new Trackball(Radius
);
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;
45 y
= -(2.0 * y
/ view_height
- 1.0);
46 if (y
< -1.0) y
= -1.0;
50 void load_trackball_transformation( void ) {
51 const matrix3x3_type
& Tmp
= TB
->get_current_rotation();
53 GLdouble glmatrix
[16];
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
);
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
);
87 void on_right_button_down (int x
, int y
) {
89 last_point
= vec3f(x
, y
, 0);
92 void on_right_button_up (int x
, int y
) {
96 void on_mouse_move (int x
, int y
) {
101 case ROTATE
: { // Left-mouse button is being held down
104 NormalizeCoordinates(Xnorm
, Ynorm
);
105 TB
->drag(Xnorm
, Ynorm
);
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;
134 // Save the location of the current point for the next movement.
136 last_point
= cur_point
;
140 void reset_view ( void ) {
142 Xcenter
=Ycenter
=Zcenter
=0.0; // rezoom and recenter as well
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
;
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
;