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])))
55 #define glRotated glRotatef
56 #define glTranslated glTranslatef
59 * These global variables control which object is drawn,
60 * and how it is drawn. No object uses all of these
63 static int function_index;
64 static int slices = 16;
65 static int stacks = 16;
66 static double irad = .25;
67 static double orad = 1.0; /* doubles as size for objects other than Torus */
69 static double offset[ 3 ] = { 0, 0, 0 };
70 static GLboolean show_info = GL_TRUE;
72 static GLboolean persProject = GL_TRUE;
73 static GLboolean animateXRot = GL_FALSE;
76 * These one-liners draw particular objects, fetching appropriate
77 * information from the above globals. They are just thin wrappers
78 * for the FreeGLUT objects.
80 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
81 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
82 static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
83 static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
84 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
85 static void drawWireOctahedron(void) { glutWireOctahedron (); }
86 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
87 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
88 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
89 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
90 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
91 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
92 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
93 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
94 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
95 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
96 static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
97 static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
98 static void drawSolidCone(void) { glutSolidCone(orad,orad,slices,stacks); } /* orad doubles as size input */
99 static void drawWireCone(void) { glutWireCone(orad,orad,slices,stacks); } /* orad doubles as size input */
100 static void drawSolidCylinder(void) { glutSolidCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
101 static void drawWireCylinder(void) { glutWireCylinder(orad,orad,slices,stacks); } /* orad doubles as size input */
104 * This structure defines an entry in our function-table.
108 const char * const name;
109 void (*solid) (void);
113 #define ENTRY(e) {#e, drawSolid##e, drawWire##e}
114 static const entry table [] =
119 ENTRY (Dodecahedron),
120 ENTRY (RhombicDodecahedron),
122 ENTRY (SierpinskiSponge),
123 /* ENTRY (Teapot), */
128 /* ENTRY (Cuboctahedron) */
133 Does printf()-like work using freeglut
134 glutBitmapString(). Uses a fixed font. Prints
135 at the indicated row/column position.
137 Limitation: Cannot address pixels.
138 Limitation: Renders in screen coords, not model coords.
140 static void shapesPrintf (int row, int col, const char *fmt, ...)
144 /* GLUT callback Handlers */
147 resize(int width, int height)
149 ar = (float) width / (float) height;
151 glViewport(0, 0, width, height);
154 static void display(void)
156 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
157 const double a = t*90.0;
158 const double b = (animateXRot?t:1)*60.0;
160 glMatrixMode(GL_PROJECTION);
162 glFrustumf(-ar, ar, -1.0, 1.0, 2.0, 100.0);
163 glMatrixMode(GL_MODELVIEW);
166 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
168 glEnable(GL_LIGHTING);
173 glTranslated(0,1.2,-6);
176 table [function_index].solid ();
180 glTranslated(0,-1.2,-6);
183 table [function_index].wire ();
186 glDisable(GL_LIGHTING);
187 glColor4f(0.1,0.1,0.4,1.0);
194 key(unsigned char key, int x, int y)
200 case 'q': glutLeaveMainLoop () ; break;
203 case 'i': show_info = ( show_info == GL_TRUE ) ? GL_FALSE : GL_TRUE; break;
206 case '+': slices++; printf("%d,%d\n", slices, stacks); break;
209 case '_': if( slices > -1 ) slices--; break;
212 case '<': if( stacks > -1 ) stacks--; break;
215 case '>': stacks++; break;
218 case '(': if( depth > -1 ) depth--; break;
221 case ')': ++depth; break;
224 case 'p': persProject=!persProject; break;
227 case 'r': animateXRot=!animateXRot; break;
236 static void special (int key, int x, int y)
240 case GLUT_KEY_PAGE_UP: ++function_index; break;
241 case GLUT_KEY_PAGE_DOWN: --function_index; break;
242 case GLUT_KEY_UP: orad *= 2; break;
243 case GLUT_KEY_DOWN: orad /= 2; break;
245 case GLUT_KEY_RIGHT: irad *= 2; break;
246 case GLUT_KEY_LEFT: irad /= 2; break;
252 if (0 > function_index)
253 function_index = NUMBEROF (table) - 1;
255 if (NUMBEROF (table) <= ( unsigned )function_index)
267 onMouseClick(int button, int state, int x, int y) {
268 if (state == GLUT_DOWN)
269 special(GLUT_KEY_PAGE_UP, 0, 0);
272 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
273 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
274 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
275 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
277 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
278 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
279 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
280 const GLfloat high_shininess[] = { 100.0f };
282 /* Program entry point */
285 main(int argc, char *argv[])
287 glutInitWindowSize(640,480);
288 glutInitWindowPosition(40,40);
289 glutInit(&argc, argv);
290 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
292 glutCreateWindow("FreeGLUT Shapes");
294 glutReshapeFunc(resize);
295 glutDisplayFunc(display);
296 glutKeyboardFunc(key);
297 glutSpecialFunc(special);
299 glutMouseFunc(onMouseClick);
301 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
303 glClearColor(1,1,1,1);
304 glEnable(GL_CULL_FACE);
307 glEnable(GL_DEPTH_TEST);
308 glDepthFunc(GL_LESS);
311 glEnable(GL_NORMALIZE);
312 glEnable(GL_COLOR_MATERIAL);
314 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
315 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
316 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
317 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
319 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
320 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
321 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
322 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
327 /* DUMP MEMORY LEAK INFORMATION */
328 _CrtDumpMemoryLeaks () ;