Row vector, column vector, row Primary Order Matrix, column Primary Order Matrix (row vector, column vector, row major-matrix, column-major matrix)

Source: Internet
Author: User
There are more than N versions on the Internet about this topic. Today, I will also talk about this topic.
(1) first, regardless of DX or OpenGL, the vectors and matrices expressed are defined according to the standard in Linear Algebra:
"The product of matrix A and B the element C (ij) in column J of row I of matrix C is equal to the sum of the product of the corresponding elements in column J of row I of." (Practical mathematics manual, Science Press, Second Edition)
For example, C12 = A11 * B11 + A12 * B21 + A12 * b13...

(2) After clarifying this, let's look at the "Storage Method of Matrices". There are two ways to store matrices. One is "Row-major order) /row priority ", and the other is" column Primary Order (column-Major Order)/column priority"
1) direct3d uses row-based primary storage

"Effect matrix parameters and HLSL matrix variables can define whether the value is a row-major or column-major matrix; however, the DirectX APIs always treat d3dmatrix and d3dxmatrix as row-major. "(see section d3d9 document/casting and conversion)
2) OpenGL uses primary column Storage
"The M parameter points to a 4x4 matrix of single-or double-precision floating-point values stored in column-major order. That is, the matrix is stored as follows"
(See the msdn glloadmatrixf API description)

The storage sequence illustrates how matrices in linear algebra are stored in linear memory arrays. d3d stores each row in an array by row, while OpenGL stores each column in each row of the array:
The same matrix of Linear Algebra has different storage sequence in d3d and OGL.
Line generation: A11, A12, A13, A14 d3d: A11, A12, A13, A14 GL: A11, A21, A31, a41
A21, A22, A23, A24 A21, A22, A23, A24 A12, A22, A32, a42
A31, A32, A33, A34 A31, A32, A33, A34 A13, A23, A33, A43
A41, a42, A43, A44 a41, a42, A43, A44 A14, A24, A34, A44

(3) matrix multiplication sequence and rules

The definition of matrix multiplication in linear algebra is definite. However, there are differences between left multiplication and right multiplication in different implementations, or "pre-multiply" or "post-multiply )"
This rule depends on the vector representation, that is, the row vector or the column vector. If it is a row vector, it is actually a row matrix. The "Row x column" that represents linear algebra is the forward multiplication. The same is true for matrix multiplication.
For example, in d3d,

D3d is a row vector, row-first storage, OpenGL is a column vector, and column-first storage. Although the same matrix is stored in d3d or OpenGL, the conversion results are the same,
Because OpenGL transforms a vector as a column vector and multiply each column of the same matrix to realize the same Transformation in linear algebra.

It is usually difficult to see the OpenGL transformation CoordinateCodeThe following code is from OpenGL source code. Let's take a look at the true colors of vertex transformation"

Void fastcall _ glxform3 (_ glcoord * res, const _ glfloat V [3], const _ glmatrix * m)
{
_ Glfloat x = V [0];
_ Glfloat y = V [1];
_ Glfloat z = V [2];

Res-> X = x * m-> matrix [0] [0] + y * m-> matrix [1] [0] + z * m-> matrix [2] [0]
+ M-> matrix [3] [0];
Res-> Y = x * m-> matrix [0] [1] + y * m-> matrix [1] [1] + z * m-> matrix [2] [1]
+ M-> matrix [3] [1];
Res-> Z = x * m-> matrix [0] [2] + y * m-> matrix [1] [2] + z * m-> matrix [2] [2]
+ M-> matrix [3] [2];
Res-> W = x * m-> matrix [0] [3] + y * m-> matrix [1] [3] + z * m-> matrix [2] [3]
+ M-> matrix [3] [3];
}

It can be seen that, as described above, "the OpenGL column vector is multiplied by each column of the Matrix, and it still represents the multiplication of the linear algebra row vector and each row of the matrix"
Let's take a look at the OpenGL Matrix Multiplication. "Use each column of A to multiply each row of B ".

/*
** Compute r = a * B, where R can equal B.
*/
void fastcall _ glmultmatrix (_ glmatrix * r, const _ glmatrix * a, const _ glmatrix * B)
{< br> _ glfloat B00, B01, B02, b03;
_ glfloat B10, B11, B12, B13;
_ glfloat B20, b21, B22, B23;
_ glfloat B30, b31, B32, B33;
glint I;

B00 = B-> matrix [0] [0]; B01 = B-> matrix [0] [1];
B02 = B-> matrix [0] [2]; b03 = B-> matrix [0] [3];
B10 = B-> matrix [1] [0]; B11 = B-> matrix [1] [1];
B12 = B-> matrix [1] [2]; B13 = B-> matrix [1] [3];
B20 = B-> matrix [2] [0]; B21 = B-> matrix [2] [1];
B22 = B-> matrix [2] [2]; B23 = B-> matrix [2] [3];
B30 = B-> matrix [3] [0]; b31 = B-> matrix [3] [1];
B32 = B-> matrix [3] [2]; B33 = B-> matrix [3] [3];

for (I = 0; I <4; I ++) {
r-> matrix [I] [0] = A-> matrix [I] [0] * B00 + A-> matrix [I] [1] * B10
+ A-> matrix [I] [2] * B20 + A-> matrix [I] [3] * B30;
r-> matrix [I] [1] = A-> matrix [I] [0] * B01 + A-> matrix [I] [1] * B11
+ A-> matrix [I] [2] * B21 + A-> matrix [I] [3] * b31;
r-> matrix [I] [2] = A-> matrix [I] [0] * B02 + A-> matrix [I] [1] * B12
+ A-> matrix [I] [2] * B22 + A-> matrix [I] [3] * B32;
r-> matrix [I] [3] = A-> matrix [I] [0] * b03 + A-> matrix [I] [1] * B13
+ A-> matrix [I] [2] * B23 + A-> matrix [I] [3] * B33;

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.