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 :
glu.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)
Rendering quality
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 :
glu.gluDeleteQuadric(quadric)
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.
Rendering |
//New quadric object |
R: switch between GLU_LINE, GLU_FILL and GLU_SILHOUETTE
+/-: incease/decrease shape quality
Remember to download the GraphicEngine-1.1.2 to run this tutorial !
Tutorial 7 src (8 ko) //Port to Jogl JSR-231 initially done by Magarrett Dias
If you've got any remarks on this tutorial, please let
me know to improve
it.
Thanks for your feedback.
Copyright © 2004-2012 Jérôme Jouvie - All rights reserved. | http://jerome.jouvie.free.fr/ |