In the last tutorial I taught you how to add color to polygons and quads. In this tutorial I will teach you how to rotate
the colored polygons around an axis.
Using the code from the last tutorial, we will be adding to a few places in the code. I will rewrite the entire section of
code below so it's easy for you to figure out what's been added, and what needs to be replaced.
We'll start off by adding the two variables to keep track of the rotation for each object. We do this right at the beginning
of the program. You'll notice below I've added two lines after BOOL keys[256]. These lines set up two floating point
variables that we can use to spin the objects with very fine accuracy. Floating point allows decimal numbers. Meaning we're
not stuck using 1, 2, 3 for the angle, we can use 1.1, 1.7, 2.3, or even 1.015 for fine accuracy. You'll find that
floating point numbers are essential to OpenGL programming.
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The GLaux Library
static HGLRC hRC; // Permanent Rendering Context
static HDC hDC; // Private GDI Device Context
BOOL keys[256]; // Array Used For The Keyboard Routine
GLfloat rtri; // Angle For The Triangle ( NEW )
GLfloat rquad; // Angle For The Quad ( NEW )
Now we need to modify the DrawGLScene() code. I will rewrite the entire procedure. This should make it easier for you
to see what changes I have made to the original code. I'll explain why lines have been modified, and what exactly it is
that the new lines do. The next section of code is exactly the same as in the last tutorial.
GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(-1.5f,0.0f,-6.0f); // Move Into The Screen And Left
The next line of code is new. glRotatef(Angle,Xtrue,Ytrue,Ztrue) is responsible for rotating the object around an axis.
You will get alot of use out of this command. Angle is some number (usually stored in a variable) that represents how much
you would like to spin the object. Xtrue, Ytrue and Ztrue are either 0.0f, or 1.0f. If any of those parameters are 1.0f,
OpenGL will spin the object on that axis. So if you had glRotatef(10.0f,0.0f,1.0f,0.0f), the object would spin 10 degrees
on the Y axis. If you had glRotatef(5.0f,1.0f,0.0f,1.0f), the object would spin 5 on both the X axis and the Z axis.
To better understand rotation, think of it like this. You have a spin top. From the top of the top to the point on the
bottom is the Y axis. When we spin the top (spin the Y axis), the top spins left to right. If we think of the axel
between the tires of a truck as the X axis, when we spin the axel (the X axis), the tires spin up and down. Make sense?
So in the following line of code, if rtri stored the number 7, we would spin 7 on the Y axis (left to right). You can try
experimenting with the code. Change the 0.0f's to 1.0f's, and the 1.0f to a 0.0f for different results.
glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
The next section of code has not changed. It draws a colorful smooth blended triangle made from one polygon. It will
draw the triangle on the left side of the screen in it's rotated position.
glBegin(GL_POLYGON); // Start Drawing A Polygon
glColor3f(1.0f,0.0f,0.0f); // Set Top Point Of Polygon To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // First Point Of The Polygon (Triangle)
glColor3f(0.0f,1.0f,0.0f); // Set Left Point Of Polygon To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Second Point Of The Polygon
glColor3f(0.0f,0.0f,1.0f); // Set Right Point Of Polygon To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Third Point Of The Polygon
glEnd(); // Done Drawing The Polygon
You'll notice in the code below, that we've added another glLoadIdentity(). We do this to reset the view. If we didn't
reset the view and we translated after the object had been rotated, you would get very unexpected results. Because the
axis has been rotated, it may not be pointing in the direction you think. So if we translate left on the X axis, we
may end up moving up or down instead, depending on how much we've rotated on each axis. Try taking the glLoadIdentity()
line out to see what I mean.
Once the scene has been reset, so X is running left to right, Y up and down, and Z in and out, we translate. You'll
notice we're only moving 1.5 to the right instead of 3.0 like we did in the last lesson. When we reset the screen, our
focus moves to the center of the screen. meaning we're no longer 1.5 units to the left, we're back at 0.0. So to get to
1.5 on the right side of zero we dont have to move 1.5 from left to center then 1.5 to the right (total of 3.0) we only
have to move from center to the right which is just 1.5 units.
After we have moved to our new location on the right side of the screen, we rotate the quad, on the X axis. This will
cause the square to spin up and down.
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units
glRotatef(rquad,1.0f,0.0f,0.0f); // Rotate The Quad On The X axis ( NEW )
This section of code remains the same. It draws a blue square made from one quad. It will draw the square on the
right side of the screen in it's rotated position.
glColor3f(0.5f,0.5f,1.0f); // Set The Color To A Nice Blue Shade
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right Of The Quad
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left Of The Quad
glEnd(); // Done Drawing The Quad
The next two lines are new. Think of rtri, and rquad as containers. At the top of our program we made the containers
(GLfloat rtri, and GLfloat rquad). When we built the containers they had nothing in them. The first line below ADDS
0.2 to that container. So each time we check the value in the rtri container after this section of code, it will have gone
up by 0.2. The rquad container decreases by 0.15. So every time we check the rquad container, it will have gone down by
0.15. Going down will cause the object to spin the opposite direction it would spin if you were going up.
Try chaning the + to a - in the line below see how the object spins the other direction. Try changing the values from
0.2 to 1.0. The higher the number, the faster the object will spin. The lower the number, the slower it will spin.
rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
}
In this tutorial I have tried to explain in as much detail, how to rotate objects around an axis. Play around with the code,
try spinning the objects, on the Z axis, the X & Y, or all three :) If you have comments or questions please email me. If
you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know.
I want to make the best OpenGL tutorials I can. I'm interested in hearing your feedback.
Jeff Molofee (NeHe)
* DOWNLOAD Visual C++ Code For This Lesson.
* DOWNLOAD Delphi Code For This Lesson.
( Conversion by Brad Choate )
* DOWNLOAD Linux Code For This Lesson.
( Conversion by Richard Campbell )
* DOWNLOAD BeOS Code For This Lesson.
( Conversion by Chris Herborth )