Really fix tabs->space
[freeglut] / src / fg_geometry.c
index 312acfc..d50c93c 100644 (file)
@@ -68,7 +68,7 @@ static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals, GLsizei n
 
 /* 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)
+                                  GLint attribute_v_coord, GLint attribute_v_normal)
 {
     GLuint vbo_coords, vbo_normals;
     GLuint numVertices = numFaces * numEdgePerFace;
@@ -76,17 +76,17 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
     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);
+        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);
+        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) {
@@ -117,18 +117,18 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n
 
     /* Draw per face (TODO: could use glMultiDrawArrays if available) */
     for (i=0; i<numFaces; i++)
-       glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
+        glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
     
     
     if (vbo_coords != 0)
-       fghDisableVertexAttribArray(attribute_v_coord);
+        fghDisableVertexAttribArray(attribute_v_coord);
     if (vbo_normals != 0)
-       fghDisableVertexAttribArray(attribute_v_normal);
+        fghDisableVertexAttribArray(attribute_v_normal);
     
     if (vbo_coords != 0)
-       fghDeleteBuffers(1, &vbo_coords);
+        fghDeleteBuffers(1, &vbo_coords);
     if (vbo_normals != 0)
-       fghDeleteBuffers(1, &vbo_normals);
+        fghDeleteBuffers(1, &vbo_normals);
 }
 
 static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace)
@@ -138,11 +138,11 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
 
     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);
+        fghDrawGeometryWire20(vertices, normals, numFaces, numEdgePerFace,
+                          attribute_v_coord, attribute_v_normal);
 #ifndef GL_ES_VERSION_2_0
     else
-       fghDrawGeometryWire11(vertices, normals, numFaces, numEdgePerFace);
+        fghDrawGeometryWire11(vertices, normals, numFaces, numEdgePerFace);
 #endif
 }
 
@@ -159,7 +159,7 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num
 /* 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)
+                   GLsizei numVertices, GLsizei numVertIdxs)
 {
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_NORMAL_ARRAY);
@@ -178,36 +178,36 @@ static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLubyte
 
 /* 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)
+                   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);
-       }
+        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);
+        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);
+        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(
+        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
@@ -218,9 +218,9 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
     };
     
     if (vbo_normals) {
-       fghEnableVertexAttribArray(attribute_v_normal);
-       fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
-       fghVertexAttribPointer(
+        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
@@ -231,40 +231,40 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLubyte
     };
     
     if (vertIdxs == NULL) {
-       glDrawArrays(GL_TRIANGLES, 0, numVertices);
+        glDrawArrays(GL_TRIANGLES, 0, numVertices);
     } else {
-       fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
-       glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, 0);
+        fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
+        glDrawElements(GL_TRIANGLES, numVertIdxs, GL_UNSIGNED_BYTE, 0);
     }
     
     if (vbo_coords != 0)
-       fghDisableVertexAttribArray(attribute_v_coord);
+        fghDisableVertexAttribArray(attribute_v_coord);
     if (vbo_normals != 0)
-       fghDisableVertexAttribArray(attribute_v_normal);
+        fghDisableVertexAttribArray(attribute_v_normal);
     
     if (vbo_coords != 0)
-       fghDeleteBuffers(1, &vbo_coords);
+        fghDeleteBuffers(1, &vbo_coords);
     if (vbo_normals != 0)
-       fghDeleteBuffers(1, &vbo_normals);
+        fghDeleteBuffers(1, &vbo_normals);
     if (ibo_elements != 0)
-       fghDeleteBuffers(1, &ibo_elements);
+        fghDeleteBuffers(1, &ibo_elements);
 }
 
 static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs,
-                                GLsizei numVertices, GLsizei numVertIdxs)
+                 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);
+        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);
+        fghDrawGeometrySolid11(vertices, normals, vertIdxs,
+                               numVertices, numVertIdxs);
 #endif
 }
 
@@ -796,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 */
     }
 
     
@@ -945,11 +950,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, CUBE_VERT_PER_OBJ_TRI);
+                             CUBE_VERT_PER_OBJ, CUBE_VERT_PER_OBJ_TRI);
 
     if (dSize!=1.f)
         /* cleanup allocated memory */
@@ -1005,7 +1010,7 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     GLfloat *vertices, *normals;
 
     if (slices * stacks > 65535)
-       fgWarning("fghSphere: too many slices or stacks requested, indices will wrap");
+        fgWarning("fghSphere: too many slices or stacks requested, indices will wrap");
 
     /* Generate vertices and normals */
     fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
@@ -1069,54 +1074,55 @@ static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useW
     }
     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.
+         * 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(GLushort));
-        bottomIdx = malloc((slices+2)*sizeof(GLushort));
-        stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLushort));
-        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");
         }
 
-        /* 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 */
+        /* 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)
         {
-            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 */
+            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 */
@@ -1125,20 +1131,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_SHORT,topIdx);
         /*draw stacks*/
-        for (i=0; i<stacks-2; i++)
+        for (i=0; i<stacks; 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);
 
         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_NORMAL_ARRAY);
 
         /* cleanup allocated memory */
-        free(topIdx);
-        free(bottomIdx);
         free(stripIdx);
     }
     
@@ -1192,8 +1192,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 */
 
@@ -1262,8 +1267,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 */
 
@@ -1464,15 +1474,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  ;
@@ -1550,15 +1570,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  ;