1 | /****************************************************************************
|
---|
2 |
|
---|
3 | example2.cpp
|
---|
4 |
|
---|
5 | A simple GLUT program using the GLUI User Interface Library
|
---|
6 |
|
---|
7 | -----------------------------------------------------------------------
|
---|
8 |
|
---|
9 | 9/9/98 Paul Rademacher (rademach@cs.unc.edu)
|
---|
10 |
|
---|
11 | ****************************************************************************/
|
---|
12 |
|
---|
13 | #include <string.h>
|
---|
14 | #include "glut.h"
|
---|
15 | #include "glui.h"
|
---|
16 |
|
---|
17 | float xy_aspect;
|
---|
18 | int last_x, last_y;
|
---|
19 | float rotationX = 0.0, rotationY = 0.0;
|
---|
20 | int main_window;
|
---|
21 |
|
---|
22 |
|
---|
23 | /** These are the live variables passed into GLUI ***/
|
---|
24 | int wireframe = 0;
|
---|
25 | int obj = 0;
|
---|
26 | int segments = 8;
|
---|
27 | char text[200] = {"Hello World!"};
|
---|
28 |
|
---|
29 | GLUI_Checkbox *checkbox;
|
---|
30 | GLUI_Spinner *spinner;
|
---|
31 | GLUI_RadioGroup *radio;
|
---|
32 | GLUI_EditText *edittext;
|
---|
33 |
|
---|
34 | /**************************************** control_cb() *******************/
|
---|
35 | /* GLUI control callback */
|
---|
36 |
|
---|
37 | void control_cb( int control )
|
---|
38 | {
|
---|
39 | /********************************************************************
|
---|
40 | Here we'll print the user id of the control that generated the
|
---|
41 | callback, and we'll also explicitly get the values of each control.
|
---|
42 | Note that we really didn't have to explicitly get the values, since
|
---|
43 | they are already all contained within the live variables:
|
---|
44 | 'wireframe', 'segments', 'obj', and 'text'
|
---|
45 | ********************************************************************/
|
---|
46 |
|
---|
47 | printf( "callback: %d\n", control );
|
---|
48 | printf( " checkbox: %d\n", checkbox->get_int_val() );
|
---|
49 | printf( " spinner: %d\n", spinner->get_int_val() );
|
---|
50 | printf( " radio group: %d\n", radio->get_int_val() );
|
---|
51 | printf( " text: %s\n", edittext->get_text() );
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**************************************** myGlutKeyboard() **********/
|
---|
56 |
|
---|
57 | void myGlutKeyboard(unsigned char Key, int x, int y)
|
---|
58 | {
|
---|
59 | switch(Key)
|
---|
60 | {
|
---|
61 | case 27:
|
---|
62 | case 'q':
|
---|
63 | exit(0);
|
---|
64 | break;
|
---|
65 | };
|
---|
66 |
|
---|
67 | glutPostRedisplay();
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /***************************************** myGlutMenu() ***********/
|
---|
72 |
|
---|
73 | void myGlutMenu( int value )
|
---|
74 | {
|
---|
75 | myGlutKeyboard( value, 0, 0 );
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /***************************************** myGlutIdle() ***********/
|
---|
80 |
|
---|
81 | void myGlutIdle( void )
|
---|
82 | {
|
---|
83 | /* According to the GLUT specification, the current window is
|
---|
84 | undefined during an idle callback. So we need to explicitly change
|
---|
85 | it if necessary */
|
---|
86 | if ( glutGetWindow() != main_window)
|
---|
87 | glutSetWindow(main_window);
|
---|
88 |
|
---|
89 | glutPostRedisplay();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /***************************************** myGlutMouse() **********/
|
---|
93 |
|
---|
94 | void myGlutMouse(int button, int button_state, int x, int y )
|
---|
95 | {
|
---|
96 | if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN ) {
|
---|
97 | last_x = x;
|
---|
98 | last_y = y;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /***************************************** myGlutMotion() **********/
|
---|
104 |
|
---|
105 | void myGlutMotion(int x, int y )
|
---|
106 | {
|
---|
107 | rotationX += (float) (y - last_y);
|
---|
108 | rotationY += (float) (x - last_x);
|
---|
109 |
|
---|
110 | last_x = x;
|
---|
111 | last_y = y;
|
---|
112 |
|
---|
113 | glutPostRedisplay();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**************************************** myGlutReshape() *************/
|
---|
117 |
|
---|
118 | void myGlutReshape( int x, int y )
|
---|
119 | {
|
---|
120 | xy_aspect = (float)x / (float)y;
|
---|
121 | glViewport( 0, 0, x, y );
|
---|
122 |
|
---|
123 | glutPostRedisplay();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /***************************************** myGlutDisplay() *****************/
|
---|
127 |
|
---|
128 | void myGlutDisplay( void )
|
---|
129 | {
|
---|
130 | glClearColor( .9f, .9f, .9f, 1.0f );
|
---|
131 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
---|
132 |
|
---|
133 | glMatrixMode( GL_PROJECTION );
|
---|
134 | glLoadIdentity();
|
---|
135 | glFrustum( -xy_aspect*.08, xy_aspect*.08, -.08, .08, .1, 15.0 );
|
---|
136 |
|
---|
137 | glMatrixMode( GL_MODELVIEW );
|
---|
138 | glLoadIdentity();
|
---|
139 | glTranslatef( 0.0f, 0.0f, -1.6f );
|
---|
140 | glRotatef( rotationY, 0.0, 1.0, 0.0 );
|
---|
141 | glRotatef( rotationX, 1.0, 0.0, 0.0 );
|
---|
142 |
|
---|
143 | /*** Now we render object, using the variables 'obj', 'segments', and
|
---|
144 | 'wireframe'. These are _live_ variables, which are transparently
|
---|
145 | updated by GLUI ***/
|
---|
146 |
|
---|
147 | if ( obj == 0 ) {
|
---|
148 | if ( wireframe )
|
---|
149 | glutWireSphere( .6, segments, segments );
|
---|
150 | else
|
---|
151 | glutSolidSphere( .6, segments, segments );
|
---|
152 | }
|
---|
153 | else if ( obj == 1 ) {
|
---|
154 | if ( wireframe )
|
---|
155 | glutWireTorus( .2,.5,16,segments );
|
---|
156 | else
|
---|
157 | glutSolidTorus( .2,.5,16,segments );
|
---|
158 | }
|
---|
159 |
|
---|
160 | glDisable( GL_LIGHTING ); /* Disable lighting while we render text */
|
---|
161 | glMatrixMode( GL_PROJECTION );
|
---|
162 | glLoadIdentity();
|
---|
163 | gluOrtho2D( 0.0, 100.0, 0.0, 100.0 );
|
---|
164 | glMatrixMode( GL_MODELVIEW );
|
---|
165 | glLoadIdentity();
|
---|
166 | glColor3ub( 0, 0, 0 );
|
---|
167 | glRasterPos2i( 10, 10 );
|
---|
168 |
|
---|
169 | // printf( "text: %s\n", text );
|
---|
170 |
|
---|
171 | /*** Render the live character array 'text' ***/
|
---|
172 | int i;
|
---|
173 | for( i=0; i<(int)strlen( text ); i++ )
|
---|
174 | glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, text[i] );
|
---|
175 |
|
---|
176 | glEnable( GL_LIGHTING );
|
---|
177 |
|
---|
178 | glutSwapBuffers();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**************************************** main() ********************/
|
---|
183 |
|
---|
184 | int main(int argc, char* argv[])
|
---|
185 | {
|
---|
186 | /****************************************/
|
---|
187 | /* Initialize GLUT and create window */
|
---|
188 | /****************************************/
|
---|
189 |
|
---|
190 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
|
---|
191 | glutInitWindowPosition( 50, 50 );
|
---|
192 | glutInitWindowSize( 300, 300 );
|
---|
193 |
|
---|
194 | main_window = glutCreateWindow( "GLUI Example 2" );
|
---|
195 | glutDisplayFunc( myGlutDisplay );
|
---|
196 | glutReshapeFunc( myGlutReshape );
|
---|
197 | glutKeyboardFunc( myGlutKeyboard );
|
---|
198 | glutMotionFunc( myGlutMotion );
|
---|
199 | glutMouseFunc( myGlutMouse );
|
---|
200 |
|
---|
201 | /****************************************/
|
---|
202 | /* Set up OpenGL lights */
|
---|
203 | /****************************************/
|
---|
204 |
|
---|
205 | GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
|
---|
206 | GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
|
---|
207 | GLfloat light0_position[] = {1.0f, 1.0f, 1.0f, 0.0f};
|
---|
208 |
|
---|
209 | glEnable(GL_LIGHTING);
|
---|
210 | glEnable(GL_LIGHT0);
|
---|
211 | glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
|
---|
212 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
|
---|
213 | glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
|
---|
214 |
|
---|
215 | /****************************************/
|
---|
216 | /* Enable z-buferring */
|
---|
217 | /****************************************/
|
---|
218 |
|
---|
219 | glEnable(GL_DEPTH_TEST);
|
---|
220 |
|
---|
221 | /****************************************/
|
---|
222 | /* Here's the GLUI code */
|
---|
223 | /****************************************/
|
---|
224 |
|
---|
225 | GLUI *glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags,
|
---|
226 | x, and y */
|
---|
227 | glui->add_statictext( "GLUI Example 2" );
|
---|
228 | glui->add_separator();
|
---|
229 | checkbox = glui->add_checkbox( "Wireframe", &wireframe, 1, control_cb );
|
---|
230 | spinner = glui->add_spinner( "Segments:",GLUI_SPINNER_INT, &segments,
|
---|
231 | 2, control_cb );
|
---|
232 | spinner->set_int_limits( 3, 60 );
|
---|
233 | edittext = glui->add_edittext( "Text:", GLUI_EDITTEXT_TEXT, text,
|
---|
234 | 3, control_cb );
|
---|
235 | GLUI_Panel *obj_panel = glui->add_panel( "Object Type" );
|
---|
236 | radio = glui->add_radiogroup_to_panel(obj_panel,&obj,4,control_cb);
|
---|
237 | glui->add_radiobutton_to_group( radio, "Sphere" );
|
---|
238 | glui->add_radiobutton_to_group( radio, "Torus" );
|
---|
239 | glui->add_button( "Quit", 0,(GLUI_Update_CB)exit );
|
---|
240 |
|
---|
241 | glui->set_main_gfx_window( main_window );
|
---|
242 |
|
---|
243 | /* We register the idle callback with GLUI, *not* with GLUT */
|
---|
244 | GLUI_Master.set_glutIdleFunc( myGlutIdle );
|
---|
245 |
|
---|
246 | glutMainLoop();
|
---|
247 |
|
---|
248 | return 0;
|
---|
249 | }
|
---|