4 This program is a test harness for the various shapes
5 in OpenGLUT. It may also be useful to see which
6 parameters control what behavior in the OpenGLUT
9 Spinning wireframe and solid-shaded shapes are
10 displayed. Some parameters can be adjusted.
13 - <tt>Esc </tt> Quit
14 - <tt>q Q </tt> Quit
15 - <tt>i I </tt> Show info
16 - <tt>p P </tt> Toggle perspective or orthographic projection
17 - <tt>= + </tt> Increase \a slices
18 - <tt>- _ </tt> Decreate \a slices
19 - <tt>, < </tt> Decreate \a stacks
20 - <tt>. > </tt> Increase \a stacks
21 - <tt>9 ( </tt> Decreate \a depth (Sierpinski Sponge)
22 - <tt>0 ) </tt> Increase \a depth (Sierpinski Sponge)
23 - <tt>up </tt> Increase "outer radius"
24 - <tt>down </tt> Decrease "outer radius"
25 - <tt>left </tt> Decrease "inner radius"
26 - <tt>right</tt> Increase "inner radius"
27 - <tt>PgUp </tt> Next shape-drawing function
28 - <tt>PgDn </tt> Prev shape-drawing function
30 \author Written by Nigel Stewart November 2003
32 \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
33 OpenGLUT branched from freeglut in February, 2004.
35 \image html openglut_shapes.png OpenGLUT Geometric Shapes Demonstration
36 \include demos/shapes/shapes.c
39 #include <GL/freeglut.h>
40 #include <GL/freeglut_ext.h>
47 /* DUMP MEMORY LEAKS */
52 * This macro is only intended to be used on arrays, of course.
54 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
56 #define glRotated glRotatef
57 #define glTranslated glTranslatef
60 * These global variables control which object is drawn,
61 * and how it is drawn. No object uses all of these
64 static int function_index;
65 static int slices = 16;
66 static int stacks = 16;
67 static double irad = .25;
68 static double orad = 1.0; /* doubles as size for objects other than Torus */
70 static double offset[ 3 ] = { 0, 0, 0 };
71 static GLboolean show_info = GL_TRUE;
73 static GLboolean persProject = GL_TRUE;
74 static GLboolean animateXRot = GL_FALSE;
77 * These one-liners draw particular objects, fetching appropriate
78 * information from the above globals. They are just thin wrappers
79 * for the FreeGLUT objects.
81 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
82 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
83 static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
84 static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
85 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
86 static void drawWireOctahedron(void) { glutWireOctahedron (); }
87 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
88 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
89 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
90 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
91 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
92 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
93 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
94 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
95 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
96 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
97 static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
98 static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
99 static void drawSolidCone(void) { glutSolidCone(orad,orad,slices,stacks); } /* orad doubles as size input */
100 static void drawWireCone(void) { glutWireCone(orad,orad,slices,stacks); } /* orad doubles as size input */
101 static void drawSolidCylinder(void) { glutSolidCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
102 static void drawWireCylinder(void) { glutWireCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
105 * This structure defines an entry in our function-table.
109 const char * const name;
110 void (*solid) (void);
114 #define ENTRY(e) {#e, drawSolid##e, drawWire##e}
115 static const entry table [] =
120 ENTRY (Dodecahedron),
121 ENTRY (RhombicDodecahedron),
123 ENTRY (SierpinskiSponge),
124 /* ENTRY (Teapot), */
129 /* ENTRY (Cuboctahedron) */
134 Does printf()-like work using freeglut
135 glutBitmapString(). Uses a fixed font. Prints
136 at the indicated row/column position.
138 Limitation: Cannot address pixels.
139 Limitation: Renders in screen coords, not model coords.
141 static void shapesPrintf (int row, int col, const char *fmt, ...)
145 /* GLUT callback Handlers */
148 resize(int width, int height)
150 ar = (float) width / (float) height;
152 glViewport(0, 0, width, height);
155 static void display(void)
157 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
158 const double a = t*90.0;
159 const double b = (animateXRot?t:1)*60.0;
161 glMatrixMode(GL_PROJECTION);
163 glFrustumf(-ar, ar, -1.0, 1.0, 2.0, 100.0);
164 glMatrixMode(GL_MODELVIEW);
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
169 glEnable(GL_LIGHTING);
174 glTranslated(0,1.2,-6);
177 table [function_index].solid ();
181 glTranslated(0,-1.2,-6);
184 table [function_index].wire ();
187 glDisable(GL_LIGHTING);
188 glColor4f(0.1,0.1,0.4,1.0);
195 key(unsigned char key, int x, int y)
201 case 'q': glutLeaveMainLoop () ; break;
204 case 'i': show_info = ( show_info == GL_TRUE ) ? GL_FALSE : GL_TRUE; break;
207 case '+': slices++; printf("%d,%d\n", slices, stacks); break;
210 case '_': if( slices > -1 ) slices--; break;
213 case '<': if( stacks > -1 ) stacks--; break;
216 case '>': stacks++; break;
219 case '(': if( depth > -1 ) depth--; break;
222 case ')': ++depth; break;
225 case 'p': persProject=!persProject; break;
228 case 'r': animateXRot=!animateXRot; break;
237 static void special (int key, int x, int y)
241 case GLUT_KEY_PAGE_UP: ++function_index; break;
242 case GLUT_KEY_PAGE_DOWN: --function_index; break;
243 case GLUT_KEY_UP: orad *= 2; break;
244 case GLUT_KEY_DOWN: orad /= 2; break;
246 case GLUT_KEY_RIGHT: irad *= 2; break;
247 case GLUT_KEY_LEFT: irad /= 2; break;
253 if (0 > function_index)
254 function_index = NUMBEROF (table) - 1;
256 if (NUMBEROF (table) <= ( unsigned )function_index)
268 onMouseClick(int button, int state, int x, int y) {
269 if (state == GLUT_DOWN)
270 special(GLUT_KEY_PAGE_UP, 0, 0);
273 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
274 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
275 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
276 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
278 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
279 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
280 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
281 const GLfloat high_shininess[] = { 100.0f };
283 /* Program entry point */
285 void init_context() {
286 printf("init_context\n"); fflush(stdout);
287 glClearColor(1,1,1,1);
288 glEnable(GL_CULL_FACE);
291 glEnable(GL_DEPTH_TEST);
292 glDepthFunc(GL_LESS);
295 glEnable(GL_NORMALIZE);
296 glEnable(GL_COLOR_MATERIAL);
298 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
299 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
300 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
301 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
303 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
304 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
305 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
306 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
310 main(int argc, char *argv[])
312 glutInitWindowSize(640,480);
313 glutInitWindowPosition(40,40);
314 glutInit(&argc, argv);
315 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
317 glutCreateWindow("FreeGLUT Shapes");
319 glutReshapeFunc(resize);
320 glutDisplayFunc(display);
321 glutKeyboardFunc(key);
322 glutSpecialFunc(special);
324 glutMouseFunc(onMouseClick);
325 glutInitContextFunc(init_context);
327 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
332 /* DUMP MEMORY LEAK INFORMATION */
333 _CrtDumpMemoryLeaks () ;