Removing the build errors and a build warning that crept in for MSVC 6.0. There...
[freeglut] / src / fg_geometry.c
index 572b7e3..312acfc 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,26 +64,214 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_NORMAL_ARRAY);
 }
-static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs, GLsizei numVertices, GLsizei numEdgePerFace)
+#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, vbo_normals;
+    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
+ *
+ * - If the shape is naturally triangulated (numEdgePerFace==3), each
+ *   vertex+normal pair is used only once, so no vertex indices.
+ * 
+ * - If the shape was triangulated (DECOMPOSE_TO_TRIANGLE), some
+ *   vertex+normal pairs are reused, so use vertex indices.
+ */
+
+/* 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);
 
     glVertexPointer(3, GL_FLOAT, 0, vertices);
     glNormalPointer(GL_FLOAT, 0, normals);
-    if (numEdgePerFace==3)
+    if (vertIdxs == NULL)
         glDrawArrays(GL_TRIANGLES, 0, numVertices);
     else
-       /* The number of elements is passed as numVertices */
-        glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_BYTE, vertIdxs);
+        glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, vertIdxs);
 
     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, vbo_normals, ibo_elements;
+    
+    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 triangles, so
- * generate an index vector here, using the below sampling scheme.
+ * We'll use glDrawElements to draw all shapes that are not naturally
+ * composed of triangles, so generate an index vector here, using the
+ * below sampling scheme.
  * Be careful to keep winding of all triangles counter-clockwise,
  * assuming that input has correct winding...
  */
@@ -93,7 +285,7 @@ static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace,
     switch (numEdgePerFace)
     {
     case 3:
-        /* nothing to do here, we'll drawn with glDrawArrays */
+        /* nothing to do here, we'll draw with glDrawArrays */
         break;
     case 4:
         vertSamps = vert4Decomp;
@@ -146,7 +338,7 @@ static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLfloat *verti
 /* Cache of input to glDrawArrays or glDrawElements
  * In general, we build arrays with all vertices or normals.
  * We cant compress this and use glDrawElements as all combinations of
- * vertex and normals are unique.
+ * vertices and normals are unique.
  */
 #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\
     static GLboolean name##Cached = FALSE;\
@@ -622,6 +814,81 @@ static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GL
     }
 }
 
+static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat **vertices, GLfloat **normals, int* nVert)
+{
+    int i,j;
+    int idx = 0;    /* idx into vertex/normal buffer */
+    GLfloat x,y,z;
+
+    /* Pre-computed circle */
+    GLfloat *sint1,*cost1;
+    GLfloat *sint2,*cost2;
+
+    /* number of unique vertices */
+    if (slices==0 || stacks<2)
+    {
+        /* nothing to generate */
+        *nVert = 0;
+        return;
+    }
+    *nVert = slices*(stacks-1)+2;
+
+    /* precompute values on unit circle */
+    fghCircleTable(&sint1,&cost1,-slices,FALSE);
+    fghCircleTable(&sint2,&cost2, stacks,TRUE);
+
+    /* 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");
+    }
+
+    /* top */
+    (*vertices)[0] = 0.f;
+    (*vertices)[1] = 0.f;
+    (*vertices)[2] = radius;
+    (*normals )[0] = 0.f;
+    (*normals )[1] = 0.f;
+    (*normals )[2] = 1.f;
+    idx = 3;
+
+    /* each stack */
+    for( i=1; i<stacks; i++ )
+    {
+        for(j=0; j<slices; j++, idx+=3)
+        {
+            x = cost1[j]*sint2[i];
+            y = sint1[j]*sint2[i];
+            z = cost2[i];
+
+            (*vertices)[idx  ] = x*radius;
+            (*vertices)[idx+1] = y*radius;
+            (*vertices)[idx+2] = z*radius;
+            (*normals )[idx  ] = x;
+            (*normals )[idx+1] = y;
+            (*normals )[idx+2] = z;
+        }
+    }
+
+    /* bottom */
+    (*vertices)[idx  ] =  0.f;
+    (*vertices)[idx+1] =  0.f;
+    (*vertices)[idx+2] = -radius;
+    (*normals )[idx  ] =  0.f;
+    (*normals )[idx+1] =  0.f;
+    (*normals )[idx+2] = -1.f;
+
+    /* Done creating vertices, release sin and cos tables */
+    free(sint1);
+    free(cost1);
+    free(sint2);
+    free(cost2);
+}
+
 
 /* -- INTERNAL DRAWING functions --------------------------------------- */
 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
@@ -641,7 +908,7 @@ static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GL
         else\
         {\
             fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\
-                                 nameCaps##_VERT_PER_OBJ_TRI,                     nameCaps##_NUM_EDGE_PER_FACE);\
+                                 nameCaps##_VERT_PER_OBJ, nameCaps##_VERT_PER_OBJ_TRI); \
         }\
     }
 #define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps)                        _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
@@ -678,9 +945,11 @@ 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_TRI,               CUBE_NUM_EDGE_PER_FACE);
+        fghDrawGeometrySolid(vertices, cube_norms, cube_vertIdxs,
+                            CUBE_VERT_PER_OBJ, CUBE_VERT_PER_OBJ_TRI);
 
     if (dSize!=1.f)
         /* cleanup allocated memory */
@@ -719,107 +988,178 @@ static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale
 
         /* Draw and cleanup */
         if (useWireMode)
-            fghDrawGeometryWire (vertices,normals,             numFace,TETRAHEDRON_NUM_EDGE_PER_FACE);
+            fghDrawGeometryWire (vertices,normals,numFace,TETRAHEDRON_NUM_EDGE_PER_FACE);
         else
-            fghDrawGeometrySolid(vertices,normals,NULL,numVert,        TETRAHEDRON_NUM_EDGE_PER_FACE);
+            fghDrawGeometrySolid(vertices,normals,NULL,numVert,numVert);
 
         free(vertices);
         free(normals );
     }
 }
-#endif /* GL_ES_VERSION_2_0 */
 
 
-/* -- INTERFACE FUNCTIONS ---------------------------------------------- */
-
-
-#ifndef EGL_VERSION_1_0
-/*
- * Draws a solid sphere
- */
-void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
+#ifndef GL_ES_VERSION_2_0
+static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useWireMode )
 {
-    int i,j;
+    int i,j,idx, nVert;
+    GLfloat *vertices, *normals;
 
-    /* Adjust z and radius as stacks are drawn. */
-    GLfloat radf = (GLfloat)radius;
-    GLfloat z0,z1;
-    GLfloat r0,r1;
+    if (slices * stacks > 65535)
+       fgWarning("fghSphere: too many slices or stacks requested, indices will wrap");
 
-    /* Pre-computed circle */
+    /* Generate vertices and normals */
+    fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
+    
+    if (nVert==0)
+        /* nothing to draw */
+        return;
 
-    GLfloat *sint1,*cost1;
-    GLfloat *sint2,*cost2;
+    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.
+         */
 
-    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
+        sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
+        stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
 
-    fghCircleTable(&sint1,&cost1,-slices,FALSE);
-    fghCircleTable(&sint2,&cost2, stacks,TRUE);
+        /* generate for each stack */
+        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... */
+        }
 
-    /* The top stack is covered with a triangle fan */
+        /* 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;
+            }
+        }
 
-    z0 = 1;
-    z1 = cost2[(stacks>0)?1:0];
-    r0 = 0;
-    r1 = sint2[(stacks>0)?1:0];
+        /* draw */
+        glEnableClientState(GL_VERTEX_ARRAY);
+        glEnableClientState(GL_NORMAL_ARRAY);
 
-    glBegin(GL_TRIANGLE_FAN);
+        glVertexPointer(3, GL_FLOAT, 0, vertices);
+        glNormalPointer(GL_FLOAT, 0, normals);
+        /*draw slices*/
+        for (i=0; i<slices; i++)
+            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_SHORT,stackIdx+i*slices);
 
-        glNormal3f(0,0,1);
-        glVertex3f(0,0,radf);
+        glDisableClientState(GL_VERTEX_ARRAY);
+        glDisableClientState(GL_NORMAL_ARRAY);
 
-        for (j=slices; j>=0; j--)
+        /* cleanup allocated memory */
+        free(sliceIdx);
+        free(stackIdx);
+    }
+    else
+    {
+        GLushort  *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.
+         */
+
+        /* Allocate buffers for indices, bail out if memory allocation fails */
+        topIdx = malloc((slices+2)*sizeof(GLushort));
+        bottomIdx = malloc((slices+2)*sizeof(GLushort));
+        stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLushort));
+        if (!(topIdx) || !(bottomIdx) || !(stripIdx))
         {
-            glNormal3f(cost1[j]*r1,      sint1[j]*r1,      z1     );
-            glVertex3f(cost1[j]*r1*radf, sint1[j]*r1*radf, z1*radf);
+            free(topIdx);
+            free(bottomIdx);
+            free(stripIdx);
+            fgError("Failed to allocate memory in fghGenerateSphere");
         }
 
-    glEnd();
+        /* TODO: Can do top and bottom as Triangle strip as well
+           (just need to repeat top/btoom vertex a lot). Then we can draw
+           the whole thing with just one index array and one for-looped call
+           to glDrawElements.. That'll make it easier to reuse code with other
+           Circular objects too
+           */
+        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 */
+
+        /* 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)
+        {
+            GLushort 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;
+        }
 
-    /* Cover each stack with a quad strip, except the top and bottom stacks */
 
-    for( i=1; i<stacks-1; i++ )
-    {
-        z0 = z1; z1 = cost2[i+1];
-        r0 = r1; r1 = sint2[i+1];
+        /* draw */
+        glEnableClientState(GL_VERTEX_ARRAY);
+        glEnableClientState(GL_NORMAL_ARRAY);
 
-        glBegin(GL_QUAD_STRIP);
+        glVertexPointer(3, GL_FLOAT, 0, vertices);
+        glNormalPointer(GL_FLOAT, 0, normals);
+        /*draw top*/
+        glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_SHORT,topIdx);
+        /*draw stacks*/
+        for (i=0; i<stacks-2; i++)
+            glDrawElements(GL_TRIANGLE_STRIP,(slices+1)*2,GL_UNSIGNED_SHORT,stripIdx+i*(slices+1)*2);
+        /*draw bottom*/
+        glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_SHORT,bottomIdx);
 
-            for(j=0; j<=slices; j++)
-            {
-                glNormal3d(cost1[j]*r1,      sint1[j]*r1,      z1     );
-                glVertex3d(cost1[j]*r1*radf, sint1[j]*r1*radf, z1*radf);
-                glNormal3d(cost1[j]*r0,      sint1[j]*r0,      z0     );
-                glVertex3d(cost1[j]*r0*radf, sint1[j]*r0*radf, z0*radf);
-            }
+        glDisableClientState(GL_VERTEX_ARRAY);
+        glDisableClientState(GL_NORMAL_ARRAY);
 
-        glEnd();
+        /* cleanup allocated memory */
+        free(topIdx);
+        free(bottomIdx);
+        free(stripIdx);
     }
+    
+    /* cleanup allocated memory */
+    free(vertices);
+    free(normals);
+}
 
-    /* The bottom stack is covered with a triangle fan */
-
-    z0 = z1;
-    r0 = r1;
-
-    glBegin(GL_TRIANGLE_FAN);
 
-        glNormal3d(0,0,-1);
-        glVertex3d(0,0,-radius);
 
-        for (j=0; j<=slices; j++)
-        {
-            glNormal3d(cost1[j]*r0,      sint1[j]*r0,      z0     );
-            glVertex3d(cost1[j]*r0*radf, sint1[j]*r0*radf, z0*radf);
-        }
+/* -- INTERFACE FUNCTIONS ---------------------------------------------- */
 
-    glEnd();
 
-    /* Release sin and cos tables */
+/*
+ * Draws a solid sphere
+ */
+void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
+{
+    FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
 
-    free(sint1);
-    free(cost1);
-    free(sint2);
-    free(cost2);
+    fghSphere( radius, slices, stacks, FALSE );
 }
 
 /*
@@ -827,71 +1167,14 @@ void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
  */
 void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
 {
-    int i,j;
-
-    /* Adjust z and radius as stacks and slices are drawn. */
-    GLfloat radf = (GLfloat)radius;
-    GLfloat r;
-    GLfloat x,y,z;
-
-    /* Pre-computed circle */
-
-    GLfloat *sint1,*cost1;
-    GLfloat *sint2,*cost2;
-
     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
 
-    fghCircleTable(&sint1,&cost1,-slices,FALSE);
-    fghCircleTable(&sint2,&cost2, stacks,TRUE);
-
-    /* Draw a line loop for each stack */
-
-    for (i=1; i<stacks; i++)
-    {
-        z = cost2[i];
-        r = sint2[i];
-
-        glBegin(GL_LINE_LOOP);
-
-            for(j=0; j<=slices; j++)
-            {
-                x = cost1[j];
-                y = sint1[j];
-
-                glNormal3f(x,y,z);
-                glVertex3f(x*r*radf,y*r*radf,z*radf);
-            }
-
-        glEnd();
-    }
-
-    /* Draw a line loop for each slice */
-
-    for (i=0; i<slices; i++)
-    {
-        glBegin(GL_LINE_STRIP);
-
-            for(j=0; j<=stacks; j++)
-            {
-                x = cost1[i]*sint2[j];
-                y = sint1[i]*sint2[j];
-                z = cost2[j];
-
-                glNormal3f(x,y,z);
-                glVertex3f(x*radf,y*radf,z*radf);
-            }
-
-        glEnd();
-    }
-
-    /* Release sin and cos tables */
-
-    free(sint1);
-    free(cost1);
-    free(sint2);
-    free(cost2);
+    fghSphere( radius, slices, stacks, TRUE );
+    
 }
+#endif /* GL_ES_VERSION_2_0 */
 
+#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid cone
  */
@@ -938,11 +1221,10 @@ void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint s
 
     glEnd();
 
-    /* Cover each stack with a quad strip, except the top stack */
-
-    for( i=0; i<stacks-1; i++ )
+    /* Cover each stack with a triangle strip */
+    for( i=0; i<stacks; i++ )
     {
-        glBegin(GL_QUAD_STRIP);
+        glBegin(GL_TRIANGLE_STRIP);
 
             for(j=0; j<=slices; j++)
             {
@@ -957,22 +1239,6 @@ void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint s
         glEnd();
     }
 
-    /* The top stack is covered with individual triangles */
-
-    glBegin(GL_TRIANGLES);
-
-        glNormal3f(cost[0]*sinn, sint[0]*sinn, cosn);
-
-        for (j=0; j<slices; j++)
-        {
-            glVertex3f(cost[j+0]*r0,   sint[j+0]*r0,            z0    );
-            glVertex3f(0,              0,              (GLfloat)height);
-            glNormal3f(cost[j+1]*sinn, sint[j+1]*sinn,          cosn  );
-            glVertex3f(cost[j+1]*r0,   sint[j+1]*r0,            z0    );
-        }
-
-    glEnd();
-
     /* Release sin and cos tables */
 
     free(sint);
@@ -1093,7 +1359,7 @@ void FGAPIENTRY glutSolidCylinder(double radius, double height, GLint slices, GL
         if (i==stacks)
             z1 = (GLfloat)height;
 
-        glBegin(GL_QUAD_STRIP);
+        glBegin(GL_TRIANGLE_STRIP);
             for (j=0; j<=slices; j++ )
             {
                 glNormal3f(cost[j],      sint[j],      0  );