The previous section taught you how to color triangles and quadrilaterals. This lesson will teach you how to rotate these colored objects around their coordinate axes.
In fact, just add a few lines to the code in the previous section. Let's rewrite the entire routine again. It is convenient for you to know what has been added and what has been modified.
Add two variables to control the rotation of these two objects. These two variables are added after the other variables at the beginning of the program (bool fullscreen=TRUE; two lines below). They are floating point type variables that allow us to rotate objects very precisely. Floating point numbers contain decimal places, which means we don't need to use angles of 1, 2, 3... You will find that floating point numbers are the basis of OpenGL programming. The new variable called rtri is used to rotate the triangle, and rquad is used to rotate the quadrilateral.
Var
…
rtri : GLfloat; // Angle for triangle (new)
rquad : GLfloat; // Angle for quadrilateral (new)
…
Then modify glDraw() as follows:
PRocedure glDraw();
Begin
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer
glLoadIdentity(); //Reset the current model observation matrix
//The next line of code is new. glRotatef(Angle,Xvector,Yvector,Zvector) is responsible for rotating the object around a certain axis.
//This command has many uses. Angle is usually a variable representing the angle through which the object is rotated.
//The three parameters Xvector, Yvector and Zvector jointly determine the direction of the rotation axis.
//For example, the vector described by (1,0,0) passes through 1 unit of the X coordinate axis and is directed to the right.
//The vector described by (-1,0,0) passes through 1 unit of the X coordinate axis, but in the left direction.
//D. Michael Traub: Provides the above explanation of Xvector, Yvector and Zvector.
//To better understand the rotation of X, Y and Z, let me give some examples...
//X-Axis - You are using a table saw. The axis at the center of the blade is placed from left to right (just like the X-axis in OpenGL).
//The sharp saw teeth rotate wildly around the X-axis, seemingly turning upward or downward.
//Depends on the direction in which the saw blade starts to turn. This is the same situation as when we rotate something around the X axis in OpenGL.
//(Translator's note: If you put your face towards the monitor at this moment, you will definitely be sawed open^-^.)
//Y-axis - Let's say you are in the center of a huge tornado, with the center of the tornado pointing from the ground to the sky (just like the Y-axis in OpenGL).
//Garbage and debris rotate wildly around the Y-axis from left to right or from right to left.
//This is the same situation as when we rotate something around the Y axis in OpenGL.
//Z-Axis - You are looking at a fan from straight ahead. The center of the fan is pointed right towards you (like the Z-axis in OpenGL).
//The fan blades rotate wildly clockwise or counterclockwise around the Z-axis. This is the same situation as when we rotate something around the Z axis in OpenGL.
glTranslatef(-1.5, 0.0, -6.0); // Move left 1.5 units and into screen 6.0
//In the following line of code, if rtri is equal to 7, we rotate the triangle by 7 from left to right around the Y axis.
//You can also change the value of the parameter to rotate the triangle around the X and Y axes simultaneously.
glRotatef(rtri, 0.0, 1.0, 0.0); // Rotate the triangle around the Y axis (new)
//There are no changes to the code below. A colored gradient triangle is drawn on the left side of the screen and rotated from left to right around the Y axis.
glBegin(GL_TRIANGLES); // Draw triangles
glColor3f(1.0, 0.0, 0.0); //Set the current color to red
glVertex3f(0.0, 1.0, 0.0); // Upper vertex
glColor3f(0.0, 1.0, 0.0); //Set the current color to green
glVertex3f(-1.0, -1.0, 0.0); // Lower left
glColor3f(0.0, 0.0, 1.0); //Set the current color to blue
glVertex3f(1.0, -1.0, 0.0); // Lower right
glEnd(); // End of triangle drawing
//You'll notice in the code below that we've added another glLoadIdentity() call.
//The purpose is to reset the model observation matrix.
//If we do not reset and call glTranslate directly, unexpected results will occur.
//Because the coordinate axis has been rotated, it is probably not in the direction you want.
//So what we originally wanted to move the object left and right may become up and down, depending on how much you rotate the coordinate axis.
//Try to comment out glLoadIdentity() and see what results will appear.
//After resetting the model observation matrix, the X, Y, and Z axes are all reset, and we call glTranslate.
//You'll notice that this time we only moved 1.5 units to the right instead of 3.0 units like last lesson.
//Because when we reset the scene, the focus returned to the center of the scene (at 0.0).
//This way you only need to move 1.5 units to the right.
//When we move to the new position, rotate the quad around the X axis. The square will rotate up and down.
glLoadIdentity(); //Reset model observation matrix
glTranslatef(1.5, 0.0, -6.0); // Move right 1.5 units and move into screen 6.0
glRotatef(rquad, 1.0, 0.0, 0.0); // Rotate the quadrilateral around the X axis (new)
glBegin(GL_QUADS); // Draw a square
glColor3f(0.6, 0.2, 2.0); //Set the current color to purple
glVertex3f(-1.0, 1.0, 0.0); // Upper left
glVertex3f(1.0, 1.0, 0.0); // Upper right
glVertex3f(1.0, -1.0, 0.0); // Lower left
glVertex3f(-1.0, -1.0, 0.0); // Lower right
glEnd(); // End of square drawing
//The next two lines are new.
//If rtri and rquad are imagined as containers,
//Then at the beginning of the program we created containers (rtri, and rquad).
//When the container is created, it is empty.
//The first line of code below adds 0.2 to the container.
//So every time we run the previous code, the value in the rtri container will be increased by 0.2 here.
//The following line reduces the value in the rquad container by 0.15.
//Similarly, every time we run the previous code, the value in the rquad container will drop by 0.15 here.
//Falling will eventually cause the object to rotate in the opposite direction to the direction of growth.
rtri := rtri + 0.2; // Increase the rotation variable of the triangle (new)
rquad := rquad - 0.15; // Reduce the rotation variable of the quadrilateral (new)
End;