Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#12 FreeGLUT should have a cylinder shape

closed
nobody
Easy (28)
5
2003-12-03
2003-09-30
Nigel Stewart
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
    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<=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<=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>=0; i-- )
    glVertex3d(cost[i]*width, sint[i]*width, height);
    glEnd();
    }

    /*
    * Cover the sides
    */

    glBegin( GL_QUAD_STRIP );
    for( i=0; i<=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
    Nigel Stewart
    2003-09-30

    Logged In: YES
    user_id=338692

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

    OK!

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

     
  • Nigel Stewart
    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
    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
    Nigel Stewart
    2003-10-07

    FreeGLUT 2.0.0 shapes.c output

     
    Attachments
  • Nigel Stewart
    Nigel Stewart
    2003-10-07

    GLUT 3.7 shapes.c output

     
    Attachments
  • Nigel Stewart
    Nigel Stewart
    2003-10-07

    Updated freeglut_geometry.c shapes.c output

     
    Attachments
  • Nigel Stewart
    Nigel Stewart
    2003-11-24

    Updated freeglut_geometry.c

     
    Attachments
  • Nigel Stewart
    Nigel Stewart
    2003-11-24

    Updated shapes.c output (Now matches GLUT 3.7)

     
    Attachments
  • Nigel Stewart
    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
    Nigel Stewart
    2003-11-24

    Proposed demo program - sphere, cone and torus

     
    Attachments
  • Nigel Stewart
    Nigel Stewart
    2003-12-03

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

    Logged In: YES
    user_id=338692

    Resolved in the recent CVS updates and (upcoming) 2.0.2