X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=9489a3bc005a6a72e934b60aaed51c52d69b848e;hb=93284cce995c8be6950de7858c5200013e63afa0;hp=f9973fbd406ac5f4b3032a503fca82dd7f0c8a58;hpb=87653f06a12512952a629eb3380980cfe445584c;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index f9973fb..9489a3b 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -27,6 +27,8 @@ #include #include "fg_internal.h" +#include "fg_gl2.h" +#include /* * Need more types of polyhedra? See CPolyhedron in MRPT @@ -42,15 +44,18 @@ * decomposition needed. We use the "first" parameter in glDrawArrays to go * from face to face. */ -static void fghDrawGeometryWire(GLdouble *vertices, GLdouble *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; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); - glVertexPointer(3, GL_DOUBLE, 0, vertices); - glNormalPointer(GL_DOUBLE, 0, normals); + glVertexPointer(3, GL_FLOAT, 0, vertices); + glNormalPointer(GL_FLOAT, 0, normals); /* Draw per face (TODO: could use glMultiDrawArrays if available) */ for (i=0; i= 2.0 */ +static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace, + GLint attribute_v_coord, GLint attribute_v_normal) +{ + GLuint vbo_coords = 0, vbo_normals = 0; + 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; iWindow.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_DOUBLE, 0, vertices); - glNormalPointer(GL_DOUBLE, 0, normals); - if (numEdgePerFace==3) + glVertexPointer(3, GL_FLOAT, 0, vertices); + glNormalPointer(GL_FLOAT, 0, normals); + if (vertIdxs == NULL) glDrawArrays(GL_TRIANGLES, 0, numVertices); else - 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 = 0, vbo_normals = 0, ibo_elements = 0; + + 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); + } + + /* Clean existing bindings before clean-up */ + /* Android showed instability otherwise */ + fghBindBuffer(FGH_ARRAY_BUFFER, 0); + fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 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... */ static GLubyte vert4Decomp[6] = {0,1,2, 0,2,3}; /* quad : 4 input vertices, 6 output (2 triangles) */ static GLubyte vert5Decomp[9] = {0,1,2, 0,2,4, 4,2,3}; /* pentagon: 5 input vertices, 9 output (3 triangles) */ -static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLdouble *vertices, GLubyte *vertIndices, GLdouble *normals, GLdouble *vertOut, GLdouble *normOut, GLubyte *vertIdxOut) +static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut, GLubyte *vertIdxOut) { int i,j,numEdgeIdxPerFace; GLubyte *vertSamps = NULL; 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; @@ -110,7 +309,7 @@ static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, for (i=0; i 0 ) { - GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */ + double local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */ unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ; scale /= 2.0 ; for ( i = 0 ; i < TETRAHEDRON_NUM_FACES ; i++ ) @@ -563,6 +762,7 @@ static void fghSierpinskiSpongeGenerate ( int numLevels, GLdouble offset[3], GLd } } +#ifndef GL_ES_VERSION_2_0 /* -- Now the various shapes involving circles -- */ /* * Compute lookup table of cos and sin values forming a circle @@ -574,7 +774,7 @@ static void fghSierpinskiSpongeGenerate ( int numLevels, GLdouble offset[3], GLd * The last entry is exactly the same as the first * The sign of n can be flipped to get the reverse loop */ -static void fghCircleTable(GLdouble **sint, GLdouble **cost, const int n, const GLboolean halfCircle) +static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GLboolean halfCircle) { int i; @@ -582,11 +782,11 @@ static void fghCircleTable(GLdouble **sint, GLdouble **cost, const int n, const const int size = abs(n); /* Determine the angle between samples */ - const GLdouble angle = (halfCircle?1:2)*M_PI/(GLdouble)( ( n == 0 ) ? 1 : n ); + const GLfloat angle = (halfCircle?1:2)*(GLfloat)M_PI/(GLfloat)( ( n == 0 ) ? 1 : n ); /* Allocate memory for n samples, plus duplicate of first entry at the end */ - *sint = malloc(sizeof(GLdouble) * (size+1)); - *cost = malloc(sizeof(GLdouble) * (size+1)); + *sint = malloc(sizeof(GLfloat) * (size+1)); + *cost = malloc(sizeof(GLfloat) * (size+1)); /* Bail out if memory allocation fails, fgError never returns */ if (!(*sint) || !(*cost)) @@ -602,15 +802,20 @@ static void fghCircleTable(GLdouble **sint, GLdouble **cost, const int n, const for (i=1; i 65535) /* TODO: must have a better solution than this low limit, at least for architectures where gluint is available */ + fgWarning("fghGenerateSphere: too many slices or stacks requested, indices will wrap"); + + /* 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 0 ) ? stacks : 1 ); + const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); + + /* 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 */ + + + + /* number of unique vertices */ + if (slices==0 || stacks<1) + { + /* nothing to generate */ + *nVert = 0; + return; + } + *nVert = slices*(stacks+2)+1; /* need an extra stack for closing off bottom with correct normals */ + + if ((*nVert) > 65535) + fgWarning("fghGenerateCone: too many slices or stacks requested, indices will wrap"); + + /* Pre-computed circle */ + fghCircleTable(&sint,&cost,-slices,FALSE); + + /* 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"); + } + + /* bottom */ + (*vertices)[0] = 0.f; + (*vertices)[1] = 0.f; + (*vertices)[2] = z; + (*normals )[0] = 0.f; + (*normals )[1] = 0.f; + (*normals )[2] = -1.f; + idx = 3; + /* other on bottom (get normals right) */ + for (j=0; j 0 ) ? stacks : 1 ); + + /* Pre-computed circle */ + GLfloat *sint,*cost; + + /* number of unique vertices */ + if (slices==0 || stacks<1) + { + /* nothing to generate */ + *nVert = 0; + return; + } + *nVert = slices*(stacks+3)+2; /* need two extra stacks for closing off top and bottom with correct normals */ + + if ((*nVert) > 65535) + fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap"); + + /* Pre-computed circle */ + fghCircleTable(&sint,&cost,-slices,FALSE); + + /* 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 fghGenerateCylinder"); + } + + z=0; + /* top on Z-axis */ + (*vertices)[0] = 0.f; + (*vertices)[1] = 0.f; + (*vertices)[2] = 0.f; + (*normals )[0] = 0.f; + (*normals )[1] = 0.f; + (*normals )[2] = -1.f; + idx = 3; + /* other on top (get normals right) */ + for (j=0; j0)?1:0]; - r0 = 0.0; - r1 = sint2[(stacks>0)?1:0]; + /* Generate vertices and normals */ + fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert); + + if (nVert==0) + /* nothing to draw */ + return; - glBegin(GL_TRIANGLE_FAN); + 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. + */ + + sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort)); + stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort)); + if (!(stackIdx) || !(sliceIdx)) + { + free(stackIdx); + free(sliceIdx); + fgError("Failed to allocate memory in fghGenerateSphere"); + } - glNormal3d(0,0,1); - glVertex3d(0,0,radius); + /* generate for each stack */ + for (i=0,idx=0; i=0; j--) + /* generate for each slice */ + for (i=0,idx=0; i 0 ) ? stacks : 1 ); - const GLdouble rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); - - /* Scaling factors for vertex normals */ + int i,j,idx, nVert; + GLfloat *vertices, *normals; - const GLdouble cosn = ( height / sqrt ( height * height + base * base )); - const GLdouble sinn = ( base / sqrt ( height * height + base * base )); + /* Generate vertices and normals */ + /* Note, (stacks+1)*slices vertices for side of object, 2*slices+2 for top and bottom closures */ + fghGenerateCylinder((GLfloat)radius,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert); - /* Pre-computed circle */ - - GLdouble *sint,*cost; - - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" ); - - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Cover the circular base with a triangle fan... */ + if (nVert==0) + /* nothing to draw */ + return; - z0 = 0.0; - z1 = zStep; - - r0 = base; - r1 = r0 - rStep; + 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. + */ + + stackIdx = malloc(slices*(stacks+1)*sizeof(GLushort)); + sliceIdx = malloc(slices*2 *sizeof(GLushort)); + if (!(stackIdx) || !(sliceIdx)) + { + free(stackIdx); + free(sliceIdx); + fgError("Failed to allocate memory in fghGenerateCylinder"); + } - glBegin(GL_TRIANGLE_FAN); + /* generate for each stack */ + for (i=0,idx=0; i 0 ) ? stacks : 1 ); - const GLdouble rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + fghSphere( radius, slices, stacks, FALSE ); +} - /* Scaling factors for vertex normals */ +/* + * Draws a wire sphere + */ +void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks) +{ + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" ); - const GLdouble cosn = ( height / sqrt ( height * height + base * base )); - const GLdouble sinn = ( base / sqrt ( height * height + base * base )); + fghSphere( radius, slices, stacks, TRUE ); + +} +#endif /* GL_ES_VERSION_2_0 */ - /* Pre-computed circle */ +#ifndef EGL_VERSION_1_0 +/* + * Draws a solid cone + */ +void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks ) +{ + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" ); - GLdouble *sint,*cost; + fghCone( base, height, slices, stacks, FALSE ); +} +/* + * Draws a wire cone + */ +void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint stacks) +{ FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Draw the stacks... */ - - for (i=0; i 0 ) ? stacks : 1 ); - - /* Pre-computed circle */ - - GLdouble *sint,*cost; - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" ); - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Cover the base and top */ - - glBegin(GL_TRIANGLE_FAN); - glNormal3d(0.0, 0.0, -1.0 ); - glVertex3d(0.0, 0.0, 0.0 ); - for (j=0; j<=slices; j++) - glVertex3d(cost[j]*radius, sint[j]*radius, 0.0); - glEnd(); - - glBegin(GL_TRIANGLE_FAN); - glNormal3d(0.0, 0.0, 1.0 ); - glVertex3d(0.0, 0.0, height); - for (j=slices; j>=0; j--) - glVertex3d(cost[j]*radius, sint[j]*radius, height); - glEnd(); - - /* Do the stacks */ - - z0 = 0.0; - z1 = zStep; - - for (i=1; i<=stacks; i++) - { - if (i==stacks) - z1 = height; - - glBegin(GL_QUAD_STRIP); - for (j=0; j<=slices; j++ ) - { - glNormal3d(cost[j], sint[j], 0.0 ); - glVertex3d(cost[j]*radius, sint[j]*radius, z0 ); - glVertex3d(cost[j]*radius, sint[j]*radius, z1 ); - } - glEnd(); - - z0 = z1; z1 += zStep; - } - - /* Release sin and cos tables */ - - free(sint); - free(cost); + fghCylinder( radius, height, slices, stacks, FALSE ); } /* * Draws a wire cylinder */ -void FGAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks) +void FGAPIENTRY glutWireCylinder(double radius, double height, GLint slices, GLint stacks) { - int i,j; - - /* Step in z and radius as stacks are drawn. */ - - GLdouble z = 0.0; - const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); - - /* Pre-computed circle */ - - GLdouble *sint,*cost; - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" ); - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Draw the stacks... */ - - for (i=0; i<=stacks; i++) - { - if (i==stacks) - z = height; - - glBegin(GL_LINE_LOOP); - - for( j=0; j