Turning: Jeff Molofee (Nehe) OpenGL Tutorial - Fourth lesson

zhaozj2021-02-08  266

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 // windows header file #include // OpenGL32 library header file #include // Glu32 library header file #include // Glaux library header file hglrc hrc = null; // Permanent coloring description table HDC HDC = NULL; // Private GDI device description table hwnd hwnd = null; // Save our window handle Hinstance Hinstance; / / Save the program Bool Keys [256]; // The array of Bool Active = true; // window of the keyboard routine, the default is true bool fullscreen = true; / / full screen flag default setting Full-screen mode GLFLOAT RTRI; // Used in a triangular angle (new) GLFLOAT RQUAD; // Used for the angle of the four-shaped (new) then we modify the code of DrawGlscene (). The following code is the same as the previous lesson. INT Drawglscene (GLVOID) // This process includes all draw code {glclear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth cache GLLoadIndIndentity (); // Reset model observation matrix GLTranslateF (-1.5F, 0.0F, -6.0F); // Left shift 1.5 unit, and move into screen 6.0 Next line code is new. GLROTATEF (Angle, XVector, Yvector, Zvector) is responsible for rotating the object around a shaft. This command has a lot of use. Angle is usually a variable represents an angle of the object. XVector, YVector, and Zvector three parameters determine the direction of the rotating shaft. The vector described in (1, 0, 0) is at one unit of the X coordinate axis and is to the right. (-1, 0, 0) The vector described is at one unit of the X coordinate axis, but the direction is left. D. Michael Traub: The above explanation for XVector, YVector, and Zvector is provided. In order to better understand the rotation of x, y and z, I will give some examples ... X-axis - you are using a desk saw. The axis of the saw blade is placed from left to right (just like the X axis in OpenGL). The sharp serrated is rotated around the X axis, it looks forward, either turn down. Depending on the direction of the saw blade begins. This is the same as what we rotate around the X-axis in OpenGL. (Translator Note: This will make the face to make the face to the display, and make a sawing flower ^ - ^.) Y-axis - Suppose you are in a huge tornado center, pointing from the ground from the ground to the sky ( The y-axis in OpenGL). Garbage and fragments around the Y-axis from left to right or from right to left mad. This is the same as what we rotate around the Y axis in OpenGL. Z-axis - you look at a fan from the front. The center of the fan is just moving towards you (just like the Z axis in OpenGL). The blade of the fan is rotated around the Z axis or counterclockwise.

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.

转载请注明原文地址:https://www.9cbs.com/read-2227.html

New Post(0)