glutCones now refreshed too
[freeglut] / src / fg_geometry.c
index a40ec7b..7a446d9 100644 (file)
 
 #include <GL/freeglut.h>
 #include "fg_internal.h"
+#include "fg_gl2.h"
+#include <math.h>
 
 /*
  * Need more types of polyhedra? See CPolyhedron in MRPT
  */
 
 
-#ifndef GL_ES_VERSION_2_0
 /* General functions for drawing geometry
  * Solids are drawn by glDrawArrays if composed of triangles, or by
  * glDrawElements if consisting of squares or pentagons that were
  * decomposition needed. We use the "first" parameter in glDrawArrays to go
  * from face to face.
  */
-static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace)
+
+/* Version for OpenGL (ES) 1.1 */
+#ifndef GL_ES_VERSION_2_0
+static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace)
 {
     int i;
     
@@ -60,6 +64,88 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_NORMAL_ARRAY);
 }
+#endif
+
+/* Version for OpenGL (ES) >= 2.0 */
+static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace,
+                                  GLint attribute_v_coord, GLint attribute_v_normal)
+{
+    GLuint vbo_coords = 0, vbo_normals = 0;
+    GLuint numVertices = numFaces * numEdgePerFace;
+
+    int i;
+
+    if (numVertices > 0 && attribute_v_coord != -1) {
+        fghGenBuffers(1, &vbo_coords);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+        fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
+                      vertices, FGH_STATIC_DRAW);
+    }
+    
+    if (numVertices > 0 && attribute_v_normal != -1) {
+        fghGenBuffers(1, &vbo_normals);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+        fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
+                      normals, FGH_STATIC_DRAW);
+    }
+    
+    if (vbo_coords) {
+        fghEnableVertexAttribArray(attribute_v_coord);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+        fghVertexAttribPointer(
+            attribute_v_coord,  /* attribute */
+            3,                  /* number of elements per vertex, here (x,y,z) */
+            GL_FLOAT,           /* the type of each element */
+            GL_FALSE,           /* take our values as-is */
+            0,                  /* no extra data between each position */
+            0                   /* offset of first element */
+        );
+    }
+
+    if (vbo_normals) {
+        fghEnableVertexAttribArray(attribute_v_normal);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+        fghVertexAttribPointer(
+            attribute_v_normal, /* attribute */
+            3,                  /* number of elements per vertex, here (x,y,z) */
+            GL_FLOAT,           /* the type of each element */
+            GL_FALSE,           /* take our values as-is */
+            0,                  /* no extra data between each position */
+            0                   /* offset of first element */
+        );
+    }
+
+    /* Draw per face (TODO: could use glMultiDrawArrays if available) */
+    for (i=0; i<numFaces; i++)
+        glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
+    
+    
+    if (vbo_coords != 0)
+        fghDisableVertexAttribArray(attribute_v_coord);
+    if (vbo_normals != 0)
+        fghDisableVertexAttribArray(attribute_v_normal);
+    
+    if (vbo_coords != 0)
+        fghDeleteBuffers(1, &vbo_coords);
+    if (vbo_normals != 0)
+        fghDeleteBuffers(1, &vbo_normals);
+}
+
+static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace)
+{
+    GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
+    GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
+
+    if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
+        /* User requested a 2.0 draw */
+        fghDrawGeometryWire20(vertices, normals, numFaces, numEdgePerFace,
+                          attribute_v_coord, attribute_v_normal);
+#ifndef GL_ES_VERSION_2_0
+    else
+        fghDrawGeometryWire11(vertices, normals, numFaces, numEdgePerFace);
+#endif
+}
+
 
 /* Draw the geometric shape with filled triangles
  *
@@ -69,8 +155,11 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
  * - If the shape was triangulated (DECOMPOSE_TO_TRIANGLE), some
  *   vertex+normal pairs are reused, so use vertex indices.
  */
-static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
-                                GLsizei numVertices, GLsizei numVertIdxs)
+
+/* Version for OpenGL (ES) 1.1 */
+#ifndef GL_ES_VERSION_2_0
+static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
+                                   GLsizei numVertices, GLsizei numVertIdxs)
 {
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_NORMAL_ARRAY);
@@ -85,8 +174,99 @@ static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *v
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_NORMAL_ARRAY);
 }
+#endif
 
+/* Version for OpenGL (ES) >= 2.0 */
+static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
+                                   GLsizei numVertices, GLsizei numVertIdxs,
+                                   GLint attribute_v_coord, GLint attribute_v_normal)
+{
+    GLuint vbo_coords = 0, vbo_normals = 0, ibo_elements = 0;
+    
+    if (numVertices > 0 && attribute_v_coord != -1) {
+        fghGenBuffers(1, &vbo_coords);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+        fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
+                      vertices, FGH_STATIC_DRAW);
+    }
+    
+    if (numVertices > 0 && attribute_v_normal != -1) {
+        fghGenBuffers(1, &vbo_normals);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+        fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
+                      normals, FGH_STATIC_DRAW);
+    }
+    
+    if (vertIdxs != NULL) {
+        fghGenBuffers(1, &ibo_elements);
+        fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
+        fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
+                      vertIdxs, FGH_STATIC_DRAW);
+    }
+    
+    if (vbo_coords) {
+        fghEnableVertexAttribArray(attribute_v_coord);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
+        fghVertexAttribPointer(
+            attribute_v_coord,  /* attribute */
+            3,                  /* number of elements per vertex, here (x,y,z) */
+            GL_FLOAT,           /* the type of each element */
+            GL_FALSE,           /* take our values as-is */
+            0,                  /* no extra data between each position */
+            0                   /* offset of first element */
+        );
+    };
+    
+    if (vbo_normals) {
+        fghEnableVertexAttribArray(attribute_v_normal);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
+        fghVertexAttribPointer(
+            attribute_v_normal, /* attribute */
+            3,                  /* number of elements per vertex, here (x,y,z) */
+            GL_FLOAT,           /* the type of each element */
+            GL_FALSE,           /* take our values as-is */
+            0,                  /* no extra data between each position */
+            0                   /* offset of first element */
+        );
+    };
+    
+    if (vertIdxs == NULL) {
+        glDrawArrays(GL_TRIANGLES, 0, numVertices);
+    } else {
+        fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
+        glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, 0);
+    }
+    
+    if (vbo_coords != 0)
+        fghDisableVertexAttribArray(attribute_v_coord);
+    if (vbo_normals != 0)
+        fghDisableVertexAttribArray(attribute_v_normal);
+    
+    if (vbo_coords != 0)
+        fghDeleteBuffers(1, &vbo_coords);
+    if (vbo_normals != 0)
+        fghDeleteBuffers(1, &vbo_normals);
+    if (ibo_elements != 0)
+        fghDeleteBuffers(1, &ibo_elements);
+}
 
+static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
+                                 GLsizei numVertices, GLsizei numVertIdxs)
+{
+    GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
+    GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
+
+    if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
+        /* User requested a 2.0 draw */
+        fghDrawGeometrySolid20(vertices, normals, vertIdxs,
+                               numVertices, numVertIdxs,
+                               attribute_v_coord, attribute_v_normal);
+#ifndef GL_ES_VERSION_2_0
+    else
+        fghDrawGeometrySolid11(vertices, normals, vertIdxs,
+                               numVertices, numVertIdxs);
+#endif
+}
 
 /* Shape decomposition to triangles
  * We'll use glDrawElements to draw all shapes that are not naturally
@@ -124,7 +304,7 @@ static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace,
     for (i=0; i<numFaces; i++)
     {
         int normIdx         = i*3;
-        int faceIdxVertIdx  = i*numEdgePerFace; // index to first element of "row" in vertex indices
+        int faceIdxVertIdx  = i*numEdgePerFace; /* index to first element of "row" in vertex indices */
         for (j=0; j<numEdgePerFace; j++)
         {
             int outIdx  = i*numEdgePerFace*3+j*3;
@@ -222,7 +402,7 @@ static GLubyte cube_vi[CUBE_VERT_PER_OBJ] =
     7,4,3,2,
     4,7,6,5
 };
-DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE);
+DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE)
 
 /* -- Dodecahedron -- */
 /* Magic Numbers:  It is possible to create a dodecahedron by attaching two
@@ -298,7 +478,7 @@ static GLubyte dodecahedron_vi[DODECAHEDRON_VERT_PER_OBJ] =
     18,  1,  0,  5,  9, 
     19, 14, 13, 10, 12
 };
-DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON);
+DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
 
 
 /* -- Icosahedron -- */
@@ -377,7 +557,7 @@ static GLubyte icosahedron_vi[ICOSAHEDRON_VERT_PER_OBJ] =
     11,  6,  7 ,
     11, 10,  6 
 };
-DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON);
+DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON)
 
 /* -- Octahedron -- */
 #define OCTAHEDRON_NUM_VERT           6
@@ -424,7 +604,7 @@ static GLubyte octahedron_vi[OCTAHEDRON_VERT_PER_OBJ] =
     3, 4, 2,
     3, 5, 4
 };
-DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON);
+DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON)
 
 /* -- RhombicDodecahedron -- */
 #define RHOMBICDODECAHEDRON_NUM_VERT            14
@@ -485,7 +665,7 @@ static GLubyte rhombicdodecahedron_vi[RHOMBICDODECAHEDRON_VERT_PER_OBJ] =
     7, 11, 13, 12,
     8, 12, 13,  9
 };
-DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON);
+DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
 
 /* -- Tetrahedron -- */
 /* Magic Numbers:  r0 = ( 1, 0, 0 )
@@ -529,7 +709,7 @@ static GLubyte tetrahedron_vi[TETRAHEDRON_VERT_PER_OBJ] =
     0, 3, 1,
     0, 1, 2
 };
-DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON);
+DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON)
 
 /* -- Sierpinski Sponge -- */
 static unsigned int ipow (int x, unsigned int y)
@@ -577,6 +757,7 @@ static void fghSierpinskiSpongeGenerate ( int numLevels, double offset[3], GLflo
     }
 }
 
+#ifndef GL_ES_VERSION_2_0
 /* -- Now the various shapes involving circles -- */
 /*
  * Compute lookup table of cos and sin values forming a circle
@@ -616,8 +797,13 @@ static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GL
 
     for (i=1; i<size; i++)
     {
+#ifdef __cplusplus
         (*sint)[i] = sinf(angle*i);
         (*cost)[i] = cosf(angle*i);
+#else
+        (*sint)[i] = (float)sin((double)(angle*i));
+        (*cost)[i] = (float)cos((double)(angle*i));
+#endif  /* __cplusplus */
     }
 
     
@@ -709,6 +895,88 @@ static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloa
     free(cost2);
 }
 
+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+1)+1;
+
+    /* 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;
+
+    /* 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);
+}
+#endif
 
 /* -- INTERNAL DRAWING functions --------------------------------------- */
 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
@@ -765,22 +1033,22 @@ static void fghCube( GLfloat dSize, GLboolean useWireMode )
         vertices = cube_verts;
 
     if (useWireMode)
-        fghDrawGeometryWire (vertices, cube_norms,
-                            CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE);
+        fghDrawGeometryWire(vertices, cube_norms,
+                            CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE);
     else
         fghDrawGeometrySolid(vertices, cube_norms, cube_vertIdxs,
-                            CUBE_VERT_PER_OBJ, CUBE_VERT_PER_OBJ_TRI);
+                             CUBE_VERT_PER_OBJ, 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);
+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 )
 {
@@ -818,11 +1086,15 @@ static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale
 }
 
 
+#ifndef GL_ES_VERSION_2_0
 static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useWireMode )
 {
     int i,j,idx, nVert;
     GLfloat *vertices, *normals;
 
+    if (slices * stacks > 65535)
+        fgWarning("fghSphere: too many slices or stacks requested, indices will wrap");
+
     /* Generate vertices and normals */
     fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
     
@@ -832,37 +1104,43 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
 
     if (useWireMode)
     {
-        GLuint  *sliceIdx, *stackIdx;
+        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(GLuint));
-        stackIdx = malloc(slices*(stacks-1)*sizeof(GLuint));
-
-        /* generate for each stack */
-        for (i=0,idx=0; i<slices; i++)
+        sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
+        stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
+        if (!(stackIdx) || !(sliceIdx))
         {
-            GLuint 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... */
+            free(stackIdx);
+            free(sliceIdx);
+            fgError("Failed to allocate memory in fghGenerateSphere");
         }
 
         /* generate for each stack */
         for (i=0,idx=0; i<stacks-1; i++)
         {
-            GLuint offset = 1+i*slices;             /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
+            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 */
         glEnableClientState(GL_VERTEX_ARRAY);
         glEnableClientState(GL_NORMAL_ARRAY);
@@ -871,10 +1149,10 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
         glNormalPointer(GL_FLOAT, 0, normals);
         /*draw slices*/
         for (i=0; i<slices; i++)
-            glDrawElements(GL_LINE_STRIP,stacks+1,GL_UNSIGNED_INT,sliceIdx+i*(stacks+1));
+            glDrawElements(GL_LINE_STRIP,stacks+1,GL_UNSIGNED_SHORT,sliceIdx+i*(stacks+1));
         /*draw stacks*/
         for (i=0; i<stacks-1; i++)
-            glDrawElements(GL_LINE_LOOP, slices,GL_UNSIGNED_INT,stackIdx+i*slices);
+            glDrawElements(GL_LINE_LOOP, slices,GL_UNSIGNED_SHORT,stackIdx+i*slices);
 
         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -885,48 +1163,55 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     }
     else
     {
-        GLuint  *topIdx, *bottomIdx, *stripIdx;
         /* First, generate vertex index arrays for drawing with glDrawElements
-         * Top and bottom are covered with a triangle fan
-         * Each other stack with triangle strip. Only need to generate on
-         * of those as we'll have to draw each stack separately, and can
-         * just use different offsets in 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 */
-        topIdx = malloc((slices+2)*sizeof(GLuint));
-        bottomIdx = malloc((slices+2)*sizeof(GLuint));
-        stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLuint));
-        if (!(topIdx) || !(bottomIdx) || !(stripIdx))
+        stripIdx = malloc((slices+1)*2*(stacks)*sizeof(GLushort));
+        if (!(stripIdx))
         {
-            free(topIdx);
-            free(bottomIdx);
             free(stripIdx);
             fgError("Failed to allocate memory in fghGenerateSphere");
         }
 
-        topIdx[0]=0;
-        topIdx[1] = 1;                              /* repeat first slice's idx for closing off shape */
-        for (j=slices, idx=2; j>0; j--, idx++)
-            topIdx[idx] = j;
-
-        bottomIdx[0]=nVert-1;                       /* zero based index, last element in array... */
-        for (j=0, idx=1; j<slices; j++, idx++)
-            bottomIdx[idx] = nVert-(slices+1)+j;
-        bottomIdx[idx] = nVert-(slices+1);          /* repeat first slice's idx for closing off shape */
+        /* 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,idx=0; i<stacks-2; i++, idx+=2)
+        for (i=0; i<stacks-2; i++, idx+=2)
         {
-            GLuint offset = 1+i*slices;             /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
+            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+0;
+            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 */
@@ -935,20 +1220,14 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
 
         glVertexPointer(3, GL_FLOAT, 0, vertices);
         glNormalPointer(GL_FLOAT, 0, normals);
-        /*draw top*/
-        glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_INT,topIdx);
         /*draw stacks*/
-        for (i=0; i<stacks-2; i++)
-            glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_INT,stripIdx+i*(slices+1)*2);
-        /*draw bottom*/
-        glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_INT,bottomIdx);
+        for (i=0; i<stacks; i++)
+            glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_SHORT,stripIdx+i*(slices+1)*2);
 
         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_NORMAL_ARRAY);
 
         /* cleanup allocated memory */
-        free(topIdx);
-        free(bottomIdx);
         free(stripIdx);
     }
     
@@ -957,13 +1236,143 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     free(normals);
 }
 
-#endif /* GL_ES_VERSION_2_0 */
+static void fghCone( double base, double height, GLint slices, GLint stacks, GLboolean useWireMode )
+{
+    int i,j,idx, nVert;
+    GLfloat *vertices, *normals;
+
+    if (slices * stacks > 65535)
+        fgWarning("fghCone: too many slices or stacks requested, indices will wrap");
+
+    /* Generate vertices and normals */
+    fghGenerateCone((GLfloat)base,(GLfloat)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+1)*sizeof(GLushort));
+        sliceIdx = malloc(slices*2         *sizeof(GLushort));
+        if (!(stackIdx) || !(sliceIdx))
+        {
+            free(stackIdx);
+            free(sliceIdx);
+            fgError("Failed to allocate memory in fghGenerateCone");
+        }
+
+        /* 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++] = offset;
+            sliceIdx[idx++] = offset+stacks*slices;
+        }
+
+        /* draw */
+        glEnableClientState(GL_VERTEX_ARRAY);
+        glEnableClientState(GL_NORMAL_ARRAY);
+
+        glVertexPointer(3, GL_FLOAT, 0, vertices);
+        glNormalPointer(GL_FLOAT, 0, normals);
+        /*draw slices*/
+        glDrawElements(GL_LINES,slices*2,GL_UNSIGNED_SHORT,sliceIdx);
+        /*draw stacks*/
+        for (i=0; i<stacks; i++)
+            glDrawElements(GL_LINE_LOOP, slices,GL_UNSIGNED_SHORT,stackIdx+i*slices);
+
+        glDisableClientState(GL_VERTEX_ARRAY);
+        glDisableClientState(GL_NORMAL_ARRAY);
+
+        /* 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));
+        if (!(stripIdx))
+        {
+            free(stripIdx);
+            fgError("Failed to allocate memory in fghGenerateCone");
+        }
+
+        /* 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*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 */
+        glEnableClientState(GL_VERTEX_ARRAY);
+        glEnableClientState(GL_NORMAL_ARRAY);
+
+        glVertexPointer(3, GL_FLOAT, 0, vertices);
+        glNormalPointer(GL_FLOAT, 0, normals);
+        /*draw stacks*/
+        for (i=0; i<stacks+1; i++)
+            glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_SHORT,stripIdx+i*(slices+1)*2);
+
+        glDisableClientState(GL_VERTEX_ARRAY);
+        glDisableClientState(GL_NORMAL_ARRAY);
+
+        /* cleanup allocated memory */
+        free(stripIdx);
+    }
+
+    /* cleanup allocated memory */
+    free(vertices);
+    free(normals);
+}
 
 
 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
 
 
-#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid sphere
  */
@@ -984,75 +1393,17 @@ void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
     fghSphere( radius, slices, stacks, TRUE );
     
 }
+#endif /* GL_ES_VERSION_2_0 */
 
+#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid cone
  */
 void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks )
 {
-    int i,j;
-
-    /* Step in z and radius as stacks are drawn. */
-
-    GLfloat z0,z1;
-    GLfloat r0,r1;
-
-    const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
-    const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 );
-
-    /* Scaling factors for vertex normals */
-
-    const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
-    const GLfloat sinn = ( (GLfloat)base   / sqrtf( height * height + base * base ));
-
-    /* Pre-computed circle */
-
-    GLfloat *sint,*cost;
-
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" );
 
-    fghCircleTable(&sint,&cost,-slices,FALSE);
-
-    /* Cover the circular base with a triangle fan... */
-
-    z0 = 0;
-    z1 = zStep;
-
-    r0 = (GLfloat)base;
-    r1 = r0 - rStep;
-
-    glBegin(GL_TRIANGLE_FAN);
-
-        glNormal3f(0,0,-1);
-        glVertex3f(0,0, z0 );
-
-        for (j=0; j<=slices; j++)
-            glVertex3f(cost[j]*r0, sint[j]*r0, z0);
-
-    glEnd();
-
-    /* Cover each stack with a triangle strip */
-    for( i=0; i<stacks; i++ )
-    {
-        glBegin(GL_TRIANGLE_STRIP);
-
-            for(j=0; j<=slices; j++)
-            {
-                glNormal3f(cost[j]*cosn, sint[j]*cosn, sinn);
-                glVertex3f(cost[j]*r0,   sint[j]*r0,   z0  );
-                glVertex3f(cost[j]*r1,   sint[j]*r1,   z1  );
-            }
-
-            z0 = z1; z1 += zStep;
-            r0 = r1; r1 -= rStep;
-
-        glEnd();
-    }
-
-    /* Release sin and cos tables */
-
-    free(sint);
-    free(cost);
+    fghCone( base, height, slices, stacks, FALSE );
 }
 
 /*
@@ -1060,66 +1411,9 @@ void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint s
  */
 void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint stacks)
 {
-    int i,j;
-
-    /* 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 */
-
-    const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
-    const GLfloat sinn = ( (GLfloat)base   / sqrtf( height * height + base * base ));
-
-    /* Pre-computed circle */
-
-    GLfloat *sint,*cost;
-
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" );
 
-    fghCircleTable(&sint,&cost,-slices,FALSE);
-
-    /* Draw the stacks... */
-
-    for (i=0; i<stacks; i++)
-    {
-        glBegin(GL_LINE_LOOP);
-
-            for( j=0; j<slices; j++ )
-            {
-                glNormal3f(cost[j]*sinn, sint[j]*sinn, cosn);
-                glVertex3f(cost[j]*r,    sint[j]*r,    z   );
-            }
-
-        glEnd();
-
-        z += zStep;
-        r -= rStep;
-    }
-
-    /* Draw the slices */
-
-    r = (GLfloat)base;
-
-    glBegin(GL_LINES);
-
-        for (j=0; j<slices; j++)
-        {
-            glNormal3f(cost[j]*sinn, sint[j]*sinn,          cosn  );
-            glVertex3f(cost[j]*r,    sint[j]*r,             0     );
-            glVertex3f(0,            0,            (GLfloat)height);
-        }
-
-    glEnd();
-
-    /* Release sin and cos tables */
-
-    free(sint);
-    free(cost);
+    fghCone( base, height, slices, stacks, TRUE );
 }
 
 
@@ -1274,15 +1568,25 @@ void FGAPIENTRY glutWireTorus( double dInnerRadius, double dOuterRadius, GLint n
 
   for( j=0; j<nRings; j++ )
   {
+#ifdef __cplusplus
     cpsi = cosf( psi ) ;
     spsi = sinf( psi ) ;
+#else
+    cpsi = (float)cos( (double)psi ) ;
+    spsi = (float)sin( (double)psi ) ;
+#endif  /* __cplusplus */
     phi = 0.0f;
 
     for( i=0; i<nSides; i++ )
     {
       int offset = 3 * ( j * nSides + i ) ;
+#ifdef __cplusplus
       cphi = cosf( phi ) ;
       sphi = sinf( phi ) ;
+#else
+      cphi = (float)cos( (double)phi ) ;
+      sphi = (float)sin( (double)phi ) ;
+#endif  /* __cplusplus */
       *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
       *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
       *(vertex + offset + 2) =                    sphi * iradius  ;
@@ -1360,15 +1664,25 @@ void FGAPIENTRY glutSolidTorus( double dInnerRadius, double dOuterRadius, GLint
 
   for( j=0; j<nRings; j++ )
   {
+#ifdef __cplusplus
     cpsi = cosf( psi ) ;
     spsi = sinf( psi ) ;
+#else
+    cpsi = (float)cos( (double)psi ) ;
+    spsi = (float)sin( (double)psi ) ;
+#endif  /* __cplusplus */
     phi = 0.0f;
 
     for( i=0; i<nSides; i++ )
     {
       int offset = 3 * ( j * nSides + i ) ;
+#ifdef __cplusplus
       cphi = cosf( phi ) ;
       sphi = sinf( phi ) ;
+#else
+      cphi = (float)cos( (double)phi ) ;
+      sphi = (float)sin( (double)phi ) ;
+#endif  /* __cplusplus */
       *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
       *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
       *(vertex + offset + 2) =                    sphi * iradius  ;
@@ -1433,10 +1747,10 @@ void FGAPIENTRY glutSolidCube( double dSize )
     fghCube( (GLfloat)dSize, FALSE );
 }
 
-DECLARE_SHAPE_INTERFACE(Dodecahedron);
-DECLARE_SHAPE_INTERFACE(Icosahedron);
-DECLARE_SHAPE_INTERFACE(Octahedron);
-DECLARE_SHAPE_INTERFACE(RhombicDodecahedron);
+DECLARE_SHAPE_INTERFACE(Dodecahedron)
+DECLARE_SHAPE_INTERFACE(Icosahedron)
+DECLARE_SHAPE_INTERFACE(Octahedron)
+DECLARE_SHAPE_INTERFACE(RhombicDodecahedron)
 
 void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, double offset[3], double scale )
 {
@@ -1449,7 +1763,7 @@ void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, double offset[3], do
     fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, FALSE );
 }
 
-DECLARE_SHAPE_INTERFACE(Tetrahedron);
+DECLARE_SHAPE_INTERFACE(Tetrahedron)
 
 
 /*** END OF FILE ***/