From 3dbdb2176419cd2dabb3c19e1b24568bdc96be4e Mon Sep 17 00:00:00 2001 From: Diederick Niehorster Date: Sat, 28 Apr 2012 10:57:40 +0000 Subject: [PATCH] porting torus: split off vertex generation fixed drawing to work with this git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1266 7f0cb862-5218-0410-a997-914c9d46530a --- src/fg_geometry.c | 263 ++++++++++++++++++++++++----------------------------- 1 file changed, 119 insertions(+), 144 deletions(-) diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 9489a3b..28d2c73 100644 --- a/src/fg_geometry.c +++ b/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