Tutorial 07 : Quadric Shapes

Tutorial Download Section

In the previous tutorials, we have drawn basic shapes. Here, we will use OpenGl Utility Library (GLU) to draw more quadric shapes like sphere and cylinder.
This tutorial is an application of transformations shown in Lesson 2 .

To creates quadric shapes with GLU, just begin with the creation of a quadric object with gluNewQuadric :
In Gl4java, this method return a long value (reference to a quadric object). In Jogl, it is cleaner with the use of a GLUquadric object.
The type of these objects are different, but their use is identical in both APIs.

We can defines few rendering properties. If you don't defines these properties, the default value will be used.
    glu.gluQuadricTexture(quadric, value)       //Default: false
    glu.gluQuadricDrawStyle(quadric, value)     //Default: GLU_FILL
    glu.gluQuadricNormals(quadric, value)       //Default: GLU_SMOOTH
    glu.gluQuadricOrientation(quadric, value)   //Default: GLU_OUTSIDE
The first method defines if the shape is textured (true or false).
The second defines the rendering style of the quadric (GLU_POINT, GLU_LINE, GLU_FILL or GLU_SILHOUETTE).
The third defines if the shape is drawn with normal and, if so, specify if it is per face or per vertex normal (GLU_NONE, GLU_FLAT or GLU_SMOOTH). Normals are used with light, see Tutorials 12-13 for more informations.
The last methods defines normal orientation, either points outisde the shape or inside (GLU_OUTSIDE or GLU_INSIDE).

Now we have creates and defines all the properties that we need. We can draw all quadric shapes that we would with the quadric object.
To draw a sphere :
    glu.gluSphere(quadric, radius, slices, rings)
To draw a cylinder (or a cone if a radius is equal to 0) :
    glu.gluCylinder(quadric, bottomRadius, topRadius, height, slices, rings)
To draw a CD (or a disk if internalRadius is equal to 0) :
    glu.gluDisk(quadric, internalRadius, externalRadius, slices, rings)
To draw a partial CD (or a piece of a disk) :
    glu.gluPartialDisk(quadric, internalRadius, externalRadius, slices, rings, startAngle, angle)

slices and rings parameters defines the quality of the quadric drawn. It is directly in relation with number of vertice drawn. High values  makes accurate shape but it requieres more time for rendering (due to a high number of vertices to draw).

To see the effect of these parameters, draw a shape in line style (GLU_LINE) and change the value of the parameters. Here are some screenshots showing how shapes are discretized.

Rendering quality


When the quadric object is finished to use, delete it with :

For more details on what the following function do, take a look at Lesson 2.

When we draw complicated scene, it is usefull to remember some positions.
We can do this with these two methods :
    gl.glPushmatrix()       //equivalent to 'save current position'
    gl.glPopMatrix()        //equivalent to 'load the last position saved'

Read Lesson 2 to know more about this.

The cylinder and the sphere have their own rotation and translation. To draw the sphere inside the cylinder, we have to remember where is the center of the cylinder before applying its transformations (rotation ...). Thats's why we surround the code with push/pop matrix.

The code for rendering the cylinder and the sphere is surrounded to remember the scene center.
The rendering of the disk and cone are similar.


    //New quadric object
    GLUquadric quadric = glu.gluNewQuadric();    //In Jogl
//    long quadric = glu.gluNewQuadric();        //In Gl4java
    //Turn on the texture mode for quadrics
    glu.gluQuadricTexture(quadric, true);

    //Draw the cylinder and the sphere
    gl.glPushMatrix();                        //save first position
        //Draw the cylinder
        gl.glTranslatef(0.0f, -1.0f, 0.0f);      //Move to cylinder center
        gl.glPushMatrix();                       //save cylinder center
            gl.glRotatef(cylinderRot, 1.0f, 0.0f, 0.f);  //cylinder rotation
            gl.glTranslatef(-1.5f,0.0f,0.0f);            //go to the bottom of the cylinder
            gl.glRotatef(90.f, 0.0f, 1.0f, 0.0f);        //put the cylinder horizontal
            //Cylinder render (fill or line)
            glu.gluQuadricDrawStyle(quadric, render1);
            glu.gluCylinder(quadric, 1, 1, 3, 20, 20);
        gl.glPopMatrix();                        //load cylinder center
        //Draw the sphere
        gl.glTranslatef(sphereTrans, 0.0f, 0.0f);        //movement of the sphere inside the cylinder
        gl.glRotatef(sphereRot, 1.0f, 1.0f, 1.0f);
        glu.gluQuadricDrawStyle(quadric, render2);
        glu.gluSphere(quadric, 0.5f, 20, 20);
    gl.glPopMatrix();                        //load first position
    //Draw the cone
        gl.glTranslatef(1.5f, 0.65f, 0.0f);
        gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(cdRot, 0.0f, 0.0f, 1.0f);
        glu.gluQuadricDrawStyle(quadric, render1);
        glu.gluCylinder(quadric, 0.60, 0, 2.5f, 20, 20);
    //Delete the quadric object

+/-: incease/decrease shape quality

Remember to download the GraphicEngine-1.1.2 to run this tutorial !

If you've got any remarks on this tutorial, please let me know to improve it.
Thanks for your feedback.

Previous Tutorial


Next turorial

Copyright © 2004-2012 Jérôme Jouvie - All rights reserved. http://jerome.jouvie.free.fr/