Declare OpenGL 2.0 dynamically-loaded functions for internal use
[freeglut] / src / fg_geometry.c
index 438d447..4dbee68 100644 (file)
@@ -823,6 +823,9 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     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,19 +835,19 @@ 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));
+        sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
+        stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
 
         /* generate for each stack */
         for (i=0,idx=0; i<slices; i++)
         {
-            GLuint offset = 1+i;                    /* start at 1 (0 is top vertex), and we advance one slice as we go along */
+            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++)
             {
@@ -856,7 +859,7 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
         /* 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;
@@ -871,10 +874,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,7 +888,7 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     }
     else
     {
-        GLuint  *topIdx, *bottomIdx, *stripIdx;
+        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
@@ -894,9 +897,9 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
          */
 
         /* 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));
+        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))
         {
             free(topIdx);
@@ -905,6 +908,12 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
             fgError("Failed to allocate memory in fghGenerateSphere");
         }
 
+        /* 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++)
@@ -918,7 +927,7 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
         /* 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)
         {
-            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 */
+            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;
@@ -936,12 +945,12 @@ 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);
+        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_INT,stripIdx+i*(slices+1)*2);
+            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_INT,bottomIdx);
+        glDrawElements(GL_TRIANGLE_FAN,slices+2,GL_UNSIGNED_SHORT,bottomIdx);
 
         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -963,7 +972,6 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
 
 
-#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid sphere
  */
@@ -985,6 +993,7 @@ void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
     
 }
 
+#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid cone
  */
@@ -1031,11 +1040,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++)
             {
@@ -1050,22 +1058,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);
@@ -1186,7 +1178,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  );