-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwinmain.cpp
496 lines (349 loc) · 12.1 KB
/
winmain.cpp
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* Tutorial UI for progressive Mesh Reduction via Edge Collapse
Antonio Rueda Toicen, 2012
demo based on paper and code by Stan Melax
http://dev.gameres.com/Program/Visual/3D/PolygonReduction.pdf
*/
#include <string>
#include <GL/glui.h>
#include <vector> // std vector
#include "Vector.h" // 3D point with overloaded cross and dot products
#include <iostream>
/*fps variables*/
int frame=0,time,timebase=0;
double fps;
using namespace std;
extern void InitModel(string fileName);
extern void RenderModel();
extern int render_num;
extern int initial_num;
float xy_aspect;
int last_x, last_y;
float rotationX = 0.0f, rotationY=0.0f;
extern int render_num;
/*live variables passed into GLUI*/
int main_window;
int current_off;
int wireframe = 1;
int light0_enabled = 1;
int light1_enabled = 1;
float light0_intensity = 1.0;
float light1_intensity = .4;
float scale = 1.0;
float view_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
float obj_pos[] = { 0.0, 0.0, 0.0 };
/** Pointers to the windows and some of the controls we'll create **/
GLUI *glui, *glui2;
GLUI_Spinner *light0_spinner, *light1_spinner;
GLUI_RadioGroup *radio;
GLUI_Panel *obj_panel;
/********** User IDs for callbacks ********/
#define LIGHT0_ENABLED_ID 200
#define LIGHT1_ENABLED_ID 201
#define LIGHT0_INTENSITY_ID 250
#define LIGHT1_INTENSITY_ID 260
#define ENABLE_ID 300
#define DISABLE_ID 301
#define SHOW_ID 302
#define HIDE_ID 303
/********** Miscellaneous global variables **********/
GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
GLfloat light0_position[] = {.5f, .5f, 1.0f, 0.0f};
GLfloat light1_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
GLfloat light1_diffuse[] = {.9f, .6f, 0.0f, 1.0f};
GLfloat light1_position[] = {-1.0f, -1.0f, 1.0f, 0.0f};
GLfloat lights_rotation[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
/******************
GLUI CONTROL CALLBACK
*******************/
void control_cb (int control)
{
if (control == LIGHT0_ENABLED_ID){
if(light0_enabled){
glEnable(GL_LIGHT0);
light0_spinner->enable();
}
else {
glDisable(GL_LIGHT0);
light0_spinner->disable();
}
}
else if( control ==LIGHT1_ENABLED_ID ){
if( light1_enabled ){
glEnable(GL_LIGHT1);
light1_spinner->enable();
}
else {
glDisable(GL_LIGHT1);
light1_spinner->disable();
}
}
else if ( control == LIGHT0_INTENSITY_ID ) {
float v[] = {
light0_diffuse[0], light0_diffuse[1],
light0_diffuse[2], light0_diffuse[3] };
v[0] *= light0_intensity;
v[1] *= light0_intensity;
v[2] *= light0_intensity;
glLightfv(GL_LIGHT0, GL_DIFFUSE, v );
}
else if ( control == LIGHT1_INTENSITY_ID ) {
float v[] = {
light1_diffuse[0], light1_diffuse[1],
light1_diffuse[2], light1_diffuse[3] };
v[0] *= light1_intensity;
v[1] *= light1_intensity;
v[2] *= light1_intensity;
glLightfv(GL_LIGHT1, GL_DIFFUSE, v );
}
else if ( control == ENABLE_ID )
{
glui2->enable();
}
else if ( control == DISABLE_ID )
{
glui2->disable();
}
else if ( control == SHOW_ID )
{
glui2->show();
}
else if ( control == HIDE_ID )
{
glui2->hide();
}
}
void myGlutKeyboard(unsigned char Key, int x, int y)
{
switch(Key)
{
case 27:
case 'q':
exit(0);
break;
case '-':
if (render_num > 0){
render_num--;
}
break;
case '+':
if(render_num < initial_num){
render_num++;
}
break;
};
glutPostRedisplay();
}
/***************************************** myGlutMenu() ***********/
void myGlutMenu( int value )
{
myGlutKeyboard( value, 0, 0 );
}
/***************************************** myGlutIdle() ***********/
void myGlutIdle( void )
{
/* According to the GLUT specification, the current window is
undefined during an idle callback. So we need to explicitly change
it if necessary */
if ( glutGetWindow() != main_window ) {
glutSetWindow (main_window);
}
glutPostRedisplay();
}
/***************************************** myGlutMouse() **********/
void myGlutMouse(int button, int button_state, int x, int y ) // adaptar para el drag con quaternion
{
}
/***************************************** myGlutMotion() **********/
void myGlutMotion(int x, int y )
{
glutPostRedisplay();
}
/**************************************** myGlutReshape() *************/
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 );
xy_aspect = (float)tw / (float)th;
glutPostRedisplay();
}
void myGlutDisplay( void )
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // paint window black
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 15.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glMultMatrixf( lights_rotation );
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLoadIdentity();
glTranslatef( 0.0, 0.0, -2.6f );
glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
glMultMatrixf( view_rotate );
glScalef( scale, scale, scale );
RenderModel();
glEnable( GL_LIGHTING );
char window_title[60];
frame++;
time=glutGet(GLUT_ELAPSED_TIME);
if ((time - timebase) > 1000) {
fps = (frame*1000.0/(time-timebase));
// printf("fps: %4.2f \n", fps);
sprintf(window_title, "Progressive Mesh Reduction - FPS: %f", fps);
glutSetWindowTitle(window_title);
timebase = time;
frame = 0;
}
glutSwapBuffers();
}
int main(int argc, char* argv[]){
int selection;
for (;;) {
cout<<"Please indicate the .off's model index to load"<<endl;
cout<<"1- rabbit"<<endl;
cout<<"2- cube"<<endl;
cout<<"3- eagle"<<endl;
cout<<"4- jacky"<<endl;
cout<<"5- monster"<<endl;
cout<<"6- nemesis"<<endl;
cout<<"7- parakeet"<<endl;
cout<<"8- sphere"<<endl;
cout<<"9- tiger"<<endl;
cout<<"10- turtle"<<endl;
cout<<"11- woman"<<endl;
cin>>selection;
if(selection>0 && selection<12)
break;
}
string selection_array[] = {"", "rabbit.off",
"cube.off",
"eagle.off",
"jacky.off",
"monster.off",
"nemesis.off",
"parakeet.off",
"sphere.off",
"tiger.off",
"turtle.off",
"woman.off"
};
InitModel(selection_array[selection]);
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition( 150, 150 );
glutInitWindowSize( 900, 700 );
main_window = glutCreateWindow( "HW 1 CG III - Antonio Rueda" );
glutDisplayFunc( myGlutDisplay );
GLUI_Master.set_glutReshapeFunc( myGlutReshape );
GLUI_Master.set_glutKeyboardFunc( myGlutKeyboard );
GLUI_Master.set_glutSpecialFunc( NULL );
GLUI_Master.set_glutMouseFunc( myGlutMouse );
glutMotionFunc( myGlutMotion );
/****************************************/
/* Set up OpenGL lights */
/****************************************/
glEnable(GL_LIGHTING);
glEnable( GL_NORMALIZE );
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
/****************************************/
/* Enable z-buferring */
/****************************************/
glEnable(GL_DEPTH_TEST);
/****************************************/
/* Here's the GLUI code */
/****************************************/
printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );
/*** Create the side subwindow ***/
glui = GLUI_Master.create_glui_subwindow( main_window,
GLUI_SUBWINDOW_RIGHT );
obj_panel = new GLUI_Rollout(glui, "Properties", true );
/***** Control for object params *****/
new GLUI_Checkbox( obj_panel, "Wireframe", &wireframe, 1, control_cb );
GLUI_Spinner *spinner =
new GLUI_Spinner( obj_panel, "Vertices:", &render_num);
spinner->set_int_limits(0, initial_num );
spinner->set_alignment (GLUI_ALIGN_RIGHT);
GLUI_Spinner *scale_spinner =
new GLUI_Spinner( obj_panel, "Scale:", &scale);
scale_spinner->set_float_limits( .2f, 4.0 );
scale_spinner->set_alignment (GLUI_ALIGN_RIGHT);
/******** Add some controls for lights ********/
GLUI_Rollout *roll_lights = new GLUI_Rollout(glui, "Lights", true );
GLUI_Panel *light0 = new GLUI_Panel( roll_lights, "Light 1" );
GLUI_Panel *light1 = new GLUI_Panel( roll_lights, "Light 2" );
new GLUI_Checkbox( light0, "Enabled", &light0_enabled,
LIGHT0_ENABLED_ID, control_cb );
light0_spinner =
new GLUI_Spinner( light0, "Intensity:",
&light0_intensity, LIGHT0_INTENSITY_ID,
control_cb );
light0_spinner->set_float_limits( 0.0, 1.0 );
GLUI_Scrollbar *sb;
sb = new GLUI_Scrollbar( light0, "Red",GLUI_SCROLL_HORIZONTAL,
&light0_diffuse[0],LIGHT0_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
sb = new GLUI_Scrollbar( light0, "Green",GLUI_SCROLL_HORIZONTAL,
&light0_diffuse[1],LIGHT0_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
sb = new GLUI_Scrollbar( light0, "Blue",GLUI_SCROLL_HORIZONTAL,
&light0_diffuse[2],LIGHT0_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
new GLUI_Checkbox( light1, "Enabled", &light1_enabled,
LIGHT1_ENABLED_ID, control_cb );
light1_spinner =
new GLUI_Spinner( light1, "Intensity:",
&light1_intensity, LIGHT1_INTENSITY_ID,
control_cb );
light1_spinner->set_float_limits( 0.0, 1.0 );
sb = new GLUI_Scrollbar( light1, "Red",GLUI_SCROLL_HORIZONTAL,
&light1_diffuse[0],LIGHT1_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
sb = new GLUI_Scrollbar( light1, "Green",GLUI_SCROLL_HORIZONTAL,
&light1_diffuse[1],LIGHT1_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
sb = new GLUI_Scrollbar( light1, "Blue",GLUI_SCROLL_HORIZONTAL,
&light1_diffuse[2],LIGHT1_INTENSITY_ID,control_cb);
sb->set_float_limits(0,1);
new GLUI_StaticText( glui, "" );
//new GLUI_Button( glui, "Show Move Controls", SHOW_ID, control_cb );
new GLUI_StaticText( glui, "" ); // blank UI row
/****** A 'quit' button *****/
new GLUI_Button( glui, " Quit ", 0,(GLUI_Update_CB)exit );
/**** Link windows to GLUI, and register idle callback ******/
glui->set_main_gfx_window( main_window );
/*** Create the bottom subwindow ***/
glui2 = GLUI_Master.create_glui_subwindow ( main_window,
GLUI_SUBWINDOW_BOTTOM );
glui2->set_main_gfx_window ( main_window );
GLUI_Rotation *view_rot = new GLUI_Rotation(glui2, "Object", view_rotate );
view_rot->set_spin( 1.0 );
new GLUI_Column( glui2, false );
new GLUI_Column( glui2, false );
GLUI_Rotation *lights_rot = new GLUI_Rotation(glui2, "Light 1", lights_rotation );
lights_rot->set_spin( .82 );
new GLUI_Column( glui2, false );
GLUI_Translation *trans_xy =
new GLUI_Translation(glui2, "Object XY", GLUI_TRANSLATION_XY, obj_pos );
trans_xy->set_speed( .005 );
new GLUI_Column( glui2, false );
GLUI_Translation *trans_z =
new GLUI_Translation( glui2, "Object Z", GLUI_TRANSLATION_Z, &obj_pos[2] );
trans_z->set_speed( .005 );
#if 0
/**** We register the idle callback with GLUI, *not* with GLUT ****/
GLUI_Master.set_glutIdleFunc( myGlutIdle );
#endif
glutMainLoop();
return 0;
}