diff --git a/freeglut/freeglut/src/fg_geometry.c b/freeglut/freeglut/src/fg_geometry.c index 9489a3b..28d2c73 100644 --- a/freeglut/freeglut/src/fg_geometry.c +++ b/freeglut/freeglut/src/fg_geometry.c @@ -853,7 +853,7 @@ static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloa /* 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)) + if (!(*vertices) || !(*normals)) { free(*vertices); free(*normals); @@ -949,7 +949,7 @@ void fghGenerateCone( /* 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)) + if (!(*vertices) || !(*normals)) { free(*vertices); free(*normals); @@ -1031,7 +1031,7 @@ void fghGenerateCylinder( /* 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)) + if (!(*vertices) || !(*normals)) { free(*vertices); free(*normals); @@ -1098,6 +1098,67 @@ void fghGenerateCylinder( free(sint); free(cost); } + +void fghGenerateTorus( + double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, /* input */ + GLfloat **vertices, GLfloat **normals, int* nVert /* output */ + ) +{ + GLfloat iradius = (float)dInnerRadius; + GLfloat oradius = (float)dOuterRadius; + int i, j; + + /* Pre-computed circle */ + GLfloat *spsi, *cpsi; + GLfloat *sphi, *cphi; + + /* number of unique vertices */ + if (nSides<2 || nRings<2) + { + /* nothing to generate */ + *nVert = 0; + return; + } + *nVert = nSides * nRings; + + if ((*nVert) > 65535) + fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap"); + + /* precompute values on unit circle */ + fghCircleTable(&spsi,&cpsi, nRings,FALSE); + fghCircleTable(&sphi,&cphi,-nSides,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 fghGenerateTorus"); + } + + for( j=0; j