About OpenGL and osg matrices (RPM)

Source: Internet
Author: User

About the matrix of OpenGL and OSG

Matrix is really a very magical mathematical tool, although purely from a mathematical point of view, it does not have any special significance, but once used in the space of the coordinate transformation, it will "Meet the wind and wind into a dragon", a great show of divinity. A simple tool that implements complex functions is indicative of the need to take some effort to understand it. Here's a quick introduction to the transformation matrix in OpenGL.

1 principle of the transformation matrix
The transformation matrix in OpenGL is defined in this way:
[Xx, Yx, Zx, Tx]
[Xy, Yy, Zy, Ty]
M = [Xz, Yz, Zz, Tz]
[0, 0, 0, 1]

In fact, we can understand this transformation matrix, which represents a local coordinate system, which is to move the origin of the World coordinate system (TX, Ty,tz), the x-axis (Xx, Xy, Xz), the y-axis (Yx, Yy, Yz), z-axis to (Zx, Zy,zz) and formed. Using it to transform the point V in a world coordinate system is to get the point in this local coordinate system.
To prove this is easy, we can think of it from a more general point of view, assuming we use the matrix Ma to represent the coordinate system a, MB to represent the coordinate system b,mt representing the conversion from A to B, then:
Mt * Ma = Mb
Mt * ma * (MA) ^-1 = Mb * (MA) ^-1
Although the matrix does not conform to the multiplicative commutative law, it conforms to the multiplicative binding law, and thus:
Mt* (MA * (MA) ^-1) = Mb * (MA) ^-1
Mt = Mb * (Ma) ^-1
This is the expression for the A to B transformation matrix, and now we are converting from the world coordinate system to the local coordinate system, and the world coordinate system represented by a is a unit matrix, so:
Mt = Mb
That is, the matrix representation of the local coordinate system is the transformation matrix from the world coordinate system to the local coordinate system.

We further analyze that if we use this matrix to transform a point V (Vx, Vy, Vz, 1), we need to put this point right-multiply transformation matrix

[Xx, Yx, Zx, Tx] [Vx]
[Xy, Yy, Zy, Ty] [Vy]
V ' = m*t= [Xz, Yz, Zz, Tz] * [Vz]
[0, 0, 0, 1] [1]

For the X component of the V-transform, Vx ' = xx*vx + Yx*vy + zx*vz +tx, we can find that the X component that affects v is the X-component and the X-components of the rotation of the z-axis, and for the Y of V the same is true for the z component.

2 row main sequence, column main sequence
OpenGL It is recommended to use a one-dimensional array to represent this transformation matrix: typedef glfloat MATRIX16[16];
In order to quickly access the x-axis, y-axis, and z-axis, the array is represented by the column main sequence:
[M0, M4, M8, M12]
[M1, M5, M9, M13]
[m2, M6, M10,m14]
[M3, M7, m11,m15]< br> so, in order to access the x-axis, that is, to access M0, M1, M2, because they are contiguous storage space, so the speed is faster, on the contrary, if we array in line main order to represent this matrix:
[M0, M1, M2, M3]
[M4, M5, M6, M7]
[M8, M9, M10, M11]
[M12, M13, M14, M15]
we found that in order to access the x-axis, M0, M4, M8, it was a discontinuous address, so the speed slowed down.
So we can see why OpenGL uses the matrix of the column main sequence, because it defines a transformation matrix that, if stored in an array by column main order, can have a faster access to the X, Y, Z axis. That is, if I have to put this matrix in the form of the main sequence of the array can also be, but the speed is a bit slow. (Of course, we're going to tell OpenGL that we're in the main sequence of lines).

In fact, if we were to represent the transformation matrix in a different way:
[Xx, Xy, Xz, 0]
[Yx, Yy, Yz, 0]
M ' = [Zx, Zy, Zz, 0]
[Tx, Ty, Tz, 1]

This matrix is the transpose of the previous transformation matrix, and it is a good deal to put this matrix into an array by the main sequence of rows. The reason is obvious, in order to quickly access the x-axis, we want xx,xy, XZ is continuous storage, then naturally to row storage.

In fact, if I were to design OpenGL, I would choose to represent the transformation matrix in the second way, for the following reasons:
If I want to convert a point V, followed by the conversion of three transformation matrices L, M, N, then for the first way:
V ' = N (m* (l*v)) = (n*m*l) * V
Our combinatorial transformation matrix is n*m*l, just the opposite of the conversion process we defined, but if we were to represent it in the second way, I converted a point to the left by the transformation matrix instead of right-multiplying:
V ' = ((v*l) *m) *n = V * (l*m*n)
The combination transformation matrix is in the order of our transformation, it is more intuitive, and then we store this matrix by the main sequence of the row, the speed is still.

32-D array Storage matrix
Many people have the mistaken understanding that if you use a two-dimensional array to represent the transformation matrix in OpenGL, the speed is slower, and this understanding is more or less derived from the <<opengl Super Treasure > Description of the >. But is that the truth? The
two-dimensional array is as follows:
typedef glfloat MATRIX44[4][4];

The logical two-dimensional array, as we understand it, is represented as:
[M00, M01, M02, M03]
[M10, M11, M12, M13]
[M20, M21, M22, M23]
[M30, M31, M32, m33]< br> because of this logical model, it leads us to the mistaken understanding that the
X-axis is represented by M00, M10, M20, and they are discontinuous, so slower, but this is just its logical model, and if it is understood by a logical model, the logical model of a one-dimensional array is:
[M0, M1 , M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14,m15]
then can we say that one-dimensional arrays simply cannot be used to represent matrices? Of course not.
In fact, whether it is a one-dimensional array or a two-dimensional array, its physical model in memory is 16 consecutive float-type memory units:
One-dimensional arrays: [M0, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15]
Two-dimensional array: [M00, M01, M02, M03, M10, M11, M12, M13, M20, M21, M22, M23,m30, M31, M32, M33]
see here, since a one-dimensional array can be represented in the column main order and Soon, why is the two-dimensional array unhappy? They don't have the same name except for the visit:
[M00, M10, M20, M30]
[M01, M11, M21, M31]
[M02, M12, M22, M32]
[M03, M13, M23, m33] we can see that the transformation matrix represented by a two-dimensional array in the main sequence of columns is such that access to the x-axis is accessed m00, M01, M02, continuous, as fast.
Just, this representation and we understand the logical model of the two-dimensional array is not very uniform, some is not intuitive. This is true in OpenGL Red Book: The element of a two-dimensional array m[i][j] will be located in column I of the OpenGL transformation matrix, line J, and therefore prone to confusion of rows and columns, in order to avoid confusion, it is recommended to use a one-dimensional array representation. The real reason is to avoid confusion of rows, not speed.

find OSG the use of storage and matrix transformations for matrices is somewhat inconsistent with OpenGL usage:

1. When using Glmultmatrix/glloadmatrix to set a matrix in OpenGL, the parameter matrix needs to be stored in the column main order, whereas the matrix (MATRIXD) in OSG is stored in the row main order ( The Glmultmatrix/glloadmatrix is still used to set the Matrix ), and the two are transpose each other.

2. In red Book, OpenGL applies a matrix transformation to vertex coordinates, which should be the left multiplicative matrix (v ' = MXV); However, when I calculate the viewport coordinates of the vertex coordinate projection in OSG, I need to use right multiplication to get the correct result. The code for calculating the cropped coordinates is as follows:
 

OSG::MATRIXMATMVP =view->getcamera ()->getviewmatrix () *view->getcamera ()->getprojectionmatrix ();
V =matmvp.premult (v); Right multiplication matrix
That is, the matrix transformation in OpenGL is: Matrix x column Vector
The matrix transformation in OSG is: row vector x matrix
If for the same vector V (x,y,z,w) (can be used as a row vector can also be used as a column vector), the same matrix transform m, using the left multiplication and the use of the right multiply the results are obviously different;

Considering the above two points alone, it seems to be incomprehensible.
But consider the results of the following scenarios:
[X,Y,Z,W]X[M0,M1,M2...M15] (row main order) and [M0,M1,M2...M15] (column main order) X[X,Y,Z,W]
(or writing: Vxm and MTXV (MT for M's transpose))

About OpenGL and osg matrices (RPM)

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.