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>r R </tt> Toggle fixed or animated rotation around model X-axis
18 - <tt>s S </tt> Toggle toggle fixed function or shader render path
19 - <tt>n N </tt> Toggle visualization of object's normal vectors
20 - <tt>= + </tt> Increase \a slices
21 - <tt>- _ </tt> Decreate \a slices
22 - <tt>, < </tt> Decreate \a stacks
23 - <tt>. > </tt> Increase \a stacks
24 - <tt>9 ( </tt> Decreate \a depth (Sierpinski Sponge)
25 - <tt>0 ) </tt> Increase \a depth (Sierpinski Sponge)
26 - <tt>up </tt> Increase "outer radius"
27 - <tt>down </tt> Decrease "outer radius"
28 - <tt>left </tt> Decrease "inner radius"
29 - <tt>right</tt> Increase "inner radius"
30 - <tt>PgUp </tt> Next shape-drawing function
31 - <tt>PgDn </tt> Prev shape-drawing function
33 \author Written by Nigel Stewart November 2003
35 \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
36 OpenGLUT branched from freeglut in February, 2004.
38 \image html openglut_shapes.png OpenGLUT Geometric Shapes Demonstration
39 \include demos/shapes/shapes.c
42 #include <GL/freeglut.h>
52 /* DUMP MEMORY LEAKS */
56 /* report GL errors, if any, to stderr */
57 void checkError(const char *functionName)
60 while (( error = glGetError() ) != GL_NO_ERROR) {
61 fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
66 * OpenGL 2+ shader mode needs some function and macro definitions,
67 * avoiding a dependency on additional libraries like GLEW or the
70 #ifndef GL_FRAGMENT_SHADER
71 #define GL_FRAGMENT_SHADER 0x8B30
74 #ifndef GL_VERTEX_SHADER
75 #define GL_VERTEX_SHADER 0x8B31
78 #ifndef GL_COMPILE_STATUS
79 #define GL_COMPILE_STATUS 0x8B81
82 #ifndef GL_LINK_STATUS
83 #define GL_LINK_STATUS 0x8B82
86 #ifndef GL_INFO_LOG_LENGTH
87 #define GL_INFO_LOG_LENGTH 0x8B84
90 typedef ptrdiff_t ourGLsizeiptr;
91 typedef char ourGLchar;
97 #ifndef GL_VERSION_2_0
98 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
99 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
100 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
101 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
102 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
103 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
104 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
105 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
106 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
107 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
108 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
109 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
110 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
111 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
112 typedef void (APIENTRY *PFNGLUNIFORMMATRIX3FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
115 PFNGLCREATESHADERPROC gl_CreateShader;
116 PFNGLSHADERSOURCEPROC gl_ShaderSource;
117 PFNGLCOMPILESHADERPROC gl_CompileShader;
118 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
119 PFNGLATTACHSHADERPROC gl_AttachShader;
120 PFNGLLINKPROGRAMPROC gl_LinkProgram;
121 PFNGLUSEPROGRAMPROC gl_UseProgram;
122 PFNGLGETSHADERIVPROC gl_GetShaderiv;
123 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
124 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
125 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
126 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
127 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
128 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
129 PFNGLUNIFORMMATRIX3FVPROC gl_UniformMatrix3fv;
131 void initExtensionEntries(void)
133 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
134 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
135 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
136 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
137 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
138 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
139 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
140 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
141 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
142 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
143 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
144 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
145 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
146 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
147 gl_UniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC) glutGetProcAddress ("glUniformMatrix3fv");
148 if (!gl_CreateShader || !gl_ShaderSource || !gl_CompileShader || !gl_CreateProgram || !gl_AttachShader || !gl_LinkProgram || !gl_UseProgram || !gl_GetShaderiv || !gl_GetShaderInfoLog || !gl_GetProgramiv || !gl_GetProgramInfoLog || !gl_GetAttribLocation || !gl_GetUniformLocation || !gl_UniformMatrix4fv || !gl_UniformMatrix3fv)
150 fprintf (stderr, "glCreateShader, glShaderSource, glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glUseProgram, glGetShaderiv, glGetShaderInfoLog, glGetProgramiv, glGetProgramInfoLog, glGetAttribLocation, glGetUniformLocation, glUniformMatrix4fv or gl_UniformMatrix3fv not found");
155 const ourGLchar *vertexShaderSource[] = {
157 " * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Smooth_Specular_Highlights",
158 " * This file is in the public domain.",
159 " * Contributors: Sylvain Beucler",
161 "attribute vec3 fg_coord;",
162 "attribute vec3 fg_normal;",
163 "varying vec4 position; /* position of the vertex (and fragment) in world space */",
164 "varying vec3 varyingNormalDirection; /* surface normal vector in world space */",
165 "uniform mat4 m, p; /* don't need v, as always identity in our demo */",
166 "uniform mat3 m_3x3_inv_transp;",
170 " vec4 fg_coord4 = vec4(fg_coord, 1.0);",
171 " position = m * fg_coord4;",
172 " varyingNormalDirection = normalize(m_3x3_inv_transp * fg_normal);",
174 " mat4 mvp = p*m; /* normally p*v*m */",
175 " gl_Position = mvp * fg_coord4;",
179 const ourGLchar *fragmentShaderSource[] = {
181 " * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Smooth_Specular_Highlights",
182 " * This file is in the public domain.",
183 " * Contributors: Martin Kraus, Sylvain Beucler",
185 "varying vec4 position; /* position of the vertex (and fragment) in world space */",
186 "varying vec3 varyingNormalDirection; /* surface normal vector in world space */",
187 "/* uniform mat4 v_inv; // in this demo, the view matrix is always an identity matrix */",
189 "struct lightSource",
194 " float constantAttenuation, linearAttenuation, quadraticAttenuation;",
195 " float spotCutoff, spotExponent;",
196 " vec3 spotDirection;",
198 "lightSource light0 = lightSource(",
199 " vec4(2.0, 5.0, 5.0, 0.0),",
200 " vec4(1.0, 1.0, 1.0, 1.0),",
201 " vec4(1.0, 1.0, 1.0, 1.0),",
204 " vec3(0.0, 0.0, 0.0)",
206 "vec4 scene_ambient = vec4(0.2, 0.2, 0.2, 1.0);",
215 "material frontMaterial = material(",
216 " vec4(1.0, 0.0, 0.0, 1.0),",
217 " vec4(1.0, 0.0, 0.0, 1.0),",
218 " vec4(1.0, 1.0, 1.0, 1.0),",
224 " vec3 normalDirection = normalize(varyingNormalDirection);",
225 " /* vec3 viewDirection = normalize(vec3(v_inv * vec4(0.0, 0.0, 0.0, 1.0) - position)); */",
226 " vec3 viewDirection = normalize(vec3(vec4(0.0, 0.0, 0.0, 1.0) - position)); /* in this demo, the view matrix is always an identity matrix */",
227 " vec3 lightDirection;",
228 " float attenuation;",
230 " if (0.0 == light0.position.w) /* directional light? */",
232 " attenuation = 1.0; /* no attenuation */",
233 " lightDirection = normalize(vec3(light0.position));",
235 " else /* point light or spotlight (or other kind of light) */",
237 " vec3 positionToLightSource = vec3(light0.position - position);",
238 " float distance = length(positionToLightSource);",
239 " lightDirection = normalize(positionToLightSource);",
240 " attenuation = 1.0 / (light0.constantAttenuation",
241 " + light0.linearAttenuation * distance",
242 " + light0.quadraticAttenuation * distance * distance);",
244 " if (light0.spotCutoff <= 90.0) /* spotlight? */",
246 " float clampedCosine = max(0.0, dot(-lightDirection, light0.spotDirection));",
247 " if (clampedCosine < cos(radians(light0.spotCutoff))) /* outside of spotlight cone? */",
249 " attenuation = 0.0;",
253 " attenuation = attenuation * pow(clampedCosine, light0.spotExponent); ",
258 " vec3 ambientLighting = vec3(scene_ambient) * vec3(frontMaterial.ambient);",
260 " vec3 diffuseReflection = attenuation ",
261 " * vec3(light0.diffuse) * vec3(frontMaterial.diffuse)",
262 " * max(0.0, dot(normalDirection, lightDirection));",
264 " vec3 specularReflection;",
265 " if (dot(normalDirection, lightDirection) < 0.0) /* light source on the wrong side? */",
267 " specularReflection = vec3(0.0, 0.0, 0.0); /* no specular reflection */",
269 " else /* light source on the right side */",
271 " specularReflection = attenuation * vec3(light0.specular) * vec3(frontMaterial.specular) ",
272 " * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), frontMaterial.shininess);",
275 " gl_FragColor = vec4(ambientLighting + diffuseReflection + specularReflection, 1.0);",
279 GLint getAttribOrUniformLocation(const char* name, GLuint program, GLboolean isAttrib)
283 GLint attrib = gl_GetAttribLocation(program, name);
286 fprintf(stderr, "Warning: Could not bind attrib %s\n", name);
289 checkError ("getAttribOrUniformLocation");
294 GLint uniform = gl_GetUniformLocation(program, name);
297 fprintf(stderr, "Warning: Could not bind uniform %s\n", name);
300 checkError ("getAttribOrUniformLocation");
306 GLint attribute_fg_coord = -1, attribute_fg_normal = -1;
307 GLint uniform_m = -1, uniform_p = -1, uniform_m_3x3_inv_transp = -1;
308 GLint shaderReady = 0; /* Set to 1 when all initialization went well, to -1 when shader somehow unusable. */
312 void compileAndCheck(GLuint shader)
315 gl_CompileShader (shader);
316 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
317 if (status == GL_FALSE) {
320 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
321 infoLog = (ourGLchar*) malloc (infoLogLength);
322 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
323 fprintf (stderr, "compile log: %s\n", infoLog);
326 checkError ("compileAndCheck");
329 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
331 GLuint shader = gl_CreateShader (type);
332 gl_ShaderSource (shader, count, string, NULL);
334 checkError ("compileShaderSource");
336 compileAndCheck (shader);
340 void linkAndCheck(GLuint program)
343 gl_LinkProgram (program);
344 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
345 if (status == GL_FALSE) {
348 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
349 infoLog = (ourGLchar*) malloc (infoLogLength);
350 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
351 fprintf (stderr, "link log: %s\n", infoLog);
354 checkError ("linkAndCheck");
357 void createProgram(GLuint vertexShader, GLuint fragmentShader)
359 program = gl_CreateProgram ();
360 if (vertexShader != 0) {
361 gl_AttachShader (program, vertexShader);
363 if (fragmentShader != 0) {
364 gl_AttachShader (program, fragmentShader);
367 checkError ("createProgram");
369 linkAndCheck (program);
372 void initShader(void)
374 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
375 GLuint vertexShader =
376 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
378 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
379 GLuint fragmentShader =
380 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
382 checkError ("initShader - 1");
384 createProgram (vertexShader, fragmentShader);
386 gl_UseProgram (program);
388 attribute_fg_coord = getAttribOrUniformLocation("fg_coord" , program, GL_TRUE);
389 attribute_fg_normal = getAttribOrUniformLocation("fg_normal" , program, GL_TRUE);
390 uniform_m = getAttribOrUniformLocation("m" , program, GL_FALSE);
391 uniform_p = getAttribOrUniformLocation("p" , program, GL_FALSE);
392 uniform_m_3x3_inv_transp= getAttribOrUniformLocation("m_3x3_inv_transp" , program, GL_FALSE);
396 if (attribute_fg_coord==-1 || attribute_fg_normal==-1 ||
397 uniform_m==-1 || uniform_p==-1 || uniform_m_3x3_inv_transp==-1)
402 checkError ("initShader - 2");
406 * This macro is only intended to be used on arrays, of course.
408 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
411 * These global variables control which object is drawn,
412 * and how it is drawn. No object uses all of these
415 static int function_index;
416 static int slices = 16;
417 static int stacks = 16;
418 static double irad = .25;
419 static double orad = 1.0; /* doubles as size for objects other than Torus */
420 static int depth = 4;
421 static double offset[ 3 ] = { 0, 0, 0 };
422 static GLboolean show_info = GL_TRUE;
424 static GLboolean persProject = GL_TRUE;
425 static GLboolean animateXRot = GL_FALSE;
426 static GLboolean useShader = GL_FALSE;
427 static GLboolean visNormals = GL_FALSE;
428 static GLboolean flat;
431 * Enum to tell drawSizeInfo what to draw for each object
433 #define GEO_NO_SIZE 0
436 #define GEO_INNER_OUTER_RAD 4
438 #define GEO_BASE_HEIGHT 16
439 #define GEO_RAD_HEIGHT 32
442 * These one-liners draw particular objects, fetching appropriate
443 * information from the above globals. They are just thin wrappers
444 * for the FreeGLUT objects.
446 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
447 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
448 static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
449 static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
450 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
451 static void drawWireOctahedron(void) { glutWireOctahedron (); }
452 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
453 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
454 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
455 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
456 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
457 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
458 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
459 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
460 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
461 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
462 static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
463 static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
464 static void drawSolidCone(void) { glutSolidCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
465 static void drawWireCone(void) { glutWireCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
466 static void drawSolidCylinder(void) { glutSolidCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
467 static void drawWireCylinder(void) { glutWireCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
468 /* per Glut manpage, it should be noted that the teapot is rendered
469 * with clockwise winding for front facing polygons...
470 * Same for the teacup and teaspoon
472 static void drawSolidTeapot(void)
473 { glFrontFace(GL_CW); glutSolidTeapot(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
474 static void drawWireTeapot(void)
475 { glFrontFace(GL_CW); glutWireTeapot(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
476 static void drawSolidTeacup(void)
477 { glFrontFace(GL_CW); glutSolidTeacup(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
478 static void drawWireTeacup(void)
479 { glFrontFace(GL_CW); glutWireTeacup(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
480 static void drawSolidTeaspoon(void)
481 { glFrontFace(GL_CW); glutSolidTeaspoon(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
482 static void drawWireTeaspoon(void)
483 { glFrontFace(GL_CW); glutWireTeaspoon(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
485 #define RADIUSFAC 0.70710678118654752440084436210485f
487 static void drawSolidCuboctahedron(void)
489 GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
490 glBegin( GL_TRIANGLES );
491 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( 0.0, radius, radius ); glVertex3d( radius, 0.0, radius );
492 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( radius, 0.0,-radius ); glVertex3d( 0.0, radius,-radius );
493 glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( radius,-radius, 0.0 ); glVertex3d( radius, 0.0, radius ); glVertex3d( 0.0,-radius, radius );
494 glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( radius,-radius, 0.0 ); glVertex3d( 0.0,-radius,-radius ); glVertex3d( radius, 0.0,-radius );
495 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d(-radius, 0.0, radius ); glVertex3d( 0.0, radius, radius );
496 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d( 0.0, radius,-radius ); glVertex3d(-radius, 0.0,-radius );
497 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-radius,-radius, 0.0 ); glVertex3d( 0.0,-radius, radius ); glVertex3d(-radius, 0.0, radius );
498 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-radius,-radius, 0.0 ); glVertex3d(-radius, 0.0,-radius ); glVertex3d( 0.0,-radius,-radius );
502 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 );
503 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 );
504 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 );
505 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 );
506 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 );
507 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 );
511 static void drawWireCuboctahedron(void)
513 GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
514 glBegin( GL_LINE_LOOP );
515 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 );
517 glBegin( GL_LINE_LOOP );
518 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 );
520 glBegin( GL_LINE_LOOP );
521 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 );
523 glBegin( GL_LINE_LOOP );
524 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 );
526 glBegin( GL_LINE_LOOP );
527 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 );
529 glBegin( GL_LINE_LOOP );
530 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 );
537 * This structure defines an entry in our function-table.
541 const char * const name;
542 void (*solid) (void);
544 int drawSizeInfoFlag;
547 #define ENTRY(e,f) {#e, drawSolid##e, drawWire##e,f}
548 static const entry table [] =
550 ENTRY (Tetrahedron,GEO_NO_SIZE),
551 ENTRY (Cube,GEO_SIZE),
552 ENTRY (Octahedron,GEO_NO_SIZE),
553 ENTRY (Dodecahedron,GEO_NO_SIZE),
554 ENTRY (RhombicDodecahedron,GEO_NO_SIZE),
555 ENTRY (Icosahedron,GEO_NO_SIZE),
556 ENTRY (SierpinskiSponge,GEO_SCALE),
557 ENTRY (Teapot,GEO_SIZE),
558 ENTRY (Teacup,GEO_SIZE),
559 ENTRY (Teaspoon,GEO_SIZE),
560 ENTRY (Torus,GEO_INNER_OUTER_RAD),
561 ENTRY (Sphere,GEO_RAD),
562 ENTRY (Cone,GEO_BASE_HEIGHT),
563 ENTRY (Cylinder,GEO_RAD_HEIGHT),
564 ENTRY (Cuboctahedron,GEO_SIZE) /* This one doesn't work when in shader mode and is then skipped */
569 Does printf()-like work using freeglut
570 glutBitmapString(). Uses a fixed font. Prints
571 at the indicated row/column position.
573 Limitation: Cannot address pixels.
574 Limitation: Renders in screen coords, not model coords.
576 static void shapesPrintf (int row, int col, const char *fmt, ...)
578 static char buf[256];
580 void *font = GLUT_BITMAP_9_BY_15;
584 #if defined(WIN32) && !defined(__CYGWIN__)
585 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
587 (void) vsnprintf (buf, sizeof(buf), fmt, args);
591 glGetIntegerv(GL_VIEWPORT,viewport);
596 glMatrixMode(GL_PROJECTION);
600 glOrtho(0,viewport[2],0,viewport[3],-1,1);
604 glutBitmapWidth(font, ' ') * col,
605 - glutBitmapHeight(font) * row + viewport[3]
607 glutBitmapString (font, (unsigned char*)buf);
610 glMatrixMode(GL_MODELVIEW);
614 /* Print info about the about the current shape and render state on the screen */
615 static void DrawSizeInfo(int *row)
617 switch (table [function_index].drawSizeInfoFlag)
622 shapesPrintf ((*row)++, 1, "Size Up Down : %f", orad);
625 shapesPrintf ((*row)++, 1, "Scale Up Down : %f", orad);
627 case GEO_INNER_OUTER_RAD:
628 shapesPrintf ((*row)++, 1, "Inner radius Left Right: %f", irad);
629 shapesPrintf ((*row)++, 1, "Outer radius Up Down : %f", orad);
632 shapesPrintf ((*row)++, 1, "Radius Up Down : %f", orad);
634 case GEO_BASE_HEIGHT:
635 shapesPrintf ((*row)++, 1, "Base Left Right: %f", irad);
636 shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
639 shapesPrintf ((*row)++, 1, "Radius Left Right: %f", irad);
640 shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
645 static void drawInfo()
648 shapesPrintf (row++, 1, "Shape PgUp PgDn: %s", table [function_index].name);
649 shapesPrintf (row++, 1, "Slices +-: %d Stacks <>: %d", slices, stacks);
650 shapesPrintf (row++, 1, "nSides +-: %d nRings <>: %d", slices, stacks);
651 shapesPrintf (row++, 1, "Depth (): %d", depth);
654 shapesPrintf (row++, 1, "Perspective projection (p)");
656 shapesPrintf (row++, 1, "Orthographic projection (p)");
658 shapesPrintf (row++, 1, "Using shader (s)");
660 shapesPrintf (row++, 1, "Using fixed function pipeline (s)");
662 shapesPrintf (row++, 1, "Flat shading (f)");
664 shapesPrintf (row++, 1, "Smooth shading (f)");
667 shapesPrintf (row++, 1, "2D rotation (r)");
669 shapesPrintf (row++, 1, "1D rotation (r)");
670 shapesPrintf (row++, 1, "visualizing normals: %i (n)",visNormals);
673 /* GLUT callback Handlers */
675 resize(int width, int height)
677 ar = (float) width / (float) height;
679 glViewport(0, 0, width, height);
682 static void display(void)
684 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
685 const double a = t*89.0;
686 const double b = (animateXRot?t:1)*67.0;
688 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
690 glutSetOption(GLUT_GEOMETRY_VISUALIZE_NORMALS,visNormals); /* Normals visualized or not? */
692 glShadeModel(flat ? GL_FLAT : GL_SMOOTH); /* flat or gouraud shading */
694 if (useShader && !shaderReady)
697 if (useShader && shaderReady)
699 /* setup use of shader (and vertex buffer by FreeGLUT) */
700 gl_UseProgram (program);
701 glutSetVertexAttribCoord3(attribute_fg_coord);
702 glutSetVertexAttribNormal(attribute_fg_normal);
703 /* There is also a glutSetVertexAttribTexCoord2, which is used only when drawing the teapot, teacup or teaspoon */
705 gl_matrix_mode(GL_PROJECTION);
708 gl_frustum(-ar, ar, -1.f, 1.f, 2.f, 100.f);
710 gl_ortho(-ar*3, ar*3, -3.f, 3.f, 2.f, 100.f);
711 gl_UniformMatrix4fv (uniform_p, 1, GL_FALSE, get_matrix(GL_PROJECTION));
714 gl_matrix_mode(GL_MODELVIEW);
718 /* Not in reverse order like normal OpenGL, our util library multiplies the matrices in the order they are specified in */
719 gl_rotatef((float)a,0,0,1);
720 gl_rotatef((float)b,1,0,0);
721 gl_translatef(0,1.2f,-6);
722 gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
723 gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
724 table [function_index].solid ();
728 gl_rotatef((float)a,0,0,1);
729 gl_rotatef((float)b,1,0,0);
730 gl_translatef(0,-1.2f,-6);
731 gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
732 gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
733 table [function_index].wire ();
737 glutSetVertexAttribCoord3(-1);
738 glutSetVertexAttribNormal(-1);
740 checkError ("display");
744 /* fixed function pipeline */
745 glMatrixMode(GL_PROJECTION);
748 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
750 glOrtho(-ar*3, ar*3, -3.0, 3.0, 2.0, 100.0);
751 glMatrixMode(GL_MODELVIEW);
754 glEnable(GL_LIGHTING);
759 glTranslated(0,1.2,-6);
762 table [function_index].solid ();
766 glTranslated(0,-1.2,-6);
769 table [function_index].wire ();
772 glDisable(GL_LIGHTING);
773 glColor3d(0.1,0.1,0.4);
777 /* print info to screen */
780 /* print to command line instead */
781 printf ( "Shape %d slides %d stacks %d\n", function_index, slices, stacks ) ;
788 key(unsigned char key, int x, int y)
794 case 'q': glutLeaveMainLoop () ; break;
797 case 'i': show_info=!show_info; break;
800 case '+': slices++; break;
803 case '_': if( slices > -1 ) slices--; break;
806 case '<': if( stacks > -1 ) stacks--; break;
809 case '>': stacks++; break;
812 case '(': if( depth > -1 ) depth--; break;
815 case ')': ++depth; break;
818 case 'p': persProject=!persProject; break;
821 case 'r': animateXRot=!animateXRot; break;
825 useShader=!useShader;
826 /* Cuboctahedron can't be shown when in shader mode, move to next */
827 if (useShader && NUMBEROF (table)-1 == ( unsigned )function_index)
837 case 'n': visNormals=!visNormals; break;
846 static void special (int key, int x, int y)
850 case GLUT_KEY_PAGE_UP: ++function_index; break;
851 case GLUT_KEY_PAGE_DOWN: --function_index; break;
852 case GLUT_KEY_UP: orad *= 2; break;
853 case GLUT_KEY_DOWN: orad /= 2; break;
855 case GLUT_KEY_RIGHT: irad *= 2; break;
856 case GLUT_KEY_LEFT: irad /= 2; break;
862 if (0 > function_index)
863 function_index = NUMBEROF (table) - 1;
865 if (NUMBEROF (table) <= ( unsigned )function_index)
868 /* Cuboctahedron can't be shown when in shader mode, skip it */
869 if (useShader && NUMBEROF (table)-1 == ( unsigned )function_index)
871 if (key==GLUT_KEY_PAGE_UP)
885 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
886 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
887 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
888 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
890 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
891 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
892 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
893 const GLfloat high_shininess[] = { 100.0f };
895 /* Program entry point */
898 main(int argc, char *argv[])
900 glutInitWindowSize(800,600);
901 glutInitWindowPosition(40,40);
902 glutInit(&argc, argv);
903 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
905 glutCreateWindow("FreeGLUT Shapes");
907 glutReshapeFunc(resize);
908 glutDisplayFunc(display);
909 glutKeyboardFunc(key);
910 glutSpecialFunc(special);
913 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
915 glClearColor(1,1,1,1);
916 glEnable(GL_CULL_FACE);
919 glEnable(GL_DEPTH_TEST);
920 glDepthFunc(GL_LESS);
923 glEnable(GL_NORMALIZE);
924 glEnable(GL_COLOR_MATERIAL);
926 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
927 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
928 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
929 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
931 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
932 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
933 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
934 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
936 initExtensionEntries();
941 /* DUMP MEMORY LEAK INFORMATION */
942 _CrtDumpMemoryLeaks () ;