+ /* Step in z as stacks are drawn. */
+ GLfloat radf = (GLfloat)radius;
+ GLfloat z;
+ const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
+
+ /* Pre-computed circle */
+ GLfloat *sint,*cost;
+
+ /* number of unique vertices */
+ if (slices==0 || stacks<1)
+ {
+ /* nothing to generate */
+ *nVert = 0;
+ return;
+ }
+ *nVert = slices*(stacks+3)+2; /* need two extra stacks for closing off top and bottom with correct normals */
+
+ if ((*nVert) > 65535)
+ /*
+ * limit of glushort, thats 256*256 subdivisions, should be enough in practice. See note above
+ */
+ fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap");
+
+ /* Pre-computed circle */
+ fghCircleTable(&sint,&cost,-slices,GL_FALSE);
+
+ /* Allocate vertex and normal buffers, bail out if memory allocation fails */
+ *vertices = malloc((*nVert)*3*sizeof(GLfloat));
+ *normals = malloc((*nVert)*3*sizeof(GLfloat));
+ if (!(*vertices) || !(*normals))
+ {
+ free(*vertices);
+ free(*normals);
+ fgError("Failed to allocate memory in fghGenerateCylinder");
+ }
+
+ z=0;
+ /* top on Z-axis */
+ (*vertices)[0] = 0.f;
+ (*vertices)[1] = 0.f;
+ (*vertices)[2] = 0.f;
+ (*normals )[0] = 0.f;
+ (*normals )[1] = 0.f;
+ (*normals )[2] = -1.f;
+ idx = 3;
+ /* other on top (get normals right) */
+ for (j=0; j<slices; j++, idx+=3)
+ {
+ (*vertices)[idx ] = cost[j]*radf;
+ (*vertices)[idx+1] = sint[j]*radf;
+ (*vertices)[idx+2] = z;
+ (*normals )[idx ] = 0.f;
+ (*normals )[idx+1] = 0.f;
+ (*normals )[idx+2] = -1.f;
+ }
+
+ /* each stack */
+ for (i=0; i<stacks+1; i++ )
+ {
+ for (j=0; j<slices; j++, idx+=3)
+ {
+ (*vertices)[idx ] = cost[j]*radf;
+ (*vertices)[idx+1] = sint[j]*radf;
+ (*vertices)[idx+2] = z;
+ (*normals )[idx ] = cost[j];
+ (*normals )[idx+1] = sint[j];
+ (*normals )[idx+2] = 0.f;
+ }
+
+ z += zStep;
+ }
+
+ /* other on bottom (get normals right) */
+ z -= zStep;
+ for (j=0; j<slices; j++, idx+=3)
+ {
+ (*vertices)[idx ] = cost[j]*radf;
+ (*vertices)[idx+1] = sint[j]*radf;
+ (*vertices)[idx+2] = z;
+ (*normals )[idx ] = 0.f;
+ (*normals )[idx+1] = 0.f;
+ (*normals )[idx+2] = 1.f;
+ }
+
+ /* bottom */
+ (*vertices)[idx ] = 0.f;
+ (*vertices)[idx+1] = 0.f;
+ (*vertices)[idx+2] = height;
+ (*normals )[idx ] = 0.f;
+ (*normals )[idx+1] = 0.f;
+ (*normals )[idx+2] = 1.f;
+
+ /* Release sin and cos tables */
+ free(sint);
+ free(cost);
+}
+
+void fghGenerateTorus(
+ double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, /* input */
+ GLfloat **vertices, GLfloat **normals, int* nVert /* output */
+ )
+{
+ GLfloat iradius = (float)dInnerRadius;
+ GLfloat oradius = (float)dOuterRadius;
+ int i, j;
+
+ /* Pre-computed circle */
+ GLfloat *spsi, *cpsi;
+ GLfloat *sphi, *cphi;
+
+ /* number of unique vertices */
+ if (nSides<2 || nRings<2)
+ {
+ /* nothing to generate */
+ *nVert = 0;
+ return;
+ }
+ *nVert = nSides * nRings;
+
+ if ((*nVert) > 65535)
+ /*
+ * limit of glushort, thats 256*256 subdivisions, should be enough in practice. See note above
+ */
+ fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap");
+
+ /* precompute values on unit circle */
+ fghCircleTable(&spsi,&cpsi, nRings,GL_FALSE);
+ fghCircleTable(&sphi,&cphi,-nSides,GL_FALSE);
+
+ /* Allocate vertex and normal buffers, bail out if memory allocation fails */
+ *vertices = malloc((*nVert)*3*sizeof(GLfloat));
+ *normals = malloc((*nVert)*3*sizeof(GLfloat));
+ if (!(*vertices) || !(*normals))
+ {
+ free(*vertices);
+ free(*normals);
+ fgError("Failed to allocate memory in fghGenerateTorus");
+ }
+
+ for( j=0; j<nRings; j++ )
+ {
+ for( i=0; i<nSides; i++ )
+ {
+ int offset = 3 * ( j * nSides + i ) ;
+
+ (*vertices)[offset ] = cpsi[j] * ( oradius + cphi[i] * iradius ) ;
+ (*vertices)[offset+1] = spsi[j] * ( oradius + cphi[i] * iradius ) ;
+ (*vertices)[offset+2] = sphi[i] * iradius ;
+ (*normals )[offset ] = cpsi[j] * cphi[i] ;
+ (*normals )[offset+1] = spsi[j] * cphi[i] ;
+ (*normals )[offset+2] = sphi[i] ;
+ }
+ }
+
+ /* Release sin and cos tables */
+ free(spsi);
+ free(cpsi);
+ free(sphi);
+ free(cphi);
+}
+
+/* -- INTERNAL DRAWING functions --------------------------------------- */
+#define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
+ static void fgh##nameICaps( GLboolean useWireMode )\
+ {\
+ if (!name##Cached)\
+ {\
+ fgh##nameICaps##Generate();\
+ name##Cached = GL_TRUE;\
+ }\
+ \
+ if (useWireMode)\
+ {\
+ fghDrawGeometryWire (name##_verts,name##_norms,nameCaps##_VERT_PER_OBJ, \
+ NULL,nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE,GL_LINE_LOOP,\
+ NULL,0,0);\
+ }\
+ else\
+ {\
+ fghDrawGeometrySolid(name##_verts,name##_norms,NULL,nameCaps##_VERT_PER_OBJ,\
+ vertIdxs, 1, nameCaps##_VERT_PER_OBJ_TRI); \
+ }\
+ }
+#define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
+#define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs)
+
+static void fghCube( GLfloat dSize, GLboolean useWireMode )
+{
+ GLfloat *vertices;
+
+ if (!cubeCached)
+ {
+ fghCubeGenerate();
+ cubeCached = GL_TRUE;
+ }
+
+ if (dSize!=1.f)
+ {
+ /* Need to build new vertex list containing vertices for cube of different size */
+ int i;
+
+ vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLfloat));
+
+ /* Bail out if memory allocation fails, fgError never returns */
+ if (!vertices)
+ {
+ free(vertices);
+ fgError("Failed to allocate memory in fghCube");
+ }
+
+ for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
+ vertices[i] = dSize*cube_verts[i];
+ }
+ else
+ vertices = cube_verts;
+
+ if (useWireMode)
+ fghDrawGeometryWire(vertices, cube_norms, CUBE_VERT_PER_OBJ,
+ NULL,CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
+ NULL,0,0);
+ else
+ fghDrawGeometrySolid(vertices, cube_norms, NULL, CUBE_VERT_PER_OBJ,
+ cube_vertIdxs, 1, CUBE_VERT_PER_OBJ_TRI);
+
+ if (dSize!=1.f)
+ /* cleanup allocated memory */
+ free(vertices);
+}
+
+DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
+DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON)
+DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON)
+DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
+DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON)
+
+static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale, GLboolean useWireMode )
+{
+ GLfloat *vertices;
+ GLfloat * normals;
+ GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */
+ GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ;
+ GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES;
+
+ if (numTetr)