-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyboard_mouse.c
270 lines (248 loc) · 6.32 KB
/
keyboard_mouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* This file is part of vis-o-mex.
*
* Copyright (C) 2010-2011 Greg Horn <ghorn@stanford.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* keyboard_mouse.c
* openGL callbacks for keyboard and mouse input
*/
#include <GL/gl.h>
#include <GL/glut.h>
#include <unistd.h> // needed to sleep
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "keyboard_mouse.h"
#include "camera_manager.h"
#include "visualizer.h"
/* ASCII code for the escape key. */
#define ESCAPE 27
mouse_rotation_t mouse_rotation = {
.x0 = 0,
.y0 = 0,
.button = 0,
};
/* The function called whenever a key is pressed. */
void
key_pressed(unsigned char key, int x, int y)
{
// supress unused variable warnings
(void)x;
(void)y;
/* avoid thrashing this call */
usleep(100);
camera_pose_t * camera_pose = get_camera_pose();
switch(key)
{
case 'w':
{
camera_pose->focus_x += 10.0f*cos(camera_pose->phi);
camera_pose->focus_y += 10.0f*sin(camera_pose->phi);
set_camera();
break;
}
case 's':
{
camera_pose->focus_x -= 10.0f*cos(camera_pose->phi);
camera_pose->focus_y -= 10.0f*sin(camera_pose->phi);
set_camera();
break;
}
case 'd':
{
camera_pose->focus_x -= 10.0f*sin(camera_pose->phi);
camera_pose->focus_y += 10.0f*cos(camera_pose->phi);
set_camera();
break;
}
case 'a':
{
camera_pose->focus_x += 10.0f*sin(camera_pose->phi);
camera_pose->focus_y -= 10.0f*cos(camera_pose->phi);
set_camera();
break;
}
case 'r':
{
camera_pose->theta += 10.0f*3.14159f/180.0f;
if (camera_pose->theta > 80.0f*3.14159f/180.0f)
camera_pose->theta = 80.0f*3.14159f/180.0f;
set_camera();
break;
}
case 'f':
{
camera_pose->theta -= 10.0f*3.14159f/180.0f;
if (camera_pose->theta < -80.0f*3.14159f/180.0f)
camera_pose->theta = -80.0f*3.14159f/180.0f;
set_camera();
break;
}
case 'q':
{
camera_pose->phi += 10.0f*3.14159f/180.0f;
set_camera();
break;
}
case 'e':
{
camera_pose->phi -= 10.0f*3.14159f/180.0f;
set_camera();
break;
}
case 'x':
{
camera_pose->rho -= 10.0f;
if (camera_pose->rho < 10.0f)
camera_pose->rho = 10.0f;
if (camera_pose->rho > 4000.0f)
camera_pose->rho = 4000.0f;
set_camera();
break;
}
case 'z':
{
camera_pose->rho += 10.0f;
set_camera();
break;
}
case ESCAPE:
{
/* If escape is pressed, kill everything. */
// glutDestroyWindow(window);
exit(0);
printf("ignoring ESCAPE key glutDestroyWindow call\n");
break;
}
default:
break;
}// switch(key)
}
void
mouse_clicked(int button, int state, int x, int y)
{
/* avoid thrashing this call - imitating key_pressed, still not sure why this is here */
usleep(100);
mouse_rotation.button = button;
mouse_rotation.x0 = x;
mouse_rotation.y0 = y;
camera_pose_t * camera_pose = get_camera_pose();
switch (mouse_rotation.button)
{
case GLUT_MIDDLE_BUTTON: // middle button - toggle draw camera focus
{
if (state == 0)
camera_pose->draw_focus = 1;
else
camera_pose->draw_focus = 0;
break;
}
case 3: // wheel up (zoom in)
{
if (state == 0){
camera_pose->rho -= 25.0f;
if (camera_pose->rho < 1.0f)
camera_pose->rho = 1.0f;
set_camera();
}
break;
}
case 4: // wheel down (zoom out)
{
if (state == 0){
camera_pose->rho += 25.0f;
set_camera();
}
break;
}
case GLUT_RIGHT_BUTTON: // pan
{
if (state == 0)
camera_pose->draw_focus = 1;
else
camera_pose->draw_focus = 0;
break;
}
default:
{
// printf("mouse button: %d\n", mouse_rotation.button);
break;
}
} // switch(mouse_rotation.button)
}
void
mouse_moved( int x, int y)
{
usleep(100);
camera_pose_t * camera_pose = get_camera_pose();
int window_width = get_window_width();
int window_height = get_window_height();
switch (mouse_rotation.button)
{
case GLUT_LEFT_BUTTON: // rotate
{
camera_pose->phi -= ((float)( mouse_rotation.x0 - x ))/((float)(window_width))*2.0*3.14;
camera_pose->theta -= ((float)( mouse_rotation.y0 - y ))/((float)(window_height))*2.0*3.14;
if (camera_pose->theta > 0.5*3.14)
camera_pose->theta = 0.5*3.14;
if (camera_pose->theta < -0.5*3.14f)
camera_pose->theta = -0.5*3.14f;
mouse_rotation.x0 = x;
mouse_rotation.y0 = y;
set_camera();
break;
}
case GLUT_RIGHT_BUTTON: // pan
{
camera_pose->focus_x += 500.0f*cos(camera_pose->phi)*((float)(y - mouse_rotation.y0))/((float)(window_height));
camera_pose->focus_y += 500.0f*sin(camera_pose->phi)*((float)(y - mouse_rotation.y0))/((float)(window_height));;
camera_pose->focus_x += 500.0f*sin(camera_pose->phi)*((float)(x - mouse_rotation.x0))/((float)(window_width));
camera_pose->focus_y -= 500.0f*cos(camera_pose->phi)*((float)(x - mouse_rotation.x0))/((float)(window_width));;
mouse_rotation.x0 = x;
mouse_rotation.y0 = y;
set_camera();
break;
}
/*
case GLUT_MIDDLE_BUTTON: // zoom
{
camera_pose->rho += 1000.0f*((float)(y - mouse_rotation.y0))/((float)(window_height));
if (camera_pose->rho < 1.0f)
camera_pose->rho = 1.0f;
if (camera_pose->rho > 2000.0f)
camera_pose->rho = 2000.0f;
mouse_rotation.x0 = x;
mouse_rotation.y0 = y;
set_camera();
break;
}
*/
case GLUT_MIDDLE_BUTTON: // move camera focus
{
camera_pose->focus_z += 500.0f*((float)(y - mouse_rotation.y0))/((float)(window_height));
if (camera_pose->focus_z > 0.0f)
camera_pose->focus_z = 0.0f;
mouse_rotation.x0 = x;
mouse_rotation.y0 = y;
set_camera();
break;
}
default:
break;
}// switch (mouse_rotation.button)
}