X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=572b7e318612344e1ea9acadd0cbcbb957541ac2;hb=134051040e95088462463a6bd36c5081d354d0c3;hp=3a19a3b021b223c7297766fd4c7eecd97f54fc8c;hpb=9634525a2e112501938d7cc5c48ce75f8b975a38;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 3a19a3b..572b7e3 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -29,73 +29,453 @@ #include "fg_internal.h" /* - * TODO BEFORE THE STABLE RELEASE: - * - * See fghTetrahedron - * - * Following functions have been contributed by Andreas Umbach. - * - * glutWireCube() -- looks OK - * glutSolidCube() -- OK - * - * Those functions have been implemented by John Fay. - * - * glutWireTorus() -- looks OK - * glutSolidTorus() -- looks OK - * glutWireDodecahedron() -- looks OK - * glutSolidDodecahedron() -- looks OK - * glutWireOctahedron() -- looks OK - * glutSolidOctahedron() -- looks OK - * glutWireTetrahedron() -- looks OK - * glutSolidTetrahedron() -- looks OK - * glutWireIcosahedron() -- looks OK - * glutSolidIcosahedron() -- looks OK - * - * The Following functions have been updated by Nigel Stewart, based - * on FreeGLUT 2.0.0 implementations: - * - * glutWireSphere() -- looks OK - * glutSolidSphere() -- looks OK - * glutWireCone() -- looks OK - * glutSolidCone() -- looks OK + * Need more types of polyhedra? See CPolyhedron in MRPT */ -/* - * General function for drawing geometry. As for all geometry we have no - * redundancy (or hardly any in the case of cones and cylinders) in terms - * of the vertex/normal combinations, we just use glDrawArrays. - * useWireMode controls the drawing of solids (false) or wire frame - * versions (TRUE) of the geometry you pass +#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 + * decomposed into triangles (some vertices are repeated in that case). + * WireFrame drawing will have to be done per face, using GL_LINE_LOOP and + * issuing one draw call per face. Always use glDrawArrays as no triangle + * decomposition needed. We use the "first" parameter in glDrawArrays to go + * from face to face. */ -static void fghDrawGeometry(GLenum vertexMode, double* vertices, double* normals, GLsizei numVertices, GLboolean useWireMode) +static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace) { - if (useWireMode) - { - glPushAttrib(GL_POLYGON_BIT); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } + int i; + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + glVertexPointer(3, GL_FLOAT, 0, vertices); + glNormalPointer(GL_FLOAT, 0, normals); + /* Draw per face (TODO: could use glMultiDrawArrays if available) */ + for (i=0; i 0 ) { - for (q=0; q - */ -void FGAPIENTRY glutWireCube( GLdouble dSize ) -{ - double size = dSize * 0.5; - - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" ); + /* Bail out if memory allocation fails, fgError never returns */ + if (!vertices) + { + free(vertices); + fgError("Failed to allocate memory in fghCube"); + } -# define V(a,b,c) glVertex3d( a size, b size, c size ); -# define N(a,b,c) glNormal3d( a, b, c ); + for (i=0; i - */ -void FGAPIENTRY glutSolidCube( GLdouble dSize ) +DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON); +DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON); +DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON); +DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON); +DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON); + +static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale, GLboolean useWireMode ) { - double size = dSize * 0.5; + GLfloat *vertices; + GLfloat * normals; + GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */ + GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ; + GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES; - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" ); + if (numTetr) + { + /* Allocate memory */ + vertices = malloc(numVert*3 * sizeof(GLfloat)); + normals = malloc(numVert*3 * sizeof(GLfloat)); + /* Bail out if memory allocation fails, fgError never returns */ + if (!vertices || !normals) + { + free(vertices); + free(normals); + fgError("Failed to allocate memory in fghSierpinskiSponge"); + } -# define V(a,b,c) glVertex3d( a size, b size, c size ); -# define N(a,b,c) glNormal3d( a, b, c ); + /* Generate elements */ + fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals ); - /* PWO: Again, I dared to convert the code to use macros... */ - glBegin( GL_QUADS ); - N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); - N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); - N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); - N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); - N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); - N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); - glEnd(); + /* Draw and cleanup */ + if (useWireMode) + fghDrawGeometryWire (vertices,normals, numFace,TETRAHEDRON_NUM_EDGE_PER_FACE); + else + fghDrawGeometrySolid(vertices,normals,NULL,numVert, TETRAHEDRON_NUM_EDGE_PER_FACE); -# undef V -# undef N + free(vertices); + free(normals ); + } } +#endif /* GL_ES_VERSION_2_0 */ +/* -- INTERFACE FUNCTIONS ---------------------------------------------- */ + + +#ifndef EGL_VERSION_1_0 /* * Draws a solid sphere */ -void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) +void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks) { int i,j; /* Adjust z and radius as stacks are drawn. */ - - double z0,z1; - double r0,r1; + GLfloat radf = (GLfloat)radius; + GLfloat z0,z1; + GLfloat r0,r1; /* Pre-computed circle */ - double *sint1,*cost1; - double *sint2,*cost2; + GLfloat *sint1,*cost1; + GLfloat *sint2,*cost2; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" ); - fghCircleTable(&sint1,&cost1,-slices); - fghCircleTable(&sint2,&cost2,stacks*2); + fghCircleTable(&sint1,&cost1,-slices,FALSE); + fghCircleTable(&sint2,&cost2, stacks,TRUE); /* The top stack is covered with a triangle fan */ - z0 = 1.0; + z0 = 1; z1 = cost2[(stacks>0)?1:0]; - r0 = 0.0; + r0 = 0; r1 = sint2[(stacks>0)?1:0]; glBegin(GL_TRIANGLE_FAN); - glNormal3d(0,0,1); - glVertex3d(0,0,radius); + glNormal3f(0,0,1); + glVertex3f(0,0,radf); for (j=slices; j>=0; j--) { - glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); - glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); + glNormal3f(cost1[j]*r1, sint1[j]*r1, z1 ); + glVertex3f(cost1[j]*r1*radf, sint1[j]*r1*radf, z1*radf); } glEnd(); @@ -331,10 +787,10 @@ void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) for(j=0; j<=slices; j++) { - glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); - glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); - glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); - glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); + glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); + glVertex3d(cost1[j]*r1*radf, sint1[j]*r1*radf, z1*radf); + glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); + glVertex3d(cost1[j]*r0*radf, sint1[j]*r0*radf, z0*radf); } glEnd(); @@ -352,8 +808,8 @@ void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) for (j=0; j<=slices; j++) { - glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); - glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); + glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); + glVertex3d(cost1[j]*r0*radf, sint1[j]*r0*radf, z0*radf); } glEnd(); @@ -369,24 +825,24 @@ void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) /* * Draws a wire sphere */ -void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) +void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks) { int i,j; /* Adjust z and radius as stacks and slices are drawn. */ - - double r; - double x,y,z; + GLfloat radf = (GLfloat)radius; + GLfloat r; + GLfloat x,y,z; /* Pre-computed circle */ - double *sint1,*cost1; - double *sint2,*cost2; + GLfloat *sint1,*cost1; + GLfloat *sint2,*cost2; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" ); - fghCircleTable(&sint1,&cost1,-slices ); - fghCircleTable(&sint2,&cost2, stacks*2); + fghCircleTable(&sint1,&cost1,-slices,FALSE); + fghCircleTable(&sint2,&cost2, stacks,TRUE); /* Draw a line loop for each stack */ @@ -402,8 +858,8 @@ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) x = cost1[j]; y = sint1[j]; - glNormal3d(x,y,z); - glVertex3d(x*r*radius,y*r*radius,z*radius); + glNormal3f(x,y,z); + glVertex3f(x*r*radf,y*r*radf,z*radf); } glEnd(); @@ -421,8 +877,8 @@ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) y = sint1[i]*sint2[j]; z = cost2[j]; - glNormal3d(x,y,z); - glVertex3d(x*radius,y*radius,z*radius); + glNormal3f(x,y,z); + glVertex3f(x*radf,y*radf,z*radf); } glEnd(); @@ -439,46 +895,46 @@ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) /* * Draws a solid cone */ -void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks ) +void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks ) { int i,j; /* Step in z and radius as stacks are drawn. */ - double z0,z1; - double r0,r1; + GLfloat z0,z1; + GLfloat r0,r1; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); - const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 ); + const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); /* Scaling factors for vertex normals */ - const double cosn = ( height / sqrt ( height * height + base * base )); - const double sinn = ( base / sqrt ( height * height + base * base )); + const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base )); + const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base )); /* Pre-computed circle */ - double *sint,*cost; + GLfloat *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Cover the circular base with a triangle fan... */ - z0 = 0.0; + z0 = 0; z1 = zStep; - r0 = base; + r0 = (GLfloat)base; r1 = r0 - rStep; glBegin(GL_TRIANGLE_FAN); - glNormal3d(0.0,0.0,-1.0); - glVertex3d(0.0,0.0, z0 ); + glNormal3f(0,0,-1); + glVertex3f(0,0, z0 ); for (j=0; j<=slices; j++) - glVertex3d(cost[j]*r0, sint[j]*r0, z0); + glVertex3f(cost[j]*r0, sint[j]*r0, z0); glEnd(); @@ -490,9 +946,9 @@ void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLi for(j=0; j<=slices; j++) { - glNormal3d(cost[j]*cosn, sint[j]*cosn, sinn); - glVertex3d(cost[j]*r0, sint[j]*r0, z0 ); - glVertex3d(cost[j]*r1, sint[j]*r1, z1 ); + glNormal3f(cost[j]*cosn, sint[j]*cosn, sinn); + glVertex3f(cost[j]*r0, sint[j]*r0, z0 ); + glVertex3f(cost[j]*r1, sint[j]*r1, z1 ); } z0 = z1; z1 += zStep; @@ -505,14 +961,14 @@ void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLi glBegin(GL_TRIANGLES); - glNormal3d(cost[0]*sinn, sint[0]*sinn, cosn); + glNormal3f(cost[0]*sinn, sint[0]*sinn, cosn); for (j=0; j 0 ) ? stacks : 1 ); - const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 ); + const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); /* Scaling factors for vertex normals */ - const double cosn = ( height / sqrt ( height * height + base * base )); - const double sinn = ( base / sqrt ( height * height + base * base )); + const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base )); + const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base )); /* Pre-computed circle */ - double *sint,*cost; + GLfloat *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Draw the stacks... */ @@ -559,8 +1015,8 @@ void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLin for( j=0; j 0 ) ? stacks : 1 ); + GLfloat radf = (GLfloat)radius; + GLfloat z0,z1; + const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ - double *sint,*cost; + GLfloat *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" ); - fghCircleTable(&sint,&cost,-slices); + 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 ); + glNormal3f(0, 0, -1 ); + glVertex3f(0, 0, 0 ); for (j=0; j<=slices; j++) - glVertex3d(cost[j]*radius, sint[j]*radius, 0.0); + glVertex3f(cost[j]*radf, sint[j]*radf, 0); glEnd(); glBegin(GL_TRIANGLE_FAN); - glNormal3d(0.0, 0.0, 1.0 ); - glVertex3d(0.0, 0.0, height); + glNormal3f(0, 0, 1 ); + glVertex3f(0, 0, (GLfloat)height); for (j=slices; j>=0; j--) - glVertex3d(cost[j]*radius, sint[j]*radius, height); + glVertex3f(cost[j]*radf, sint[j]*radf, (GLfloat)height); glEnd(); /* Do the stacks */ - z0 = 0.0; + z0 = 0; z1 = zStep; for (i=1; i<=stacks; i++) { if (i==stacks) - z1 = height; + z1 = (GLfloat)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 ); + glNormal3f(cost[j], sint[j], 0 ); + glVertex3f(cost[j]*radf, sint[j]*radf, z0 ); + glVertex3f(cost[j]*radf, sint[j]*radf, z1 ); } glEnd(); @@ -658,36 +1114,36 @@ void FGAPIENTRY glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices /* * 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. */ - - double z = 0.0; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); + GLfloat radf = (GLfloat)radius; + GLfloat z = 0; + const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ - double *sint,*cost; + GLfloat *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Draw the stacks... */ for (i=0; i<=stacks; i++) { if (i==stacks) - z = height; + z = (GLfloat)height; glBegin(GL_LINE_LOOP); for( j=0; j 0 ) - { - GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */ - num_levels -- ; - scale /= 2.0 ; - for ( i = 0 ; i < TETR_NUM_FACES ; i++ ) - { - local_offset[0] = offset[0] + scale * tet_r[i][0] ; - local_offset[1] = offset[1] + scale * tet_r[i][1] ; - local_offset[2] = offset[2] + scale * tet_r[i][2] ; - glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ; - } - } -} - -void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale ) +void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, double offset[3], double scale ) { - int i, j ; - - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" ); - - if ( num_levels == 0 ) - { - glBegin ( GL_TRIANGLES ) ; - - for ( i = 0 ; i < TETR_NUM_FACES ; i++ ) - { - glNormal3d ( -tet_r[i][0], -tet_r[i][1], -tet_r[i][2] ) ; - for ( j = 0; j < 3; j++ ) - { - double x = offset[0] + scale * tet_r[tet_i[i][j]][0] ; - double y = offset[1] + scale * tet_r[tet_i[i][j]][1] ; - double z = offset[2] + scale * tet_r[tet_i[i][j]][2] ; - glVertex3d ( x, y, z ) ; - } - } - - glEnd () ; - } - else if ( num_levels > 0 ) - { - GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */ - num_levels -- ; - scale /= 2.0 ; - for ( i = 0 ; i < TETR_NUM_FACES ; i++ ) - { - local_offset[0] = offset[0] + scale * tet_r[i][0] ; - local_offset[1] = offset[1] + scale * tet_r[i][1] ; - local_offset[2] = offset[2] + scale * tet_r[i][2] ; - glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ; - } - } + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" ); + fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, TRUE ); } - - - -/* -- INTERFACE FUNCTIONS -------------------------------------------------- */ - - -void FGAPIENTRY glutWireTetrahedron( void ) +void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, double offset[3], double scale ) { - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTetrahedron" ); - - fghTetrahedron( TRUE ); + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" ); + fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, FALSE ); } -void FGAPIENTRY glutSolidTetrahedron( void ) -{ - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTetrahedron" ); - fghTetrahedron( FALSE ); -} +DECLARE_SHAPE_INTERFACE(Tetrahedron); /*** END OF FILE ***/