Basic concept 2 of 3D space: matrix operation

Source: Internet
Author: User

II,Matrix Operations

1.What is a matrix?

A matrix is an array of multiple groups of data arranged by square. In 3D operations, it is generally a square matrix, that is, M * N, and M = N, using matrices makes it easy and convenient to calculate 3D coordinates. The following is an example of a matrix:

There seems to be nothing special, but you can see the charm of the matrix later. I don't know why the matrix is so effective. This is discussed by mathematicians as long as we can use it.

 

2.Point multiplication and cross multiplication of Vectors

Point multiplication and cross multiplication of vectors are mathematical definitions like matrices. Point multiplication plays an important role in matrix operations. It is called Inner Product and cross multiplication is called outer product, A cross-Multiplication operation can be used to calculate a vector perpendicular to a plane composed of two vectors. This vector is also called the normal of the plane. These two calculation methods play a role in 3D operations as vector computing tools.

L vertex multiplication Formula

 

In fact, each component of two vectors is multiplied to form a new vector.

L crossover Formula

Uc = U1 * U2

The matrix of the two vectors for cross multiplication is as follows:

Specifically, x1, y1, z1, x2, y2, and z2 are the components of the vectors U1 and U2 respectively, and UC is set as the vector product of the Cross multiplication. The formula is as follows:

 

3.3D geometric Transformation Matrix

In geometric drawing, it is often necessary to move a model from one position to another, or scale and rotate the model, which is called geometric transformation. Each model has a local coordinate system. When creating a model, the specific location of the model is not taken into account. The coordinate values of all vertices in the model are relative to the local coordinate system, the model will change a lot in the application, most of which are the results of a combination of multiple changes. These changes involve a lot of complex operations, therefore, you must calculate the value of each vertex relative to the world coordinate system after model rotation, scaling, and displacement. If you only use the general trigonometric function to process points one by one, it will be daunting, the amount of computing is immeasurable. Even if you have made a great effort to write the formula, maybe the model is completely invisible because of a small mistake, let alone the animation effect.

In this case, the matrix must be used to solve these problems. In 3D computing, the 4-element coordinate system is used. Therefore, the 4*4 phalanx is used for model transformation. In the matrix structure, the element numbers are arranged in the first and last columns, in programming languages, Arrays can be used for storage and cyclic computing. To facilitate coordinate batch processing, before drawing and calculating a 3D model, first, calculate the elements required for certain transformations and fill in the matrix, then multiply all vertices of the model and the matrix one by one to convert all vertices of the model to new coordinates (unless the matrix element is set incorrectly, each data (element) in a matrix is crucial. How to generate these elements is described in Chapter 3.

The matrix structure is as follows. (A two-dimensional plane uses a matrix of 3x3 in the same principle ).

Each element in this structure is given a number, and the code of the number represents the row and column respectively.

4.Transformation Calculation Formula

A matrix can be used for vector and point transformation operations. A coordinate or vector and a 4x4 matrix can be used for point multiplication and conversion. You can use a column matrix or a row matrix to arrange data in a matrix. However, when performing multiplication, columns must be multiplied by a combination of rows and columns. OPENGL uses a column matrix. The following describes how a matrix array is arranged and how a vertex or vector is multiplied by a matrix to obtain a new coordinate.

 

P is the coordinate or vector of the original vertex. During the transformation, the four components of the current vertex P are multiplied with each row of the matrix respectively:

X' = x * M00 + y * M01 + z * M02 + w * M03

Y' = x * M10 + y * M11 + z * M12 + w * M13

Z' = x * M20 + y * M21 + z * M22 + w * M23

W = x * M30 + y * M31 + z * M32 + w * M33

Since the first three columns of the last row in the matrix of 3D operations are always 0, the result of W' is dependent on w. Therefore, we can see that the vector obtained by multiplying the matrix is also a vector.

5.Unit Matrix

There is a special matrix, which consists of a diagonal line consisting of elements in the upper left and lower right. If all the elements above are 1 and the others are 0, this matrix is called a matrix of units, the result of multiplying any vertex and the unit matrix is equal to the original coordinate of the vertex, that is, no transformation occurs.

In OPENGL, The glLoadIdentity () function is often used to set the unit matrix. Since OPENGL is a state machine, it is used to reset the matrix that may have been modified before each scenario, however, sometimes a model must undergo coordinate transformation based on the previous calculation results. For example, a car body is first drawn and then a wheel is drawn based on the current position of the car, the original matrix must be kept and transformed relative to the location of the vehicle, but sometimes it must be calculated from the origin. Therefore, matrix management is performed through a series of matrix functions, the most common operation is the matrix stack operation. But in most cases, the depth of the OPENGL matrix stack is 32, that is, only 32 matrices can be saved. Although the matrix stack is more efficient, however, in some cases, 32 matrices are not enough. In order to reduce computer computing load, we must arrange the drawing sequence in advance, which is also the necessity of class encapsulation, however, class encapsulation will increase the call overhead.

 

6.Matrix Multiplication

Sometimes you need to perform multiple consecutive transformations on a model, and each transformation must multiply all vertices of the model and the matrix one by one. If you are dealing with a complicated scenario, the calculation amount is considerable. In order to reduce the calculation amount and accelerate the scenario, multiple transformation matrices are merged in advance, and then all vertices and matrices of the model are multiplied by one-time matrix transformation. For example, scale the model first and then rotate it.

The merge method is to multiply multiple matrices to calculate the composite matrix. In 3D transformations, the two matrices involved in multiplication must be 4x4 matrices. During multiplication, each new element is also obtained after the dot Multiplication operation. The new matrix obtained is also a 4x4 matrix.

 

 

The product of the matrix is irreversible, that is, the MN is not equal to the NM. Therefore, pay attention to the order of transformation. In addition, the result of multiplication between the vertex and the composite matrix is opposite to the order of matrix merging. For example, T is a translation matrix, and R is a rotation matrix. If the transformation sequence is to be rotated first and then moved, the order of the merged matrix must be M = TR, then perform the transformation of p '= MP, and the effect is equivalent to p' = T (RP ).

 

The formula for calculating matrix multiplication is as follows:

The composite matrix calculation method is to perform dot multiplication between each row element of matrix M on the left and each column element of matrix N on the right is the corresponding element of the new matrix C. The calculation order is as follows: M starts from the first row above and extracts the four elements of each row, starting with the first column on the left of N, respectively. The extracted four elements are used for Dot multiplication, the calculation results are placed in C and arranged from top to bottom, from left to right. Double Loops are used for programming.

 

 

 

C00 = m00 * n00 + m01 * n10 + m02 * n20 + m03 * n30

C01 = m00 * n01 + m01 * n11 + m02 * n21 + m03 * n31

C02 = m00 * n02 + m01 * n12 + m02 * n22 + m03 * n32

C03 = m00 * n03 + m01 * n13 + m02 * n23 + m03 * n33

........

C30 = m30 * n00 + m31 * n10 + m32 * n20 + m33 * n30

C31 = m30 * n01 + m31 * n11 + m32 * n21 + m33 * n31

C32 = m30 * n02 + m31 * n12 + m32 * n22 + m33 * n32

C33 = m30 * n03 + m31 * n13 + m32 * n23 + m33 * n33

 

As mentioned above, after a series of calculations, the two matrices are combined to form a new composite matrix. It is easy to compile such code. Now we have several basic matrix computing tools. As long as we fill in the appropriate parameters, we can use a circular method to transform all vertices of the model and finally implement any complex transformation of the model, it is basically a mechanical operation. The rest is the core thing, that is, the generation of elements in the basic matrix.

 

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.