Many updates from John Fay.

git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@32 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
dheyse 2002-11-28 23:31:40 +00:00
parent 5e1ec95e4c
commit 6d93edeb79

View File

@ -37,32 +37,30 @@
/* /*
* TODO BEFORE THE STABLE RELEASE: * TODO BEFORE THE STABLE RELEASE:
* *
* Following functions have been contributed by Andreas Umbach. I modified * Following functions have been contributed by Andreas Umbach.
* them a bit to make them use GLib (for memory allocation).
* *
* glutWireCube() -- could add normal vectors so that lighting works * glutWireCube() -- looks OK
* glutSolidCube() -- OK * glutSolidCube() -- OK
* glutWireSphere() -- OK * glutWireSphere() -- OK
* glutSolidSphere() -- OK * glutSolidSphere() -- OK
* *
* Following functions have been implemented by me: * Following functions have been implemented by Pawel and modified by John Fay:
* *
* glutWireCone() -- looks OK * glutWireCone() -- looks OK
* glutSolidCone() -- normals are missing, there are holes in the thing * glutSolidCone() -- looks OK
* *
* Those functions need to be implemented, as nothing has been done yet. * Those functions have been implemented by John Fay.
* For now all of them draw a wire or solid cube, just to mark their presence.
* *
* glutWireTorus() -- * glutWireTorus() -- looks OK
* glutSolidTorus() -- * glutSolidTorus() -- looks OK
* glutWireDodecahedron() -- * glutWireDodecahedron() -- looks OK
* glutSolidDodecahedron() -- * glutSolidDodecahedron() -- looks OK
* glutWireOctahedron() -- * glutWireOctahedron() -- looks OK
* glutSolidOctahedron() -- * glutSolidOctahedron() -- looks OK
* glutWireTetrahedron() -- * glutWireTetrahedron() -- looks OK
* glutSolidTetrahedron() -- * glutSolidTetrahedron() -- looks OK
* glutWireIcosahedron() -- * glutWireIcosahedron() -- looks OK
* glutSolidIcosahedron() -- * glutSolidIcosahedron() -- looks OK
*/ */
@ -76,18 +74,20 @@ void FGAPIENTRY glutWireCube( GLdouble dSize )
float size = (float) dSize * 0.5f; float size = (float) dSize * 0.5f;
# define V(a,b,c) glVertex3f( a size, b size, c size ); # define V(a,b,c) glVertex3f( a size, b size, c size );
# define N(a,b,c) glNormal3f( 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 ); V(-,-,+); V(+,-,+); V(+,+,+); V(-,+,+); glEnd(); glBegin( GL_LINE_LOOP ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); glEnd();
glBegin( GL_LINE_LOOP ); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); glEnd(); glBegin( GL_LINE_LOOP ); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); glEnd();
glBegin( GL_LINES ); glBegin( GL_LINE_LOOP ); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); glEnd();
V(-,-,+); V(-,-,-); V(-,+,+); V(-,+,-); glBegin( GL_LINE_LOOP ); N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); glEnd();
V(+,-,+); V(+,-,-); V(+,+,+); V(+,+,-); glBegin( GL_LINE_LOOP ); N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); glEnd();
glEnd(); glBegin( GL_LINE_LOOP ); N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); glEnd();
# undef V # undef V
# undef N
} }
/* /*
@ -104,12 +104,12 @@ void FGAPIENTRY glutSolidCube( GLdouble dSize )
* 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 ); glBegin( GL_QUADS );
N( 1, 0, 0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
N( 0, 1, 0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
N( 0, 0, 1); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
N(-1, 0, 0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
N( 0,-1, 0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
N( 0, 0,-1); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
glEnd(); glEnd();
# undef V # undef V
@ -124,6 +124,7 @@ void FGAPIENTRY glutWireSphere( GLdouble dRadius, GLint slices, GLint stacks )
float radius = (float) dRadius, phi, psi, dpsi, dphi; float radius = (float) dRadius, phi, psi, dpsi, dphi;
float* vertex; float* vertex;
int i, j; int i, j;
float cphi, sphi, cpsi, spsi ;
/* /*
* Allocate the vertices array * Allocate the vertices array
@ -139,15 +140,19 @@ void FGAPIENTRY glutWireSphere( GLdouble dRadius, GLint slices, GLint stacks )
for( j=0; j<stacks-1; j++ ) for( j=0; j<stacks-1; j++ )
{ {
cpsi = (float)cos ( psi ) ;
spsi = (float)sin ( psi ) ;
phi = 0; phi = 0;
for( i=0; i<slices; i++ ) for( i=0; i<slices; i++ )
{ {
*(vertex + 3 * j * slices + 3 * i + 0) = sin( phi ) * sin( psi ); int offset = 3 * ( j * slices + i ) ;
*(vertex + 3 * j * slices + 3 * i + 1) = cos( phi ) * sin( psi ); cphi = (float)cos ( phi ) ;
*(vertex + 3 * j * slices + 3 * i + 2) = cos( psi ); sphi = (float)sin ( phi ) ;
*(vertex + offset + 0) = sphi * spsi ;
*(vertex + offset + 1) = cphi * spsi ;
*(vertex + offset + 2) = cpsi ;
phi += dphi; phi += dphi;
glVertex3fv( vertex + 3 * j * slices + 3 * i );
} }
psi += dpsi; psi += dpsi;
@ -161,8 +166,9 @@ void FGAPIENTRY glutWireSphere( GLdouble dRadius, GLint slices, GLint stacks )
for( j=0; j<stacks - 1; j++ ) for( j=0; j<stacks - 1; j++ )
{ {
glNormal3fv( vertex + 3 * j * slices + 3 * i ); int offset = 3 * ( j * slices + i ) ;
glVertex3fv( vertex + 3 * j * slices + 3 * i ); glNormal3fv( vertex + offset );
glVertex3fv( vertex + offset );
} }
glNormal3f(0, 0, -1); glNormal3f(0, 0, -1);
@ -176,8 +182,9 @@ void FGAPIENTRY glutWireSphere( GLdouble dRadius, GLint slices, GLint stacks )
for( i=0; i<slices; i++ ) for( i=0; i<slices; i++ )
{ {
glNormal3fv( vertex + 3 * j * slices + 3 * i ); int offset = 3 * ( j * slices + i ) ;
glVertex3fv( vertex + 3 * j * slices + 3 * i ); glNormal3fv( vertex + offset );
glVertex3fv( vertex + offset );
} }
glEnd(); glEnd();
@ -195,6 +202,7 @@ void FGAPIENTRY glutSolidSphere( GLdouble dRadius, GLint slices, GLint stacks )
float radius = (float) dRadius, phi, psi, dpsi, dphi; float radius = (float) dRadius, phi, psi, dpsi, dphi;
float *next, *tmp, *row; float *next, *tmp, *row;
int i, j; int i, j;
float cphi, sphi, cpsi, spsi ;
glPushMatrix(); glPushMatrix();
//glScalef( radius, radius, radius ); //glScalef( radius, radius, radius );
@ -236,6 +244,8 @@ void FGAPIENTRY glutSolidSphere( GLdouble dRadius, GLint slices, GLint stacks )
{ {
phi = 0; phi = 0;
psi += dpsi; psi += dpsi;
cpsi = (float)cos ( psi ) ;
spsi = (float)sin ( psi ) ;
/* get coords */ /* get coords */
glBegin( GL_QUAD_STRIP ); glBegin( GL_QUAD_STRIP );
@ -243,9 +253,11 @@ void FGAPIENTRY glutSolidSphere( GLdouble dRadius, GLint slices, GLint stacks )
/* glBegin(GL_LINE_LOOP); */ /* glBegin(GL_LINE_LOOP); */
for( i=0; i<slices; i++ ) for( i=0; i<slices; i++ )
{ {
next[ i * 3 + 0 ] = sin( phi ) * sin( psi ); cphi = (float)cos ( phi ) ;
next[ i * 3 + 1 ] = cos( phi ) * sin( psi ); sphi = (float)sin ( phi ) ;
next[ i * 3 + 2 ] = cos( psi ); next[ i * 3 + 0 ] = sphi * spsi ;
next[ i * 3 + 1 ] = cphi * spsi ;
next[ i * 3 + 2 ] = cpsi ;
glNormal3fv( row + i * 3 ); glNormal3fv( row + i * 3 );
glVertex3f( glVertex3f(
@ -304,78 +316,85 @@ void FGAPIENTRY glutSolidSphere( GLdouble dRadius, GLint slices, GLint stacks )
*/ */
void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks ) void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks )
{ {
float alt = (float) height / (float) (stacks + 1); float alt = (float) height / (float) (stacks + 1);
float angle = (float) M_PI / (float) slices * 2.0f; float angle = (float) M_PI / (float) slices * 2.0f;
float slope = (float) tan( height / base ); float slope = (float) ( height / base );
float* vertices = NULL; float sBase = (float)base ;
int i, j; float sinNormal = (float)( base / sqrt ( height * height + base * base )) ;
float cosNormal = (float)( height / sqrt ( height * height + base * base )) ;
/* float* vertices = NULL;
* We need 'slices' points on a circle int i, j;
*/
vertices = calloc( sizeof(float), 2 * (slices + 1) );
for( i=0; i<slices+1; i++ ) /*
* We need 'slices' points on a circle
*/
vertices = calloc( sizeof(float), 2 * (slices + 1) );
for( j=0; j<slices+1; j++ )
{
vertices[ j*2 + 0 ] = cos( angle * j );
vertices[ j*2 + 1 ] = sin( angle * j );
}
/*
* First the cone's bottom...
*/
for( j=0; j<slices; j++ )
{
glBegin( GL_LINE_LOOP );
glNormal3f( 0, 0, -1 );
glVertex3f( vertices[ (j+0)*2+0 ] * sBase, vertices[ (j+0)*2+1 ] * sBase, 0 );
glVertex3f( vertices[ (j+1)*2+0 ] * sBase, vertices[ (j+1)*2+1 ] * sBase, 0 );
glVertex3f( 0, 0, 0 );
glEnd();
}
/*
* Then all the stacks between the bottom and the top
*/
for( i=0; i<stacks; i++ )
{
float alt_a = i * alt, alt_b = (i + 1) * alt;
float scl_a = (height - alt_a) / slope;
float scl_b = (height - alt_b) / slope;
for( j=0; j<slices; j++ )
{ {
vertices[ i*2 + 0 ] = cos( angle * i ); glBegin( GL_LINE_LOOP );
vertices[ i*2 + 1 ] = sin( angle * i ); glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_a, vertices[(j+0)*2+1] * scl_a, alt_a );
glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glEnd();
glBegin( GL_LINE_LOOP );
glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+1)*2+0] * scl_b, vertices[(j+1)*2+1] * scl_b, alt_b );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glEnd();
} }
}
/* /*
* First the cone's bottom... * Finally have the top part drawn...
*/ */
for( i=0; i<slices; i++ ) for( j=0; j<slices; j++ )
{ {
float scl = height / slope; float scl = alt / slope;
glBegin( GL_LINE_LOOP ); glBegin( GL_LINE_LOOP );
glNormal3f( 0, 0, -1 ); glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[ (i+0)*2+0 ] * scl, vertices[ (i+0)*2+1 ] * scl, 0 ); glVertex3f( vertices[ (j+0)*2+0 ] * scl, vertices[ (j+0)*2+1 ] * scl, height - alt );
glVertex3f( vertices[ (i+1)*2+0 ] * scl, vertices[ (i+1)*2+1 ] * scl, 0 ); glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( 0, 0, 0 ); glVertex3f( vertices[ (j+1)*2+0 ] * scl, vertices[ (j+1)*2+1 ] * scl, height - alt );
glEnd(); glVertex3f( 0, 0, height );
} glEnd();
}
/*
* Then all the stacks between the bottom and the top
*
* ekhm jak wektor normalny z trojkata?
*/
for( i=0; i<stacks; i++ )
{
float alt_a = i * alt, alt_b = (i + 1) * alt;
float scl_a = (height - alt_a) / slope;
float scl_b = (height - alt_b) / slope;
for( j=0; j<slices; j++ )
{
glBegin( GL_LINE_LOOP );
glVertex3f( vertices[(j+0)*2+0] * scl_a, vertices[(j+0)*2+1] * scl_a, alt_a );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glEnd();
glBegin( GL_LINE_LOOP );
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glVertex3f( vertices[(j+1)*2+0] * scl_b, vertices[(j+1)*2+1] * scl_b, alt_b );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glEnd();
}
}
/*
* Finally have the top part drawn...
*/
for( i=0; i<slices; i++ )
{
float scl = alt / slope;
glBegin( GL_LINE_LOOP );
glVertex3f( vertices[ (i+0)*2+0 ] * scl, vertices[ (i+0)*2+1 ] * scl, height - alt );
glVertex3f( vertices[ (i+1)*2+0 ] * scl, vertices[ (i+1)*2+1 ] * scl, height - alt );
glVertex3f( 0, 0, height );
glEnd();
}
} }
/* /*
@ -383,78 +402,87 @@ void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLin
*/ */
void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks ) void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks )
{ {
float alt = (float) height / (float) (stacks + 1); float alt = (float) height / (float) (stacks + 1);
float angle = (float) M_PI / (float) slices * 2.0f; float angle = (float) M_PI / (float) slices * 2.0f;
float slope = (float) tan( height / base ); float slope = (float) ( height / base );
float* vertices = NULL; float sBase = (float)base ;
int i, j; float sinNormal = (float)( base / sqrt ( height * height + base * base )) ;
float cosNormal = (float)( height / sqrt ( height * height + base * base )) ;
/* float* vertices = NULL;
* We need 'slices' points on a circle int i, j;
*/
vertices = calloc( sizeof(float), 2 * (slices + 1) );
for( i=0; i<slices+1; i++ ) /*
* We need 'slices' points on a circle
*/
vertices = calloc( sizeof(float), 2 * (slices + 1) );
for( j=0; j<slices+1; j++ )
{
vertices[ j*2 + 0 ] = cos( angle * j );
vertices[ j*2 + 1 ] = sin( angle * j );
}
/*
* First the cone's bottom...
*/
for( j=0; j<slices; j++ )
{
float scl = height / slope;
glBegin( GL_TRIANGLES );
glNormal3f( 0, 0, -1 );
glVertex3f( vertices[ (j+0)*2+0 ] * sBase, vertices[ (j+0)*2+1 ] * sBase, 0 );
glVertex3f( vertices[ (j+1)*2+0 ] * sBase, vertices[ (j+1)*2+1 ] * sBase, 0 );
glVertex3f( 0, 0, 0 );
glEnd();
}
/*
* Then all the stacks between the bottom and the top
*/
for( i=0; i<stacks; i++ )
{
float alt_a = i * alt, alt_b = (i + 1) * alt;
float scl_a = (height - alt_a) / slope;
float scl_b = (height - alt_b) / slope;
for( j=0; j<slices; j++ )
{ {
vertices[ i*2 + 0 ] = cos( angle * i ); glBegin( GL_TRIANGLES );
vertices[ i*2 + 1 ] = sin( angle * i ); glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_a, vertices[(j+0)*2+1] * scl_a, alt_a );
glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glEnd();
glBegin( GL_TRIANGLES );
glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( vertices[(j+1)*2+0] * scl_b, vertices[(j+1)*2+1] * scl_b, alt_b );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glEnd();
} }
}
/* /*
* First the cone's bottom... * Finally have the top part drawn...
*/ */
for( i=0; i<slices; i++ ) for( j=0; j<slices; j++ )
{ {
float scl = height / slope; float scl = alt / slope;
glBegin( GL_TRIANGLES ); glBegin( GL_TRIANGLES );
glNormal3f( 0, 0, -1 ); glNormal3f( sinNormal * vertices[(j+0)*2+0], sinNormal * vertices[(j+0)*2+1], cosNormal ) ;
glVertex3f( vertices[ (i+0)*2+0 ] * scl, vertices[ (i+0)*2+1 ] * scl, 0 ); glVertex3f( vertices[ (j+0)*2+0 ] * scl, vertices[ (j+0)*2+1 ] * scl, height - alt );
glVertex3f( vertices[ (i+1)*2+0 ] * scl, vertices[ (i+1)*2+1 ] * scl, 0 ); glNormal3f( sinNormal * vertices[(j+1)*2+0], sinNormal * vertices[(j+1)*2+1], cosNormal ) ;
glVertex3f( 0, 0, 0 ); glVertex3f( vertices[ (j+1)*2+0 ] * scl, vertices[ (j+1)*2+1 ] * scl, height - alt );
glEnd(); glVertex3f( 0, 0, height );
} glEnd();
}
/*
* Then all the stacks between the bottom and the top
*
* ekhm jak wektor normalny z trojkata?
*/
for( i=0; i<stacks; i++ )
{
float alt_a = i * alt, alt_b = (i + 1) * alt;
float scl_a = (height - alt_a) / slope;
float scl_b = (height - alt_b) / slope;
for( j=0; j<slices; j++ )
{
glBegin( GL_TRIANGLES );
glVertex3f( vertices[(j+0)*2+0] * scl_a, vertices[(j+0)*2+1] * scl_a, alt_a );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glEnd();
glBegin( GL_TRIANGLES );
glVertex3f( vertices[(j+1)*2+0] * scl_a, vertices[(j+1)*2+1] * scl_a, alt_a );
glVertex3f( vertices[(j+1)*2+0] * scl_b, vertices[(j+1)*2+1] * scl_b, alt_b );
glVertex3f( vertices[(j+0)*2+0] * scl_b, vertices[(j+0)*2+1] * scl_b, alt_b );
glEnd();
}
}
/*
* Finally have the top part drawn...
*/
for( i=0; i<slices; i++ )
{
float scl = alt / slope;
glBegin( GL_TRIANGLES );
glVertex3f( vertices[ (i+0)*2+0 ] * scl, vertices[ (i+0)*2+1 ] * scl, height - alt );
glVertex3f( vertices[ (i+1)*2+0 ] * scl, vertices[ (i+1)*2+1 ] * scl, height - alt );
glVertex3f( 0, 0, height );
glEnd();
}
} }
/* /*
@ -462,7 +490,77 @@ void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLi
*/ */
void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings ) void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
{ {
glutWireSphere( dOuterRadius, 5, 5 ); float iradius = (float) dInnerRadius, oradius = (float)dOuterRadius, phi, psi, dpsi, dphi;
float* vertex, *normal;
int i, j;
float spsi, cpsi, sphi, cphi ;
/*
* Allocate the vertices array
*/
vertex = calloc( sizeof(float), 3 * nSides * nRings );
normal = calloc( sizeof(float), 3 * nSides * nRings );
glPushMatrix();
dpsi = 2.0 * M_PI / (float)nRings ;
dphi = 2.0 * M_PI / (float)nSides ;
psi = 0.0;
for( j=0; j<nRings; j++ )
{
cpsi = (float)cos ( psi ) ;
spsi = (float)sin ( psi ) ;
phi = 0.0;
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
cphi = (float)cos ( phi ) ;
sphi = (float)sin ( phi ) ;
*(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;
}
for( i=0; i<nSides; i++ )
{
glBegin( GL_LINE_LOOP );
for( j=0; j<nRings; j++ )
{
int offset = 3 * ( j * nSides + i ) ;
glNormal3fv( normal + offset );
glVertex3fv( vertex + offset );
}
glEnd();
}
for( j=0; j<nRings; j++ )
{
glBegin(GL_LINE_LOOP);
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
glNormal3fv( normal + offset );
glVertex3fv( vertex + offset );
}
glEnd();
}
free ( vertex ) ;
free ( normal ) ;
glPopMatrix();
} }
/* /*
@ -470,7 +568,74 @@ void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLi
*/ */
void FGAPIENTRY glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings ) void FGAPIENTRY glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
{ {
glutSolidSphere( dOuterRadius, 5, 5 ); float iradius = (float) dInnerRadius, oradius = (float)dOuterRadius, phi, psi, dpsi, dphi;
float* vertex, *normal;
int i, j;
float spsi, cpsi, sphi, cphi ;
/*
* Increment the number of sides and rings to allow for one more point than surface
*/
nSides ++ ;
nRings ++ ;
/*
* Allocate the vertices array
*/
vertex = calloc( sizeof(float), 3 * nSides * nRings );
normal = calloc( sizeof(float), 3 * nSides * nRings );
glPushMatrix();
dpsi = 2.0 * M_PI / (float)(nRings - 1) ;
dphi = 2.0 * M_PI / (float)(nSides - 1) ;
psi = 0.0;
for( j=0; j<nRings; j++ )
{
cpsi = (float)cos ( psi ) ;
spsi = (float)sin ( psi ) ;
phi = 0.0;
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
cphi = (float)cos ( phi ) ;
sphi = (float)sin ( phi ) ;
*(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();
free ( vertex ) ;
free ( normal ) ;
glPopMatrix();
} }
/* /*
@ -670,14 +835,14 @@ void FGAPIENTRY glutSolidTetrahedron( void )
/* /*
* *
*/ */
float r[12][3] = { { 1.0f, 0.0f, 0.0f }, float icos_r[12][3] = { { 1.0f, 0.0f, 0.0f },
{ 0.447214f, 0.894427f, 0.0f }, { 0.447214f, 0.276393f, 0.850651f }, { 0.447214f, -0.723607f, 0.525731f }, { 0.447214f, -0.723607f, -0.525731f }, { 0.447214f, 0.276393f, -0.850651f }, { 0.447214f, 0.894427f, 0.0f }, { 0.447214f, 0.276393f, 0.850651f }, { 0.447214f, -0.723607f, 0.525731f }, { 0.447214f, -0.723607f, -0.525731f }, { 0.447214f, 0.276393f, -0.850651f },
{ -0.447214f, -0.894427f, 0.0f }, { -0.447214f, -0.276393f, 0.850651f }, { -0.447214f, 0.723607f, 0.525731f }, { -0.447214f, 0.723607f, -0.525731f }, { -0.447214f, -0.276393f, -0.850651f }, { -0.447214f, -0.894427f, 0.0f }, { -0.447214f, -0.276393f, 0.850651f }, { -0.447214f, 0.723607f, 0.525731f }, { -0.447214f, 0.723607f, -0.525731f }, { -0.447214f, -0.276393f, -0.850651f },
{ -1.0f, 0.0f, 0.0f } } ; { -1.0f, 0.0f, 0.0f } } ;
int v [20][3] = { { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 5 }, { 0, 5, 1 }, int icos_v [20][3] = { { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 5 }, { 0, 5, 1 },
{ 1, 8, 2 }, { 2, 7, 3 }, { 3, 6, 4 }, { 4, 10, 5 }, { 5, 9, 1 }, { 1, 8, 2 }, { 2, 7, 3 }, { 3, 6, 4 }, { 4, 10, 5 }, { 5, 9, 1 },
{ 1, 9, 8 }, { 2, 8, 7 }, { 3, 7, 6 }, { 4, 6, 10 }, { 5, 10, 9 }, { 1, 9, 8 }, { 2, 8, 7 }, { 3, 7, 6 }, { 4, 6, 10 }, { 5, 10, 9 },
{ 11, 9, 10 }, { 11, 8, 9 }, { 11, 7, 8 }, { 11, 6, 7 }, { 11, 10, 6 } } ; { 11, 9, 10 }, { 11, 8, 9 }, { 11, 7, 8 }, { 11, 6, 7 }, { 11, 10, 6 } } ;
void FGAPIENTRY glutWireIcosahedron( void ) void FGAPIENTRY glutWireIcosahedron( void )
{ {
@ -685,14 +850,14 @@ void FGAPIENTRY glutWireIcosahedron( void )
for ( i = 0; i < 20; i++ ) for ( i = 0; i < 20; i++ )
{ {
float normal[3] ; float normal[3] ;
normal[0] = ( r[v[i][1]][1] - r[v[i][0]][1] ) * ( r[v[i][2]][2] - r[v[i][0]][2] ) - ( r[v[i][1]][2] - r[v[i][0]][2] ) * ( r[v[i][2]][1] - r[v[i][0]][1] ) ; normal[0] = ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) - ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) ;
normal[1] = ( r[v[i][1]][2] - r[v[i][0]][2] ) * ( r[v[i][2]][0] - r[v[i][0]][0] ) - ( r[v[i][1]][0] - r[v[i][0]][0] ) * ( r[v[i][2]][2] - r[v[i][0]][2] ) ; normal[1] = ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) - ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) ;
normal[2] = ( r[v[i][1]][0] - r[v[i][0]][0] ) * ( r[v[i][2]][1] - r[v[i][0]][1] ) - ( r[v[i][1]][1] - r[v[i][0]][1] ) * ( r[v[i][2]][0] - r[v[i][0]][0] ) ; normal[2] = ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) - ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) ;
glBegin ( GL_LINE_LOOP ) ; glBegin ( GL_LINE_LOOP ) ;
glNormal3fv ( normal ) ; glNormal3fv ( normal ) ;
glVertex3fv ( r[v[i][0]] ) ; glVertex3fv ( icos_r[icos_v[i][0]] ) ;
glVertex3fv ( r[v[i][1]] ) ; glVertex3fv ( icos_r[icos_v[i][1]] ) ;
glVertex3fv ( r[v[i][2]] ) ; glVertex3fv ( icos_r[icos_v[i][2]] ) ;
glEnd () ; glEnd () ;
} }
} }
@ -708,16 +873,177 @@ void FGAPIENTRY glutSolidIcosahedron( void )
for ( i = 0; i < 20; i++ ) for ( i = 0; i < 20; i++ )
{ {
float normal[3] ; float normal[3] ;
normal[0] = ( r[v[i][1]][1] - r[v[i][0]][1] ) * ( r[v[i][2]][2] - r[v[i][0]][2] ) - ( r[v[i][1]][2] - r[v[i][0]][2] ) * ( r[v[i][2]][1] - r[v[i][0]][1] ) ; normal[0] = ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) - ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) ;
normal[1] = ( r[v[i][1]][2] - r[v[i][0]][2] ) * ( r[v[i][2]][0] - r[v[i][0]][0] ) - ( r[v[i][1]][0] - r[v[i][0]][0] ) * ( r[v[i][2]][2] - r[v[i][0]][2] ) ; normal[1] = ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) - ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) ;
normal[2] = ( r[v[i][1]][0] - r[v[i][0]][0] ) * ( r[v[i][2]][1] - r[v[i][0]][1] ) - ( r[v[i][1]][1] - r[v[i][0]][1] ) * ( r[v[i][2]][0] - r[v[i][0]][0] ) ; normal[2] = ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) - ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) ;
glNormal3fv ( normal ) ; glNormal3fv ( normal ) ;
glVertex3fv ( r[v[i][0]] ) ; glVertex3fv ( icos_r[icos_v[i][0]] ) ;
glVertex3fv ( r[v[i][1]] ) ; glVertex3fv ( icos_r[icos_v[i][1]] ) ;
glVertex3fv ( r[v[i][2]] ) ; glVertex3fv ( icos_r[icos_v[i][2]] ) ;
} }
glEnd () ; glEnd () ;
} }
/*
*
*/
float rdod_r[14][3] = { { 0.0f, 0.0f, 1.0f },
{ 0.707107f, 0.000000f, 0.5f }, { 0.000000f, 0.707107f, 0.5f }, { -0.707107f, 0.000000f, 0.5f }, { 0.000000f, -0.707107f, 0.5f },
{ 0.707107f, 0.707107f, 0.0f }, { -0.707107f, 0.707107f, 0.0f }, { -0.707107f, -0.707107f, 0.0f }, { 0.707107f, -0.707107f, 0.0f },
{ 0.707107f, 0.000000f, -0.5f }, { 0.000000f, 0.707107f, -0.5f }, { -0.707107f, 0.000000f, -0.5f }, { 0.000000f, -0.707107f, -0.5f },
{ 0.0f, 0.0f, -1.0f } } ;
int rdod_v [12][4] = { { 0, 1, 5, 2 }, { 0, 2, 6, 3 }, { 0, 3, 7, 4 }, { 0, 4, 8, 1 },
{ 5, 10, 6, 2 }, { 6, 11, 7, 3 }, { 7, 12, 8, 4 }, { 8, 9, 5, 1 },
{ 5, 9, 13, 10 }, { 6, 10, 13, 11 }, { 7, 11, 13, 12 }, { 8, 12, 13, 9 } } ;
float rdod_n[12][3] = {
{ 0.353553f, 0.353553f, 0.5f }, { -0.353553f, 0.353553f, 0.5f }, { -0.353553f, -0.353553f, 0.5f }, { 0.353553f, -0.353553f, 0.5f },
{ 0.000000f, 1.000000f, 0.0f }, { -1.000000f, 0.000000f, 0.0f }, { 0.000000f, -1.000000f, 0.0f }, { 1.000000f, 0.000000f, 0.0f },
{ 0.353553f, 0.353553f, -0.5f }, { -0.353553f, 0.353553f, -0.5f }, { -0.353553f, -0.353553f, -0.5f }, { 0.353553f, -0.353553f, -0.5f }
} ;
void FGAPIENTRY glutWireRhombicDodecahedron( void )
{
int i ;
for ( i = 0; i < 12; i++ )
{
glBegin ( GL_LINE_LOOP ) ;
glNormal3fv ( rdod_n[i] ) ;
glVertex3fv ( rdod_r[rdod_v[i][0]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][1]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][2]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][3]] ) ;
glEnd () ;
}
}
/*
*
*/
void FGAPIENTRY glutSolidRhombicDodecahedron( void )
{
int i ;
glBegin ( GL_QUADS ) ;
for ( i = 0; i < 12; i++ )
{
glNormal3fv ( rdod_n[i] ) ;
glVertex3fv ( rdod_r[rdod_v[i][0]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][1]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][2]] ) ;
glVertex3fv ( rdod_r[rdod_v[i][3]] ) ;
}
glEnd () ;
}
#define NUM_FACES 4
static GLfloat tetrahedron_v[4][3] = /* Vertices */
{
{ -0.5, -0.288675134, -0.144337567 },
{ 0.5, -0.288675134, -0.144337567 },
{ 0.0, 0.577350269, -0.144337567 },
{ 0.0, 0.0, 0.672159013 }
} ;
static GLint tetrahedron_i[4][3] = /* Vertex indices */
{
{ 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 1 }, { 1, 3, 2 }
} ;
static GLfloat tetrahedron_n[4][3] = /* Normals */
{
{ 0.0, 0.0, -1.0 },
{ -0.816496581, 0.471404521, 0.333333333 },
{ 0.0, -0.942809042, 0.333333333 },
{ 0.816496581, 0.471404521, 0.333333333 }
} ;
void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, GLfloat offset[3], GLfloat scale )
{
int i, j ;
if ( num_levels == 0 )
{
for ( i = 0 ; i < NUM_FACES ; i++ )
{
glBegin ( GL_LINE_LOOP ) ;
glNormal3fv ( tetrahedron_n[i] ) ;
for ( j = 0; j < 3; j++ )
{
float x = offset[0] + scale * tetrahedron_v[tetrahedron_i[i][j]][0] ;
float y = offset[1] + scale * tetrahedron_v[tetrahedron_i[i][j]][1] ;
float z = offset[2] + scale * tetrahedron_v[tetrahedron_i[i][j]][2] ;
glVertex3f ( x, y, z ) ;
}
glEnd () ;
}
}
else
{
GLfloat local_offset[3] ; // Use a local variable to avoid buildup of roundoff errors
num_levels -- ;
scale /= 2.0 ;
local_offset[0] = offset[0] + scale * tetrahedron_v[0][0] ;
local_offset[1] = offset[1] + scale * tetrahedron_v[0][1] ;
local_offset[2] = offset[2] + scale * tetrahedron_v[0][2] ;
glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[0] += scale ;
glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[0] -= 0.5 * scale ;
local_offset[1] += 0.866025403 * scale ;
glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[1] -= 0.577350269 * scale ;
local_offset[2] += 0.816496581 * scale ;
glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
}
}
void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLfloat offset[3], GLfloat scale )
{
int i, j ;
if ( num_levels == 0 )
{
glBegin ( GL_TRIANGLES ) ;
for ( i = 0 ; i < NUM_FACES ; i++ )
{
glNormal3fv ( tetrahedron_n[i] ) ;
for ( j = 0; j < 3; j++ )
{
float x = offset[0] + scale * tetrahedron_v[tetrahedron_i[i][j]][0] ;
float y = offset[1] + scale * tetrahedron_v[tetrahedron_i[i][j]][1] ;
float z = offset[2] + scale * tetrahedron_v[tetrahedron_i[i][j]][2] ;
glVertex3f ( x, y, z ) ;
}
}
glEnd () ;
}
else
{
GLfloat local_offset[3] ; // Use a local variable to avoid buildup of roundoff errors
num_levels -- ;
scale /= 2.0 ;
local_offset[0] = offset[0] + scale * tetrahedron_v[0][0] ;
local_offset[1] = offset[1] + scale * tetrahedron_v[0][1] ;
local_offset[2] = offset[2] + scale * tetrahedron_v[0][2] ;
glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[0] += scale ;
glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[0] -= 0.5 * scale ;
local_offset[1] += 0.866025403 * scale ;
glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
local_offset[1] -= 0.577350269 * scale ;
local_offset[2] += 0.816496581 * scale ;
glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
}
}
#undef NUM_FACES
/*** END OF FILE ***/ /*** END OF FILE ***/