X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=a40ec7beec78008549d019467d14f3cf621814d5;hb=94b0f8e8b9577de862e420b69548351a223693a7;hp=2e0a3b3702a4957f7de71f6d064f18b90437a80d;hpb=e299df837915eaaf97e8e4ccbcb75bf1876661da;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 2e0a3b3..a40ec7b 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -60,25 +60,38 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); } -static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs, GLsizei numVertices, GLsizei numEdgePerFace) + +/* 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. + */ +static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLubyte *vertIdxs, + GLsizei numVertices, GLsizei numVertIdxs) { glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glNormalPointer(GL_FLOAT, 0, normals); - if (numEdgePerFace==3) + 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); } + + /* 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... */ @@ -92,7 +105,7 @@ static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, 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; @@ -145,7 +158,7 @@ static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLfloat *verti /* Cache of input to glDrawArrays or glDrawElements * In general, we build arrays with all vertices or normals. * We cant compress this and use glDrawElements as all combinations of - * vertex and normals are unique. + * vertices and normals are unique. */ #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\ static GLboolean name##Cached = FALSE;\ @@ -199,7 +212,7 @@ static GLfloat cube_n[CUBE_NUM_FACES*3] = 0.0f, 0.0f,-1.0f }; -/* Vertex indices */ +/* Vertex indices, as quads, before triangulation */ static GLubyte cube_vi[CUBE_VERT_PER_OBJ] = { 0,1,2,3, @@ -621,6 +634,81 @@ static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GL } } +static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat **vertices, GLfloat **normals, int* nVert) +{ + int i,j; + int idx = 0; /* idx into vertex/normal buffer */ + GLfloat x,y,z; + + /* Pre-computed circle */ + GLfloat *sint1,*cost1; + GLfloat *sint2,*cost2; + + /* number of unique vertices */ + if (slices==0 || stacks<2) + { + /* nothing to generate */ + *nVert = 0; + return; + } + *nVert = slices*(stacks-1)+2; + + /* 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; i0)?1:0]; - r0 = 0; - r1 = sint2[(stacks>0)?1:0]; + /* draw */ + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); - glBegin(GL_TRIANGLE_FAN); + glVertexPointer(3, GL_FLOAT, 0, vertices); + glNormalPointer(GL_FLOAT, 0, normals); + /*draw slices*/ + for (i=0; i=0; j--) + /* cleanup allocated memory */ + free(sliceIdx); + free(stackIdx); + } + else + { + GLuint *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. + */ + + /* Allocate buffers for indices, bail out if memory allocation fails */ + topIdx = malloc((slices+2)*sizeof(GLuint)); + bottomIdx = malloc((slices+2)*sizeof(GLuint)); + stripIdx = malloc((slices+1)*2*(stacks-2)*sizeof(GLuint)); + if (!(topIdx) || !(bottomIdx) || !(stripIdx)) { - glNormal3f(cost1[j]*r1, sint1[j]*r1, z1 ); - glVertex3f(cost1[j]*r1*radf, sint1[j]*r1*radf, z1*radf); + free(topIdx); + free(bottomIdx); + free(stripIdx); + fgError("Failed to allocate memory in fghGenerateSphere"); } - glEnd(); - - /* Cover each stack with a quad strip, except the top and bottom stacks */ - - for( i=1; i0; j--, idx++) + topIdx[idx] = j; - glBegin(GL_QUAD_STRIP); + bottomIdx[0]=nVert-1; /* zero based index, last element in array... */ + for (j=0, idx=1; j