+ /* 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)
+ {
+ /* Allocate memory */
+ vertices = malloc(numVert*3 * sizeof(GLfloat));
+ normals = malloc(numVert*3 * sizeof(GLfloat));
+ /* Bail out if memory allocation fails, fgError never returns */
+ if (!vertices || !normals)
+ {
+ free(vertices);
+ free(normals);
+ fgError("Failed to allocate memory in fghSierpinskiSponge");
+ }
+
+ /* Generate elements */
+ fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );
+
+ /* Draw and cleanup */
+ if (useWireMode)
+ fghDrawGeometryWire (vertices,normals,numVert,
+ NULL,numFace,TETRAHEDRON_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
+ NULL,0,0);
+ else
+ fghDrawGeometrySolid(vertices,normals,NULL,numVert,NULL,1,0);
+
+ free(vertices);
+ free(normals );
+ }
+}
+
+
+static void fghSphere( GLfloat radius, GLint slices, GLint stacks, GLboolean useWireMode )
+{
+ int i,j,idx, nVert;
+ GLfloat *vertices, *normals;
+
+ /* Generate vertices and normals */
+ fghGenerateSphere(radius,slices,stacks,&vertices,&normals,&nVert);
+
+ if (nVert==0)
+ /* nothing to draw */
+ return;
+
+ if (useWireMode)
+ {
+ GLushort *sliceIdx, *stackIdx;
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * We have a bunch of line_loops to draw for each stack, and a
+ * bunch for each slice.
+ */
+
+ sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
+ stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
+ if (!(stackIdx) || !(sliceIdx))
+ {
+ free(stackIdx);
+ free(sliceIdx);
+ fgError("Failed to allocate memory in fghSphere");
+ }
+
+ /* generate for each stack */
+ for (i=0,idx=0; i<stacks-1; i++)
+ {
+ GLushort offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ for (j=0; j<slices; j++, idx++)
+ {
+ stackIdx[idx] = offset+j;
+ }
+ }
+
+ /* generate for each slice */
+ for (i=0,idx=0; i<slices; i++)
+ {
+ GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
+ sliceIdx[idx++] = 0; /* vertex on top */
+ for (j=0; j<stacks-1; j++, idx++)
+ {
+ sliceIdx[idx] = offset+j*slices;
+ }
+ sliceIdx[idx++] = nVert-1; /* zero based index, last element in array... */
+ }
+
+ /* draw */
+ fghDrawGeometryWire(vertices,normals,nVert,
+ sliceIdx,slices,stacks+1,GL_LINE_STRIP,
+ stackIdx,stacks-1,slices);
+
+ /* cleanup allocated memory */
+ free(sliceIdx);
+ free(stackIdx);
+ }
+ else
+ {
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * All stacks, including top and bottom are covered with a triangle
+ * strip.
+ */
+ GLushort *stripIdx;
+ /* Create index vector */
+ GLushort offset;
+
+ /* Allocate buffers for indices, bail out if memory allocation fails */
+ stripIdx = malloc((slices+1)*2*(stacks)*sizeof(GLushort));
+ if (!(stripIdx))
+ {
+ free(stripIdx);
+ fgError("Failed to allocate memory in fghSphere");
+ }
+
+ /* top stack */
+ for (j=0, idx=0; j<slices; j++, idx+=2)
+ {
+ stripIdx[idx ] = j+1; /* 0 is top vertex, 1 is first for first stack */
+ stripIdx[idx+1] = 0;
+ }
+ stripIdx[idx ] = 1; /* repeat first slice's idx for closing off shape */
+ stripIdx[idx+1] = 0;
+ idx+=2;
+
+ /* middle stacks: */
+ /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
+ for (i=0; i<stacks-2; i++, idx+=2)
+ {
+ offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ for (j=0; j<slices; j++, idx+=2)
+ {
+ stripIdx[idx ] = offset+j+slices;
+ stripIdx[idx+1] = offset+j;
+ }
+ stripIdx[idx ] = offset+slices; /* repeat first slice's idx for closing off shape */
+ stripIdx[idx+1] = offset;
+ }
+
+ /* bottom stack */
+ offset = 1+(stacks-2)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ for (j=0; j<slices; j++, idx+=2)
+ {
+ stripIdx[idx ] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
+ stripIdx[idx+1] = offset+j;
+ }
+ stripIdx[idx ] = nVert-1; /* repeat first slice's idx for closing off shape */
+ stripIdx[idx+1] = offset;
+
+
+ /* draw */
+ fghDrawGeometrySolid(vertices,normals,NULL,nVert,stripIdx,stacks,(slices+1)*2);
+
+ /* cleanup allocated memory */
+ free(stripIdx);
+ }
+
+ /* cleanup allocated memory */
+ free(vertices);
+ free(normals);
+}
+
+static void fghCone( GLfloat base, GLfloat height, GLint slices, GLint stacks, GLboolean useWireMode )
+{
+ int i,j,idx, nVert;
+ GLfloat *vertices, *normals;
+
+ /* Generate vertices and normals */
+ /* Note, (stacks+1)*slices vertices for side of object, slices+1 for top and bottom closures */
+ fghGenerateCone(base,height,slices,stacks,&vertices,&normals,&nVert);
+
+ if (nVert==0)
+ /* nothing to draw */
+ return;
+
+ if (useWireMode)
+ {
+ GLushort *sliceIdx, *stackIdx;
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * We have a bunch of line_loops to draw for each stack, and a
+ * bunch for each slice.
+ */
+
+ stackIdx = malloc(slices*stacks*sizeof(GLushort));
+ sliceIdx = malloc(slices*2 *sizeof(GLushort));
+ if (!(stackIdx) || !(sliceIdx))
+ {
+ free(stackIdx);
+ free(sliceIdx);
+ fgError("Failed to allocate memory in fghCone");
+ }
+
+ /* generate for each stack */
+ for (i=0,idx=0; i<stacks; i++)
+ {
+ GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ for (j=0; j<slices; j++, idx++)
+ {
+ stackIdx[idx] = offset+j;
+ }
+ }
+
+ /* generate for each slice */
+ for (i=0,idx=0; i<slices; i++)
+ {
+ GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
+ sliceIdx[idx++] = offset+slices;
+ sliceIdx[idx++] = offset+(stacks+1)*slices;
+ }
+
+ /* draw */
+ fghDrawGeometryWire(vertices,normals,nVert,
+ sliceIdx,1,slices*2,GL_LINES,
+ stackIdx,stacks,slices);
+
+ /* cleanup allocated memory */
+ free(sliceIdx);
+ free(stackIdx);
+ }
+ else
+ {
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * All stacks, including top and bottom are covered with a triangle
+ * strip.
+ */
+ GLushort *stripIdx;
+ /* Create index vector */
+ GLushort offset;
+
+ /* Allocate buffers for indices, bail out if memory allocation fails */
+ stripIdx = malloc((slices+1)*2*(stacks+1)*sizeof(GLushort)); /*stacks +1 because of closing off bottom */
+ if (!(stripIdx))
+ {
+ free(stripIdx);
+ fgError("Failed to allocate memory in fghCone");
+ }
+
+ /* top stack */
+ for (j=0, idx=0; j<slices; j++, idx+=2)
+ {
+ stripIdx[idx ] = 0;
+ stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
+ }
+ stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
+ stripIdx[idx+1] = 1;
+ idx+=2;
+
+ /* middle stacks: */
+ /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
+ for (i=0; i<stacks; i++, idx+=2)
+ {
+ offset = 1+(i+1)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
+ for (j=0; j<slices; j++, idx+=2)
+ {
+ stripIdx[idx ] = offset+j;
+ stripIdx[idx+1] = offset+j+slices;
+ }
+ stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
+ stripIdx[idx+1] = offset+slices;
+ }
+
+ /* draw */
+ fghDrawGeometrySolid(vertices,normals,NULL,nVert,stripIdx,stacks+1,(slices+1)*2);
+
+ /* cleanup allocated memory */
+ free(stripIdx);
+ }
+
+ /* cleanup allocated memory */
+ free(vertices);
+ free(normals);
+}
+
+static void fghCylinder( GLfloat radius, GLfloat height, GLint slices, GLint stacks, GLboolean useWireMode )
+{
+ int i,j,idx, nVert;
+ GLfloat *vertices, *normals;
+
+ /* Generate vertices and normals */
+ /* Note, (stacks+1)*slices vertices for side of object, 2*slices+2 for top and bottom closures */
+ fghGenerateCylinder(radius,height,slices,stacks,&vertices,&normals,&nVert);
+
+ if (nVert==0)
+ /* nothing to draw */
+ return;
+
+ if (useWireMode)
+ {
+ GLushort *sliceIdx, *stackIdx;
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * We have a bunch of line_loops to draw for each stack, and a
+ * bunch for each slice.
+ */