+ fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+2,(slices+1)*2);
+
+ /* cleanup allocated memory */
+ free(stripIdx);
+ }
+
+ /* cleanup allocated memory */
+ free(vertices);
+ free(normals);
+}
+
+static void fghTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, GLboolean useWireMode )
+{
+ int i,j,idx, nVert;
+ GLfloat *vertices, *normals;
+
+ /* Generate vertices and normals */
+ fghGenerateTorus((GLfloat)dInnerRadius,(GLfloat)dOuterRadius,nSides,nRings, &vertices,&normals,&nVert);
+
+ if (nVert==0)
+ /* nothing to draw */
+ return;
+
+ if (useWireMode)
+ {
+ GLushort *sideIdx, *ringIdx;
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * We have a bunch of line_loops to draw each side, and a
+ * bunch for each ring.
+ */
+
+ ringIdx = malloc(nRings*nSides*sizeof(GLushort));
+ sideIdx = malloc(nSides*nRings*sizeof(GLushort));
+ if (!(ringIdx) || !(sideIdx))
+ {
+ free(ringIdx);
+ free(sideIdx);
+ fgError("Failed to allocate memory in fghTorus");
+ }
+
+ /* generate for each ring */
+ for( j=0,idx=0; j<nRings; j++ )
+ for( i=0; i<nSides; i++, idx++ )
+ ringIdx[idx] = j * nSides + i;
+
+ /* generate for each side */
+ for( i=0,idx=0; i<nSides; i++ )
+ for( j=0; j<nRings; j++, idx++ )
+ sideIdx[idx] = j * nSides + i;
+
+ /* draw */
+ fghDrawGeometryWire(vertices,normals,
+ ringIdx,nRings,nSides,GL_LINE_LOOP,
+ sideIdx,nSides,nRings);
+
+ /* cleanup allocated memory */
+ free(sideIdx);
+ free(ringIdx);
+ }
+ else
+ {
+ /* First, generate vertex index arrays for drawing with glDrawElements
+ * All stacks, including top and bottom are covered with a triangle
+ * strip.
+ */
+ GLushort *stripIdx;
+
+ /* Allocate buffers for indices, bail out if memory allocation fails */
+ stripIdx = malloc((nRings+1)*2*nSides*sizeof(GLushort));
+ if (!(stripIdx))
+ {
+ free(stripIdx);
+ fgError("Failed to allocate memory in fghTorus");
+ }