NeHe Productions: OpenGL Lesson #18Date de publication : 31/03/2006 , Date de mise à jour : 31/03/2006
Par
GB Schmick ( TipTup ) et Jeff Molofee ( NeHe ) (Autres articles)
Lesson: 18 Quadrics
Quadrics are a way of drawing complex objects that would usually take a few FOR loops and some background in trigonometry.
We'll be using the code from lesson seven. We will add 7 variables and modify the texture to add some variety :)
Okay
now move down to InitGL(), We're going to add 3 lines of code here to
initialize our quadratic. Add these 3 lines after you enable light1 but
before you return true. The first line of code initializes the
Quadratic and creates a pointer to where it will be held in memory. If
it can't be created it returns 0. The second line of code creates
smooth normals on the quadratic so lighting will look great. Other
possible values are GLU_NONE, and GLU_FLAT. Last we enable texture
mapping on our quadratic. Texture mapping is kind of awkward and never
goes the way you planned as you can tell from the crate texture.
Now
I decided to keep the cube in this tutorial so you can see how the
textures are mapped onto the quadratic object. I decided to move the
cube into its own function so when we write the draw function it will
appear more clean. Everybody should recognize this code. =P
Next is the DrawGLScene function, here I just wrote a simple if statement to draw the different objects. Also I used a
static variable (a local variable that keeps its value everytime it is called) for a cool effect when drawing the
partial disk. I'm going to rewrite the whole DrawGLScene function for clarity.
You'll notice that when I talk about the parameters being used I ignore the actual first parameter (quadratic). This
parameter is used for all the objects we draw aside from the cube, so I ignore it when I talk about the parameters.
The second object we create is going to be a Cylinder. The first parameter (1.0f) is the radius of the cylinder at
base (bottom). The second parameter (1.0f) is the radius of the cylinder at the top. The third parameter ( 3.0f) is
the height of the cylinder (how long it is). The fouth parameter (32) is how many subdivisions there are "around"
the Z axis, and finally, the fifth parameter (32) is the amount of subdivisions "along" the Z axis. The more
subdivisions there are the more detailed the object is. By increase the amount of subdivisions you add more polygons
to the object. So you end up sacrificing speed for quality. Most of the time it's easy to find a happy medium.
The third object we create will be a CD shaped disc. The first parameter (0.5f) is the inner radius of the disk. This
value can be zero, meaning there will be no hole in the middle. The larger the inner radius is, the bigger the hole in
the middle of the disc will be. The second parameter (1.5f) is the outer radius. This value should be larger than the
inner radius. If you make this value a little bit larger than the inner radius you will end up with a thing ring. If
you make this value alot larger than the inner radius you will end up with a thick ring. The third parameter (32) is
the number of slices that make up the disc. Think of slices like the slices in a pizza. The more slices you have, the
smoother the outer edge of the disc will be. Finally the fourth parameter (32) is the number of rings that make up the
disc. The rings are are similar to the tracks on a record. Circles inside circles. These ring subdivide the disc from
the inner radius to the outer radius, adding more detail. Again, the more subdivisions there are, the slow it will run.
Our fourth object is an object that I know many of you have been dying to figure out. The Sphere! This one is quite
simple. The first parameter is the radius of the sphere. In case you're not familiar with radius/diameter, etc, the
radius is the distance from the center of the object to the outside of the object. In this case our radius is 1.3f.
Next we have our subdivision "around" the Z axis (32), and our subdivision "along" the Z axis (32). The more
subdivisions you have the smoother the sphere will look. Spheres usually require quite a few subdivisions to make them
look smooth.
Our fifth object is created using the same command that we used to create a Cylinder. If you remember, when we were
creating the Cylinder the first two parameters controlled the radius of the cylinder at the bottom and the top. To
make a cone it makes sense that all we'd have to do is make the radius at one end Zero. This will create a point at
one end. So in the code below, we make the radius at the top of the cylinder equal zero. This creates our point,
which also creates our cone.
Our sixth object is created with gluPartialDisc. The object we create using this command will look exactly like the
disc we created above, but with the command gluPartialDisk there are two new parameters. The fifth parameter (part1)
is the start angle we want to start drawing the disc at. The sixth parameter is the sweep angle. The sweep angle is
the distance we travel from the current angle. We'll increase the sweep angle, which causes the disc to be slowly
drawn to the screen in a clockwise direction. Once our sweep hits 360 degrees we start to increase the start angle.
the makes it appear as if the disc is being erased, then we start all over again!
In the KillGLWindow() section of code, we need to delete the quadratic to free up system resources. We
do this with the command gluDeleteQuadratic.
Now for the final part, they key input. Just add this where we check the rest of key input.
Thats all! Now you can draw quadrics in OpenGL. Some really impressive things can be done with morphing and
quadrics. The animated disc is an example of simple morphing.
Everyone if you have time go check out my website, TipTup.Com 2000 .
GB Schmick ( TipTup )
Jeff Molofee ( NeHe )
|
Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright © 2006 Nehe Gamedev.net. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.