From 607e80088877e0cf4557cbc0e1fa4506c562f866 Mon Sep 17 00:00:00 2001 From: Diederick Niehorster Date: Sat, 21 Apr 2012 18:05:56 +0000 Subject: [PATCH] glutCones now refreshed too little edits on glutSphere too, nothign affecting functionality git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1257 7f0cb862-5218-0410-a997-914c9d46530a --- src/fg_geometry.c | 371 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 232 insertions(+), 139 deletions(-) diff --git a/src/fg_geometry.c b/src/fg_geometry.c index c482a7f..7a446d9 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -894,6 +894,88 @@ static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloa free(sint2); free(cost2); } + +void fghGenerateCone( + GLfloat base, GLfloat height, GLint slices, GLint stacks, /* input */ + GLfloat **vertices, GLfloat **normals, int* nVert /* output */ + ) +{ + int i,j; + int idx = 0; /* idx into vertex/normal buffer */ + + /* Pre-computed circle */ + GLfloat *sint,*cost; + + /* Step in z and radius as stacks are drawn. */ + GLfloat z = 0; + GLfloat r = (GLfloat)base; + + const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 ); + const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); + + /* Scaling factors for vertex normals */ +#ifdef __cplusplus + const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base )); + const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base )); +#else + const GLfloat cosn = ( (GLfloat)height / (GLfloat)sqrt( (double)(height * height + base * base) )); + const GLfloat sinn = ( (GLfloat)base / (GLfloat)sqrt( (double)(height * height + base * base) )); +#endif /* __cplusplus */ + + + + /* number of unique vertices */ + if (slices==0 || stacks<1) + { + /* nothing to generate */ + *nVert = 0; + return; + } + *nVert = slices*(stacks+1)+1; + + /* Pre-computed circle */ + fghCircleTable(&sint,&cost,-slices,FALSE); + + /* 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"); + } + + /* bottom */ + (*vertices)[0] = 0.f; + (*vertices)[1] = 0.f; + (*vertices)[2] = z; + (*normals )[0] = 0.f; + (*normals )[1] = 0.f; + (*normals )[2] = -1.f; + idx = 3; + + /* each stack */ + for (i=0; i 65535) + fgWarning("fghCone: too many slices or stacks requested, indices will wrap"); + + /* Generate vertices and normals */ + fghGenerateCone((GLfloat)base,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert); + + if (nVert==0) + /* nothing to draw */ + return; + + if (useWireMode) + { + GLushort *sliceIdx, *stackIdx; + /* First, generate vertex index arrays for drawing with glDrawElements + * We have a bunch of line_loops to draw for each stack, and a + * bunch for each slice. + */ + + stackIdx = malloc(slices*(stacks+1)*sizeof(GLushort)); + sliceIdx = malloc(slices*2 *sizeof(GLushort)); + if (!(stackIdx) || !(sliceIdx)) + { + free(stackIdx); + free(sliceIdx); + fgError("Failed to allocate memory in fghGenerateCone"); + } + + /* generate for each stack */ + for (i=0,idx=0; i 0 ) ? stacks : 1 ); - const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); - - /* Scaling factors for vertex normals */ - -#ifdef __cplusplus - const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base )); - const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base )); -#else - const GLfloat cosn = ( (GLfloat)height / (GLfloat)sqrt( (double)(height * height + base * base) )); - const GLfloat sinn = ( (GLfloat)base / (GLfloat)sqrt( (double)(height * height + base * base) )); -#endif /* __cplusplus */ - - /* Pre-computed circle */ - - GLfloat *sint,*cost; - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" ); - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Cover the circular base with a triangle fan... */ - - z0 = 0; - z1 = zStep; - - r0 = (GLfloat)base; - r1 = r0 - rStep; - - glBegin(GL_TRIANGLE_FAN); - - glNormal3f(0,0,-1); - glVertex3f(0,0, z0 ); - - for (j=0; j<=slices; j++) - glVertex3f(cost[j]*r0, sint[j]*r0, z0); - - glEnd(); - - /* Cover each stack with a triangle strip */ - for( i=0; i 0 ) ? stacks : 1 ); - const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 ); - - /* Scaling factors for vertex normals */ - -#ifdef __cplusplus - const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base )); - const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base )); -#else - const GLfloat cosn = ( (GLfloat)height / (GLfloat)sqrt( (double)(height * height + base * base) )); - const GLfloat sinn = ( (GLfloat)base / (GLfloat)sqrt( (double)(height * height + base * base) )); -#endif /* __cplusplus */ - - /* Pre-computed circle */ - - GLfloat *sint,*cost; - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); - fghCircleTable(&sint,&cost,-slices,FALSE); - - /* Draw the stacks... */ - - for (i=0; i