1
/****************************************************************************
5
A GLUT program using all the features of the GLUI User Interface Library
6
(except columns, featured in example4.cpp)
8
-----------------------------------------------------------------------
10
9/9/98 Paul Rademacher (rademach@cs.unc.edu)
12
****************************************************************************/
20
float rotationX = 0.0, rotationY = 0.0;
22
/** These are the live variables passed into GLUI ***/
27
char text[sizeof(GLUI_String)] = {"Hello World!"};
28
int light0_enabled = 1;
29
int light1_enabled = 0;
30
float light0_intensity = 1.0;
31
float light1_intensity = 1.0;
36
/** Pointers to the windows and some of the controls we'll create **/
37
GLUI *cmd_line_glui, *glui;
38
GLUI_Checkbox *checkbox;
39
GLUI_Spinner *spinner, *light0_spinner, *light1_spinner, *scale_spinner;
40
GLUI_RadioGroup *radio;
41
GLUI_EditText *edittext, *cmd_line;
42
GLUI_Panel *obj_panel;
44
/********** User IDs for callbacks ********/
45
#define CMD_LINE_ID 100
46
#define LIGHT0_ENABLED_ID 200
47
#define LIGHT1_ENABLED_ID 201
48
#define LIGHT0_INTENSITY_ID 250
49
#define LIGHT1_INTENSITY_ID 251
51
/********** Miscellaneous global variables **********/
53
GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
54
GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
55
GLfloat light0_position[] = {.5f, .5f, 1.0f, 0.0f};
57
GLfloat light1_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
58
GLfloat light1_diffuse[] = {.9f, .6f, 0.0f, 1.0f};
59
GLfloat light1_position[] = {-1.0f, -1.0f, 1.0f, 0.0f};
61
/**************************************** control_cb() *******************/
62
/* GLUI control callback */
64
void control_cb( int control )
66
if ( control == CMD_LINE_ID ) {
67
/*** User typed text into the 'command line' window ***/
68
printf( "Command (%d): %s\n", counter, cmd_line->get_text() );
69
cmd_line->set_text( "" ); /* Clear command line */
71
else if ( control == LIGHT0_ENABLED_ID ) {
72
if ( light0_enabled ) {
73
glEnable( GL_LIGHT0 );
74
light0_spinner->enable();
77
glDisable( GL_LIGHT0 );
78
light0_spinner->disable();
81
else if ( control == LIGHT1_ENABLED_ID ) {
82
if ( light1_enabled ) {
83
glEnable( GL_LIGHT1 );
84
light1_spinner->enable();
87
glDisable( GL_LIGHT1 );
88
light1_spinner->disable();
91
else if ( control == LIGHT0_INTENSITY_ID ) {
92
float v[] = { light0_diffuse[0], light0_diffuse[1],
93
light0_diffuse[2], light0_diffuse[3] };
95
v[0] *= light0_intensity;
96
v[1] *= light0_intensity;
97
v[2] *= light0_intensity;
99
glLightfv(GL_LIGHT0, GL_DIFFUSE, v );
101
else if ( control == LIGHT1_INTENSITY_ID ) {
102
float v[] = { light1_diffuse[0], light1_diffuse[1],
103
light1_diffuse[2], light1_diffuse[3] };
105
v[0] *= light1_intensity;
106
v[1] *= light1_intensity;
107
v[2] *= light1_intensity;
109
glLightfv(GL_LIGHT1, GL_DIFFUSE, v );
113
/**************************************** myGlutKeyboard() **********/
115
void myGlutKeyboard(unsigned char Key, int x, int y)
129
/***************************************** myGlutMenu() ***********/
131
void myGlutMenu( int value )
133
myGlutKeyboard( value, 0, 0 );
137
/***************************************** myGlutIdle() ***********/
139
void myGlutIdle( void )
141
/* According to the GLUT specification, the current window is
142
undefined during an idle callback. So we need to explicitly change
144
if ( glutGetWindow() != main_window )
145
glutSetWindow(main_window);
150
/****************************************************************/
151
/* This demonstrates GLUI::sync_live() */
152
/* We change the value of a variable that is 'live' to some */
153
/* control. We then call sync_live, and the control */
154
/* associated with that variable is automatically updated */
155
/* with the new value. This frees the programmer from having */
156
/* to always remember which variables are used by controls - */
157
/* simply change whatever variables are necessary, then sync */
158
/* the live ones all at once with a single call to sync_live */
159
/****************************************************************/
167
/***************************************** myGlutMouse() **********/
169
void myGlutMouse(int button, int button_state, int x, int y )
171
if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN ) {
178
/***************************************** myGlutMotion() **********/
180
void myGlutMotion(int x, int y )
182
rotationX += (float) (y - last_y);
183
rotationY += (float) (x - last_x);
191
/**************************************** myGlutReshape() *************/
193
void myGlutReshape( int x, int y )
195
xy_aspect = (float)x / (float)y;
196
glViewport( 0, 0, x, y );
201
/***************************************** myGlutDisplay() *****************/
203
void myGlutDisplay( void )
205
glClearColor( .9f, .9f, .9f, 1.0f );
206
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
208
glMatrixMode( GL_PROJECTION );
210
glFrustum( -xy_aspect*.08, xy_aspect*.08, -.08, .08, .1, 15.0 );
212
glMatrixMode( GL_MODELVIEW );
214
glTranslatef( 0.0, 0.0, -1.2f );
215
glRotatef( rotationY, 0.0, 1.0, 0.0 );
216
glRotatef( rotationX, 1.0, 0.0, 0.0 );
218
glScalef( scale, scale, scale );
220
/*** Now we render object, using the variables 'obj_type', 'segments', and
221
'wireframe'. These are _live_ variables, which are transparently
224
if ( obj_type == 0 ) {
226
glutWireSphere( .6, segments, segments );
228
glutSolidSphere( .6, segments, segments );
230
else if ( obj_type == 1 ) {
232
glutWireTorus( .2,.5,16,segments );
234
glutSolidTorus( .2,.5,16,segments );
236
else if ( obj_type == 2 ) {
238
glutWireTeapot( .5 );
240
glutSolidTeapot( .5 );
243
/* Disable lighting and set up ortho projection to render text */
244
glDisable( GL_LIGHTING );
245
glMatrixMode( GL_PROJECTION );
247
gluOrtho2D( 0.0, 100.0, 0.0, 100.0 );
248
glMatrixMode( GL_MODELVIEW );
250
glColor3ub( 0, 0, 0 );
251
glRasterPos2i( 10, 10 );
253
/*** Render the live character array 'text' ***/
255
for( i=0; i<(int)strlen( text ); i++ )
256
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, text[i] );
258
glEnable( GL_LIGHTING );
264
/**************************************** main() ********************/
266
void main(int argc, char* argv[])
268
/****************************************/
269
/* Initialize GLUT and create window */
270
/****************************************/
272
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
273
glutInitWindowPosition( 50, 50 );
274
glutInitWindowSize( 300, 300 );
276
main_window = glutCreateWindow( "GLUI Example 3" );
277
glutDisplayFunc( myGlutDisplay );
278
glutReshapeFunc( myGlutReshape );
279
glutKeyboardFunc( myGlutKeyboard );
280
glutMotionFunc( myGlutMotion );
281
glutMouseFunc( myGlutMouse );
283
/****************************************/
284
/* Set up OpenGL lights */
285
/****************************************/
287
glEnable(GL_LIGHTING);
288
glEnable( GL_NORMALIZE );
291
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
292
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
293
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
295
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
296
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
297
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
299
/****************************************/
300
/* Enable z-buferring */
301
/****************************************/
303
glEnable(GL_DEPTH_TEST);
305
/****************************************/
306
/* Here's the GLUI code */
307
/****************************************/
309
printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );
311
glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags,
313
glui->add_statictext( "GLUI Example 3" );
314
obj_panel = glui->add_panel( "Object" );
316
/***** Control for the object type *****/
318
GLUI_Panel *type_panel = glui->add_panel_to_panel( obj_panel, "Type");
319
radio = glui->add_radiogroup_to_panel(type_panel,&obj_type,4,control_cb);
320
glui->add_radiobutton_to_group( radio, "Sphere" );
321
glui->add_radiobutton_to_group( radio, "Torus" );
322
glui->add_radiobutton_to_group( radio, "Teapot" );
325
glui->add_checkbox_to_panel( obj_panel, "Wireframe", &wireframe, 1,
327
spinner = glui->add_spinner_to_panel( obj_panel, "Segments:",
328
GLUI_SPINNER_INT, &segments);
329
spinner->set_int_limits( 3, 60 );
330
spinner->set_alignment( GLUI_ALIGN_RIGHT );
333
glui->add_spinner_to_panel( obj_panel, "Scale:",
334
GLUI_SPINNER_FLOAT, &scale);
335
scale_spinner->set_float_limits( .2f, 4.0 );
336
scale_spinner->set_alignment( GLUI_ALIGN_RIGHT );
338
glui->add_separator_to_panel( obj_panel );
339
edittext = glui->add_edittext_to_panel( obj_panel, "Text:",
340
GLUI_EDITTEXT_TEXT, text );
341
edittext->set_w( 150 );
343
/******** Add some controls for lights ********/
345
GLUI_Panel *light0 = glui->add_panel( "Light 1" );
346
GLUI_Panel *light1 = glui->add_panel( "Light 2" );
348
glui->add_checkbox_to_panel( light0, "Enabled", &light0_enabled,
349
LIGHT0_ENABLED_ID, control_cb );
351
glui->add_spinner_to_panel( light0, "Intensity:", GLUI_SPINNER_FLOAT,
352
&light0_intensity, LIGHT0_INTENSITY_ID,
354
light0_spinner->set_float_limits( 0.0, 1.0 );
356
glui->add_checkbox_to_panel( light1, "Enabled", &light1_enabled,
357
LIGHT1_ENABLED_ID, control_cb );
359
glui->add_spinner_to_panel( light1, "Intensity:", GLUI_SPINNER_FLOAT,
360
&light1_intensity, LIGHT1_INTENSITY_ID,
362
light1_spinner->set_float_limits( 0.0, 1.0 );
363
light1_spinner->disable(); /* Disable this light initially */
365
/****** Add a grayed-out counter *****/
367
GLUI_EditText *counter_edittext =
368
glui->add_edittext( "Count:", GLUI_EDITTEXT_INT, &counter );
369
counter_edittext->disable();
371
/****** A 'quit' button *****/
373
glui->add_button( "Quit", 0,(GLUI_Update_CB)exit );
375
/****** Command line window ******/
377
cmd_line_glui = GLUI_Master.create_glui( "Enter command:",
380
cmd_line = cmd_line_glui->add_edittext( "Command:",
381
GLUI_EDITTEXT_TEXT, NULL,
382
CMD_LINE_ID, control_cb );
383
cmd_line->set_w( 400 ); /** Widen 'command line' control **/
385
/**** Link windows to GLUI, and register idle callback ******/
387
glui->set_main_gfx_window( main_window );
388
cmd_line_glui->set_main_gfx_window( main_window );
390
/* We register the idle callback with GLUI, not with GLUT */
391
GLUI_Master.set_glutIdleFunc( myGlutIdle );
393
/**** Regular GLUT main loop ****/