OpenGL Study Notes (4) -- rotate

Source: Internet
Author: User

2011-04-13 wcdj

 

How to rotate these color objects around the coordinate axis.

Add two variables to control the rotation of the 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 triangle angle <br/> glfloat rquad; // used for the Quadrilateral Angle

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

Int drawglscene (glvoid) // start all the painting here <br/>{< br/> glclear (gl_color_buffer_bit | gl_depth_buffer_bit ); // clear the screen and deep cache <br/> glloadidentity (); // reset the current model observation matrix <br/> /*************************** * **************************/<br/> // Add an object <br/> gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units to the left and 6.0 <br/> glrotatef (rtri, 0.0f, 1.0f, 0.0f ); // rotate the triangle around the Y axis </P> <p> glbegin (gl_triangles); // draw the triangle <br/> glcolor3f (1.0f, 0.0f, 0.0f ); // set the current color to Red <br/> glvertex3f (0.0f, 1.0f, 0.0f); // top vertex <br/> glcolor3f (0.0f, 1.0f, 0.0f ); // set the current color to green <br/> glvertex3f (-1.0f,-1.0f, 0.0f); // bottom left <br/> glcolor3f (0.0f, 0.0f, 1.0f ); // set the current color to Blue <br/> glvertex3f (1.0f,-1.0f, 0.0f); // bottom right <br/> glend (); // draw the triangle </P> <p> glloadidentity (); // reset the model observation matrix <br/> gltranslatef (1.5f, 0.0f,-6.0f ); // shift 1.5 units to the right and move the screen to 6.0 <br/> // gltranslatef (3.0f, 0.0f, 0.0f); // Shift 3 units to the right <br/> glrotatef (rquad, 1.0f, 0.0f, 0.0f); // rotate the Quadrilateral around the X axis </P> <p> glbegin (gl_quads); // draw a square <br/> glcolor3f (0.5f, 0.5f, 1.0f ); // set the current color to blue at a time <br/> glvertex3f (-1.0f, 1.0f, 0.0f); // upper left <br/> glvertex3f (1.0f, 1.0f, 0.0f ); // upper right <br/> glvertex3f (1.0f,-1.0f, 0.0f); // lower right <br/> glvertex3f (-1.0f,-1.0f, 0.0f ); // bottom left <br/> glend (); // draw the square to the end <br/> rtri + = 0.2f; // Add the rotation variable of the triangle <br/> rquad-= 0.15f; // reduce the rotation variable of the Quadrilateral </P> <p> /************************* * ****************************/<br/> return true; <br/>}

 

Refer:

Http://nehe.gamedev.net/data/lessons/lesson.asp? Lesson = 04

 

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.