-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgui.c
426 lines (351 loc) · 10.9 KB
/
gui.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// a GUI using OpenGL with GLUT and GLUI
#include <stdio.h>
#include <iostream>
#include "GL/glew.h"
#include "GL/glut.h"
#include "GL/glui.h"
#include <math.h>
using namespace std;
int main_window;
// camera info
float eye[3];
float lookat[3];
// pointers for all of the GLUI controls
GLUI *glui;
GLUI_Rollout *object_rollout;
GLUI_RadioGroup *object_type_radio;
GLUI_Rotation *object_rotation;
GLUI_Translation *object_xz_trans;
GLUI_Translation *object_y_trans;
// other controls
GLUI_Rollout *anim_rollout;
GLUI_Button *action_button;
GLUI_Checkbox *draw_floor_check;
GLUI_Checkbox *draw_object_check;
// checkbox utilizes the callback
GLUI_Checkbox *use_depth_buffer;
// the user id's that we can use to identify control callbacks
#define CB_DEPTH_BUFFER 0
#define CB_ACTION_BUTTON 1
#define CB_RESET 2
// walking action variables
GLfloat step = 0;
GLfloat live_anim_speed = 3;
// live variables
// each of these are associated with a control in the interface
// when the control is modified, these variables are automatically updated
// 0=cube, 1=sphere, 2=torus
int live_object_type;
float live_object_rotation[16];
float live_object_xz_trans[2];
float live_object_y_trans;
int live_draw_floor;
int live_draw_object;
int live_use_depth_buffer;
// normalize a three-dimensional vector
void normalize(float v[3]){
float l = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
l = 1 / (float)sqrt(l);
v[0] *= l;
v[1] *= l;
v[2] *= l;
}
// cross-product of two three-dimensional vectors into a third vector
void crossproduct(float a[3], float b[3], float res[3]){
res[0] = (a[1] * b[2] - a[2] * b[1]);
res[1] = (a[2] * b[0] - a[0] * b[2]);
res[2] = (a[0] * b[1] - a[1] * b[0]);
}
// length of a three-dimensional vector
float length(float v[3]){
return (float)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}
// when program is idle
void myGlutIdle(void){
// make sure the main window is active
if(glutGetWindow() != main_window)
glutSetWindow(main_window);
// if you have moving objects, you can do that here
// just keep redrawing the scene over and over
glutPostRedisplay();
}
// mouse handling functions for the main window
// left mouse translates, middle zooms, right rotates
// keep track of which button is down and where the last position was
int cur_button = -1;
int last_x;
int last_y;
// when a mouse action is performed
void myGlutMouse(int button, int state, int x, int y){
if (state == GLUT_DOWN)
cur_button = button;
else{
if(button == cur_button)
cur_button = -1;
}
last_x = x;
last_y = y;
}
// when the mouse is moving around
void myGlutMotion(int x, int y){
// the change in mouse position
int dx = x-last_x;
int dy = y-last_y;
float scale, len, theta;
float neye[3], neye2[3];
float f[3], r[3], u[3];
switch(cur_button){
case GLUT_LEFT_BUTTON:
// translate
f[0] = lookat[0] - eye[0];
f[1] = lookat[1] - eye[1];
f[2] = lookat[2] - eye[2];
u[0] = 0;
u[1] = 1;
u[2] = 0;
// scale the change by how far away we are
scale = sqrt(length(f)) * 0.007;
crossproduct(f, u, r);
crossproduct(r, f, u);
normalize(r);
normalize(u);
eye[0] += -r[0]*dx*scale + u[0]*dy*scale;
eye[1] += -r[1]*dx*scale + u[1]*dy*scale;
eye[2] += -r[2]*dx*scale + u[2]*dy*scale;
lookat[0] += -r[0]*dx*scale + u[0]*dy*scale;
lookat[1] += -r[1]*dx*scale + u[1]*dy*scale;
lookat[2] += -r[2]*dx*scale + u[2]*dy*scale;
break;
case GLUT_MIDDLE_BUTTON:
// zoom
f[0] = lookat[0] - eye[0];
f[1] = lookat[1] - eye[1];
f[2] = lookat[2] - eye[2];
len = length(f);
normalize(f);
// scale the change by how far away we are
len -= sqrt(len)*dx*0.03;
eye[0] = lookat[0] - len*f[0];
eye[1] = lookat[1] - len*f[1];
eye[2] = lookat[2] - len*f[2];
// make sure the eye and lookat points are sufficiently far away
// push the lookat point forward if it is too close
if(len < 1){
printf("lookat move: %f\n", len);
lookat[0] = eye[0] + f[0];
lookat[1] = eye[1] + f[1];
lookat[2] = eye[2] + f[2];
}
break;
case GLUT_RIGHT_BUTTON:
// rotate
neye[0] = eye[0] - lookat[0];
neye[1] = eye[1] - lookat[1];
neye[2] = eye[2] - lookat[2];
// first rotate in the x/z plane
theta = -dx * 0.007;
neye2[0] = (float)cos(theta)*neye[0] + (float)sin(theta)*neye[2];
neye2[1] = neye[1];
neye2[2] =-(float)sin(theta)*neye[0] + (float)cos(theta)*neye[2];
// now rotate vertically
theta = -dy * 0.007;
f[0] = -neye2[0];
f[1] = -neye2[1];
f[2] = -neye2[2];
u[0] = 0;
u[1] = 1;
u[2] = 0;
crossproduct(f, u, r);
crossproduct(r, f, u);
len = length(f);
normalize(f);
normalize(u);
neye[0] = len * ((float)cos(theta)*f[0] + (float)sin(theta)*u[0]);
neye[1] = len * ((float)cos(theta)*f[1] + (float)sin(theta)*u[1]);
neye[2] = len * ((float)cos(theta)*f[2] + (float)sin(theta)*u[2]);
eye[0] = lookat[0] - neye[0];
eye[1] = lookat[1] - neye[1];
eye[2] = lookat[2] - neye[2];
break;
}
last_x = x;
last_y = y;
glutPostRedisplay();
}
// when a keyboard action is performed
void myGlutKeyboard(unsigned char key, int x, int y){
switch(key){
// quit
case 27:
case 'q':
case 'Q':
exit(0);
break;
}
glutPostRedisplay();
}
// when the window is resized
void myGlutReshape(int x, int y){
int tx, ty, tw, th;
GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th);
glViewport(tx, ty, tw, th);
glutPostRedisplay();
}
// drawing the scene
void myGlutDisplay(void){
// draw background & clear the scene
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1, 1000);
// camera transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], lookat[0], lookat[1], lookat[2], 0, 1, 0);
// now draw on the canvas
// drawing the floor
if(live_draw_floor){
glBegin(GL_TRIANGLE_FAN);
// note: color is state and only needs to be set once
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10, 0, -10);
glVertex3f( 10, 0, -10);
glVertex3f( 10, 0, 10);
glVertex3f(-10, 0, 10);
glEnd();
}
// drawing an object (cube, sphere, or torus)
if(live_draw_object){
glColor3f(0, 1, 1);
glPushMatrix();
glTranslatef(live_object_xz_trans[0], live_object_y_trans, -live_object_xz_trans[1]);
glMultMatrixf(live_object_rotation);
switch(live_object_type){
case 0:
glutSolidCube(2);
break;
case 1:
glutSolidSphere(2, 30, 30);
break;
case 2:
glutSolidTorus(0.5, 2, 30, 30);
break;
}
glPopMatrix();
}
// update the canvas with the next view
glutSwapBuffers();
}
// callback for certain GLUI controls
void glui_cb(int control){
switch(control){
case CB_DEPTH_BUFFER:
if (live_use_depth_buffer)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
break;
case CB_ACTION_BUTTON:
if(step < 0){
step = 0;
action_button->set_name("Stop");
}else{
step = -1;
action_button->set_name("Start");
}
break;
case CB_RESET:
// put your reset callback here
break;
}
glutPostRedisplay();
}
// initial setup
void init( void){
GLenum err = glewInit();
if(GLEW_OK != err){
//failed to initialize GLEW!
printf("failed !!!!!!!!!!!!");
}
printf("%s",glewGetString(GLEW_VERSION));
}
// main running method
int main(int argc, char* argv[]){
// set up GLUT
glutInit(&argc, argv);
// create the GLUT window with parameters
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(100,100);
main_window = glutCreateWindow("Sample Interface");
init();
//printf("came into main func");
// set callbacks for different actions
glutDisplayFunc(myGlutDisplay);
GLUI_Master.set_glutReshapeFunc(myGlutReshape);
GLUI_Master.set_glutIdleFunc(myGlutIdle);
GLUI_Master.set_glutKeyboardFunc(myGlutKeyboard);
GLUI_Master.set_glutMouseFunc(myGlutMouse);
glutMotionFunc(myGlutMotion);
// create the interface subwindow and add widgets
glui = GLUI_Master.create_glui_subwindow(main_window, GLUI_SUBWINDOW_LEFT);
// initialize live variables
live_object_type = 0;
live_object_xz_trans[0] = 0;
live_object_xz_trans[1] = 0;
live_object_y_trans = 0;
live_draw_floor = 1;
live_draw_object = 1;
live_use_depth_buffer = 1;
// quit button
glui->add_button("Quit", 0, (GLUI_Update_CB)exit);
// empty space
glui->add_statictext("");
// the object rollout
object_rollout = glui->add_rollout("Object");
// the radio buttons
object_type_radio = glui->add_radiogroup_to_panel(object_rollout, &live_object_type);
glui->add_radiobutton_to_group(object_type_radio, "cube");
glui->add_radiobutton_to_group(object_type_radio, "sphere");
glui->add_radiobutton_to_group(object_type_radio, "torus");
// rotation and translation controls
// we need an extra panel to keep things grouped properly
GLUI_Panel *transform_panel = glui->add_panel_to_panel(object_rollout, "", GLUI_PANEL_NONE);
object_rotation = glui->add_rotation_to_panel(transform_panel, "Rotation", live_object_rotation);
object_rotation->reset();
glui->add_column_to_panel(transform_panel, false);
object_xz_trans = glui->add_translation_to_panel(transform_panel, "Translate XZ", GLUI_TRANSLATION_XY, live_object_xz_trans);
glui->add_column_to_panel(transform_panel, false);
object_y_trans = glui->add_translation_to_panel(transform_panel, "Translate Y", GLUI_TRANSLATION_Y, &live_object_y_trans);
object_xz_trans->scale_factor = 0.1f;
object_y_trans->scale_factor = 0.1f;
glui->add_button_to_panel(object_rollout, "Reset Object Transform", CB_RESET, glui_cb);
// empty space
glui->add_statictext("");
// the walking / camera control
anim_rollout = glui->add_rollout("Animation");
action_button = glui->add_button_to_panel(anim_rollout, "Stop", CB_ACTION_BUTTON, glui_cb);
GLUI_Spinner *spin_s = glui->add_spinner_to_panel(anim_rollout, "Speed", GLUI_SPINNER_FLOAT, &live_anim_speed);
spin_s->set_float_limits(0.1, 10.0);
// our checkbox options for deciding what to draw
glui->add_checkbox("Draw Floor", &live_draw_floor);
glui->add_checkbox("Draw Object", &live_draw_object);
// empty space
glui->add_statictext("");glui->add_checkbox("Use Depth Buffer", &live_use_depth_buffer, CB_DEPTH_BUFFER, glui_cb);
glui->set_main_gfx_window(main_window);
// initialize the camera
eye[0] = 0;
eye[1] = 4;
eye[2] = 10;
lookat[0] = 0;
lookat[1] = 0;
lookat[2] = 0;
// activate GL modes
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
// give control over to GLUT for continuous drawing
glutMainLoop();
return 0;
}