X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=c74cac4837f66bad264d69ce7ea1ba76935c8bac;hb=ee7bbd68f288bee1bf90734568422cb15cb7723e;hp=3a19a3b021b223c7297766fd4c7eecd97f54fc8c;hpb=9634525a2e112501938d7cc5c48ce75f8b975a38;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 3a19a3b..c74cac4 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -29,73 +29,451 @@ #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 +/* 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(GLdouble *vertices, GLdouble *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_DOUBLE, 0, vertices); + glNormalPointer(GL_DOUBLE, 0, normals); + /* Draw per face (TODO: could use glMultiDrawArrays if available) */ + for (i=0; i 0 ) + { + GLdouble 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++ ) + { + int idx = i*3; + local_offset[0] = offset[0] + scale * tetrahedron_v[idx ]; + local_offset[1] = offset[1] + scale * tetrahedron_v[idx+1]; + local_offset[2] = offset[2] + scale * tetrahedron_v[idx+2]; + fghSierpinskiSpongeGenerate ( numLevels, local_offset, scale, vertices+i*stride, normals+i*stride ); } } } /* -- Now the various shapes involving circles -- */ /* - * Compute lookup table of cos and sin values forming a cirle + * Compute lookup table of cos and sin values forming a circle + * (or half circle if halfCircle==TRUE) * * Notes: * It is the responsibility of the caller to free these tables @@ -170,25 +574,21 @@ static void fghTetrahedronCache() * 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(double **sint,double **cost,const int n) +static void fghCircleTable(GLdouble **sint, GLdouble **cost, const int n, const GLboolean halfCircle) { int i; - + /* Table size, the sign of n flips the circle direction */ - const int size = abs(n); /* Determine the angle between samples */ - - const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n ); + const GLdouble angle = (halfCircle?1:2)*M_PI/(GLdouble)( ( n == 0 ) ? 1 : n ); /* Allocate memory for n samples, plus duplicate of first entry at the end */ - - *sint = (double *) calloc(sizeof(double), size+1); - *cost = (double *) calloc(sizeof(double), size+1); + *sint = malloc(sizeof(GLdouble) * (size+1)); + *cost = malloc(sizeof(GLdouble) * (size+1)); /* Bail out if memory allocation fails, fgError never returns */ - if (!(*sint) || !(*cost)) { free(*sint); @@ -197,7 +597,6 @@ static void fghCircleTable(double **sint,double **cost,const int n) } /* Compute cos and sin around the circle */ - (*sint)[0] = 0.0; (*cost)[0] = 1.0; @@ -207,77 +606,130 @@ static void fghCircleTable(double **sint,double **cost,const int n) (*cost)[i] = cos(angle*i); } - /* Last sample is duplicate of the first */ - - (*sint)[size] = (*sint)[0]; - (*cost)[size] = (*cost)[0]; + + if (halfCircle) + { + (*sint)[size] = 0.0; /* sin PI */ + (*cost)[size] = -1.0; /* cos PI */ + } + else + { + /* Last sample is duplicate of the first (sin or cos of 2 PI) */ + (*sint)[size] = (*sint)[0]; + (*cost)[size] = (*cost)[0]; + } } -/* -- INTERNAL DRAWING functions to avoid code duplication ------------- */ +/* -- INTERNAL DRAWING functions --------------------------------------- */ +#define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\ + static void fgh##nameICaps( GLboolean useWireMode )\ + {\ + if (!name##Cached)\ + {\ + fgh##nameICaps##Generate();\ + name##Cached = GL_TRUE;\ + }\ + \ + if (useWireMode)\ + {\ + fghDrawGeometryWire (name##_verts,name##_norms,\ + nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE);\ + }\ + else\ + {\ + fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\ + nameCaps##_VERT_PER_OBJ_TRI, nameCaps##_NUM_EDGE_PER_FACE);\ + }\ + } +#define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL) +#define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs) -static void fghTetrahedron( GLboolean useWireMode ) +static void fghCube( GLdouble dSize, GLboolean useWireMode ) { - if (!tetrCached) - fghTetrahedronCache(); - - fghDrawGeometry(GL_TRIANGLES,tetr_verts,tetr_norms,TETR_NUM_FACES*TETR_NUM_VERT_PER_FACE,useWireMode); -} + GLdouble *vertices; + if (!cubeCached) + { + fghCubeGenerate(); + cubeCached = GL_TRUE; + } -/* -- INTERFACE FUNCTIONS ---------------------------------------------- */ + if (dSize!=1.) + { + /* Need to build new vertex list containing vertices for cube of different size */ + int i; -/* - * Draws a wireframed cube. Code contributed by Andreas Umbach - */ -void FGAPIENTRY glutWireCube( GLdouble dSize ) -{ - double size = dSize * 0.5; + vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLdouble)); - 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, GLdouble offset[3], GLdouble scale, GLboolean useWireMode ) { - double size = dSize * 0.5; + GLdouble *vertices; + GLdouble * 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(GLdouble)); + normals = malloc(numVert*3 * sizeof(GLdouble)); + /* 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 ); + } } +/* -- INTERFACE FUNCTIONS ---------------------------------------------- */ + + /* * Draws a solid sphere */ @@ -287,18 +739,18 @@ void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) /* Adjust z and radius as stacks are drawn. */ - double z0,z1; - double r0,r1; + GLdouble z0,z1; + GLdouble r0,r1; /* Pre-computed circle */ - double *sint1,*cost1; - double *sint2,*cost2; + GLdouble *sint1,*cost1; + GLdouble *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 */ @@ -375,18 +827,18 @@ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) /* Adjust z and radius as stacks and slices are drawn. */ - double r; - double x,y,z; + GLdouble r; + GLdouble x,y,z; /* Pre-computed circle */ - double *sint1,*cost1; - double *sint2,*cost2; + GLdouble *sint1,*cost1; + GLdouble *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 */ @@ -420,6 +872,7 @@ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) x = cost1[i]*sint2[j]; y = sint1[i]*sint2[j]; z = cost2[j]; + printf("j(%i):%1.3f\n",j,z); glNormal3d(x,y,z); glVertex3d(x*radius,y*radius,z*radius); @@ -445,24 +898,24 @@ void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLi /* Step in z and radius as stacks are drawn. */ - double z0,z1; - double r0,r1; + GLdouble z0,z1; + GLdouble r0,r1; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); - const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); + const GLdouble rStep = 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 GLdouble cosn = ( height / sqrt ( height * height + base * base )); + const GLdouble sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ - double *sint,*cost; + GLdouble *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... */ @@ -532,24 +985,24 @@ void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLin /* Step in z and radius as stacks are drawn. */ - double z = 0.0; - double r = base; + GLdouble z = 0.0; + GLdouble r = base; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); - const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); + const GLdouble rStep = 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 GLdouble cosn = ( height / sqrt ( height * height + base * base )); + const GLdouble sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ - double *sint,*cost; + GLdouble *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Draw the stacks... */ @@ -600,16 +1053,16 @@ void FGAPIENTRY glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices /* Step in z and radius as stacks are drawn. */ - double z0,z1; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); + GLdouble z0,z1; + const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ - double *sint,*cost; + GLdouble *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Cover the base and top */ @@ -664,16 +1117,16 @@ void FGAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, /* Step in z and radius as stacks are drawn. */ - double z = 0.0; - const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); + GLdouble z = 0.0; + const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ - double *sint,*cost; + GLdouble *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" ); - fghCircleTable(&sint,&cost,-slices); + fghCircleTable(&sint,&cost,-slices,FALSE); /* Draw the stacks... */ @@ -719,10 +1172,10 @@ void FGAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, */ void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings ) { - double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi; - double *vertex, *normal; + GLdouble iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi; + GLdouble *vertex, *normal; int i, j; - double spsi, cpsi, sphi, cphi ; + GLdouble spsi, cpsi, sphi, cphi ; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTorus" ); @@ -730,13 +1183,13 @@ void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLi if ( nRings < 1 ) nRings = 1; /* Allocate the vertices array */ - vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings ); - normal = (double *)calloc( sizeof(double), 3 * nSides * nRings ); + vertex = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings ); + normal = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings ); glPushMatrix(); - dpsi = 2.0 * M_PI / (double)nRings ; - dphi = -2.0 * M_PI / (double)nSides ; + dpsi = 2.0 * M_PI / (GLdouble)nRings ; + dphi = -2.0 * M_PI / (GLdouble)nSides ; psi = 0.0; 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 ) ; - } - } + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" ); + fghSierpinskiSponge ( num_levels, offset, scale, TRUE ); } - void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble 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 ) ; - } - } -} - - - -/* -- INTERFACE FUNCTIONS -------------------------------------------------- */ - - -void FGAPIENTRY glutWireTetrahedron( void ) -{ - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTetrahedron" ); - - fghTetrahedron( TRUE ); + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" ); + fghSierpinskiSponge ( num_levels, offset, scale, FALSE ); } -void FGAPIENTRY glutSolidTetrahedron( void ) -{ - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTetrahedron" ); - fghTetrahedron( FALSE ); -} +DECLARE_SHAPE_INTERFACE(Tetrahedron); /*** END OF FILE ***/