Add 'shapes' test for GLES1 (Android and Mesa EGL), as an independent CMake sample app
[freeglut] / src / fg_geometry.c
index 874e9bd..7306061 100644 (file)
@@ -28,6 +28,7 @@
 #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
 static void fghDrawGeometryWire11(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;
+    int i;
+    
+    glEnableClientState(GL_VERTEX_ARRAY);
+    glEnableClientState(GL_NORMAL_ARRAY);
 
-    if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1)) {
-       GLuint vbo_coords, vbo_normals;
-       GLuint numVertices = numFaces * numEdgePerFace;
-       
-       
-       if (numVertices > 0 && attribute_v_coord != -1) {
-           fghGenBuffers(1, &vbo_coords);
-           fghBindBuffer(GL_ARRAY_BUFFER, vbo_coords);
-           fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
-                         vertices, GL_STATIC_DRAW);
-       }
-       
-       if (numVertices > 0 && attribute_v_normal != -1) {
-           fghGenBuffers(1, &vbo_normals);
-           fghBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
-           fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
-                         normals, GL_STATIC_DRAW);
-       }
+    glVertexPointer(3, GL_FLOAT, 0, vertices);
+    glNormalPointer(GL_FLOAT, 0, normals);
 
-        if (vbo_coords) {
-            fghEnableVertexAttribArray(attribute_v_coord);
-            fghBindBuffer(GL_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
-            );
-       }
+    /* Draw per face (TODO: could use glMultiDrawArrays if available) */
+    for (i=0; i<numFaces; i++)
+        glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
 
-        if (vbo_normals) {
-            fghEnableVertexAttribArray(attribute_v_normal);
-            fghBindBuffer(GL_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
-            );
-       }
-    
-    
-       int i;
-    
-       /* 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);
-    }
+    glDisableClientState(GL_VERTEX_ARRAY);
+    glDisableClientState(GL_NORMAL_ARRAY);
 }
 #endif
 
@@ -123,24 +72,26 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
 {
     GLuint vbo_coords, vbo_normals;
     GLuint numVertices = numFaces * numEdgePerFace;
-    
+
+    int i;
+
     if (numVertices > 0 && attribute_v_coord != -1) {
            fghGenBuffers(1, &vbo_coords);
-           fghBindBuffer(GL_ARRAY_BUFFER, vbo_coords);
-           fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
-                         vertices, GL_STATIC_DRAW);
+           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(GL_ARRAY_BUFFER, vbo_normals);
-       fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
-                     normals, GL_STATIC_DRAW);
+       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(GL_ARRAY_BUFFER, vbo_coords);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
         fghVertexAttribPointer(
             attribute_v_coord,  // attribute
             3,                  // number of elements per vertex, here (x,y,z)
@@ -153,7 +104,7 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
 
     if (vbo_normals) {
         fghEnableVertexAttribArray(attribute_v_normal);
-        fghBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
+        fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
         fghVertexAttribPointer(
             attribute_v_normal, // attribute
             3,                  // number of elements per vertex, here (x,y,z)
@@ -164,9 +115,6 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
         );
     }
 
-
-    int i;
-    
     /* Draw per face (TODO: could use glMultiDrawArrays if available) */
     for (i=0; i<numFaces; i++)
        glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
@@ -185,7 +133,6 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
 
 static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace)
 {
-#ifndef GL_ES_VERSION_2_0
     GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
     GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
 
@@ -193,9 +140,10 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
         /* User requested a 2.0 draw */
        fghDrawGeometryWire20(vertices, normals, numFaces, numEdgePerFace,
                              attribute_v_coord, attribute_v_normal);
+#ifndef GL_ES_VERSION_2_0
     else
-#endif
        fghDrawGeometryWire11(vertices, normals, numFaces, numEdgePerFace);
+#endif
 }
 
 
@@ -213,78 +161,18 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
 static void fghDrawGeometrySolid11(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)) {
-       GLuint vbo_coords, vbo_normals, ibo_elements;
+    glEnableClientState(GL_VERTEX_ARRAY);
+    glEnableClientState(GL_NORMAL_ARRAY);
 
-       if (numVertices > 0 && attribute_v_coord != -1) {
-           fghGenBuffers(1, &vbo_coords);
-           fghBindBuffer(GL_ARRAY_BUFFER, vbo_coords);
-           fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
-                         vertices, GL_STATIC_DRAW);
-       }
-       
-       if (numVertices > 0 && attribute_v_normal != -1) {
-           fghGenBuffers(1, &vbo_normals);
-           fghBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
-           fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
-                         normals, GL_STATIC_DRAW);
-       }
-       
-       if (vertIdxs != NULL) {
-           fghGenBuffers(1, &ibo_elements);
-           fghBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
-           fghBufferData(GL_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
-                         vertIdxs, GL_STATIC_DRAW);
-       }
-       
-        if (vbo_coords) {
-            fghEnableVertexAttribArray(attribute_v_coord);
-            fghBindBuffer(GL_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(GL_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(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
-            glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, 0);
-        }
+    glVertexPointer(3, GL_FLOAT, 0, vertices);
+    glNormalPointer(GL_FLOAT, 0, normals);
+    if (vertIdxs == NULL)
+        glDrawArrays(GL_TRIANGLES, 0, numVertices);
+    else
+        glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, vertIdxs);
 
-        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);
-    }
+    glDisableClientState(GL_VERTEX_ARRAY);
+    glDisableClientState(GL_NORMAL_ARRAY);
 }
 #endif
 
@@ -297,28 +185,28 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
     
     if (numVertices > 0 && attribute_v_coord != -1) {
        fghGenBuffers(1, &vbo_coords);
-       fghBindBuffer(GL_ARRAY_BUFFER, vbo_coords);
-       fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
-                     vertices, GL_STATIC_DRAW);
+       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(GL_ARRAY_BUFFER, vbo_normals);
-       fghBufferData(GL_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
-                     normals, GL_STATIC_DRAW);
+       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(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
-       fghBufferData(GL_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
-                     vertIdxs, GL_STATIC_DRAW);
+       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(GL_ARRAY_BUFFER, vbo_coords);
+       fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
        fghVertexAttribPointer(
             attribute_v_coord,  // attribute
             3,                  // number of elements per vertex, here (x,y,z)
@@ -331,7 +219,7 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
     
     if (vbo_normals) {
        fghEnableVertexAttribArray(attribute_v_normal);
-       fghBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
+       fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
        fghVertexAttribPointer(
             attribute_v_normal, // attribute
             3,                  // number of elements per vertex, here (x,y,z)
@@ -345,7 +233,7 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
     if (vertIdxs == NULL) {
        glDrawArrays(GL_TRIANGLES, 0, numVertices);
     } else {
-       fghBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
+       fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
        glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, 0);
     }
     
@@ -365,7 +253,6 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
 static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
                                 GLsizei numVertices, GLsizei numVertIdxs)
 {
-#ifndef GL_ES_VERSION_2_0
     GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
     GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
 
@@ -374,10 +261,11 @@ static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *v
        fghDrawGeometrySolid20(vertices, normals, vertIdxs,
                               numVertices, numVertIdxs,
                               attribute_v_coord, attribute_v_normal);
+#ifndef GL_ES_VERSION_2_0
     else
-#endif
        fghDrawGeometrySolid11(vertices, normals, vertIdxs,
                               numVertices, numVertIdxs);
+#endif
 }
 
 /* Shape decomposition to triangles
@@ -908,8 +796,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 */
     }
 
     
@@ -1259,7 +1152,6 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     free(normals);
 }
 
-#endif /* GL_ES_VERSION_2_0 */
 
 
 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
@@ -1285,6 +1177,7 @@ 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
 /*
@@ -1304,8 +1197,13 @@ void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint s
 
     /* 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 */
 
     /* Pre-computed circle */
 
@@ -1374,8 +1272,13 @@ void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint st
 
     /* 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 */
 
     /* Pre-computed circle */
 
@@ -1576,15 +1479,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  ;
@@ -1662,15 +1575,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  ;