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>
46 /* DUMP MEMORY LEAKS */
51 * This macro is only intended to be used on arrays, of course.
53 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
56 * These global variables control which object is drawn,
57 * and how it is drawn. No object uses all of these
60 static int function_index;
61 static int slices = 16;
62 static int stacks = 16;
63 static double irad = .25;
64 static double orad = 1.0; /* doubles as size for objects other than Torus */
66 static double offset[ 3 ] = { 0, 0, 0 };
67 static GLboolean show_info = GL_TRUE;
69 static GLboolean persProject = GL_TRUE;
72 * These one-liners draw particular objects, fetching appropriate
73 * information from the above globals. They are just thin wrappers
74 * for the FreeGLUT objects.
76 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
77 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
78 static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
79 static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
80 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
81 static void drawWireOctahedron(void) { glutWireOctahedron (); }
82 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
83 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
84 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
85 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
86 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
87 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
88 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
89 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
90 static void drawSolidTeapot(void) { glutSolidTeapot(orad); } /* orad doubles as size input */
91 static void drawWireTeapot(void) { glutWireTeapot(orad); } /* orad doubles as size input */
92 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
93 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
94 static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
95 static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
96 static void drawSolidCone(void) { glutSolidCone(orad,orad,slices,stacks); } /* orad doubles as size input */
97 static void drawWireCone(void) { glutWireCone(orad,orad,slices,stacks); } /* orad doubles as size input */
98 static void drawSolidCylinder(void) { glutSolidCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
99 static void drawWireCylinder(void) { glutWireCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
103 static void drawSolidCuboctahedron(void)
105 glBegin( GL_TRIANGLES );
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( RADIUS, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, 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 );
111 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-RADIUS, RADIUS, 0.0 ); glVertex3d( 0.0, RADIUS,-RADIUS ); glVertex3d(-RADIUS, 0.0,-RADIUS );
112 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS, 0.0, RADIUS );
113 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-RADIUS,-RADIUS, 0.0 ); glVertex3d(-RADIUS, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS,-RADIUS );
117 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 );
118 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 );
119 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 );
120 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 );
121 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 );
122 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 );
126 static void drawWireCuboctahedron(void)
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(-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 );
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,-1.0, 0.0 ); glVertex3d( RADIUS,-RADIUS, 0.0 ); glVertex3d( 0.0,-RADIUS, RADIUS ); glVertex3d(-RADIUS,-RADIUS, 0.0 ); 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 );
143 glBegin( GL_LINE_LOOP );
144 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 );
151 * This structure defines an entry in our function-table.
155 const char * const name;
156 void (*solid) (void);
160 #define ENTRY(e) {#e, drawSolid##e, drawWire##e}
161 static const entry table [] =
166 ENTRY (Dodecahedron),
167 ENTRY (RhombicDodecahedron),
169 ENTRY (SierpinskiSponge),
175 ENTRY (Cuboctahedron)
180 Does printf()-like work using freeglut
181 glutBitmapString(). Uses a fixed font. Prints
182 at the indicated row/column position.
184 Limitation: Cannot address pixels.
185 Limitation: Renders in screen coords, not model coords.
187 static void shapesPrintf (int row, int col, const char *fmt, ...)
189 static char buf[256];
191 void *font = GLUT_BITMAP_9_BY_15;
195 #if defined(WIN32) && !defined(__CYGWIN__)
196 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
198 (void) vsnprintf (buf, sizeof(buf), fmt, args);
202 glGetIntegerv(GL_VIEWPORT,viewport);
207 glMatrixMode(GL_PROJECTION);
211 glOrtho(0,viewport[2],0,viewport[3],-1,1);
215 glutBitmapWidth(font, ' ') * col,
216 - glutBitmapHeight(font) * row + viewport[3]
218 glutBitmapString (font, (unsigned char*)buf);
221 glMatrixMode(GL_MODELVIEW);
225 /* GLUT callback Handlers */
228 resize(int width, int height)
230 ar = (float) width / (float) height;
232 glViewport(0, 0, width, height);
235 static void display(void)
237 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
238 const double a = t*90.0;
240 glMatrixMode(GL_PROJECTION);
243 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
245 glOrtho(-ar*3, ar*3, -3.0, 3.0, 2.0, 100.0);
246 glMatrixMode(GL_MODELVIEW);
249 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
251 glEnable(GL_LIGHTING);
256 glTranslated(0,1.2,-6);
259 table [function_index].solid ();
263 glTranslated(0,-1.2,-6);
266 table [function_index].wire ();
269 glDisable(GL_LIGHTING);
270 glColor3d(0.1,0.1,0.4);
273 shapesPrintf (1, 1, "Shape PgUp PgDn: %s", table [function_index].name);
274 shapesPrintf (2, 1, "Slices +-: %d Stacks <>: %d", slices, stacks);
275 shapesPrintf (3, 1, "nSides +-: %d nRings <>: %d", slices, stacks);
276 shapesPrintf (4, 1, "Depth (): %d", depth);
277 shapesPrintf (5, 1, "Outer radius Up Down : %f", orad);
278 shapesPrintf (6, 1, "Inner radius Left Right: %f", irad);
280 printf ( "Shape %d slides %d stacks %d\n", function_index, slices, stacks ) ;
288 key(unsigned char key, int x, int y)
294 case 'q': glutLeaveMainLoop () ; break;
297 case 'i': show_info = ( show_info == GL_TRUE ) ? GL_FALSE : GL_TRUE; break;
300 case '+': slices++; break;
303 case '_': if( slices > -1 ) slices--; break;
306 case '<': if( stacks > -1 ) stacks--; break;
309 case '>': stacks++; break;
312 case '(': if( depth > -1 ) depth--; break;
315 case ')': ++depth; break;
318 case 'p': persProject=!persProject; break;
327 static void special (int key, int x, int y)
331 case GLUT_KEY_PAGE_UP: ++function_index; break;
332 case GLUT_KEY_PAGE_DOWN: --function_index; break;
333 case GLUT_KEY_UP: orad *= 2; break;
334 case GLUT_KEY_DOWN: orad /= 2; break;
336 case GLUT_KEY_RIGHT: irad *= 2; break;
337 case GLUT_KEY_LEFT: irad /= 2; break;
343 if (0 > function_index)
344 function_index = NUMBEROF (table) - 1;
346 if (NUMBEROF (table) <= ( unsigned )function_index)
357 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
358 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
359 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
360 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
362 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
363 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
364 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
365 const GLfloat high_shininess[] = { 100.0f };
367 /* Program entry point */
370 main(int argc, char *argv[])
372 glutInitWindowSize(640,480);
373 glutInitWindowPosition(40,40);
374 glutInit(&argc, argv);
375 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
377 glutCreateWindow("FreeGLUT Shapes");
379 glutReshapeFunc(resize);
380 glutDisplayFunc(display);
381 glutKeyboardFunc(key);
382 glutSpecialFunc(special);
385 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
387 glClearColor(1,1,1,1);
388 glEnable(GL_CULL_FACE);
391 glEnable(GL_DEPTH_TEST);
392 glDepthFunc(GL_LESS);
395 glEnable(GL_NORMALIZE);
396 glEnable(GL_COLOR_MATERIAL);
398 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
399 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
400 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
401 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
403 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
404 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
405 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
406 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
411 /* DUMP MEMORY LEAK INFORMATION */
412 _CrtDumpMemoryLeaks () ;