X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffreeglut_geometry.c;h=3c7a835a6ce02e9b999fdffc33dd99021d732bd6;hb=9807c5fdb2f20f098c7046786908a7f3ce1dc2d4;hp=83715bb2f8b8c2a3eb21c43ba3677bf511f2e84a;hpb=2e5dc871b7c1df472978af9811d0e97b6684707c;p=freeglut diff --git a/src/freeglut_geometry.c b/src/freeglut_geometry.c index 83715bb..3c7a835 100644 --- a/src/freeglut_geometry.c +++ b/src/freeglut_geometry.c @@ -25,11 +25,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../include/GL/freeglut.h" +#include #include "freeglut_internal.h" /* @@ -39,13 +35,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. * @@ -59,6 +48,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 */ @@ -71,12 +68,12 @@ void FGAPIENTRY glutWireCube( GLdouble dSize ) { double size = dSize * 0.5; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" ); + # define V(a,b,c) glVertex3d( a size, b size, c size ); # define N(a,b,c) glNormal3d( a, b, c ); - /* - * PWO: I dared to convert the code to use macros... - */ + /* PWO: I dared to convert the code to use macros... */ glBegin( GL_LINE_LOOP ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); glEnd(); glBegin( GL_LINE_LOOP ); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); glEnd(); glBegin( GL_LINE_LOOP ); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); glEnd(); @@ -95,12 +92,12 @@ void FGAPIENTRY glutSolidCube( GLdouble dSize ) { double size = dSize * 0.5; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" ); + # define V(a,b,c) glVertex3d( a size, b size, c size ); # define N(a,b,c) glNormal3d( a, b, c ); - /* - * PWO: Again, I dared to convert the code to use macros... - */ + /* PWO: Again, I dared to convert the code to use macros... */ glBegin( GL_QUADS ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); @@ -115,376 +112,496 @@ 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 fghCircleTable(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; + + /* Table size, the sign of n flips the circle direction */ + + const int size = abs(n); - /* - * Allocate the vertices array - */ - vertex = (double *)calloc( sizeof(double), 3 * slices * (stacks - 1) ); + /* Determine the angle between samples */ - glPushMatrix(); - glScaled( radius, radius, radius ); + const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n ); - dpsi = M_PI / (stacks + 1); - dphi = 2 * M_PI / slices; - psi = dpsi; + /* Allocate memory for n samples, plus duplicate of first entry at the end */ - for( j=0; j0)?1:0]; + r0 = 0.0; + r1 = sint2[(stacks>0)?1:0]; + + glBegin(GL_TRIANGLE_FAN); + + glNormal3d(0,0,1); + glVertex3d(0,0,radius); + + for (j=slices; 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 = (double *)calloc( sizeof(double), slices * 3 ); - next = (double *)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 > 0 ) ? stacks : 1 ); + const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); - /* 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 ) ? stacks : 1 ); + const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); + + /* 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; + + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); - /* south pole */ - glBegin( GL_TRIANGLE_FAN ); - glNormal3d( 0.0, 0.0, -1.0 ); - glVertex3d( 0.0, 0.0, -radius ); - glNormal3dv( row ); - glVertex3d( radius * *(row + 0), radius * *(row + 1), radius * *(row + 2) ); + fghCircleTable(&sint,&cost,-slices); - for( i=slices-1; i>=0; i-- ) + /* Draw the stacks... */ + + for (i=0; i 0 ) ? stacks : 1 ); - /* - * First the cone's bottom... - */ - for( j=0; j=0; j--) + glVertex3d(cost[j]*radius, sint[j]*radius, height); + glEnd(); + + /* Do the stacks */ - for( j=0; j 0 ) ? stacks : 1 ); - /* - * First the cone's bottom... - */ - for( j=0; j