+static double irad = .25;
+static double orad = 1.0; /* doubles as size for objects other than Torus */
+static int depth = 4;
+static double offset[ 3 ] = { 0, 0, 0 };
+static GLboolean show_info = GL_TRUE;
+static float ar;
+static GLboolean persProject = GL_TRUE;
+static GLboolean animateXRot = GL_FALSE;
+static GLboolean useShader = GL_FALSE;
+static GLboolean visNormals = GL_FALSE;
+
+/*
+ * Enum to tell drawSizeInfo what to draw for each object
+ */
+#define GEO_NO_SIZE 0
+#define GEO_SIZE 1
+#define GEO_SCALE 2
+#define GEO_INNER_OUTER_RAD 4
+#define GEO_RAD 8
+#define GEO_BASE_HEIGHT 16
+#define GEO_RAD_HEIGHT 32
+
+/*
+ * These one-liners draw particular objects, fetching appropriate
+ * information from the above globals. They are just thin wrappers
+ * for the FreeGLUT objects.
+ */
+static void drawSolidTetrahedron(void) { glutSolidTetrahedron (); }
+static void drawWireTetrahedron(void) { glutWireTetrahedron (); }
+static void drawSolidCube(void) { glutSolidCube(orad); } /* orad doubles as size input */
+static void drawWireCube(void) { glutWireCube(orad); } /* orad doubles as size input */
+static void drawSolidOctahedron(void) { glutSolidOctahedron (); }
+static void drawWireOctahedron(void) { glutWireOctahedron (); }
+static void drawSolidDodecahedron(void) { glutSolidDodecahedron (); }
+static void drawWireDodecahedron(void) { glutWireDodecahedron (); }
+static void drawSolidRhombicDodecahedron(void) { glutSolidRhombicDodecahedron (); }
+static void drawWireRhombicDodecahedron(void) { glutWireRhombicDodecahedron (); }
+static void drawSolidIcosahedron(void) { glutSolidIcosahedron (); }
+static void drawWireIcosahedron(void) { glutWireIcosahedron (); }
+static void drawSolidSierpinskiSponge(void) { glutSolidSierpinskiSponge (depth, offset, orad);} /* orad doubles as size input */
+static void drawWireSierpinskiSponge(void) { glutWireSierpinskiSponge (depth, offset, orad); } /* orad doubles as size input */
+static void drawSolidTorus(void) { glutSolidTorus(irad,orad,slices,stacks); }
+static void drawWireTorus(void) { glutWireTorus (irad,orad,slices,stacks); }
+static void drawSolidSphere(void) { glutSolidSphere(orad,slices,stacks); } /* orad doubles as size input */
+static void drawWireSphere(void) { glutWireSphere(orad,slices,stacks); } /* orad doubles as size input */
+static void drawSolidCone(void) { glutSolidCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
+static void drawWireCone(void) { glutWireCone(irad,orad,slices,stacks); } /* irad doubles as base input, and orad as height input */
+static void drawSolidCylinder(void) { glutSolidCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
+static void drawWireCylinder(void) { glutWireCylinder(irad,orad,slices,stacks); } /* irad doubles as radius input, and orad as height input */
+/* per Glut manpage, it should be noted that the teapot is rendered
+ * with clockwise winding for front facing polygons...
+ * Same for the teacup and teaspoon
+ */
+static void drawSolidTeapot(void)
+{ glFrontFace(GL_CW); glutSolidTeapot(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+static void drawWireTeapot(void)
+{ glFrontFace(GL_CW); glutWireTeapot(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+static void drawSolidTeacup(void)
+{ glFrontFace(GL_CW); glutSolidTeacup(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+static void drawWireTeacup(void)
+{ glFrontFace(GL_CW); glutWireTeacup(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+static void drawSolidTeaspoon(void)
+{ glFrontFace(GL_CW); glutSolidTeaspoon(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+static void drawWireTeaspoon(void)
+{ glFrontFace(GL_CW); glutWireTeaspoon(orad); glFrontFace(GL_CCW); /* orad doubles as size input */}
+
+#define RADIUSFAC 0.70710678118654752440084436210485f
+
+static void drawSolidCuboctahedron(void)
+{
+ GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
+ glBegin( GL_TRIANGLES );
+ glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( 0.0, radius, radius ); glVertex3d( radius, 0.0, radius );
+ glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( radius, radius, 0.0 ); glVertex3d( radius, 0.0,-radius ); glVertex3d( 0.0, radius,-radius );
+ glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( radius,-radius, 0.0 ); glVertex3d( radius, 0.0, radius ); glVertex3d( 0.0,-radius, radius );
+ glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( radius,-radius, 0.0 ); glVertex3d( 0.0,-radius,-radius ); glVertex3d( radius, 0.0,-radius );
+ glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d(-radius, 0.0, radius ); glVertex3d( 0.0, radius, radius );
+ glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-radius, radius, 0.0 ); glVertex3d( 0.0, radius,-radius ); glVertex3d(-radius, 0.0,-radius );
+ glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-radius,-radius, 0.0 ); glVertex3d( 0.0,-radius, radius ); glVertex3d(-radius, 0.0, radius );
+ glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-radius,-radius, 0.0 ); glVertex3d(-radius, 0.0,-radius ); glVertex3d( 0.0,-radius,-radius );
+ glEnd();
+
+ glBegin( GL_QUADS );
+ 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 );
+ 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 );
+ 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 );
+ 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 );
+ 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 );
+ 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 );
+ glEnd();
+}
+
+static void drawWireCuboctahedron(void)
+{
+ GLfloat radius = RADIUSFAC*(GLfloat)orad; /* orad doubles as size */
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+ glBegin( GL_LINE_LOOP );
+ 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 );
+ glEnd();
+}
+
+#undef RADIUSFAC
+
+/*
+ * This structure defines an entry in our function-table.
+ */
+typedef struct
+{
+ const char * const name;
+ void (*solid) (void);
+ void (*wire) (void);
+ int drawSizeInfoFlag;
+} entry;
+
+#define ENTRY(e,f) {#e, drawSolid##e, drawWire##e,f}
+static const entry table [] =
+{
+ ENTRY (Tetrahedron,GEO_NO_SIZE),
+ ENTRY (Cube,GEO_SIZE),
+ ENTRY (Octahedron,GEO_NO_SIZE),
+ ENTRY (Dodecahedron,GEO_NO_SIZE),
+ ENTRY (RhombicDodecahedron,GEO_NO_SIZE),
+ ENTRY (Icosahedron,GEO_NO_SIZE),
+ ENTRY (SierpinskiSponge,GEO_SCALE),
+ ENTRY (Teapot,GEO_SIZE),
+ ENTRY (Teacup,GEO_SIZE),
+ ENTRY (Teaspoon,GEO_SIZE),
+ ENTRY (Torus,GEO_INNER_OUTER_RAD),
+ ENTRY (Sphere,GEO_RAD),
+ ENTRY (Cone,GEO_BASE_HEIGHT),
+ ENTRY (Cylinder,GEO_RAD_HEIGHT),
+ ENTRY (Cuboctahedron,GEO_SIZE) /* This one doesn't work when in shader mode and is then skipped */
+};
+#undef ENTRY
+
+/*!
+ Does printf()-like work using freeglut
+ glutBitmapString(). Uses a fixed font. Prints
+ at the indicated row/column position.
+
+ Limitation: Cannot address pixels.
+ Limitation: Renders in screen coords, not model coords.
+*/
+static void shapesPrintf (int row, int col, const char *fmt, ...)
+{
+ static char buf[256];
+ int viewport[4];
+ void *font = GLUT_BITMAP_9_BY_15;
+ va_list args;
+
+ va_start(args, fmt);
+#if defined(WIN32) && !defined(__CYGWIN__)
+ (void) _vsnprintf (buf, sizeof(buf), fmt, args);
+#else
+ (void) vsnprintf (buf, sizeof(buf), fmt, args);
+#endif
+ va_end(args);
+
+ glGetIntegerv(GL_VIEWPORT,viewport);
+
+ glPushMatrix();
+ glLoadIdentity();
+
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+
+ glOrtho(0,viewport[2],0,viewport[3],-1,1);
+
+ glRasterPos2i
+ (
+ glutBitmapWidth(font, ' ') * col,
+ - glutBitmapHeight(font) * row + viewport[3]
+ );
+ glutBitmapString (font, (unsigned char*)buf);
+
+ glPopMatrix();
+ glMatrixMode(GL_MODELVIEW);
+ glPopMatrix();
+}
+
+/* Print info about the about the current shape and render state on the screen */
+static void DrawSizeInfo(int *row)
+{
+ switch (table [function_index].drawSizeInfoFlag)
+ {
+ case GEO_NO_SIZE:
+ break;
+ case GEO_SIZE:
+ shapesPrintf ((*row)++, 1, "Size Up Down : %f", orad);
+ break;
+ case GEO_SCALE:
+ shapesPrintf ((*row)++, 1, "Scale Up Down : %f", orad);
+ break;
+ case GEO_INNER_OUTER_RAD:
+ shapesPrintf ((*row)++, 1, "Inner radius Left Right: %f", irad);
+ shapesPrintf ((*row)++, 1, "Outer radius Up Down : %f", orad);
+ break;
+ case GEO_RAD:
+ shapesPrintf ((*row)++, 1, "Radius Up Down : %f", orad);
+ break;
+ case GEO_BASE_HEIGHT:
+ shapesPrintf ((*row)++, 1, "Base Left Right: %f", irad);
+ shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
+ break;
+ case GEO_RAD_HEIGHT:
+ shapesPrintf ((*row)++, 1, "Radius Left Right: %f", irad);
+ shapesPrintf ((*row)++, 1, "Height Up Down : %f", orad);
+ break;
+ }
+}
+
+static void drawInfo()
+{
+ int row = 1;
+ shapesPrintf (row++, 1, "Shape PgUp PgDn: %s", table [function_index].name);
+ shapesPrintf (row++, 1, "Slices +-: %d Stacks <>: %d", slices, stacks);
+ shapesPrintf (row++, 1, "nSides +-: %d nRings <>: %d", slices, stacks);
+ shapesPrintf (row++, 1, "Depth (): %d", depth);
+ DrawSizeInfo(&row);
+ if (persProject)
+ shapesPrintf (row++, 1, "Perspective projection (p)");
+ else
+ shapesPrintf (row++, 1, "Orthographic projection (p)");
+ if (useShader)
+ shapesPrintf (row++, 1, "Using shader (s)");
+ else
+ shapesPrintf (row++, 1, "Using fixed function pipeline (s)");
+ if (animateXRot)
+ shapesPrintf (row++, 1, "2D rotation (r)");
+ else
+ shapesPrintf (row++, 1, "1D rotation (r)");
+ shapesPrintf (row++, 1, "visualizing normals: %i (n)",visNormals);
+}
+
+/* GLUT callback Handlers */
+static void
+resize(int width, int height)
+{
+ ar = (float) width / (float) height;
+
+ glViewport(0, 0, width, height);
+}
+
+static void display(void)
+{
+ const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+ const double a = t*89.0;
+ const double b = (animateXRot?t:1)*67.0;
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glutSetOption(GLUT_GEOMETRY_VISUALIZE_NORMALS,visNormals); /* Normals visualized or not? */
+
+ if (useShader && !shaderReady)
+ initShader();
+
+ if (useShader && shaderReady)
+ {
+ /* setup use of shader (and vertex buffer by FreeGLUT) */
+ gl_UseProgram (program);
+ glutSetVertexAttribCoord3(attribute_fg_coord);
+ glutSetVertexAttribNormal(attribute_fg_normal);
+ /* There is also a glutSetVertexAttribTexCoord2, which is used only when drawing the teapot, teacup or teaspoon */
+
+ gl_matrix_mode(GL_PROJECTION);
+ gl_load_identity();
+ if (persProject)
+ gl_frustum(-ar, ar, -1.f, 1.f, 2.f, 100.f);
+ else
+ gl_ortho(-ar*3, ar*3, -3.f, 3.f, 2.f, 100.f);
+ gl_UniformMatrix4fv (uniform_p, 1, GL_FALSE, get_matrix(GL_PROJECTION));
+
+
+ gl_matrix_mode(GL_MODELVIEW);
+ gl_load_identity();
+
+ gl_push_matrix();
+ /* Not in reverse order like normal OpenGL, our util library multiplies the matrices in the order they are specified in */
+ gl_rotatef((float)a,0,0,1);
+ gl_rotatef((float)b,1,0,0);
+ gl_translatef(0,1.2f,-6);
+ gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
+ gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
+ table [function_index].solid ();
+ gl_pop_matrix();
+
+ gl_push_matrix();
+ gl_rotatef((float)a,0,0,1);
+ gl_rotatef((float)b,1,0,0);
+ gl_translatef(0,-1.2f,-6);
+ gl_UniformMatrix4fv (uniform_m , 1, GL_FALSE, get_matrix(GL_MODELVIEW));
+ gl_UniformMatrix3fv (uniform_m_3x3_inv_transp, 1, GL_FALSE, get_inv_transpose_3x3(GL_MODELVIEW));
+ table [function_index].wire ();
+ gl_pop_matrix();
+
+ gl_UseProgram (0);
+ glutSetVertexAttribCoord3(-1);
+ glutSetVertexAttribNormal(-1);