~ubuntu-branches/ubuntu/edgy/glui/edgy

« back to all changes in this revision

Viewing changes to example3.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2001-09-23 12:57:28 UTC
  • Revision ID: james.westby@ubuntu.com-20010923125728-qbts7xit2eg1ogo2
Tags: upstream-2.1.0.beta
ImportĀ upstreamĀ versionĀ 2.1.0.beta

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 
 
3
  example3.cpp
 
4
 
 
5
  A GLUT program using all the features of the GLUI User Interface Library
 
6
  (except columns, featured in example4.cpp)
 
7
 
 
8
  -----------------------------------------------------------------------
 
9
           
 
10
  9/9/98 Paul Rademacher (rademach@cs.unc.edu)
 
11
 
 
12
****************************************************************************/
 
13
 
 
14
#include <string.h>
 
15
#include <GL/glut.h>
 
16
#include "glui.h"
 
17
 
 
18
float xy_aspect;
 
19
int   last_x, last_y;
 
20
float rotationX = 0.0, rotationY = 0.0;
 
21
 
 
22
/** These are the live variables passed into GLUI ***/
 
23
int   wireframe = 0;
 
24
int   obj_type = 1;
 
25
int   segments = 8;
 
26
int   segments2 = 8;
 
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;
 
32
int   main_window;
 
33
int   counter = 0;
 
34
float scale = 1.0;
 
35
 
 
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;
 
43
 
 
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
 
50
 
 
51
/********** Miscellaneous global variables **********/
 
52
 
 
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};
 
56
 
 
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};
 
60
 
 
61
/**************************************** control_cb() *******************/
 
62
/* GLUI control callback                                                 */
 
63
 
 
64
void control_cb( int control )
 
65
{
 
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 */
 
70
  }
 
71
  else if ( control == LIGHT0_ENABLED_ID ) {
 
72
    if ( light0_enabled ) {
 
73
      glEnable( GL_LIGHT0 );
 
74
      light0_spinner->enable();
 
75
    }
 
76
    else {
 
77
      glDisable( GL_LIGHT0 ); 
 
78
      light0_spinner->disable();
 
79
    }
 
80
  }
 
81
  else if ( control == LIGHT1_ENABLED_ID ) {
 
82
    if ( light1_enabled ) {
 
83
      glEnable( GL_LIGHT1 );
 
84
      light1_spinner->enable();
 
85
    }
 
86
    else {
 
87
      glDisable( GL_LIGHT1 ); 
 
88
      light1_spinner->disable();
 
89
    }
 
90
  }
 
91
  else if ( control == LIGHT0_INTENSITY_ID ) {
 
92
    float v[] = { light0_diffuse[0],  light0_diffuse[1],
 
93
                  light0_diffuse[2],  light0_diffuse[3] };
 
94
    
 
95
    v[0] *= light0_intensity;
 
96
    v[1] *= light0_intensity;
 
97
    v[2] *= light0_intensity;
 
98
 
 
99
    glLightfv(GL_LIGHT0, GL_DIFFUSE, v );
 
100
  }
 
101
  else if ( control == LIGHT1_INTENSITY_ID ) {
 
102
    float v[] = { light1_diffuse[0],  light1_diffuse[1],
 
103
                  light1_diffuse[2],  light1_diffuse[3] };
 
104
    
 
105
    v[0] *= light1_intensity;
 
106
    v[1] *= light1_intensity;
 
107
    v[2] *= light1_intensity;
 
108
 
 
109
    glLightfv(GL_LIGHT1, GL_DIFFUSE, v );
 
110
  }
 
111
}
 
112
 
 
113
/**************************************** myGlutKeyboard() **********/
 
114
 
 
115
void myGlutKeyboard(unsigned char Key, int x, int y)
 
116
{
 
117
  switch(Key)
 
118
  {
 
119
  case 27: 
 
120
  case 'q':
 
121
    exit(0);
 
122
    break;
 
123
  };
 
124
  
 
125
  glutPostRedisplay();
 
126
}
 
127
 
 
128
 
 
129
/***************************************** myGlutMenu() ***********/
 
130
 
 
131
void myGlutMenu( int value )
 
132
{
 
133
  myGlutKeyboard( value, 0, 0 );
 
134
}
 
135
 
 
136
 
 
137
/***************************************** myGlutIdle() ***********/
 
138
 
 
139
void myGlutIdle( void )
 
140
{
 
141
  /* According to the GLUT specification, the current window is 
 
142
     undefined during an idle callback.  So we need to explicitly change
 
143
     it if necessary */
 
144
  if ( glutGetWindow() != main_window ) 
 
145
    glutSetWindow(main_window);  
 
146
 
 
147
 
 
148
  glutPostRedisplay();
 
149
 
 
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
  /****************************************************************/
 
160
 
 
161
  counter++;
 
162
   
 
163
  glui->sync_live();
 
164
 
 
165
}
 
166
 
 
167
/***************************************** myGlutMouse() **********/
 
168
 
 
169
void myGlutMouse(int button, int button_state, int x, int y )
 
170
{
 
171
  if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN ) {
 
172
    last_x = x;
 
173
    last_y = y;
 
174
  }
 
175
}
 
176
 
 
177
 
 
178
/***************************************** myGlutMotion() **********/
 
179
 
 
180
void myGlutMotion(int x, int y )
 
181
{
 
182
  rotationX += (float) (y - last_y);
 
183
  rotationY += (float) (x - last_x);
 
184
 
 
185
  last_x = x;
 
186
  last_y = y;
 
187
 
 
188
  glutPostRedisplay(); 
 
189
}
 
190
 
 
191
/**************************************** myGlutReshape() *************/
 
192
 
 
193
void myGlutReshape( int x, int y )
 
194
{
 
195
  xy_aspect = (float)x / (float)y;
 
196
  glViewport( 0, 0, x, y );
 
197
 
 
198
  glutPostRedisplay();
 
199
}
 
200
 
 
201
/***************************************** myGlutDisplay() *****************/
 
202
 
 
203
void myGlutDisplay( void )
 
204
{
 
205
  glClearColor( .9f, .9f, .9f, 1.0f );
 
206
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
207
 
 
208
  glMatrixMode( GL_PROJECTION );
 
209
  glLoadIdentity();
 
210
  glFrustum( -xy_aspect*.08, xy_aspect*.08, -.08, .08, .1, 15.0 );
 
211
 
 
212
  glMatrixMode( GL_MODELVIEW );
 
213
  glLoadIdentity();
 
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 );
 
217
 
 
218
  glScalef( scale, scale, scale );
 
219
 
 
220
  /*** Now we render object, using the variables 'obj_type', 'segments', and
 
221
    'wireframe'.  These are _live_ variables, which are transparently 
 
222
    updated by GLUI ***/
 
223
  
 
224
  if ( obj_type == 0 ) {
 
225
    if ( wireframe )      
 
226
      glutWireSphere( .6, segments, segments );
 
227
    else                  
 
228
      glutSolidSphere( .6, segments, segments );
 
229
  }
 
230
  else if ( obj_type == 1 ) {
 
231
    if ( wireframe )
 
232
      glutWireTorus( .2,.5,16,segments );
 
233
    else
 
234
      glutSolidTorus( .2,.5,16,segments );
 
235
  }
 
236
  else if ( obj_type == 2 ) {
 
237
    if ( wireframe )
 
238
      glutWireTeapot( .5 );
 
239
    else
 
240
      glutSolidTeapot( .5 );
 
241
  }
 
242
 
 
243
  /* Disable lighting and set up ortho projection to render text */
 
244
  glDisable( GL_LIGHTING );  
 
245
  glMatrixMode( GL_PROJECTION );
 
246
  glLoadIdentity();
 
247
  gluOrtho2D( 0.0, 100.0, 0.0, 100.0  );
 
248
  glMatrixMode( GL_MODELVIEW );
 
249
  glLoadIdentity();
 
250
  glColor3ub( 0, 0, 0 );
 
251
  glRasterPos2i( 10, 10 );
 
252
 
 
253
  /*** Render the live character array 'text' ***/
 
254
  int i;
 
255
  for( i=0; i<(int)strlen( text ); i++ )
 
256
    glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, text[i] );
 
257
 
 
258
  glEnable( GL_LIGHTING );
 
259
 
 
260
  glutSwapBuffers(); 
 
261
}
 
262
 
 
263
 
 
264
/**************************************** main() ********************/
 
265
 
 
266
void main(int argc, char* argv[])
 
267
{
 
268
  /****************************************/
 
269
  /*   Initialize GLUT and create window  */
 
270
  /****************************************/
 
271
 
 
272
  glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
 
273
  glutInitWindowPosition( 50, 50 );
 
274
  glutInitWindowSize( 300, 300 );
 
275
 
 
276
  main_window = glutCreateWindow( "GLUI Example 3" );
 
277
  glutDisplayFunc( myGlutDisplay );
 
278
  glutReshapeFunc( myGlutReshape );  
 
279
  glutKeyboardFunc( myGlutKeyboard );
 
280
  glutMotionFunc( myGlutMotion );
 
281
  glutMouseFunc( myGlutMouse );
 
282
 
 
283
  /****************************************/
 
284
  /*       Set up OpenGL lights           */
 
285
  /****************************************/
 
286
 
 
287
  glEnable(GL_LIGHTING);
 
288
  glEnable( GL_NORMALIZE );
 
289
 
 
290
  glEnable(GL_LIGHT0);
 
291
  glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
 
292
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
 
293
  glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
 
294
 
 
295
  glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
 
296
  glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
 
297
  glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
 
298
 
 
299
  /****************************************/
 
300
  /*          Enable z-buferring          */
 
301
  /****************************************/
 
302
 
 
303
  glEnable(GL_DEPTH_TEST);
 
304
 
 
305
  /****************************************/
 
306
  /*         Here's the GLUI code         */
 
307
  /****************************************/
 
308
 
 
309
  printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );
 
310
 
 
311
  glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags,
 
312
                                                           x, and y */
 
313
  glui->add_statictext( "GLUI Example 3" ); 
 
314
  obj_panel = glui->add_panel( "Object" );
 
315
 
 
316
  /***** Control for the object type *****/
 
317
 
 
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" );
 
323
 
 
324
  checkbox = 
 
325
    glui->add_checkbox_to_panel( obj_panel, "Wireframe", &wireframe, 1, 
 
326
                                 control_cb );
 
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 );
 
331
 
 
332
  scale_spinner = 
 
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 );
 
337
 
 
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 );
 
342
 
 
343
  /******** Add some controls for lights ********/
 
344
 
 
345
  GLUI_Panel *light0 = glui->add_panel( "Light 1" );
 
346
  GLUI_Panel *light1 = glui->add_panel( "Light 2" );
 
347
 
 
348
  glui->add_checkbox_to_panel( light0, "Enabled", &light0_enabled,
 
349
                               LIGHT0_ENABLED_ID, control_cb );
 
350
  light0_spinner = 
 
351
    glui->add_spinner_to_panel( light0, "Intensity:", GLUI_SPINNER_FLOAT,
 
352
                                &light0_intensity, LIGHT0_INTENSITY_ID,
 
353
                                control_cb );
 
354
  light0_spinner->set_float_limits( 0.0, 1.0 );
 
355
 
 
356
  glui->add_checkbox_to_panel( light1, "Enabled", &light1_enabled,
 
357
                               LIGHT1_ENABLED_ID, control_cb );
 
358
  light1_spinner = 
 
359
    glui->add_spinner_to_panel( light1, "Intensity:", GLUI_SPINNER_FLOAT,
 
360
                                &light1_intensity, LIGHT1_INTENSITY_ID,
 
361
                                control_cb );
 
362
  light1_spinner->set_float_limits( 0.0, 1.0 );
 
363
  light1_spinner->disable();   /* Disable this light initially */
 
364
 
 
365
  /****** Add a grayed-out counter *****/
 
366
  
 
367
  GLUI_EditText *counter_edittext = 
 
368
    glui->add_edittext( "Count:", GLUI_EDITTEXT_INT, &counter );
 
369
  counter_edittext->disable();
 
370
 
 
371
  /****** A 'quit' button *****/
 
372
 
 
373
  glui->add_button( "Quit", 0,(GLUI_Update_CB)exit );
 
374
 
 
375
  /****** Command line window ******/
 
376
 
 
377
  cmd_line_glui = GLUI_Master.create_glui( "Enter command:",
 
378
                                           0, 50, 500 );
 
379
  
 
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 **/
 
384
 
 
385
  /**** Link windows to GLUI, and register idle callback ******/
 
386
  
 
387
  glui->set_main_gfx_window( main_window );
 
388
  cmd_line_glui->set_main_gfx_window( main_window );
 
389
 
 
390
  /* We register the idle callback with GLUI, not with GLUT */
 
391
  GLUI_Master.set_glutIdleFunc( myGlutIdle );
 
392
 
 
393
  /**** Regular GLUT main loop ****/  
 
394
  glutMainLoop();
 
395
}
 
396