OpenGL tutorial Rotation

Source: Internet
Author: User

Original article: Lesson 4: Rotation
Translated by: cker

In the previous lesson, I will teach you how to color triangles and quadrilateral. This lesson will teach you how to rotate these color objects around the axis.
In fact, you only need to add a few lines to the code of the previous lesson. Next I will rewrite the entire routine. So that you can know what is added and what is modified. We 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; the following two rows ). They are floating-point variables that allow us to rotate objects very accurately. Floating Point Numbers include decimal places, which means we do not need to use the angle of 1, 2, 3. You will find that floating point numbers are the basis of OpenGL programming. The new variable rtri is used to rotate the triangle, and rquad rotates the Quadrilateral.

# Include <windows. h> // windows header file
# Include <Gl/Gl. h> // header file of the opengl32 Library
# Include <Gl/Glu. h> // header file of the glu32 Library
# Include <Gl/Glaux. h> // header file of the Glaux Library

Hglrc HRC = NULL; // permanent coloring description table
HDC = NULL; // Private GDI device description table
Hwnd = NULL; // save our window handle
Hinstance; // The instance that saves the program

Bool keys [256]; // Array Used for keyboard routines
Bool active = true; // The activity flag of the window. The default value is true.
Bool fullscreen = true; // The full screen flag is set to full screen by default.
Glfloat rtri; // used for the triangle angle (new)
Glfloat rquad; // used for the Quadrilateral angle (new)

Next we modify the code of drawglscene. The following code is the same as that in the previous lesson.

Int drawglscene (glvoid) // This process includes all the drawing code
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and depth Cache
Glloadidentity (); // resets the model observation matrix.
Gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units left and 6.0 to the screen

The next line of code is new. Glrotatef (angle, xvector, yvector, zvector) is responsible for rotating objects around a certain axis. This command is useful. Angle is usually a variable that represents the angle from which the object is switched. The xvector, yvector, and zvector parameters jointly determine the direction of the rotation axis. For example, the vector described in (, 0) goes through one unit of the X axis and is directed to the right. (-, 0) the vector is directed to the left when it passes through one unit of the X axis. D. Michael Traub Note: The preceding explanations of xvector, yvector, and zvector are provided. To better understand the rotation of X, Y, and Z, I will give some examples...
X axis-you are using a saw. The axis at the center of the saw blade is placed from left to right (like the X axis in OpenGL ). The sharp sawtooth turns around the X axis. It looks either up or down. It depends on the direction when the saw blade starts to rotate. This is the same as rotating around the X axis in OpenGL.
Y axis-assuming you are in a huge tornado center, the center of the tornado points from the ground to the Sky (like the Y axis in OpenGL ). Garbage and fragments go from left to right or from right to left around the Y axis. This is the same as what we rotate around the Y axis in OpenGL.
Z axis-you are looking at a fan from the front. The center of the fan is right toward you (like the Z axis in OpenGL ). The fan blade rotates clockwise or counterclockwise around the Z axis. 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 7 from left to right around the Y axis. You can also change the parameter value so that the triangle rotates around the X and Y axes at the same time.

Glrotatef (rtri, 0.0f, 1.0f, 0.0f); // rotate the triangle around the Y axis (new)

The following code has not changed. Draw a color gradient triangle on the left of the screen and rotate it from left to right around the Y axis.

Glbegin (gl_triangles); // draw a triangle
Glcolor3f (1.0f, 0.0f, 0.0f); // set the current color to red.
Glvertex3f (0.0f, 1.0f, 0.0f); // top Vertex
Glcolor3f (0.0f, 1.0f, 0.0f); // set the current color to green.
Glvertex3f (-1.0f,-1.0f, 0.0f); // bottom left
Glcolor3f (0.0f, 0.0f, 1.0f); // set the current color to blue.
Glvertex3f (1.0f,-1.0f, 0.0f); // bottom right
Glend (); // draw the triangle

You will note that the following code adds another glloadidentity () call. The purpose is to reset the model observation matrix. If we call gltranslate without resetting, unexpected results will appear. As the axis has been rotated, it is likely that it is not in the desired direction. Therefore, if we want to move objects between the left and right, it may become up and down, depending on how much angle you have rotated the coordinate axis. What will happen after you comment out glloadidentity.
After the model observation matrix is reset, X, Y, and Z axes are reset. We call gltranslate. You will notice that this time we only set 1.5 units to the right, instead of 3.0 units in the last lesson. When we reset the scenario, the focus goes back to the center of the scenario (0.0 points ). In this way, you only need to move 1.5 units to the right.
When we move to a new position, we rotate the Quadrilateral around the X axis. The square turns up and down.

Glloadidentity (); // resets the model observation matrix.
Gltranslatef (1.5f, 0.0f,-6.0f); // move 1.5 units to the right and 6.0
Glrotatef (rquad, 1.0f, 0.0f, 0.0f); // rotate the Quadrilateral around the X axis (new)

The next code segment remains unchanged. Draw a blue square on the right of the screen.

Glcolor3f (0.5f, 0.5f, 1.0f); // set the current color to blue at a time.
Glbegin (gl_quads); // draw a square
Glvertex3f (-1.0f, 1.0f, 0.0f); // top left
Glvertex3f (1.0f, 1.0f, 0.0f); // upper right
Glvertex3f (1.0f,-1.0f, 0.0f); // bottom left
Glvertex3f (-1.0f,-1.0f, 0.0f); // bottom right
Glend (); // draw the square

The next two rows are added. If we think of rtri and rquad as containers, we have created containers (glfloat rtri and glfloat rquad) at the beginning of the program ). After the container is created, it is empty. The first line of code below is to add 0.2 to the container. Therefore, every time we run the previous code, the value in the rtri container will increase by 0.2 here. The value in the rquad container is reduced by 0.15 in the next line. Similarly, every time we run the previous code, the rquad container's value is reduced by 0.15 here. The decline will eventually lead to the opposite direction of object rotation and growth. Try to change the plus (+) and minus (-) in the following code to see how the rotation direction of an object changes. And try to change 0.2 to 1.0. The larger the number, the faster the object is. The smaller the number, the slower the object is.

Rtri + = 0.2f; // increase the rotation variable of the triangle (new)
Rquad-= 0.15f; // reduce the rotation variable of the Quadrilateral (new)
Return true; // continue running
}

Finally, change the title content in window mode.

If (Keys [vk_f1]) // is the F1 key pressed?
{
Keys [vk_f1] = false; // If yes, set the value in the corresponding key array to false.
Killglwindow (); // destroy the current window
Fullscreen =! Fullscreen; // switch to full screen/window mode
// Recreate the OpenGL window (modify)
If (! Createglwindow ("nehe's rotation tutorial ",
640,480, 16, fullscreen ))
{
Return 0; // exit the program if the window cannot be created
}
}

In this lesson, I try to explain in detail how to make the object rotate around a certain axis. Modify the code and try to rotate the object around the Z axis, X + Y axis, or all three axes. If you have any comments or suggestions, please write to me. If you think there is something wrong or can be improved, please let me know. I want to do the best OpenGL tutorial and be interested in your feedback.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lovetangtang/archive/2006/01/16/580536.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.