1 /****************************************************************************
2
3 example4.cpp
4
5 A GLUI program demonstrating nested columns
6
7 -----------------------------------------------------------------------
8
9 10/2/98 Paul Rademacher (rademach@cs.unc.edu)
10
11 ****************************************************************************/
12
13 #include <string.h>
14 #include <GL/glui.h>
15
16 float xy_aspect;
17 int last_x, last_y;
18 float rotationX = 0.0, rotationY = 0.0;
19
20 /** These are the live variables passed into GLUI ***/
21 int wireframe = 0;
22 int obj_type = 1;
23 int segments = 8;
24 int segments2 = 8;
25 int light0_enabled = 1;
26 int light1_enabled = 0;
27 float light0_intensity = 1.0;
28 float light1_intensity = 1.0;
29 int main_window;
30 float scale = 1.0;
31 int show_sphere=1;
32 int show_torus=1;
33
34 /** Pointers to the windows and some of the controls we'll create **/
35 GLUI *glui;
36 GLUI_Checkbox *checkbox;
37 GLUI_Spinner *spinner, *light0_spinner, *light1_spinner;
38 GLUI_RadioGroup *radio;
39 GLUI_Panel *obj_panel;
40
41 /********** User IDs for callbacks ********/
42 #define LIGHT0_ENABLED_ID 200
43 #define LIGHT1_ENABLED_ID 201
44 #define LIGHT0_INTENSITY_ID 250
45 #define LIGHT1_INTENSITY_ID 251
46
47 /********** Miscellaneous global variables **********/
48
49 GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
50 GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
51 GLfloat light0_position[] = {.5f, .5f, 1.0f, 0.0f};
52
53 GLfloat light1_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
54 GLfloat light1_diffuse[] = {.9f, .6f, 0.0f, 1.0f};
55 GLfloat light1_position[] = {-1.0f, -1.0f, 1.0f, 0.0f};
56
57 /**************************************** control_cb() *******************/
58 /* GLUI control callback */
59
control_cb(int control)60 void control_cb( int control )
61 {
62 if ( control == LIGHT0_ENABLED_ID ) {
63 if ( light0_enabled ) {
64 glEnable( GL_LIGHT0 );
65 light0_spinner->enable();
66 }
67 else {
68 glDisable( GL_LIGHT0 );
69 light0_spinner->disable();
70 }
71 }
72 else if ( control == LIGHT1_ENABLED_ID ) {
73 if ( light1_enabled ) {
74 glEnable( GL_LIGHT1 );
75 light1_spinner->enable();
76 }
77 else {
78 glDisable( GL_LIGHT1 );
79 light1_spinner->disable();
80 }
81 }
82 else if ( control == LIGHT0_INTENSITY_ID ) {
83 float v[] = { light0_diffuse[0], light0_diffuse[1],
84 light0_diffuse[2], light0_diffuse[3] };
85
86 v[0] *= light0_intensity;
87 v[1] *= light0_intensity;
88 v[2] *= light0_intensity;
89
90 glLightfv(GL_LIGHT0, GL_DIFFUSE, v );
91 }
92 else if ( control == LIGHT1_INTENSITY_ID ) {
93 float v[] = { light1_diffuse[0], light1_diffuse[1],
94 light1_diffuse[2], light1_diffuse[3] };
95
96 v[0] *= light1_intensity;
97 v[1] *= light1_intensity;
98 v[2] *= light1_intensity;
99
100 glLightfv(GL_LIGHT1, GL_DIFFUSE, v );
101 }
102 }
103
104 /**************************************** myGlutKeyboard() **********/
105
myGlutKeyboard(unsigned char Key,int x,int y)106 void myGlutKeyboard(unsigned char Key, int x, int y)
107 {
108 switch(Key)
109 {
110 case 27:
111 case 'q':
112 exit(0);
113 break;
114 };
115
116 glutPostRedisplay();
117 }
118
119
120 /***************************************** myGlutMenu() ***********/
121
myGlutMenu(int value)122 void myGlutMenu( int value )
123 {
124 myGlutKeyboard( value, 0, 0 );
125 }
126
127 /***************************************** myGlutMouse() **********/
128
myGlutMouse(int button,int button_state,int x,int y)129 void myGlutMouse(int button, int button_state, int x, int y )
130 {
131 if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN ) {
132 last_x = x;
133 last_y = y;
134 }
135 }
136
137
138 /***************************************** myGlutMotion() **********/
139
myGlutMotion(int x,int y)140 void myGlutMotion(int x, int y )
141 {
142 rotationX += (float) (y - last_y);
143 rotationY += (float) (x - last_x);
144
145 last_x = x;
146 last_y = y;
147
148 glutPostRedisplay();
149 }
150
151 /**************************************** myGlutReshape() *************/
152
myGlutReshape(int x,int y)153 void myGlutReshape( int x, int y )
154 {
155 xy_aspect = (float)x / (float)y;
156 glViewport( 0, 0, x, y );
157
158 glutPostRedisplay();
159 }
160
161
162 /************************************************** draw_axes() **********/
163 /* Disables lighting, then draws RGB axes */
164
draw_axes(float scale)165 void draw_axes( float scale )
166 {
167 glDisable( GL_LIGHTING );
168
169 glPushMatrix();
170 glScalef( scale, scale, scale );
171
172 glBegin( GL_LINES );
173
174 glColor3f( 1.0, 0.0, 0.0 );
175 glVertex3f( .8f, 0.05f, 0.0 ); glVertex3f( 1.0, 0.25f, 0.0 ); /* Letter X*/
176 glVertex3f( 0.8f, .25f, 0.0 ); glVertex3f( 1.0, 0.05f, 0.0 );
177 glVertex3f( 0.0, 0.0, 0.0 ); glVertex3f( 1.0, 0.0, 0.0 ); /* X axis */
178
179 glColor3f( 0.0, 1.0, 0.0 );
180 glVertex3f( 0.0, 0.0, 0.0 ); glVertex3f( 0.0, 1.0, 0.0 ); /* Y axis */
181
182 glColor3f( 0.0, 0.0, 1.0 );
183 glVertex3f( 0.0, 0.0, 0.0 ); glVertex3f( 0.0, 0.0, 1.0 ); /* Z axis */
184 glEnd();
185
186 glPopMatrix();
187
188 glEnable( GL_LIGHTING );
189 }
190
191
192 /***************************************** myGlutDisplay() *****************/
193
myGlutDisplay(void)194 void myGlutDisplay( void )
195 {
196 glClearColor( .9f, .9f, .9f, 1.0f );
197 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
198
199 glMatrixMode( GL_PROJECTION );
200 glLoadIdentity();
201 glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 15.0 );
202
203 glMatrixMode( GL_MODELVIEW );
204 glLoadIdentity();
205 glTranslatef( 0.0, 0.0, -2.6f );
206
207 glScalef( scale, scale, scale );
208
209 /*** Now we render object, using the variables 'obj_type', 'segments', and
210 'wireframe'. These are _live_ variables, which are transparently
211 updated by GLUI ***/
212
213 glPushMatrix();
214 glTranslatef( -.5, 0.0, 0.0 );
215 glRotatef( rotationY, 0.0, 1.0, 0.0 );
216 glRotatef( rotationX, 1.0, 0.0, 0.0 );
217 if ( wireframe && show_sphere)
218 glutWireSphere( .4, segments, segments );
219 else if ( show_sphere )
220 glutSolidSphere( .4, segments, segments );
221 draw_axes(.52f);
222 glPopMatrix();
223
224 glPushMatrix();
225 glTranslatef( .5, 0.0, 0.0 );
226 glRotatef( rotationY, 0.0, 1.0, 0.0 );
227 glRotatef( rotationX, 1.0, 0.0, 0.0 );
228 if ( wireframe && show_torus )
229 glutWireTorus( .15,.3,16,segments );
230 else if ( show_torus )
231 glutSolidTorus( .15,.3,16,segments );
232 draw_axes(.52f);
233 glPopMatrix();
234
235 glutSwapBuffers();
236 }
237
238
239 /**************************************** main() ********************/
240
main(int argc,char * argv[])241 int main(int argc, char* argv[])
242 {
243 /****************************************/
244 /* Initialize GLUT and create window */
245 /****************************************/
246
247 glutInit(&argc, argv);
248 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
249 glutInitWindowPosition( 50, 50 );
250 glutInitWindowSize( 300, 300 );
251
252 main_window = glutCreateWindow( "GLUI Example 4" );
253 glutDisplayFunc( myGlutDisplay );
254 glutReshapeFunc( myGlutReshape );
255 glutKeyboardFunc( myGlutKeyboard );
256 glutMotionFunc( myGlutMotion );
257 glutMouseFunc( myGlutMouse );
258
259 /****************************************/
260 /* Set up OpenGL lights */
261 /****************************************/
262
263 glEnable(GL_LIGHTING);
264 glEnable( GL_NORMALIZE );
265
266 glEnable(GL_LIGHT0);
267 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
268 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
269 glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
270
271 glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
272 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
273 glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
274
275 /****************************************/
276 /* Enable z-buferring */
277 /****************************************/
278
279 glEnable(GL_DEPTH_TEST);
280
281 /****************************************/
282 /* Here's the GLUI code */
283 /****************************************/
284
285 printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );
286
287 glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags,
288 x, and y */
289
290 /*** Add invisible panel to hold rest of controls ***/
291 GLUI_Panel *panel1 = new GLUI_Panel(glui, "", GLUI_PANEL_NONE );
292
293 obj_panel = new GLUI_Panel( panel1, "Objects" );
294
295 /***** Control for object params *****/
296
297 checkbox =
298 new GLUI_Checkbox( obj_panel, "Wireframe", &wireframe, 1, control_cb );
299 spinner = new GLUI_Spinner( obj_panel, "Segments:", &segments);
300 spinner->set_int_limits( 3, 60 );
301 spinner->set_alignment( GLUI_ALIGN_RIGHT );
302
303 GLUI_Spinner *scale_spinner =
304 new GLUI_Spinner( obj_panel, "Scale:", &scale);
305 scale_spinner->set_float_limits( .2f, 4.0 );
306 scale_spinner->set_alignment( GLUI_ALIGN_RIGHT );
307
308 GLUI_Panel *panel2 = new GLUI_Panel( obj_panel, "", GLUI_PANEL_NONE );
309 new GLUI_Checkbox( panel2, "Sphere", &show_sphere );
310 new GLUI_Column( panel2 );
311 new GLUI_Checkbox( panel2, "Torus", &show_torus );
312
313
314 /*** Start a new column in this panel ***/
315 new GLUI_Column( panel1, false ); /* 'false' means don't draw bar */
316
317
318 /******** Add some controls for lights ********/
319
320 GLUI_Panel *light0 = new GLUI_Panel( panel1, "Light 1" );
321 GLUI_Panel *light1 = new GLUI_Panel( panel1, "Light 2" );
322
323 new GLUI_Checkbox( light0, "Enabled", &light0_enabled,
324 LIGHT0_ENABLED_ID, control_cb );
325 light0_spinner =
326 new GLUI_Spinner( light0, "Intensity:",
327 &light0_intensity, LIGHT0_INTENSITY_ID,
328 control_cb );
329 light0_spinner->set_float_limits( 0.0, 1.0 );
330
331 new GLUI_Checkbox( light1, "Enabled", &light1_enabled,
332 LIGHT1_ENABLED_ID, control_cb );
333 light1_spinner =
334 new GLUI_Spinner( light1, "Intensity:",
335 &light1_intensity, LIGHT1_INTENSITY_ID,
336 control_cb );
337 light1_spinner->set_float_limits( 0.0, 1.0 );
338 light1_spinner->disable(); /* Disable this light initially */
339
340
341 /****** A 'quit' button *****/
342
343 new GLUI_Button( glui, "Quit", 0,(GLUI_Update_CB)exit );
344
345
346 /**** Link windows to GLUI ******/
347
348 glui->set_main_gfx_window( main_window );
349
350 /**** Regular GLUT main loop ****/
351
352 glutMainLoop();
353
354 return EXIT_SUCCESS;
355 }
356
357