Lesson 2 OpenGL Rotation

Source: Internet
Author: User

Rotation:

In this lesson, I will teach you how to rotate triangles and quadrilateral. The triangle in the left graph rotates along the Y axis, and the Quadrilateral rotates along the X axis.

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.

Glfloat rtri; // used for the angle of the triangle glfloat rquad; // used for the angle of the Quadrilateral

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 the depth cache glloadidentity (); // reset the model observation matrix gltranslatef (-1.5f, 0.0f,-6.0f); // shift the value 1.5 to the left and move it to the screen 6.0

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: Provides the preceding explanations for xvector, yvector, and zvector.
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. (Note: If you want to bring your face together to the display, you must be cut off ^-^ .)

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

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); // glcolor3f (0.0f, 0.0f, 1.0f); // set the current color to Blue glvertex3f (1.0f,-1.0f, 0.0f); // glend () in the lower right corner; // The triangle is drawn.

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 (); // reset the model observation matrix gltranslatef (1.5f, 0.0f,-6.0f); // shift 1.5 units to the right and move to the screen 6.0 glrotatef (rquad, 1.0f, 0.0f, 0.0f); // rotate the Quadrilateral around the X axis

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 glbegin (gl_quads) at a time; // draw a square glvertex3f (-1.0f, 1.0f, 0.0f ); // upper left glvertex3f (1.0f, 1.0f, 0.0f); // upper right glvertex3f (1.0f,-1.0f, 0.0f); // lower left glvertex3f (-1.0f,-1.0f, 0.0f ); // glend () in the lower right corner; // 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 rquad of the triangle-= 0.15f; // decrease the rotation variable return true; // continue to run}

Finally, change the title content in window mode.

// Recreate the OpenGL window if (! Createglwindow ("nehe's rotating instance", 640,480, 16, fullscreen ))

 

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 email 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.

 

Lesson 2 OpenGL Rotation

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.