## #12 FreeGLUT should have a cylinder shape

closed
nobody
5
2003-12-03
2003-09-30
No

GLUT has cube, sphere, cone and torus shapes.
It would also be handy to have a cylinder shape.
Similar in formulation to a cone, specify the
width, height and slices. For completeness, also
a wireframe version.

## Discussion

• Nigel Stewart - 2003-09-30

Logged In: YES
user_id=338692

/*
* Draws a solid cylinder
*/
void FGAPIENTRY glutSolidCylinder( GLdouble width, GLdouble
height, GLint slices, GLint closed )
{
int i;

/*
* Radial angle of each slice
*/

const double angle = -2 * M_PI / (double) slices;

/*
* Pre-computed lookup table of cos and sin values forming
a cirle
*/

double *sint = NULL;
double *cost = NULL;

cost = (double *) calloc( sizeof(double), slices + 1);
sint = (double *) calloc( sizeof(double), slices + 1);

assert(cost);
assert(sint);

if (!cost || !sint)
{
free(cost);
free(sint);
return;
}

for (i=0; i&lt;=slices; i++)
{
cost[i] = cos(angle*i);
sint[i] = sin(angle*i);
}

/*
* Cover the base and top
*/

if (closed)
{
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0.0, 0.0, -1.0 );
glVertex3d(0.0, 0.0, 0.0 );
for (i=0; i&lt;=slices; i++ )
glVertex3d(cost[i]*width, sint[i]*width, 0.0);
glEnd();

glBegin(GL_TRIANGLE_FAN);
glNormal3d(0.0, 0.0, 1.0 );
glVertex3d(0.0, 0.0, height);
for (i=slices; i&gt;=0; i-- )
glVertex3d(cost[i]*width, sint[i]*width, height);
glEnd();
}

/*
* Cover the sides
*/

for( i=0; i&lt;=slices; i++ )
{
glNormal3d(cost[i], sint[i], 0.0 );
glVertex3d(cost[i]*width, sint[i]*width, 0.0 );
glVertex3d(cost[i]*width, sint[i]*width, height);
}
glEnd();

/*
* Release sin/cos tables
*/
free(sint);
free(cost);
}

• Nigel Stewart - 2003-09-30

Logged In: YES
user_id=338692

&gt; I suggest printing a message to {stderr}, and perhaps calling
&gt; abort(). (For the former, I think that there's an fgError()
&gt; function that might be better than writing directly to
{stderr}.
&gt; Yes?)

OK!

&gt;&gt;assert(cost);
&gt;&gt;assert(sint);
&gt;&gt;
&gt;&gt;if (!cost || !sint)
&gt;&gt;{
&gt;&gt; free(cost);
&gt;&gt; free(sint);
&gt;&gt; fgError(&quot;Failed to allocate memory in glutSolidCylinder&quot;);
&gt;&gt; return;
&gt;&gt;}

• Nigel Stewart - 2003-10-05

Logged In: YES
user_id=338692

The 2nd version of freeglut_geometry.c
attached here as a file also implements
wire and solid sphere.

• Nigel Stewart - 2003-10-07

Logged In: YES
user_id=338692

The attached program shapes.c is proposed as a new freeglut
demo program to display a rotating sphere, cone and torus
as solid and wireframe.

Screenshots of freeglut-2.0.0, the updated shapes, and GLUT
output are also attached.

• Nigel Stewart - 2003-10-07

FreeGLUT 2.0.0 shapes.c output

• Nigel Stewart - 2003-10-07

GLUT 3.7 shapes.c output

• Nigel Stewart - 2003-10-07

Updated freeglut_geometry.c shapes.c output

• Nigel Stewart - 2003-11-24

Updated freeglut_geometry.c

• Nigel Stewart - 2003-11-24

Updated shapes.c output (Now matches GLUT 3.7)

• Nigel Stewart - 2003-11-24

Logged In: YES
user_id=338692

Attached to this record on SourceForge, updated
freeglut_geometry.c file with fixed and tidied sphere,
cone, cylinder implementations and fixed torus implementation.

Also attached, screen shots illustrating consistency with
GLUT 3.7, and inconsistency with FreeGLUT 2.0.0.

A proposed addition to the FreeGLUT sample programs
shapes.c draws animated sphere, cone and torus in both
shaded and wireframe modes. Proposed location:
freeglut/progs/demos/shapes

In freeglut_ext.h, the interface for drawing cylinders needs
to be included...

/*
* Geometry functions, see freeglut_geometry.c
*/
FGAPI void FGAPIENTRY glutWireCylinder( GLdouble radius,
GLdouble height, GLint slices, GLint stacks);
FGAPI void FGAPIENTRY glutSolidCylinder( GLdouble radius,
GLdouble height, GLint slices, GLint stacks);

• Nigel Stewart - 2003-11-24

Proposed demo program - sphere, cone and torus

• Nigel Stewart - 2003-12-03
• status: open --> closed

• Nigel Stewart - 2003-12-03

Logged In: YES
user_id=338692

Resolved in the recent CVS updates and (upcoming) 2.0.2

## Get latest updates about Open Source Projects, Conferences and News.

JavaScript is required for this form.

No, thanks