Game Development Reference
In-Depth Information
need to introduce some new drawing functions, akin to DrawBox() , which render
objects of differing shapes and sizes. Thanks to the rigorous refactoring we per-
formed on our rendering code back in Chapter 4 , Object Management and Debug
Rendering , we have made this whole process fairly trivial on ourselves.
DrawSphere() uses a new OpenGL primitive type, GL_QUAD_STRIP , to create
strips of quads. A quad is made of four points, rather than three points for a triangle.
A strip of quads is built two vertices at a time, since they are connected together in
strips end-to-end. This is a much more efficient way of rendering many primitives in
one step.
In order to generate a spherical object of a given radius with quads, we have to it-
erate laterally, find the angles of this segment, then iterate longitudinally, and draw
them.
Meanwhile, to draw a cylinder we can make use of a new helper function in OpenGL
to build the cylinder piece-by-piece using quadrics to build two disks via gluDisk() ,
and a cylindrical hull via gluCylinder() . These quadric functions are built into
FreeGLUT, providing an interface with which we can build meshes using simple
mathematical equations. There are various types of quadrics that are available in the
FreeGLUT library, which you can find in the documentation and/or source code.
To save space we won't cover any code snippets here, since there are far too many
bite-size simple commands being introduced. But, take the time to look at the new
functions DrawSphere() , DrawCylinder() , and the changes to DrawShape() .
Our application now renders a yellow sphere and green cylinder to accompany our
two original boxes. Try shooting boxes at them and observe their motion. They be-
have exactly as we would expect a physics object of that shape to behave! The
following screenshot shows our new cylindrical and spherical objects added to our
scene:
Search WWH ::




Custom Search