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>
51 /* DUMP MEMORY LEAKS */
55 /* report GL errors, if any, to stderr */
56 void checkError(const char *functionName)
59 while (( error = glGetError() ) != GL_NO_ERROR) {
60 fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
65 * OpenGL 2+ shader mode needs some function and macro definitions,
66 * avoiding a dependency on additional libraries like GLEW or the
69 #ifndef GL_FRAGMENT_SHADER
70 #define GL_FRAGMENT_SHADER 0x8B30
73 #ifndef GL_VERTEX_SHADER
74 #define GL_VERTEX_SHADER 0x8B31
77 #ifndef GL_COMPILE_STATUS
78 #define GL_COMPILE_STATUS 0x8B81
81 #ifndef GL_LINK_STATUS
82 #define GL_LINK_STATUS 0x8B82
85 #ifndef GL_INFO_LOG_LENGTH
86 #define GL_INFO_LOG_LENGTH 0x8B84
89 typedef ptrdiff_t ourGLsizeiptr;
90 typedef char ourGLchar;
96 #ifndef GL_VERSION_2_0
97 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
98 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
99 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
100 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
101 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
102 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
103 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
104 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
105 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
106 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
107 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
108 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
109 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
110 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
111 typedef void (APIENTRY *PFNGLUNIFORMMATRIX3FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
114 PFNGLCREATESHADERPROC gl_CreateShader;
115 PFNGLSHADERSOURCEPROC gl_ShaderSource;
116 PFNGLCOMPILESHADERPROC gl_CompileShader;
117 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
118 PFNGLATTACHSHADERPROC gl_AttachShader;
119 PFNGLLINKPROGRAMPROC gl_LinkProgram;
120 PFNGLUSEPROGRAMPROC gl_UseProgram;
121 PFNGLGETSHADERIVPROC gl_GetShaderiv;
122 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
123 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
124 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
125 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
126 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
127 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
128 PFNGLUNIFORMMATRIX3FVPROC gl_UniformMatrix3fv;
130 void initExtensionEntries(void)
132 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
133 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
134 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
135 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
136 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
137 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
138 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
139 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
140 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
141 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
142 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
143 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
144 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
145 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
146 gl_UniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC) glutGetProcAddress ("glUniformMatrix3fv");
147 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)
149 fprintf (stderr, "glCreateShader, glShaderSource, glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glUseProgram, glGetShaderiv, glGetShaderInfoLog, glGetProgramiv, glGetProgramInfoLog, glGetAttribLocation, glGetUniformLocation, glUniformMatrix4fv or gl_UniformMatrix3fv not found");
154 const ourGLchar *vertexShaderSource[] = {
156 " * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Smooth_Specular_Highlights",
157 " * This file is in the public domain.",
158 " * Contributors: Sylvain Beucler",
160 "attribute vec3 fg_coord;",
161 "attribute vec3 fg_normal;",
162 "varying vec4 position; /* position of the vertex (and fragment) in world space */",
163 "varying vec3 varyingNormalDirection; /* surface normal vector in world space */",
164 "uniform mat4 m, p; /* don't need v, as always identity in our demo */",
165 "uniform mat3 m_3x3_inv_transp;",
169 " vec4 fg_coord4 = vec4(fg_coord, 1.0);",
170 " position = m * fg_coord4;",
171 " varyingNormalDirection = normalize(m_3x3_inv_transp * fg_normal);",
173 " mat4 mvp = p*m; /* normally p*v*m */",
174 " gl_Position = mvp * fg_coord4;",
178 const ourGLchar *fragmentShaderSource[] = {
180 " * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Smooth_Specular_Highlights",
181 " * This file is in the public domain.",
182 " * Contributors: Martin Kraus, Sylvain Beucler",
184 "varying vec4 position; /* position of the vertex (and fragment) in world space */",
185 "varying vec3 varyingNormalDirection; /* surface normal vector in world space */",
186 "/* uniform mat4 v_inv; // in this demo, the view matrix is always an identity matrix */",
188 "struct lightSource",
193 " float constantAttenuation, linearAttenuation, quadraticAttenuation;",
194 " float spotCutoff, spotExponent;",
195 " vec3 spotDirection;",
197 "lightSource light0 = lightSource(",
198 " vec4(2.0, 5.0, 5.0, 0.0),",
199 " vec4(1.0, 1.0, 1.0, 1.0),",
200 " vec4(1.0, 1.0, 1.0, 1.0),",
203 " vec3(0.0, 0.0, 0.0)",
205 "vec4 scene_ambient = vec4(0.2, 0.2, 0.2, 1.0);",
214 "material frontMaterial = material(",
215 " vec4(1.0, 0.0, 0.0, 1.0),",
216 " vec4(1.0, 0.0, 0.0, 1.0),",
217 " vec4(1.0, 1.0, 1.0, 1.0),",
223 " vec3 normalDirection = normalize(varyingNormalDirection);",
224 " /* vec3 viewDirection = normalize(vec3(v_inv * vec4(0.0, 0.0, 0.0, 1.0) - position)); */",
225 " 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 */",
226 " vec3 lightDirection;",
227 " float attenuation;",
229 " if (0.0 == light0.position.w) /* directional light? */",
231 " attenuation = 1.0; /* no attenuation */",
232 " lightDirection = normalize(vec3(light0.position));",
234 " else /* point light or spotlight (or other kind of light) */",
236 " vec3 positionToLightSource = vec3(light0.position - position);",
237 " float distance = length(positionToLightSource);",
238 " lightDirection = normalize(positionToLightSource);",
239 " attenuation = 1.0 / (light0.constantAttenuation",
240 " + light0.linearAttenuation * distance",
241 " + light0.quadraticAttenuation * distance * distance);",
243 " if (light0.spotCutoff <= 90.0) /* spotlight? */",
245 " float clampedCosine = max(0.0, dot(-lightDirection, light0.spotDirection));",
246 " if (clampedCosine < cos(radians(light0.spotCutoff))) /* outside of spotlight cone? */",
248 " attenuation = 0.0;",
252 " attenuation = attenuation * pow(clampedCosine, light0.spotExponent); ",
257 " vec3 ambientLighting = vec3(scene_ambient) * vec3(frontMaterial.ambient);",
259 " vec3 diffuseReflection = attenuation ",
260 " * vec3(light0.diffuse) * vec3(frontMaterial.diffuse)",
261 " * max(0.0, dot(normalDirection, lightDirection));",
263 " vec3 specularReflection;",
264 " if (dot(normalDirection, lightDirection) < 0.0) /* light source on the wrong side? */",
266 " specularReflection = vec3(0.0, 0.0, 0.0); /* no specular reflection */",
268 " else /* light source on the right side */",
270 " specularReflection = attenuation * vec3(light0.specular) * vec3(frontMaterial.specular) ",
271 " * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), frontMaterial.shininess);",
274 " gl_FragColor = vec4(ambientLighting + diffuseReflection + specularReflection, 1.0);",
278 GLint getAttribOrUniformLocation(const char* name, GLuint program, GLboolean isAttrib)
282 GLint attrib = gl_GetAttribLocation(program, name);
285 fprintf(stderr, "Warning: Could not bind attrib %s\n", name);
292 GLint uniform = gl_GetUniformLocation(program, name);
295 fprintf(stderr, "Warning: Could not bind uniform %s\n", name);
300 checkError ("getAttribOrUniformLocation");
304 GLint attribute_fg_coord = -1, attribute_fg_normal = -1;
305 GLint uniform_m = -1, uniform_p = -1, uniform_m_3x3_inv_transp = -1;
306 GLint shaderReady = 0; /* Set to 1 when all initialization went well, to -1 when shader somehow unusable. */
310 void compileAndCheck(GLuint shader)
313 gl_CompileShader (shader);
314 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
315 if (status == GL_FALSE) {
318 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
319 infoLog = (ourGLchar*) malloc (infoLogLength);
320 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
321 fprintf (stderr, "compile log: %s\n", infoLog);
324 checkError ("compileAndCheck");
327 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
329 GLuint shader = gl_CreateShader (type);
330 gl_ShaderSource (shader, count, string, NULL);
332 checkError ("compileShaderSource");
334 compileAndCheck (shader);
338 void linkAndCheck(GLuint program)
341 gl_LinkProgram (program);
342 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
343 if (status == GL_FALSE) {
346 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
347 infoLog = (ourGLchar*) malloc (infoLogLength);
348 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
349 fprintf (stderr, "link log: %s\n", infoLog);
352 checkError ("linkAndCheck");
355 void createProgram(GLuint vertexShader, GLuint fragmentShader)
357 program = gl_CreateProgram ();
358 if (vertexShader != 0) {
359 gl_AttachShader (program, vertexShader);
361 if (fragmentShader != 0) {
362 gl_AttachShader (program, fragmentShader);
365 checkError ("createProgram");
367 linkAndCheck (program);
370 void initShader(void)
372 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
373 GLuint vertexShader =
374 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
376 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
377 GLuint fragmentShader =
378 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
380 checkError ("initShader - 1");
382 createProgram (vertexShader, fragmentShader);
384 gl_UseProgram (program);
386 attribute_fg_coord = getAttribOrUniformLocation("fg_coord" , program, GL_TRUE);
387 attribute_fg_normal = getAttribOrUniformLocation("fg_normal" , program, GL_TRUE);
388 uniform_m = getAttribOrUniformLocation("m" , program, GL_FALSE);
389 uniform_p = getAttribOrUniformLocation("p" , program, GL_FALSE);
390 uniform_m_3x3_inv_transp= getAttribOrUniformLocation("m_3x3_inv_transp" , program, GL_FALSE);
394 if (attribute_fg_coord==-1 || attribute_fg_normal==-1 ||
395 uniform_m==-1 || uniform_p==-1 || uniform_m_3x3_inv_transp==-1)
400 checkError ("initShader - 2");
404 * This macro is only intended to be used on arrays, of course.
406 #define NUMBEROF(x) ((sizeof(x))/(sizeof(x[0])))
409 * These global variables control which object is drawn,
410 * and how it is drawn. No object uses all of these
413 static int function_index;
414 static int slices = 16;
415 static int stacks = 16;
416 static double irad = .25;
417 static double orad = 1.0; /* doubles as size for objects other than Torus */
418 static int depth = 4;
419 static double offset[ 3 ] = { 0, 0, 0 };
420 static GLboolean show_info = GL_TRUE;
422 static GLboolean persProject = GL_TRUE;
423 static GLboolean animateXRot = GL_FALSE;
424 static GLboolean useShader = GL_FALSE;
425 static GLboolean visNormals = GL_FALSE;
428 * Enum to tell drawSizeInfo what to draw for each object
430 #define GEO_NO_SIZE 0
433 #define GEO_INNER_OUTER_RAD 4
435 #define GEO_BASE_HEIGHT 16
436 #define GEO_RAD_HEIGHT 32
439 * These one-liners draw particular objects, fetching appropriate
440 * information from the above globals. They are just thin wrappers
441 * for the FreeGLUT objects.
443 static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
444 static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
445 static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
446 static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
447 static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
448 static void drawWireOctahedron(void) { glutWireOctahedron (); }
449 static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
450 static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
451 static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
452 static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
453 static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
454 static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
455 static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
456 static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
457 static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
458 static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
459 static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
460 static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
461 static void drawSolidCone(void) { glutSolidCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
462 static void drawWireCone(void) { glutWireCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
463 static void drawSolidCylinder(void) { glutSolidCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
464 static void drawWireCylinder(void) { glutWireCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
465 static void drawSolidTeapot(void)
467 /* per Glut manpage, it should be noted that the teapot is rendered
468 * with clockwise winding for front facing polygons...
471 glutSolidTeapot(orad); /* orad doubles as size input */
474 static void drawWireTeapot(void)
476 /* per Glut manpage, it should be noted that the teapot is rendered
477 * with clockwise winding for front facing polygons...
480 glutWireTeapot(orad); /* orad doubles as size input */
484 #define RADIUSFAC 0.70710678118654752440084436210485f
486 static void drawSolidCuboctahedron(void)
488 GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
489 glBegin( GL_TRIANGLES );
490 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( 0.0, radius, radius ); glVertex3d( radius, 0.0, radius );
491 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( radius, 0.0,-radius ); glVertex3d( 0.0, radius,-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( 0.0,-radius,-radius ); glVertex3d( radius, 0.0,-radius );
494 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d(-radius, 0.0, radius ); glVertex3d( 0.0, radius, radius );
495 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d( 0.0, radius,-radius ); glVertex3d(-radius, 0.0,-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(-radius, 0.0,-radius ); glVertex3d( 0.0,-radius,-radius );
501 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 );
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( 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 );
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, 0.0, 1.0 ); glVertex3d( radius, 0.0, radius ); glVertex3d( 0.0, radius, radius ); glVertex3d(-radius, 0.0, radius ); 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 );
510 static void drawWireCuboctahedron(void)
512 GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
513 glBegin( GL_LINE_LOOP );
514 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 );
516 glBegin( GL_LINE_LOOP );
517 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 );
519 glBegin( GL_LINE_LOOP );
520 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 );
522 glBegin( GL_LINE_LOOP );
523 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 );
525 glBegin( GL_LINE_LOOP );
526 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 );
528 glBegin( GL_LINE_LOOP );
529 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 );
536 * This structure defines an entry in our function-table.
540 const char * const name;
541 void (*solid) (void);
543 int drawSizeInfoFlag;
546 #define ENTRY(e,f) {#e, drawSolid##e, drawWire##e,f}
547 static const entry table [] =
549 ENTRY (Tetrahedron,GEO_NO_SIZE),
550 ENTRY (Cube,GEO_SIZE),
551 ENTRY (Octahedron,GEO_NO_SIZE),
552 ENTRY (Dodecahedron,GEO_NO_SIZE),
553 ENTRY (RhombicDodecahedron,GEO_NO_SIZE),
554 ENTRY (Icosahedron,GEO_NO_SIZE),
555 ENTRY (SierpinskiSponge,GEO_SCALE),
556 ENTRY (Teapot,GEO_SIZE),
557 ENTRY (Torus,GEO_INNER_OUTER_RAD),
558 ENTRY (Sphere,GEO_RAD),
559 ENTRY (Cone,GEO_BASE_HEIGHT),
560 ENTRY (Cylinder,GEO_RAD_HEIGHT),
561 ENTRY (Cuboctahedron,GEO_SIZE) /* This one doesn't work when in shader mode and is then skipped */
566 Does printf()-like work using freeglut
567 glutBitmapString(). Uses a fixed font. Prints
568 at the indicated row/column position.
570 Limitation: Cannot address pixels.
571 Limitation: Renders in screen coords, not model coords.
573 static void shapesPrintf (int row, int col, const char *fmt, ...)
575 static char buf[256];
577 void *font = GLUT_BITMAP_9_BY_15;
581 #if defined(WIN32) && !defined(__CYGWIN__)
582 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
584 (void) vsnprintf (buf, sizeof(buf), fmt, args);
588 glGetIntegerv(GL_VIEWPORT,viewport);
593 glMatrixMode(GL_PROJECTION);
597 glOrtho(0,viewport[2],0,viewport[3],-1,1);
601 glutBitmapWidth(font, ' ') * col,
602 - glutBitmapHeight(font) * row + viewport[3]
604 glutBitmapString (font, (unsigned char*)buf);
607 glMatrixMode(GL_MODELVIEW);
611 /* Print info about the about the current shape and render state on the screen */
612 static void DrawSizeInfo(int *row)
614 switch (table [function_index].drawSizeInfoFlag)
619 shapesPrintf ((*row)++, 1, "Size Up Down : %f", orad);
622 shapesPrintf ((*row)++, 1, "Scale Up Down : %f", orad);
624 case GEO_INNER_OUTER_RAD:
625 shapesPrintf ((*row)++, 1, "Inner radius Left Right: %f", irad);
626 shapesPrintf ((*row)++, 1, "Outer radius Up Down : %f", orad);
629 shapesPrintf ((*row)++, 1, "Radius Up Down : %f", orad);
631 case GEO_BASE_HEIGHT:
632 shapesPrintf ((*row)++, 1, "Base Left Right: %f", irad);
633 shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
636 shapesPrintf ((*row)++, 1, "Radius Left Right: %f", irad);
637 shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
642 static void drawInfo()
645 shapesPrintf (row++, 1, "Shape PgUp PgDn: %s", table [function_index].name);
646 shapesPrintf (row++, 1, "Slices +-: %d Stacks <>: %d", slices, stacks);
647 shapesPrintf (row++, 1, "nSides +-: %d nRings <>: %d", slices, stacks);
648 shapesPrintf (row++, 1, "Depth (): %d", depth);
651 shapesPrintf (row++, 1, "Perspective projection (p)");
653 shapesPrintf (row++, 1, "Orthographic projection (p)");
655 shapesPrintf (row++, 1, "Using shader (s)");
657 shapesPrintf (row++, 1, "Using fixed function pipeline (s)");
659 shapesPrintf (row++, 1, "2D rotation (r)");
661 shapesPrintf (row++, 1, "1D rotation (r)");
662 shapesPrintf (row++, 1, "visualizing normals: %i (n)",visNormals);
665 /* GLUT callback Handlers */
667 resize(int width, int height)
669 ar = (float) width / (float) height;
671 glViewport(0, 0, width, height);
674 static void display(void)
676 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
677 const double a = t*89.0;
678 const double b = (animateXRot?t:1)*67.0;
680 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
682 glutSetOption(GLUT_GEOMETRY_VISUALIZE_NORMALS,visNormals); /* Normals visualized or not? */
684 if (useShader && !shaderReady)
687 if (useShader && shaderReady)
689 /* setup use of shader (and vertex buffer by FreeGLUT) */
690 gl_UseProgram (program);
691 glutSetVertexAttribCoord3(attribute_fg_coord);
692 glutSetVertexAttribNormal(attribute_fg_normal);
694 gl_matrix_mode(GL_PROJECTION);
697 gl_frustum(-ar, ar, -1.f, 1.f, 2.f, 100.f);
699 gl_ortho(-ar*3, ar*3, -3.f, 3.f, 2.f, 100.f);
700 gl_UniformMatrix4fv (uniform_p, 1, GL_FALSE, get_matrix(GL_PROJECTION));
703 gl_matrix_mode(GL_MODELVIEW);
707 /* Not in reverse order like normal OpenGL, our util library multiplies the matrices in the order they are specified in */
708 gl_rotatef((float)a,0,0,1);
709 gl_rotatef((float)b,1,0,0);
710 gl_translatef(0,1.2f,-6);
711 gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
712 gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
713 table [function_index].solid ();
717 gl_rotatef((float)a,0,0,1);
718 gl_rotatef((float)b,1,0,0);
719 gl_translatef(0,-1.2f,-6);
720 gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
721 gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
722 table [function_index].wire ();
726 glutSetVertexAttribCoord3(-1);
727 glutSetVertexAttribNormal(-1);
729 checkError ("display");
733 /* fixed function pipeline */
734 glMatrixMode(GL_PROJECTION);
737 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
739 glOrtho(-ar*3, ar*3, -3.0, 3.0, 2.0, 100.0);
740 glMatrixMode(GL_MODELVIEW);
743 glEnable(GL_LIGHTING);
748 glTranslated(0,1.2,-6);
751 table [function_index].solid ();
755 glTranslated(0,-1.2,-6);
758 table [function_index].wire ();
761 glDisable(GL_LIGHTING);
762 glColor3d(0.1,0.1,0.4);
766 /* print info to screen */
769 /* print to command line instead */
770 printf ( "Shape %d slides %d stacks %d\n", function_index, slices, stacks ) ;
777 key(unsigned char key, int x, int y)
783 case 'q': glutLeaveMainLoop () ; break;
786 case 'i': show_info=!show_info; break;
789 case '+': slices++; break;
792 case '_': if( slices > -1 ) slices--; break;
795 case '<': if( stacks > -1 ) stacks--; break;
798 case '>': stacks++; break;
801 case '(': if( depth > -1 ) depth--; break;
804 case ')': ++depth; break;
807 case 'p': persProject=!persProject; break;
810 case 'r': animateXRot=!animateXRot; break;
814 useShader=!useShader;
815 /* Cuboctahedron can't be shown when in shader mode, move to next */
816 if (useShader && NUMBEROF (table)-1 == ( unsigned )function_index)
821 case 'n': visNormals=!visNormals; break;
830 static void special (int key, int x, int y)
834 case GLUT_KEY_PAGE_UP: ++function_index; break;
835 case GLUT_KEY_PAGE_DOWN: --function_index; break;
836 case GLUT_KEY_UP: orad *= 2; break;
837 case GLUT_KEY_DOWN: orad /= 2; break;
839 case GLUT_KEY_RIGHT: irad *= 2; break;
840 case GLUT_KEY_LEFT: irad /= 2; break;
846 if (0 > function_index)
847 function_index = NUMBEROF (table) - 1;
849 if (NUMBEROF (table) <= ( unsigned )function_index)
852 /* Cuboctahedron can't be shown when in shader mode, skip it */
853 if (useShader && NUMBEROF (table)-1 == ( unsigned )function_index)
854 if (key==GLUT_KEY_PAGE_UP)
867 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
868 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
869 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
870 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
872 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
873 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
874 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
875 const GLfloat high_shininess[] = { 100.0f };
877 /* Program entry point */
880 main(int argc, char *argv[])
882 glutInitWindowSize(800,600);
883 glutInitWindowPosition(40,40);
884 glutInit(&argc, argv);
885 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
887 glutCreateWindow("FreeGLUT Shapes");
889 glutReshapeFunc(resize);
890 glutDisplayFunc(display);
891 glutKeyboardFunc(key);
892 glutSpecialFunc(special);
895 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
897 glClearColor(1,1,1,1);
898 glEnable(GL_CULL_FACE);
901 glEnable(GL_DEPTH_TEST);
902 glDepthFunc(GL_LESS);
905 glEnable(GL_NORMALIZE);
906 glEnable(GL_COLOR_MATERIAL);
908 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
909 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
910 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
911 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
913 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
914 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
915 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
916 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
918 initExtensionEntries();
923 /* DUMP MEMORY LEAK INFORMATION */
924 _CrtDumpMemoryLeaks () ;