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>= + </tt> Increase \a slices
17 - <tt>- _ </tt> Decreate \a slices
18 - <tt>, < </tt> Decreate \a stacks
19 - <tt>. > </tt> Increase \a stacks
20 - <tt>9 ( </tt> Decreate \a depth (Sierpinski Sponge)
21 - <tt>0 ) </tt> Increase \a depth (Sierpinski Sponge)
22 - <tt>up </tt> Increase "outer radius"
23 - <tt>down </tt> Decrease "outer radius"
24 - <tt>left </tt> Decrease "inner radius"
25 - <tt>right</tt> Increase "inner radius"
26 - <tt>PgUp </tt> Next shape-drawing function
27 - <tt>PgDn </tt> Prev shape-drawing function
29 \author Written by Nigel Stewart November 2003
31 \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
32 OpenGLUT branched from freeglut in February, 2004.
34 \image html openglut_shapes.png OpenGLUT Geometric Shapes Demonstration
35 \include demos/shapes/shapes.c
38 #include <GL/freeglut.h>
45 /* DUMP MEMORY LEAKS */
50 * This macro is only intended to be used on arrays, of course.
52 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
55 * These global variables control which object is drawn,
56 * and how it is drawn. No object uses all of these
59 static int function_index;
60 static int slices = 16;
61 static int stacks = 16;
62 static double irad = .25;
63 static double orad = 1.0;
65 static double offset[ 3 ] = { 0, 0, 0 };
66 static GLboolean show_info = GL_TRUE;
69 * These one-liners draw particular objects, fetching appropriate
70 * information from the above globals. They are just thin wrappers
71 * for the OpenGLUT objects.
73 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
74 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
75 static void drawSolidCube(void) { glutSolidCube(1); }
76 static void drawWireCube(void) { glutWireCube(1); }
77 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
78 static void drawWireOctahedron(void) { glutWireOctahedron (); }
79 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
80 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
81 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
82 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
83 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
84 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
85 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, 1); }
86 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, 1); }
87 static void drawSolidTeapot(void) { glutSolidTeapot(1); }
88 static void drawWireTeapot(void) { glutWireTeapot(1); }
89 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
90 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
91 static void drawSolidSphere(void) { glutSolidSphere(1,slices,stacks); }
92 static void drawWireSphere(void) { glutWireSphere(1,slices,stacks); }
93 static void drawSolidCone(void) { glutSolidCone(1,1,slices,stacks); }
94 static void drawWireCone(void) { glutWireCone(1,1,slices,stacks); }
95 static void drawSolidCylinder(void) { glutSolidCylinder(1,1,slices,stacks); }
96 static void drawWireCylinder(void) { glutWireCylinder(1,1,slices,stacks); }
100 static void drawSolidCuboctahedron(void)
102 glBegin( GL_TRIANGLES );
103 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS, RADIUS ); glVertex3d( RADIUS, 0.0, RADIUS );
104 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( RADIUS, RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS,-RADIUS );
105 glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( RADIUS, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, RADIUS );
106 glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS,-RADIUS ); glVertex3d( RADIUS, 0.0,-RADIUS );
107 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, RADIUS );
108 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS,-RADIUS ); glVertex3d(-RADIUS, 0.0,-RADIUS );
109 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS, 0.0, RADIUS );
110 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS,-RADIUS );
114 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 );
115 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 );
116 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 );
117 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 );
118 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 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 );
123 static void drawWireCuboctahedron(void)
125 glBegin( GL_LINE_LOOP );
126 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 );
128 glBegin( GL_LINE_LOOP );
129 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 );
131 glBegin( GL_LINE_LOOP );
132 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 );
134 glBegin( GL_LINE_LOOP );
135 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 );
137 glBegin( GL_LINE_LOOP );
138 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 );
140 glBegin( GL_LINE_LOOP );
141 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 );
148 * This structure defines an entry in our function-table.
152 const char * const name;
153 void (*solid) (void);
157 #define ENTRY(e) {#e, drawSolid##e, drawWire##e}
158 static const entry table [] =
163 ENTRY (Dodecahedron),
164 ENTRY (RhombicDodecahedron),
166 ENTRY (SierpinskiSponge),
172 ENTRY (Cuboctahedron)
177 Does printf()-like work using freeglut/OpenGLUT
178 glutBitmapString(). Uses a fixed font. Prints
179 at the indicated row/column position.
181 Limitation: Cannot address pixels.
182 Limitation: Renders in screen coords, not model coords.
184 static void shapesPrintf (int row, int col, const char *fmt, ...)
186 static char buf[256];
188 void *font = GLUT_BITMAP_9_BY_15;
192 #if defined(WIN32) && !defined(__CYGWIN__)
193 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
195 (void) vsnprintf (buf, sizeof(buf), fmt, args);
199 glGetIntegerv(GL_VIEWPORT,viewport);
204 glMatrixMode(GL_PROJECTION);
208 glOrtho(0,viewport[2],0,viewport[3],-1,1);
212 glutBitmapWidth(font, ' ') * col,
213 - glutBitmapHeight(font) * (row+2) + viewport[3]
215 glutBitmapString (font, (unsigned char*)buf);
218 glMatrixMode(GL_MODELVIEW);
222 /* GLUT callback Handlers */
225 resize(int width, int height)
227 const float ar = (float) width / (float) height;
229 glViewport(0, 0, width, height);
231 glMatrixMode(GL_PROJECTION);
233 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
235 glMatrixMode(GL_MODELVIEW);
239 static void display(void)
241 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
242 const double a = t*90.0;
244 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
246 glEnable(GL_LIGHTING);
251 glTranslated(0,1.2,-6);
254 table [function_index].solid ();
258 glTranslated(0,-1.2,-6);
261 table [function_index].wire ();
264 glDisable(GL_LIGHTING);
265 glColor3d(0.1,0.1,0.4);
268 shapesPrintf (1, 3, "Shape PgUp PgDn: %s", table [function_index].name);
269 shapesPrintf (2, 3, "Slices +-: %d Stacks <>: %d", slices, stacks);
270 shapesPrintf (3, 3, "nSides +-: %d nRings <>: %d", slices, stacks);
271 shapesPrintf (4, 3, "Depth (): %d", depth);
272 shapesPrintf (5, 3, "Outer radius Up Down : %f", orad);
273 shapesPrintf (6, 3, "Inner radius Left Right: %f", irad);
275 printf ( "Shape %d slides %d stacks %d\n", function_index, slices, stacks ) ;
283 key(unsigned char key, int x, int y)
289 case 'q': glutLeaveMainLoop () ; break;
292 case 'i': show_info = ( show_info == GL_TRUE ) ? GL_FALSE : GL_TRUE; break;
295 case '+': slices++; break;
298 case '_': if( slices > -1 ) slices--; break;
301 case '<': if( stacks > -1 ) stacks--; break;
304 case '>': stacks++; break;
307 case '(': if( depth > -1 ) depth--; break;
310 case ')': ++depth; break;
319 static void special (int key, int x, int y)
323 case GLUT_KEY_PAGE_UP: ++function_index; break;
324 case GLUT_KEY_PAGE_DOWN: --function_index; break;
325 case GLUT_KEY_UP: orad *= 2; break;
326 case GLUT_KEY_DOWN: orad /= 2; break;
328 case GLUT_KEY_RIGHT: irad *= 2; break;
329 case GLUT_KEY_LEFT: irad /= 2; break;
335 if (0 > function_index)
336 function_index = NUMBEROF (table) - 1;
338 if (NUMBEROF (table) <= ( unsigned )function_index)
349 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
350 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
351 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
352 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
354 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
355 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
356 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
357 const GLfloat high_shininess[] = { 100.0f };
359 /* Program entry point */
362 main(int argc, char *argv[])
364 glutInitWindowSize(640,480);
365 glutInitWindowPosition(40,40);
366 glutInit(&argc, argv);
367 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
369 glutCreateWindow("OpenGLUT Shapes");
371 glutReshapeFunc(resize);
372 glutDisplayFunc(display);
373 glutKeyboardFunc(key);
374 glutSpecialFunc(special);
377 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
379 glClearColor(1,1,1,1);
380 glEnable(GL_CULL_FACE);
383 glEnable(GL_DEPTH_TEST);
384 glDepthFunc(GL_LESS);
387 glEnable(GL_NORMALIZE);
388 glEnable(GL_COLOR_MATERIAL);
390 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
391 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
392 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
393 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
395 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
396 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
397 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
398 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
403 /* DUMP MEMORY LEAK INFORMATION */
404 _CrtDumpMemoryLeaks () ;