X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffg_geometry.c;h=a40ec7beec78008549d019467d14f3cf621814d5;hb=94b0f8e8b9577de862e420b69548351a223693a7;hp=ee249846b932774208c33004884a7ccff169fe51;hpb=7425c4c305215701db45769fe9d43f76088fe055;p=freeglut diff --git a/src/fg_geometry.c b/src/fg_geometry.c index ee24984..a40ec7b 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -33,169 +33,102 @@ */ -/* 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(GLdouble *vertices, GLdouble *normals, GLboolean *edgeFlags, GLsizei numVertices, GLsizei numFaces, GLsizei numEdgePerFace, GLboolean useWireMode) +static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numFaces, GLsizei numEdgePerFace) { -# ifdef FREEGLUT_GLES - /* Solid drawing is the same for OpenGL 1.x and OpenGL ES 1.x, just - * no edge flags for ES. - * WireFrame drawing will have to be done per face though, using - * GL_LINE_LOOP and issuing one draw call per face. For triangles, - * we use glDrawArrays directly on the vertex data for each face, - * while for shapes that are composed of quads or pentagons, we use - * glDrawElements with index vector {0,1,2,5} or {0,1,2,8,5}, - * respectively. - * We use the first parameter in glDrawArrays or glDrawElements to - * go from face to face. - */ - if (useWireMode) - { - /* setup reading the right elements from vertex array */ - GLubyte vertIdx4[4] = {0,1,2,5}; - GLubyte vertIdx5[5] = {0,1,2,8,5}; - GLubyte *indices = NULL; - int vertStride, i, j; - - switch (numEdgePerFace) - { - case 3: - vertStride = 3; /* there are 3 vertices for each face in the array */ - break; - case 4: - indices = vertIdx4; - vertStride = 6; /* there are 6 vertices for each face in the array */ - break; - case 5: - indices = vertIdx5; - vertStride = 9; /* there are 9 vertices for each face in the array */ - break; - } - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - - glVertexPointer(3, GL_DOUBLE, 0, vertices); - glNormalPointer(GL_DOUBLE, 0, normals); - - if (numEdgePerFace==3) - 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++ ) @@ -644,7 +579,8 @@ 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 + * (or half circle if halfCircle==TRUE) * * Notes: * It is the responsibility of the caller to free these tables @@ -652,25 +588,21 @@ 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(double **sint,double **cost,const int n) +static void fghCircleTable(GLfloat **sint, GLfloat **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 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 = (double *) calloc(sizeof(double), size+1); - *cost = (double *) calloc(sizeof(double), 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)) { free(*sint); @@ -679,25 +611,107 @@ 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; for (i=1; i0)?1:0]; - r0 = 0.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)) { - glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); - glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); + 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 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(); - /* Cover each stack with a quad strip, except the top stack */ - - for( i=0; i 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... */ @@ -1066,8 +1091,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); + glBegin(GL_TRIANGLE_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(); @@ -1165,36 +1190,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