Principles of glloadidentity, gltranslatef, and glrotatef in OpenGL [Post]

Source: Internet
Author: User

Glloadidentity, gltranslatef, and glrotatef principles in OpenGL

 
The diagonal line of the unit matrix is 1, and the remaining elements are all 0 matrices.
In matrix multiplication, there is a matrix that plays a special role, just like 1 in the multiplication of numbers. We call this matrix A matrix of units.
It is a square matrix. All elements except the diagonal lines (called the main diagonal) from the upper left corner to the lower right corner are 0.

Coordinate in OpenGLHomogeneous coordinates(X, y, z) is expressed as (x', y', Z', h), where X = x'/h; y = y'/h; z = Z'/h. generally, H is set to 1. for example, the point (2, 3, 4) in a space is expressed as (2, 3, 4, 1) in OpenGL ). the homogeneous coordinate representation method is suitable for matrix operations and can easily represent infinitely distant points. For example, (1/0,) indicates infinitely distant points on the X axis, because is infinitely infinite, 0/0 = 0.

Next we will talk about the point matrix (linear algebra) knowledge. The translation, rotation, scaling, and other transformations in OpenGL are linear transformations expressed by matrix multiplication. Take translation transformation as an example. Please refer to the official description of the gltranslatef function. Suppose it is a bit (3, 3). If we move the point 2 units along the X axis, 3 units along the Y axis, and 4 units along the Z axis, the point will be (3 + 2, 3 + 3, 4 + 4) = (5, 6, 7 ). in a matrix:

The matrix on the left is called a translation transformation matrix. If we replace 2, 3, and 4 with X, Y, and Z, we use it to multiply the vectors represented by an homogeneous coordinate, this vector can be translated (x, y, z ). both rotation and scaling transformations are represented by a matrix like Translation Transformations. Here, you don't need to care about the length of these matrices. You only need to know how long they are multiplied by a vector represented by a homogeneous coordinate, so that the vector can undergo the necessary transformation.

Record the translation transformation matrix as T (x, y, z), and the Rotation Transformation Matrix as R (X, Y, Z, S), which indicates the round vector (x, y, z) rotate the S angle, and mark the vector as X. Here we only need to know that they are matrices. Now we need to move a vertex x, such as (3, 3, 3, 1), to (2, 2) Unit, and then rotate the angle of 30 degrees around the Y axis, it is represented by a matrix, that is, R (,) * t (, 2) * X. It can be understood that the matrix closest to X acts first. It is very important to understand this order. In this way, all transformations can be expressed by multiplying a matrix. This is also true in the computer.

After introducing the basic mathematical knowledge, the role and mechanism of OpenGL are described below. OpenGL has a transformation matrix stack, which is as advanced as a bullet clip. Every vector in OpenGL enters the OpenGL world after being defined. It must first multiply by the stack top transformation matrix of the transformation matrix stack. As shown in:

After understanding the above knowledge, it is much easier to understand what the glloadidentity, gltranslatef, and glrotatef functions do. These functions are the operations on this stack:

  • Gltranslatef: Right multiplication of T (x, y, z) and stack top transformation matrix. Assume that the current top-layer transformation matrix of the stack is m, which is equivalent to modifying m to M * t.
  • Glrotatef: Right multiplication of R (X, Y, Z, S) and top stack transformation matrix of stack.
  • Glloadidentity: sets the stack top transformation matrix of the stack to the unit matrix.
  • Glpushmatrix: Copies the top transform matrix of the stack and pushes it to the stack. The so-called push pushes a matrix into the stack just like a plug-in. At this time, the top of the stack is the new matrix. Note that all the defined vectors work with the top of the stack transformation matrix.
  • Glpopmatrix: pop the top transform matrix of the stack.

The lecture is over. Let's have a few exercises.

1. the OpenGL code is: glloadidentity (); gltranslate3f (, 1); glrotate3f (,); glvertex3f (, 1); what is the top transform matrix of the stack? (, 1) What is the point in the OpenGL world?

A: The top transform matrix of the stack is T (, 1) * R (,), and the coordinates from (, 1) to OpenGL are T (, 1) * R (,) * (, 1 ).

2. Why can the combination of glpushmatrix and glpopmatrix isolate the transformations in these two functions so that they do not affect the following points?

A: The New Transformation Matrix of glpushmatrix is a copy of the original stack top transformation matrix, so it inherits the previous transformation. When you execute the gltranslatef and glrotatef functions, the top-stack transformation matrix is modified. All vertices before glpopmatrix are affected by the top-stack transformation matrix. Then, glpopmatrix is used to pop the top-stack transformation matrix, at this time, the stack top transformation matrix is restored to the original stack top transformation matrix.

3. Why sometimes gltranslate3f and glrotate3f can be reversed and sometimes cannot?

A: matrix A multiplied by matrix B may not be equal to matrix B multiplied by matrix A. Many of them are just coincidence when they are equal.

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.