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>= + </tt> Increase \a slices
16 - <tt>- _ </tt> Decreate \a slices
17 - <tt>, < </tt> Decreate \a stacks
18 - <tt>. > </tt> Increase \a stacks
19 - <tt>9 ( </tt> Decreate \a depth (Sierpinski Sponge)
20 - <tt>0 ) </tt> Increase \a depth (Sierpinski Sponge)
21 - <tt>up </tt> Increase "outer radius"
22 - <tt>down </tt> Decrease "outer radius"
23 - <tt>left </tt> Decrease "inner radius"
24 - <tt>right</tt> Increase "inner radius"
25 - <tt>PgUp </tt> Next shape-drawing function
26 - <tt>PgDn </tt> Prev shape-drawing function
28 \author Written by Nigel Stewart November 2003
30 \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
31 OpenGLUT branched from freeglut in February, 2004.
33 \image html openglut_shapes.png OpenGLUT Geometric Shapes Demonstration
34 \include demos/shapes/shapes.c
37 #include <GL/freeglut.h>
44 #include <crtdbg.h> // DUMP MEMORY LEAKS
48 * This macro is only intended to be used on arrays, of course.
50 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
53 * These global variables control which object is drawn,
54 * and how it is drawn. No object uses all of these
57 static int function_index;
58 static int slices = 16;
59 static int stacks = 16;
60 static double irad = .25;
61 static double orad = 1.0;
63 static double offset[ 3 ] = { 0, 0, 0 };
67 * These one-liners draw particular objects, fetching appropriate
68 * information from the above globals. They are just thin wrappers
69 * for the OpenGLUT objects.
71 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
72 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
73 static void drawSolidCube(void) { glutSolidCube(1); }
74 static void drawWireCube(void) { glutWireCube(1); }
75 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
76 static void drawWireOctahedron(void) { glutWireOctahedron (); }
77 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
78 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
79 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
80 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
81 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
82 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
83 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, 1); }
84 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, 1); }
85 static void drawSolidTeapot(void) { glutSolidTeapot(1); }
86 static void drawWireTeapot(void) { glutWireTeapot(1); }
87 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
88 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
89 static void drawSolidSphere(void) { glutSolidSphere(1,slices,stacks); }
90 static void drawWireSphere(void) { glutWireSphere(1,slices,stacks); }
91 static void drawSolidCone(void) { glutSolidCone(1,1,slices,stacks); }
92 static void drawWireCone(void) { glutWireCone(1,1,slices,stacks); }
93 static void drawSolidCylinder(void) { glutSolidCylinder(1,1,slices,stacks); }
94 static void drawWireCylinder(void) { glutWireCylinder(1,1,slices,stacks); }
95 static void drawSolidCuboctahedron(void)
98 glBegin( GL_TRIANGLES );
99 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS, RADIUS ); glVertex3d( RADIUS, 0.0, RADIUS );
100 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS,-RADIUS );
101 glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, RADIUS );
102 glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS,-RADIUS ); glVertex3d( RADIUS, 0.0,-RADIUS );
103 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, RADIUS );
104 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS,-RADIUS ); glVertex3d(-RADIUS, 0.0,-RADIUS );
105 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS, 0.0, RADIUS );
106 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS,-RADIUS );
110 glNormal3d( 1.0, 0.0, 0.0 ); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0,-RADIUS );
111 glNormal3d(-1.0, 0.0, 0.0 ); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0, RADIUS );
112 glNormal3d( 0.0, 1.0, 0.0 ); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS,-RADIUS ); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS, RADIUS );
113 glNormal3d( 0.0,-1.0, 0.0 ); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS,-RADIUS );
114 glNormal3d( 0.0, 0.0, 1.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, RADIUS ); glVertex3d(-RADIUS, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, RADIUS );
115 glNormal3d( 0.0, 0.0,-1.0 ); glVertex3d( RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS,-RADIUS ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS,-RADIUS );
119 static void drawWireCuboctahedron(void)
124 * This structure defines an entry in our function-table.
128 const char * const name;
129 void (*solid) (void);
133 #define ENTRY(e) {#e, drawSolid##e, drawWire##e}
134 static const entry table [] =
139 ENTRY (Dodecahedron),
140 ENTRY (RhombicDodecahedron),
142 ENTRY (SierpinskiSponge),
148 ENTRY (Cuboctahedron)
153 Does printf()-like work using freeglut/OpenGLUT
154 glutBitmapString(). Uses a fixed font. Prints
155 at the indicated row/column position.
157 Limitation: Cannot address pixels.
158 Limitation: Renders in screen coords, not model coords.
160 \note Uses a fixed, 256-byte array for holding strings.
161 The best way around this would be to use vasprintf(),
162 but that is not available on WIN32, I believe.
163 Another alternative would be to write our own formatter
164 from scratch and emit the characters one at a time to
165 the GLUT bitmap single-character drawing routine.
166 We could also use vsnprintf(), but I'm not sure if
169 static void shapesPrintf (int row, int col, const char *fmt, ...)
171 static char buf[256];
173 void *font = GLUT_BITMAP_9_BY_15;
177 (void) vsprintf (buf, fmt, args);
180 glGetIntegerv(GL_VIEWPORT,viewport);
185 glMatrixMode(GL_PROJECTION);
189 glOrtho(0,viewport[2],0,viewport[3],-1,1);
193 glutBitmapWidth(font, ' ') * col,
194 - glutBitmapHeight(font) * (row+2) + viewport[3]
196 glutBitmapString (font, buf);
199 glMatrixMode(GL_MODELVIEW);
203 /* GLUT callback Handlers */
206 resize(int width, int height)
208 const float ar = (float) width / (float) height;
210 glViewport(0, 0, width, height);
212 glMatrixMode(GL_PROJECTION);
214 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
216 glMatrixMode(GL_MODELVIEW);
220 static void display(void)
222 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
223 const double a = t*90.0;
225 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
227 glEnable(GL_LIGHTING);
229 printf ( "Shape %d slides %d stacks %d\n", function_index, slices, stacks ) ;
234 glTranslated(0,1.2,-6);
237 table [function_index].solid ();
241 glTranslated(0,-1.2,-6);
244 table [function_index].wire ();
247 glDisable(GL_LIGHTING);
248 glColor3d(0.1,0.1,0.4);
250 /* shapesPrintf (1, 3, "Shape PgUp PgDn: %s", table [function_index].name);
251 shapesPrintf (2, 3, " Slices +-: %d Stacks <>: %d", slices, stacks);
252 shapesPrintf (3, 3, " nSides +-: %d nRings <>: %d", slices, stacks);
253 shapesPrintf (4, 3, " Depth (): %d", depth);
254 shapesPrintf (5, 3, " Outer radius Up Down : %f", orad);
255 shapesPrintf (6, 3, " Inner radius Left Right: %f", irad);
262 key(unsigned char key, int x, int y)
268 case 'q': glutLeaveMainLoop () ; break;
271 case '+': slices++; break;
274 case '_': if( slices > -1 ) slices--; break;
277 case '<': if( stacks > -1 ) stacks--; break;
280 case '>': stacks++; break;
283 case '(': if( depth > -1 ) depth--; break;
286 case ')': ++depth; break;
295 static void special (int key, int x, int y)
299 case GLUT_KEY_PAGE_UP: ++function_index; break;
300 case GLUT_KEY_PAGE_DOWN: --function_index; break;
301 case GLUT_KEY_UP: orad *= 2; break;
302 case GLUT_KEY_DOWN: orad /= 2; break;
304 case GLUT_KEY_RIGHT: irad *= 2; break;
305 case GLUT_KEY_LEFT: irad /= 2; break;
311 if (0 > function_index)
312 function_index = NUMBEROF (table) - 1;
314 if (NUMBEROF (table) <= ( unsigned )function_index)
325 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
326 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
327 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
328 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
330 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
331 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
332 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
333 const GLfloat high_shininess[] = { 100.0f };
335 /* Program entry point */
338 main(int argc, char *argv[])
340 glutInitWindowSize(640,480);
341 glutInitWindowPosition(40,40);
342 glutInit(&argc, argv);
343 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
345 glutCreateWindow("OpenGLUT Shapes");
347 glutReshapeFunc(resize);
348 glutDisplayFunc(display);
349 glutKeyboardFunc(key);
350 glutSpecialFunc(special);
353 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
355 glClearColor(1,1,1,1);
356 glEnable(GL_CULL_FACE);
359 glEnable(GL_DEPTH_TEST);
360 glDepthFunc(GL_LESS);
363 glEnable(GL_NORMALIZE);
364 glEnable(GL_COLOR_MATERIAL);
366 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
367 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
368 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
369 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
371 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
372 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
373 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
374 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
379 _CrtDumpMemoryLeaks () ; // DUMP MEMORY LEAK INFORMATION