Removing the build errors and a build warning that crept in for MSVC 6.0. There...
[freeglut] / src / fg_geometry.c
index 438d447..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,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, 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
  *
@@ -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, 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 naturally
@@ -818,11 +998,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,19 +1016,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 +1040,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 +1055,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 +1069,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 +1078,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 +1089,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 +1108,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 +1126,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);
@@ -957,13 +1147,11 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     free(normals);
 }
 
-#endif /* GL_ES_VERSION_2_0 */
 
 
 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
 
 
-#ifndef EGL_VERSION_1_0
 /*
  * Draws a solid sphere
  */
@@ -984,7 +1172,9 @@ 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
  */
@@ -1031,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++)
             {
@@ -1050,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);
@@ -1186,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  );