Jeff Molofee (nehe) OpenGL Tutorial Fourth lesson Translated by CKER I taught you the triangle and quadrilateral coloring. This lesson I will teach you how to rotate these colored objects around the coordinate axis. In fact, it is only necessary to add a few lines on the code from the previous class. Below I will rewrite the entire routine. Convenient you know what has increased and modified. We increase two variables to control the rotation of these two objects. These two variables are added to the other variables behind the beginning of the program (Bool Fullscreen = True; the next two lines). They are floating point types of variables that we can rotate the object very accurately. The floating point number contains a decimal location, which means that we do not need to use the angle of 1, 2, 3 ... You will find that the floating point is the foundation of OpenGL programming. The new variable is called RTRI to rotate the triangle, and the RQUAD rotates the quadrilateral. #include
This is the same as what we rotate around the Z axis in OpenGL. In the following line of code, if Rtri is equal to 7, we rotate the triangle from left to the right from left to the left. You can also change the value of the parameters, let the triangle rotate around the X and Y axes. GLROTATEF (RTRI, 0.0F, 1.0F, 0.0F); // There is no change in the following code about the Y-axis rotating triangle (new). A color gradient triangle is painted on the left side of the screen and rotates from left to the right around the Y axis. Glbegin (GL_TRIANGLES); // Draw triangle Glcolor3f (1.0F, 0.0F, 0.0F); // Set the current color to red GlvertEX3F (0.0F, 1.0F, 0.0F); // On the top Glcolor3f (0.0F, 1.0 f, 0.0f); // Set the current color to green glvertex3f (-1.0F, -1.0F, 0.0F); // Left Glcolor3f (0.0F, 0.0F, 1.0F); // Set the current color to blue Glvertex3f (1.0F, -1.0F, 0.0F); // Right glend (); // Triangle Drawing End You will pay attention to the following code we add another GLLoadIndentity () call. The purpose is to reset the model observation matrix. If we don't reset, call GLTRANSLATE directly, the result is expected. Because the coordinate axis has been rotated, it is likely that there is no direction you want. So we originally wanted to move objects left and right, it might be able to move up and down, depending on how much angle you rotate the coordinate axis. Try out what results will appear after commenting with GLLOADIDENTITY (). After resetting the model observation of the matrix, the x, y, the z axis are reset, we call GLTranslate. You will notice that this time we only have 1.5 units, not 3.0 units of the last class. Because we reset the scene, the focus returned to the center of the scene (0.0). This will only need to move the 1.5 unit to the right. When we move to a new position, rotate the quadrilateral around the X-axis. Squares will rotate up and down. GLLoadIdentity (); // Reset model observation matrix GLTRANSLATEF (1.5F, 0.0F, -6.0F); // Right shift 1.5 unit, and move into the screen 6.0 GLROTATEF (RQUAD, 1.0F, 0.0F, 0.0F); / / Rotate the quadrilateral (new) around the X-axis remains unchanged. Painting a blue square on the right side of the screen. Glcolor3f (0.5F, 0.5F, 1.0F); // Disposable When the current color is set to blue glbegin (GL_QUADS); // Draw square GlvertEX3F (-1.0F, 1.0F, 0.0F); // On the left GlvertEX3F ( 1.0F, 1.0F, 0.0F); // On the upper right glvertex3f (1.0F, -1.0F, 0.0F); // The lower left GlvertEX3F (-1.0F, -1.0F, 0.0F); // Right glend () ; // The two lines of the square drawn are added. If RTRI and RQUAD imagination are containers, then in the start of the program we created a container (GLFLOAT RTRI, and GLFLOAT RQUAD). After the container is created, it is empty. The following first line of code is to add 0.2 to the container. So each time we run the previous code, you will increase the value in the RTRI container in the RTRI container. The rear line reduces the value of the RQUAD container by 0.15. Also every time we run the previous code, you will fall here to the value of the RQUAD container falls 0.15. The decline will eventually lead to the opposite direction of the direction of the rotation of the object. Try changing the and -, how to change the direction of the rotation of the object. I tried to change 0.2 to 1.0. The larger the number, the faster the object is turned, the smaller this number, the slower the object is transferred.
RTRI = 0.2f; // Increase triangle rotation variable (new) RQUAD- = 0.15F; // Reduce quadrilateral rotation modulus (new) return true; // Continue to run} Last to window mode The title in window mode . IF (Keys [vk_f1]) // F1 button presses? {keys [vk_f1] = false; // ! fullscreen; // Switch full screen / window mode // Rebuild the OpenGL window (Modify) if (! CreateGLWindow ("Nehe's Rotation Tutorial", 640, 480, 16, fullscreen) {return 0; // If the window is not created, the program exits }} In this lesson, I tried to explain how to explain how to turn the object around a axis. Changing the code, try turning the object around the Z axis, the X Y axis or all three axes :). If you have any comments or suggestions, please give me email. If you think there is something wrong or improve, please tell me. I want to be the best OpenGL tutorial and interested in your feedback. {Translator: NEHE's document seems very simple, it seems very much. But if you want to come to such a master, have you seen a few? Or that sentence, I am not a master, I hope you are sincere. } The following is the source code download link.