Translate, rotate, scale matrix stack operations in OpenGL

Source: Internet
Author: User

In OpenGL, the geometric transformations of the elements are linear transformations, which are realized by matrix transformation. The coordinates in OpenGL are expressed in homogeneous coordinates, i.e. (x, y, z), in which x=x '/h; Y=y '/h; Z=z '/h. Usually h takes 1. such as the point in Space (2,3,4), in OpenGL will be expressed as (2,3,4,1). The homogeneous coordinate representation is suitable for the matrix operation, it is also very convenient to represent the point of infinity, for example (1,0,0,0) represents infinity on the x axis point, because 1/0 is infinity, here the agreement 0/0=0.

Example: Point (1,1,1) shifts the vector (2,3,4) to a unit (3,4,5)

The matrix is represented as

1 0 0 2 1 3

0 1 0 3 * 1 = 4

0 0 1 4 1 5

The matrix on the left is a translation transformation matrix, and if you change 2, 3, and 4 to X, Y, and z, multiply it by a vector represented by a homogeneous coordinate, you can shift the vector (x, y, z) units. The translation transformation matrix is recorded as t (x, y, z) and the rotation transformation matrix is R (x,y,z,w).

Cases:

Gltranslatef (50.0, 0.0, 0.0)//Shift 50 in the positive direction of X.

GLROTATE3F (90,0.0,0.0,1.0)//rotate counterclockwise 90 ° along the x-axis.

The matrix is represented as T (50,0,0) *r (1,0,0,90) *x.

OpenGL has a matrix stack, which is advanced, and the matrix closest to X is the first to function.

voidMydisplay (void) {Glmatrixmode (gl_projection);    Glloadidentity (); Gluortho2d (-500.0,500.0, -500.0,500.0); Glclearcolor (1.0,1.0,1.0,0.0);    Glclear (Gl_color_buffer_bit);    Glmatrixmode (Gl_modelview);    Glloadidentity (); glcolor3f (0.0,0.0,0.0);    Glbegin (Gl_lines); GLVERTEX2F (-500.0,0.0); GLVERTEX2F (500.0,0.0); GLVERTEX2F (0.0, -500.0); GLVERTEX2F (0.0,500.0);    Glend (); glcolor3f (1.0,0.0,0.0); Glrecti ( -, -, $, Max);//Initial Rectangleglcolor3f (0.0,1.0,0.0); Gltranslatef (50.0,50.0,0.0);//translationGlrecti ( -, -, $, Max); //Reset the current Modelview matrixGlloadidentity ();//Resets the current matrix to the unit arrayGLCOLOR3F (0.0,0.0,1.0); Glrotatef (90.0,0.0,0.0,1.0);//Rotation counter-clockwiseGlrecti ( -, -, $, Max); //Reset the current Modelview matrixglloadidentity (); glcolor3f (1.0,1.0,0.0); Gltranslatef (-100.0,0.0,0.0);//translationGlscalef (0.5,1.0,1.0);//Scale all x-coordinates down by half .Glrecti ( -, -, $, Max); Glflush ();}

Add:

In OpenGL, the function Gltranslatef is to move the origin of the coordinates.  The corresponding 3 parameters correspond to 3 axes.  If you call Gltranslatef (1.0f,0.0f,0.0f) and draw a small ball, then call Gltranslatef (0.0f,1.0f,0.0f) and draw a small ball.  At this point, the two balls, one on the other right. So, if you want to have two balls in the X and Y axes, you need to call the Glloadidentity () function before the second draw to return the coordinate origin to the position.

Translate, rotate, scale matrix stack operations in OpenGL

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.