X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffreeglut_geometry.c;h=5468551ceb70bf4e5a7910dca6a873febd75b6de;hb=4f58e4f75c3d57437da215f4119e9991b77272e0;hp=23ba7d6a6e31260679b939316031da39c30c9c4a;hpb=646676b8dbf8ab504ac8a275fe9a63a403a3190b;p=freeglut diff --git a/src/freeglut_geometry.c b/src/freeglut_geometry.c index 23ba7d6..5468551 100644 --- a/src/freeglut_geometry.c +++ b/src/freeglut_geometry.c @@ -29,9 +29,7 @@ #include "config.h" #endif -#define G_LOG_DOMAIN "freeglut-geometry" - -#include "../include/GL/freeglut.h" +#include #include "freeglut_internal.h" /* @@ -41,13 +39,6 @@ * * glutWireCube() -- looks OK * glutSolidCube() -- OK - * glutWireSphere() -- OK - * glutSolidSphere() -- OK - * - * Following functions have been implemented by Pawel and modified by John Fay: - * - * glutWireCone() -- looks OK - * glutSolidCone() -- looks OK * * Those functions have been implemented by John Fay. * @@ -61,6 +52,14 @@ * glutSolidTetrahedron() -- looks OK * glutWireIcosahedron() -- looks OK * glutSolidIcosahedron() -- looks OK + * + * The Following functions have been updated by Nigel Stewart, based + * on FreeGLUT 2.0.0 implementations: + * + * glutWireSphere() -- looks OK + * glutSolidSphere() -- looks OK + * glutWireCone() -- looks OK + * glutSolidCone() -- looks OK */ @@ -117,376 +116,475 @@ void FGAPIENTRY glutSolidCube( GLdouble dSize ) } /* - * Draws a wire sphere. Code contributed by Andreas Umbach + * Compute lookup table of cos and sin values forming a cirle + * + * Notes: + * It is the responsibility of the caller to free these tables + * The size of the table is (n+1) to form a connected loop + * The last entry is exactly the same as the first + * The sign of n can be flipped to get the reverse loop */ -void FGAPIENTRY glutWireSphere( GLdouble dRadius, GLint slices, GLint stacks ) + +static void circleTable(double **sint,double **cost,const int n) { - double radius = dRadius, phi, psi, dpsi, dphi; - double *vertex; - int i, j; - double cphi, sphi, cpsi, spsi ; + int i; - /* - * Allocate the vertices array - */ - vertex = calloc( sizeof(double), 3 * slices * (stacks - 1) ); + /* 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; + + /* 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); - glPushMatrix(); - glScaled( radius, radius, radius ); + /* Bail out if memory allocation fails, fgError never returns */ - dpsi = M_PI / (stacks + 1); - dphi = 2 * M_PI / slices; - psi = dpsi; + if (!(*sint) || !(*cost)) + { + free(*sint); + free(*cost); + fgError("Failed to allocate memory in circleTable"); + } + + /* Compute cos and sin around the circle */ - for( j=0; j=0; j--) { - int offset = 3 * ( j * slices + i ) ; - cphi = cos ( phi ) ; - sphi = sin ( phi ) ; - *(vertex + offset + 0) = sphi * spsi ; - *(vertex + offset + 1) = cphi * spsi ; - *(vertex + offset + 2) = cpsi ; - phi += dphi; + glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); + glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); } - psi += dpsi; - } + glEnd(); - for( i=0; i + * Draws a solid cone */ -void FGAPIENTRY glutSolidSphere( GLdouble dRadius, GLint slices, GLint stacks ) +void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks ) { - double radius = dRadius, phi, psi, dpsi, dphi; - double *next, *tmp, *row; - int i, j; - double cphi, sphi, cpsi, spsi ; + int i,j; - glPushMatrix(); - /* glScalef( radius, radius, radius ); */ + /* Step in z and radius as stacks are drawn. */ - row = calloc( sizeof(double), slices * 3 ); - next = calloc( sizeof(double), slices * 3 ); + double z0,z1; + double r0,r1; - dpsi = M_PI / (stacks + 1); - dphi = 2 * M_PI / slices; - psi = dpsi; - phi = 0; + const double zStep = height/stacks; + const double rStep = base/stacks; - /* init first line + do polar cap */ - glBegin( GL_TRIANGLE_FAN ); - glNormal3d( 0.0, 0.0, 1.0 ); - glVertex3d( 0.0, 0.0, radius ); + /* Scaling factors for vertex normals */ - for( i=0; i=0; i-- ) + /* Scaling factors for vertex normals */ + + const double cosn = ( height / sqrt ( height * height + base * base )); + const double sinn = ( base / sqrt ( height * height + base * base )); + + /* Pre-computed circle */ + + double *sint,*cost; + circleTable(&sint,&cost,-slices); + + /* Draw the stacks... */ + + for (i=0; i=0; j--) + glVertex3d(cost[j]*radius, sint[j]*radius, height); + glEnd(); + + /* Do the stacks */ + + z0 = 0.0; + z1 = zStep; - for( j=0; j