- GLfloat iradius = (float)dInnerRadius, oradius = (float)dOuterRadius;
- GLfloat phi, psi, dpsi, dphi;
- GLfloat *vertex, *normal;
- int i, j;
- GLfloat spsi, cpsi, sphi, cphi ;
-
- FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTorus" );
-
- if ( nSides < 1 ) nSides = 1;
- if ( nRings < 1 ) nRings = 1;
-
- /* Increment the number of sides and rings to allow for one more point than surface */
- nSides ++ ;
- nRings ++ ;
-
- /* Allocate the vertices array */
- vertex = (GLfloat *)calloc( sizeof(GLfloat), 3 * nSides * nRings );
- normal = (GLfloat *)calloc( sizeof(GLfloat), 3 * nSides * nRings );
-
- glPushMatrix();
-
- dpsi = 2.0f * (GLfloat)M_PI / (GLfloat)(nRings - 1) ;
- dphi = -2.0f * (GLfloat)M_PI / (GLfloat)(nSides - 1) ;
- psi = 0.0f;
-
- for( j=0; j<nRings; j++ )
- {
-#ifdef __cplusplus
- cpsi = cosf( psi ) ;
- spsi = sinf( psi ) ;
-#else
- cpsi = (float)cos( (double)psi ) ;
- spsi = (float)sin( (double)psi ) ;
-#endif /* __cplusplus */
- phi = 0.0f;
-
- for( i=0; i<nSides; i++ )
- {
- int offset = 3 * ( j * nSides + i ) ;
-#ifdef __cplusplus
- cphi = cosf( phi ) ;
- sphi = sinf( phi ) ;
-#else
- cphi = (float)cos( (double)phi ) ;
- sphi = (float)sin( (double)phi ) ;
-#endif /* __cplusplus */
- *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
- *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
- *(vertex + offset + 2) = sphi * iradius ;
- *(normal + offset + 0) = cpsi * cphi ;
- *(normal + offset + 1) = spsi * cphi ;
- *(normal + offset + 2) = sphi ;
- phi += dphi;
- }
-
- psi += dpsi;
- }
-
- glBegin( GL_QUADS );
- for( i=0; i<nSides-1; i++ )
- {
- for( j=0; j<nRings-1; j++ )
- {
- int offset = 3 * ( j * nSides + i ) ;
- glNormal3fv( normal + offset );
- glVertex3fv( vertex + offset );
- glNormal3fv( normal + offset + 3 );
- glVertex3fv( vertex + offset + 3 );
- glNormal3fv( normal + offset + 3 * nSides + 3 );
- glVertex3fv( vertex + offset + 3 * nSides + 3 );
- glNormal3fv( normal + offset + 3 * nSides );
- glVertex3fv( vertex + offset + 3 * nSides );
- }
- }
-
- glEnd();