1 /****************************************************************************
2 
3   example5.cpp
4 
5   A GLUI program demonstrating subwindows, rotation controls,
6 	translation controls, and listboxes
7 
8   -----------------------------------------------------------------------
9 
10   7/10/98 Paul Rademacher (rademach@cs.unc.edu)
11 
12 ****************************************************************************/
13 
14 #include <string.h>
15 #include <GL/glui.h>
16 
17 float xy_aspect;
18 int   last_x, last_y;
19 float rotationX = 0.0, rotationY = 0.0;
20 
21 /** These are the live variables passed into GLUI ***/
22 int   wireframe = 0;
23 int   obj_type = 1;
24 int   segments = 8;
25 int   segments2 = 8;
26 int   light0_enabled = 1;
27 int   light1_enabled = 1;
28 float light0_intensity = 1.0;
29 float light1_intensity = .4;
30 int   main_window;
31 float scale = 1.0;
32 int   show_sphere=1;
33 int   show_torus=1;
34 int   show_axes = 1;
35 int   show_text = 1;
36 float sphere_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
37 float torus_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
38 float view_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
39 float obj_pos[] = { 0.0, 0.0, 0.0 };
40 char *string_list[] = { "Hello World!", "Foo", "Testing...", "Bounding box: on" };
41 int   curr_string = 0;
42 
43 /** Pointers to the windows and some of the controls we'll create **/
44 GLUI *glui, *glui2;
45 GLUI_Spinner    *light0_spinner, *light1_spinner;
46 GLUI_RadioGroup *radio;
47 GLUI_Panel      *obj_panel;
48 
49 /********** User IDs for callbacks ********/
50 #define LIGHT0_ENABLED_ID    200
51 #define LIGHT1_ENABLED_ID    201
52 #define LIGHT0_INTENSITY_ID  250
53 #define LIGHT1_INTENSITY_ID  260
54 #define ENABLE_ID            300
55 #define DISABLE_ID           301
56 #define SHOW_ID              302
57 #define HIDE_ID              303
58 
59 
60 /********** Miscellaneous global variables **********/
61 
62 GLfloat light0_ambient[] =  {0.1f, 0.1f, 0.3f, 1.0f};
63 GLfloat light0_diffuse[] =  {.6f, .6f, 1.0f, 1.0f};
64 GLfloat light0_position[] = {.5f, .5f, 1.0f, 0.0f};
65 
66 GLfloat light1_ambient[] =  {0.1f, 0.1f, 0.3f, 1.0f};
67 GLfloat light1_diffuse[] =  {.9f, .6f, 0.0f, 1.0f};
68 GLfloat light1_position[] = {-1.0f, -1.0f, 1.0f, 0.0f};
69 
70 GLfloat lights_rotation[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
71 
72 /**************************************** control_cb() *******************/
73 /* GLUI control callback                                                 */
74 
control_cb(int control)75 void control_cb( int control )
76 {
77   if ( control == LIGHT0_ENABLED_ID ) {
78     if ( light0_enabled ) {
79       glEnable( GL_LIGHT0 );
80       light0_spinner->enable();
81     }
82     else {
83       glDisable( GL_LIGHT0 );
84       light0_spinner->disable();
85     }
86   }
87   else if ( control == LIGHT1_ENABLED_ID ) {
88     if ( light1_enabled ) {
89       glEnable( GL_LIGHT1 );
90       light1_spinner->enable();
91     }
92     else {
93       glDisable( GL_LIGHT1 );
94       light1_spinner->disable();
95     }
96   }
97   else if ( control == LIGHT0_INTENSITY_ID ) {
98     float v[] = {
99       light0_diffuse[0],  light0_diffuse[1],
100       light0_diffuse[2],  light0_diffuse[3] };
101 
102     v[0] *= light0_intensity;
103     v[1] *= light0_intensity;
104     v[2] *= light0_intensity;
105 
106     glLightfv(GL_LIGHT0, GL_DIFFUSE, v );
107   }
108   else if ( control == LIGHT1_INTENSITY_ID ) {
109     float v[] = {
110       light1_diffuse[0],  light1_diffuse[1],
111       light1_diffuse[2],  light1_diffuse[3] };
112 
113     v[0] *= light1_intensity;
114     v[1] *= light1_intensity;
115     v[2] *= light1_intensity;
116 
117     glLightfv(GL_LIGHT1, GL_DIFFUSE, v );
118   }
119   else if ( control == ENABLE_ID )
120   {
121     glui2->enable();
122   }
123   else if ( control == DISABLE_ID )
124   {
125     glui2->disable();
126   }
127   else if ( control == SHOW_ID )
128   {
129     glui2->show();
130   }
131   else if ( control == HIDE_ID )
132   {
133     glui2->hide();
134   }
135 }
136 
137 /**************************************** myGlutKeyboard() **********/
138 
myGlutKeyboard(unsigned char Key,int x,int y)139 void myGlutKeyboard(unsigned char Key, int x, int y)
140 {
141   switch(Key)
142   {
143   case 27:
144   case 'q':
145     exit(0);
146     break;
147   };
148 
149   glutPostRedisplay();
150 }
151 
152 
153 /***************************************** myGlutMenu() ***********/
154 
myGlutMenu(int value)155 void myGlutMenu( int value )
156 {
157   myGlutKeyboard( value, 0, 0 );
158 }
159 
160 
161 /***************************************** myGlutIdle() ***********/
162 
myGlutIdle(void)163 void myGlutIdle( void )
164 {
165   /* According to the GLUT specification, the current window is
166      undefined during an idle callback.  So we need to explicitly change
167      it if necessary */
168   if ( glutGetWindow() != main_window )
169     glutSetWindow(main_window);
170 
171   /*  GLUI_Master.sync_live_all();  -- not needed - nothing to sync in this
172                                        application  */
173 
174   glutPostRedisplay();
175 }
176 
177 /***************************************** myGlutMouse() **********/
178 
myGlutMouse(int button,int button_state,int x,int y)179 void myGlutMouse(int button, int button_state, int x, int y )
180 {
181 }
182 
183 
184 /***************************************** myGlutMotion() **********/
185 
myGlutMotion(int x,int y)186 void myGlutMotion(int x, int y )
187 {
188   glutPostRedisplay();
189 }
190 
191 /**************************************** myGlutReshape() *************/
192 
myGlutReshape(int x,int y)193 void myGlutReshape( int x, int y )
194 {
195   int tx, ty, tw, th;
196   GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
197   glViewport( tx, ty, tw, th );
198 
199   xy_aspect = (float)tw / (float)th;
200 
201   glutPostRedisplay();
202 }
203 
204 
205 /************************************************** draw_axes() **********/
206 /* Disables lighting, then draws RGB axes                                */
207 
draw_axes(float scale)208 void draw_axes( float scale )
209 {
210   glDisable( GL_LIGHTING );
211 
212   glPushMatrix();
213   glScalef( scale, scale, scale );
214 
215   glBegin( GL_LINES );
216 
217   glColor3f( 1.0, 0.0, 0.0 );
218   glVertex3f( .8f, 0.05f, 0.0 );  glVertex3f( 1.0, 0.25f, 0.0 ); /* Letter X */
219   glVertex3f( 0.8f, .25f, 0.0 );  glVertex3f( 1.0, 0.05f, 0.0 );
220   glVertex3f( 0.0, 0.0, 0.0 );  glVertex3f( 1.0, 0.0, 0.0 ); /* X axis      */
221 
222   glColor3f( 0.0, 1.0, 0.0 );
223   glVertex3f( 0.0, 0.0, 0.0 );  glVertex3f( 0.0, 1.0, 0.0 ); /* Y axis      */
224 
225   glColor3f( 0.0, 0.0, 1.0 );
226   glVertex3f( 0.0, 0.0, 0.0 );  glVertex3f( 0.0, 0.0, 1.0 ); /* Z axis    */
227   glEnd();
228 
229   glPopMatrix();
230 
231   glEnable( GL_LIGHTING );
232 }
233 
234 
235 /***************************************** myGlutDisplay() *****************/
236 
myGlutDisplay(void)237 void myGlutDisplay( void )
238 {
239   glClearColor( .9f, .9f, .9f, 1.0f );
240   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
241 
242   glMatrixMode( GL_PROJECTION );
243   glLoadIdentity();
244   glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 15.0 );
245 
246   glMatrixMode( GL_MODELVIEW );
247 
248   glLoadIdentity();
249   glMultMatrixf( lights_rotation );
250   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
251 
252   glLoadIdentity();
253   glTranslatef( 0.0, 0.0, -2.6f );
254   glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
255   glMultMatrixf( view_rotate );
256 
257   glScalef( scale, scale, scale );
258 
259   /*** Now we render object, using the variables 'obj_type', 'segments', and
260     'wireframe'.  These are _live_ variables, which are transparently
261     updated by GLUI ***/
262 
263   glPushMatrix();
264   glTranslatef( -.5, 0.0, 0.0 );
265   glMultMatrixf( sphere_rotate );
266   if ( wireframe && show_sphere)
267     glutWireSphere( .4, segments, segments );
268   else if ( show_sphere )
269     glutSolidSphere( .4, segments, segments );
270   if ( show_axes )
271     draw_axes(.52f);
272   glPopMatrix();
273 
274   glPushMatrix();
275   glTranslatef( .5, 0.0, 0.0 );
276   glMultMatrixf( torus_rotate );
277   if ( wireframe && show_torus )
278     glutWireTorus( .15,.3,16,segments );
279   else if ( show_torus )
280     glutSolidTorus( .15,.3,16,segments );
281   if ( show_axes )
282     draw_axes(.52f);
283   glPopMatrix();
284 
285   if ( show_text )
286   {
287     glDisable( GL_LIGHTING );  /* Disable lighting while we render text */
288     glMatrixMode( GL_PROJECTION );
289     glLoadIdentity();
290     gluOrtho2D( 0.0, 100.0, 0.0, 100.0  );
291     glMatrixMode( GL_MODELVIEW );
292     glLoadIdentity();
293     glColor3ub( 0, 0, 0 );
294     glRasterPos2i( 10, 10 );
295 
296     /*  printf( "text: %s\n", text );              */
297 
298     /*** Render the live character array 'text' ***/
299     int i;
300     for( i=0; i<(int)strlen( string_list[curr_string] ); i++ )
301       glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, string_list[curr_string][i] );
302   }
303 
304   glEnable( GL_LIGHTING );
305 
306 
307   glutSwapBuffers();
308 }
309 
310 
311 /**************************************** main() ********************/
312 
main(int argc,char * argv[])313 int main(int argc, char* argv[])
314 {
315   /****************************************/
316   /*   Initialize GLUT and create window  */
317   /****************************************/
318 
319   glutInit(&argc, argv);
320   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
321   glutInitWindowPosition( 50, 50 );
322   glutInitWindowSize( 800, 600 );
323 
324   main_window = glutCreateWindow( "GLUI Example 5" );
325   glutDisplayFunc( myGlutDisplay );
326   GLUI_Master.set_glutReshapeFunc( myGlutReshape );
327   GLUI_Master.set_glutKeyboardFunc( myGlutKeyboard );
328   GLUI_Master.set_glutSpecialFunc( NULL );
329   GLUI_Master.set_glutMouseFunc( myGlutMouse );
330   glutMotionFunc( myGlutMotion );
331 
332   /****************************************/
333   /*       Set up OpenGL lights           */
334   /****************************************/
335 
336   glEnable(GL_LIGHTING);
337   glEnable( GL_NORMALIZE );
338 
339   glEnable(GL_LIGHT0);
340   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
341   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
342   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
343 
344   glEnable(GL_LIGHT1);
345   glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
346   glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
347   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
348 
349   /****************************************/
350   /*          Enable z-buferring          */
351   /****************************************/
352 
353   glEnable(GL_DEPTH_TEST);
354 
355   /****************************************/
356   /*         Here's the GLUI code         */
357   /****************************************/
358 
359   printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );
360 
361   /*** Create the side subwindow ***/
362   glui = GLUI_Master.create_glui_subwindow( main_window,
363 					    GLUI_SUBWINDOW_RIGHT );
364 
365   obj_panel = new GLUI_Rollout(glui, "Properties", false );
366 
367   /***** Control for object params *****/
368 
369   new GLUI_Checkbox( obj_panel, "Wireframe", &wireframe, 1, control_cb );
370   GLUI_Spinner *spinner =
371     new GLUI_Spinner( obj_panel, "Segments:", &segments);
372   spinner->set_int_limits( 3, 60 );
373   spinner->set_alignment( GLUI_ALIGN_RIGHT );
374 
375   GLUI_Spinner *scale_spinner =
376     new GLUI_Spinner( obj_panel, "Scale:", &scale);
377   scale_spinner->set_float_limits( .2f, 4.0 );
378   scale_spinner->set_alignment( GLUI_ALIGN_RIGHT );
379 
380 
381   /******** Add some controls for lights ********/
382 
383   GLUI_Rollout *roll_lights = new GLUI_Rollout(glui, "Lights", false );
384 
385   GLUI_Panel *light0 = new GLUI_Panel( roll_lights, "Light 1" );
386   GLUI_Panel *light1 = new GLUI_Panel( roll_lights, "Light 2" );
387 
388   new GLUI_Checkbox( light0, "Enabled", &light0_enabled,
389                      LIGHT0_ENABLED_ID, control_cb );
390   light0_spinner =
391     new GLUI_Spinner( light0, "Intensity:",
392                       &light0_intensity, LIGHT0_INTENSITY_ID,
393                       control_cb );
394   light0_spinner->set_float_limits( 0.0, 1.0 );
395   GLUI_Scrollbar *sb;
396   sb = new GLUI_Scrollbar( light0, "Red",GLUI_SCROLL_HORIZONTAL,
397                            &light0_diffuse[0],LIGHT0_INTENSITY_ID,control_cb);
398   sb->set_float_limits(0,1);
399   sb = new GLUI_Scrollbar( light0, "Green",GLUI_SCROLL_HORIZONTAL,
400                            &light0_diffuse[1],LIGHT0_INTENSITY_ID,control_cb);
401   sb->set_float_limits(0,1);
402   sb = new GLUI_Scrollbar( light0, "Blue",GLUI_SCROLL_HORIZONTAL,
403                            &light0_diffuse[2],LIGHT0_INTENSITY_ID,control_cb);
404   sb->set_float_limits(0,1);
405   new GLUI_Checkbox( light1, "Enabled", &light1_enabled,
406                      LIGHT1_ENABLED_ID, control_cb );
407   light1_spinner =
408     new GLUI_Spinner( light1, "Intensity:",
409                       &light1_intensity, LIGHT1_INTENSITY_ID,
410                       control_cb );
411   light1_spinner->set_float_limits( 0.0, 1.0 );
412   sb = new GLUI_Scrollbar( light1, "Red",GLUI_SCROLL_HORIZONTAL,
413                            &light1_diffuse[0],LIGHT1_INTENSITY_ID,control_cb);
414   sb->set_float_limits(0,1);
415   sb = new GLUI_Scrollbar( light1, "Green",GLUI_SCROLL_HORIZONTAL,
416                            &light1_diffuse[1],LIGHT1_INTENSITY_ID,control_cb);
417   sb->set_float_limits(0,1);
418   sb = new GLUI_Scrollbar( light1, "Blue",GLUI_SCROLL_HORIZONTAL,
419                            &light1_diffuse[2],LIGHT1_INTENSITY_ID,control_cb);
420   sb->set_float_limits(0,1);
421 
422 
423   /*** Add another rollout ***/
424   GLUI_Rollout *options = new GLUI_Rollout(glui, "Options", true );
425   new GLUI_Checkbox( options, "Draw sphere", &show_sphere );
426   new GLUI_Checkbox( options, "Draw torus", &show_torus );
427   new GLUI_Checkbox( options, "Draw axes", &show_axes );
428   new GLUI_Checkbox( options, "Draw text", &show_text );
429 
430   /**** Add listbox ****/
431   new GLUI_StaticText( glui, "" );
432   GLUI_Listbox *list = new GLUI_Listbox( glui, "Text:", &curr_string );
433   int i;
434   for( i=0; i<4; i++ )
435     list->add_item( i, string_list[i] );
436 
437   new GLUI_StaticText( glui, "" );
438 
439 
440   /*** Disable/Enable buttons ***/
441   new GLUI_Button( glui, "Disable movement", DISABLE_ID, control_cb );
442   new GLUI_Button( glui, "Enable movement", ENABLE_ID, control_cb );
443   new GLUI_Button( glui, "Hide", HIDE_ID, control_cb );
444   new GLUI_Button( glui, "Show", SHOW_ID, control_cb );
445 
446   new GLUI_StaticText( glui, "" );
447 
448   /****** A 'quit' button *****/
449   new GLUI_Button( glui, "Quit", 0,(GLUI_Update_CB)exit );
450 
451 
452   /**** Link windows to GLUI, and register idle callback ******/
453 
454   glui->set_main_gfx_window( main_window );
455 
456 
457   /*** Create the bottom subwindow ***/
458   glui2 = GLUI_Master.create_glui_subwindow( main_window,
459                                              GLUI_SUBWINDOW_BOTTOM );
460   glui2->set_main_gfx_window( main_window );
461 
462   GLUI_Rotation *view_rot = new GLUI_Rotation(glui2, "Objects", view_rotate );
463   view_rot->set_spin( 1.0 );
464   new GLUI_Column( glui2, false );
465   GLUI_Rotation *sph_rot = new GLUI_Rotation(glui2, "Sphere", sphere_rotate );
466   sph_rot->set_spin( .98 );
467   new GLUI_Column( glui2, false );
468   GLUI_Rotation *tor_rot = new GLUI_Rotation(glui2, "Torus", torus_rotate );
469   tor_rot->set_spin( .98 );
470   new GLUI_Column( glui2, false );
471   GLUI_Rotation *lights_rot = new GLUI_Rotation(glui2, "Blue Light", lights_rotation );
472   lights_rot->set_spin( .82 );
473   new GLUI_Column( glui2, false );
474   GLUI_Translation *trans_xy =
475     new GLUI_Translation(glui2, "Objects XY", GLUI_TRANSLATION_XY, obj_pos );
476   trans_xy->set_speed( .005 );
477   new GLUI_Column( glui2, false );
478   GLUI_Translation *trans_x =
479     new GLUI_Translation(glui2, "Objects X", GLUI_TRANSLATION_X, obj_pos );
480   trans_x->set_speed( .005 );
481   new GLUI_Column( glui2, false );
482   GLUI_Translation *trans_y =
483     new GLUI_Translation( glui2, "Objects Y", GLUI_TRANSLATION_Y, &obj_pos[1] );
484   trans_y->set_speed( .005 );
485   new GLUI_Column( glui2, false );
486   GLUI_Translation *trans_z =
487     new GLUI_Translation( glui2, "Objects Z", GLUI_TRANSLATION_Z, &obj_pos[2] );
488   trans_z->set_speed( .005 );
489 
490 #if 0
491   /**** We register the idle callback with GLUI, *not* with GLUT ****/
492   GLUI_Master.set_glutIdleFunc( myGlutIdle );
493 #endif
494 
495   /**** Regular GLUT main loop ****/
496 
497   glutMainLoop();
498 
499   return EXIT_SUCCESS;
500 }
501 
502