First, understanding matrix

  • Suppose there is a point in space and describe its position using xyz. Now, after we rotate it around any position by a certain Angle, we need to know the new position of this point, and we need to calculate it by a matrix.
  • Because the x of the new position depends not only on the original x but also on the rotation parameters, and even on the y and z coordinates.
  • It makes sense for a matrix to have only one row or one column. Only one row or column of numbers can be called a vector. You can also call it a matrix.
  • So in OpenGL, matrices are ordered primarily by columns.

In other programming standards, many matrix libraries define a matrix using a two-dimensional array. In The conventions of OpenGL, you can use one-dimensional arrays and two-dimensional arrays, but the preference is to use one-dimensional arrays. The reason for this is that OpenGL uses the column-Major matrix sorting convention

Vectors: Look at the vectors in the previous article.

** matrix: ** Let’s look at the definition given by Baidu:

Square matrix: A matrix with the same number of rows and columns. For example, 2 x 2, 3 x 3, 4 x 4 matrices.

** Row matrix, column matrix: ** is defined according to different programming, is a priority order

** element matrix: ** a matrix in which all the diagonal elements are 1 and all the other elements are 0. Or you could view it as 1.

Initializing a matrix in OpenGL

// Typedef float M3DMatrix33f[9]; // Typedef float M3DMatrix44f[16];Copy the code

There are three ways to initialize the element matrix:

GLFloat m[] = {1,0,0,0, //X Column 0,1,0,0, //Y Column 0,0,1,0, M3DMatrix44f m = {1,0,0,0, //X Column 0,1,0,0, //Y Column 0,0,1,0, Void m3dLoadIdentity44f(m3dLoadIdentity44f m); //Z Column 0,0,0,1 // Translation}Copy the code

How does a 4 x 4 matrix represent a position and direction in 3D space?

  • The diagram below. The column vectors are specially labeled: the last row of the matrix is 0, and only the last element is 1
  • If you multiply all the vertex vectors of an object by this matrix, you can transform the entire object to a given position and direction in space

Second, the use of matrix

1. Matrix transpose

Fold along the diagonal so that the row matrix becomes the column matrix or the column matrix becomes the row matrix.

2. Scalar x matrix

3. Vector x matrix

  • A vector can also be viewed as a 1 x n matrix, or an n x 1 matrix. Again, you can only multiply if the columns in the front are equal to the rows in the back. Otherwise there’s no point.
  • For example, 1 x 3 is the vector and 3 x 3 is the matrix. The vector x matrix is fine. The matrix x vector is meaningless.

In this way, we can conclude:

  • When you multiply a row vector by a matrix, you get a row vector;
  • When you multiply a column vector by a matrix, you get a column vector;
  • When you multiply a row vector right by a matrix, it’s meaningless;
  • When you multiply a column vector left by a matrix, it’s meaningless;
  • Each element in the resulting vector is the dot product of the original vector with individual rows or columns in the matrix;
  • Matrix and vector multiplication satisfies the distributive law for vector addition, for vectors V, W and matrix M,

(v + w)M = vM + wM;

4. Matrix X matrix

  • The columns of the front matrix are equal to the rows of the back matrix

  • 1. If any matrix M is multiplied by square matrix S, no matter which side you multiply it by, the size of the matrix will be the same as the original matrix, subject to the rule of multiplication. Assuming, of course, that multiplication makes sense. If S is the identity matrix, the result is the original matrix M, i.e. MI = IM = M. ,
  • 2. Matrix multiplication does not satisfy the commutative law, that is :AB! = BA
  • 3. Matrix multiplication satisfies the associative law, namely :(AB)C = A(BC). Assuming that the dimension of ABC makes multiplication meaningful, note that if (AB)C makes sense, then A(BC) must make sense.
  • 4. Matrix multiplication also satisfies the associative law with scalar or vector, i.e. :(kA)B = k(AB) = A(kB); (vA)B = v(AB);
  • 5. The transpose of the matrix product is equivalent to the transpose of the matrix and the multiplication in reverse order, i.e. :(AB)T = BT AT

5. Multiply the matrix left in OpenGL

In linear algebra, matrix multiplication coordinates are computed from left to right (by right) as follows:

Vertex vector after transformation = V_local x M_model x M_view x M_pro (M*V*P) Vertex vector after transformation = vertex x model matrix x observation matrix x projection matrix

However, in OpenGL the order is reversed (multiplied left) as follows:

Transform vertex vector = M_pro x M_view x M_model x V_local (P*V*M) Transform vertex vector = projection matrix X view transformation matrix X model matrix X vertex

    1. Copy the top stack matrix from the top of the stack into the temporary variable mTemp
    1. Multiply the top stack matrix mTemp by mMatrix
    1. Put the result back into the top space of the stack

The first order of the matrices in our code snippet: