X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=2c6f344c2fdb245e3d5cf5d403f0ec606e2245d7;hb=da66462c2b7ce3185bc714cbabade787291c5c6f;hp=5f4b0e1bb6679c6e81c4bdc5f1cd12ec15b727e9;hpb=fa40b6be28fa060c4ddbe72850d49c1e9d06a469;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 5f4b0e1..2c6f344 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -29,140 +29,449 @@ #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)*TETR_VERT_ELEM_PER_TETR; + unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ; scale /= 2.0 ; - for ( i = 0 ; i < TETR_NUM_FACES ; i++ ) + for ( i = 0 ; i < TETRAHEDRON_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]; + 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 ); } } @@ -271,7 +565,7 @@ static void fghSierpinskiSpongeGenerate ( int numLevels, GLdouble offset[3], GLd /* -- 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 * * Notes: * It is the responsibility of the caller to free these tables @@ -323,54 +617,106 @@ static void fghCircleTable(double **sint,double **cost,const int n) } -/* -- 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 fghCube( GLdouble dSize, GLboolean useWireMode ) { + GLdouble *vertices; + if (!cubeCached) { fghCubeGenerate(); - cubeCached = TRUE; + cubeCached = GL_TRUE; } if (dSize!=1.) { - /* Need to build new */ - fghDrawGeometry(GL_QUADS,cube_verts,cube_norms,CUBE_VERT_PER_TETR,useWireMode); + /* Need to build new vertex list containing vertices for cube of different size */ + int i; + + vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLdouble)); + + /* Bail out if memory allocation fails, fgError never returns */ + if (!vertices) + { + free(vertices); + fgError("Failed to allocate memory in fghCube"); + } + + for (i=0; i