+void fghGenerateCone(
+ GLfloat base, GLfloat height, GLint slices, GLint stacks, /* input */
+ GLfloat **vertices, GLfloat **normals, int* nVert /* output */
+ )
+{
+ int i,j;
+ int idx = 0; /* idx into vertex/normal buffer */
+
+ /* Pre-computed circle */
+ GLfloat *sint,*cost;
+
+ /* Step in z and radius as stacks are drawn. */
+ GLfloat z = 0;
+ GLfloat r = (GLfloat)base;
+
+ const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
+ const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 );
+
+ /* Scaling factors for vertex normals */
+#ifdef __cplusplus
+ const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
+ const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base ));
+#else
+ const GLfloat cosn = ( (GLfloat)height / (GLfloat)sqrt( (double)(height * height + base * base) ));
+ const GLfloat sinn = ( (GLfloat)base / (GLfloat)sqrt( (double)(height * height + base * base) ));
+#endif /* __cplusplus */
+
+
+
+ /* number of unique vertices */
+ if (slices==0 || stacks<1)
+ {
+ /* nothing to generate */
+ *nVert = 0;
+ return;
+ }
+ *nVert = slices*(stacks+2)+1; /* need an extra stack for closing off bottom with correct normals */
+
+ if ((*nVert) > 65535)
+ fgWarning("fghGenerateCone: too many slices or stacks requested, indices will wrap");
+
+ /* Pre-computed circle */
+ fghCircleTable(&sint,&cost,-slices,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 fghGenerateSphere");
+ }
+
+ /* bottom */
+ (*vertices)[0] = 0.f;
+ (*vertices)[1] = 0.f;
+ (*vertices)[2] = z;
+ (*normals )[0] = 0.f;
+ (*normals )[1] = 0.f;
+ (*normals )[2] = -1.f;
+ idx = 3;
+ /* other on bottom (get normals right) */
+ for (j=0; j<slices; j++, idx+=3)
+ {
+ (*vertices)[idx ] = cost[j]*r;
+ (*vertices)[idx+1] = sint[j]*r;
+ (*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]*r;
+ (*vertices)[idx+1] = sint[j]*r;
+ (*vertices)[idx+2] = z;
+ (*normals )[idx ] = cost[j]*sinn;
+ (*normals )[idx+1] = sint[j]*sinn;
+ (*normals )[idx+2] = cosn;
+ }
+
+ z += zStep;
+ r -= rStep;
+ }
+
+ /* Release sin and cos tables */
+ free(sint);
+ free(cost);
+}
+
+void fghGenerateCylinder(
+ GLfloat radius, GLfloat height, GLint slices, GLint stacks, /* input */
+ GLfloat **vertices, GLfloat **normals, int* nVert /* output */
+ )
+{
+ int i,j;
+ int idx = 0; /* idx into vertex/normal buffer */
+
+ /* 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)
+ fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap");
+
+ /* Pre-computed circle */
+ fghCircleTable(&sint,&cost,-slices,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)
+ fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap");
+
+ /* precompute values on unit circle */
+ fghCircleTable(&spsi,&cpsi, nRings,FALSE);
+ fghCircleTable(&sphi,&cphi,-nSides,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);
+}