The next section of code has not changed. It draws a colorful smooth blended triangle. The triangle will be drawn on the left side of the screen, and will be rotated on it's Y axis causing it to spin left to right.
glBegin(GL_TRIANGLES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Set Top Point Of Triangle To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // First Point Of The Triangle
glColor3f(0.0f,1.0f,0.0f); // Set Left Point Of Triangle To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Second Point Of The Triangle
glColor3f(0.0f,0.0f,1.0f); // Set Right Point Of Triangle To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Third Point Of The Triangle
glEnd(); // Done Drawing The Triangle
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. If 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(); // Reset The Current Modelview Matrix
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0
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 )
return TRUE; // Keep Going
}
Finally change the code to toggle window / fullscreen mode so that the title at the top of the window is proper.
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window ( Modified )
if (!CreateGLWindow("NeHe's Rotation Tutorial",640,480,16,fullscreen)) {
return 0; // Quit If Window Was Not Created
}
}
In this tutorial I have tried to explain in as much detail as possible, 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.
* DOWNLOAD Visual C++ Code For This Lesson.
* DOWNLOAD ASM Code For This Lesson. (Conversion by Foolman)
* DOWNLOAD Borland C++ Builder 5.0 Code For This Lesson. (Conversion by Neil Flynn)
* DOWNLOAD Code Warrior 5 Code For This Lesson. (Conversion by Scott Lupton)
* DOWNLOAD Cygwin Code For This Lesson. (Conversion by Stephan Ferraro)
* DOWNLOAD Delphi Code For This Lesson. (Conversion by Peter De Jaegher)
* DOWNLOAD Game GLUT Code For This Lesson. (Conversion by Milikas Anastasios)
* DOWNLOAD Genu Code For This Lesson. (Conversion by Louis-Charles Dumais)
* DOWNLOAD GLUT Code For This Lesson. (Conversion by Andy Restad)
* DOWNLOAD Irix Code For This Lesson. (Conversion by Lakmal Gunasekara)
* DOWNLOAD Java Code For This Lesson. (Conversion by Jeff Kirby)
* DOWNLOAD Jedi-SDL Code For This Lesson. (Conversion by Dominique Louis)
* DOWNLOAD Linux Code For This Lesson. (Conversion by Richard Campbell)
* DOWNLOAD Linux/GLX Code For This Lesson. (Conversion by Mihael Vrbanec)
* DOWNLOAD Linux/SDL Code For This Lesson. (Conversion by Ti Leggett)