vector
Before we get to vectors, what are scalars
- Scalar: only size, e.g. 1,12,13, etc
- A vector is a directed scalar, that is, it has not only magnitude but also direction
Unit vector A unit vector is a vector of length 1, which is calculated by the following formula
If a vector is not a unit vector, it can be converted to a unit vector by unitization, that is, a non-zero vector divided by the magnitude of the vector, as shown in the figure below
The vector dot product
-
The dot product can only occur between two vectors, and the two vectors must be unit vectors. If they are not, you need to unit the vector and then dot the vector
-
The dot product is going to give you the cosine of the Angle between the two vectors which is cosine alpha, between minus 1, 1, which is a scalar
-
So in OpenGL, there are two functions for the dot product
- M3dDotProduct3: Obtain the dot product between 2 vector quantities, that is, cosine = cosα
- M3dGetAngleBetweenVector3: obtaining the Angle of the Angle between two vectors, the alpha = arccos (cosine)
Vector cross-product
-
The cross product between two vectors gives you the same vector, and it’s perpendicular to the plane of the two vectors,
-
Since the result is perpendicular to the plane formed by the two vectors, it can also be understood that the resulting result is the normal of the plane
-
OpenGL also provides an API for cross product
- M3dCrossProduct3: Get the cross product between 2 vectors to get a new vector
Vector in the OpenGL
- Vector representation: there are two ways, three and four dimensions, as shown in the figure
Data types in the Math3D library | instructions |
---|---|
M3DVector3f | Represents a three-dimensional vector quantity (x,y,z) |
M3DVector4f | W is the scaling factor. In a typical case,w is set to 1.0. The x,y, and z values are scaled by dividing w by w |
-
I’m going to summarize the API for dot and cross products
matrix
Unit matrix
- The data on the main diagonal are all 1’s and the other elements are all 0’s, which is the element matrix
- The vector X element matrix is equal to the vector X 1, which doesn’t change anything
- The premise of vector multiplication is: the number of columns of the vector == the number of rows of the element matrix
Matrix classification
- Row priority matrix: read row by row
- Column priority matrix: read column by column
- The relationship between the two is: the row priority matrix can be transposed to the column priority matrix
The dot product of a matrix
-
A matrix can be dotted with the premise that two matrices have the same number of rows and columns
-
Matrix A · matrix B = matrix C – Rule: The product of the first element of matrix A and the first element of matrix B = the first element of matrix C
The cross product of a matrix
-
Matrix can be cross product premise: the number of columns of the first matrix = the number of rows of the second matrix
-
Matrix A X matrix B is equal to matrix C
-
Rule: The composition of the product of the first row of matrix A and the first column of matrix B equals the first element of matrix C
-
Matrices in OpenGL
-
There are three initialization methods for element matrices in OpenGL
-
Define a one-dimensional array with GLFloat
-
Create a cell matrix using M3DMatrix44f
-
- Create the element matrix using the method 'm3dLoadIdentity44f'Copy the code
void m3dLoadIdentity44f(M3DMatrix44f m);
Copy the code
- In OpenGL, the most used matrix is created by one-dimensional array, and it is stipulated to use matrix sorting mainly by column.
- The matrices in OpenGL are all 4×4, and each column is a vector of four elements, as shown in the figure
- The first column is the X-axis
- The second column is the y direction
- The third column represents the z direction
- The fourth column represents the swap position
- Column vectors are specially labeled to indicate that this matrix is dominated by columns, mainly reflected in that the last row of the matrix is 0, and only the last element is 1
Understand matrix multiplication in OpenGL
Mathematical Angle
-
In mathematics, for the convenience of calculation, the row matrix is used as the standard, and the calculation is carried out from left to right. Therefore, in mathematics, vertices are represented as row vectors
-
To understand the calculation of MVP matrix from a mathematical perspective, because the vertices are row vectors, to satisfy the specified conditions of matrix multiplication (i.e. the premise of cross product), the MVP matrix must be placed on the right, belonging to the right product
-
Vertex vector = V_local * M_model * M_view * M_pro
-
Vertex vector = vertex * model matrix * observation matrix * projection matrix
-
OpenGL Angle
-
The matrix in OpenGL is defined as columns, so vertices are represented as column vectors
-
From the perspective of OpenGL to understand the calculation of MVP matrix, because the vertices are column vectors, if the item is matrix rules, it needs to meet the conditions of matrix multiplication, need to reverse the order of MVP matrix to PVM, and put on the left of the column vector, belongs to the left multiplication
OpenGL matrix stack matrix multiplication source analysis from the OpenGL matrix stack matrix multiplication source analysis, there are mainly the following 3 steps – from the top of the stack to obtain the top of the stack matrix, copy to mTemp
-
Multiply the top stack matrix mTemp by mMatrix
-
Puts the result back on the top of the stack, overwriting the top of the stack matrix
While we observe in the way that the observer does not move and the object moves, according to the previous Demo code
- In the ChangeSize function, the projection matrix is obtained, the projection matrix is pressed to the top of the projection matrix stack and multiplied by the top of the model view matrix stack to cover the top of the stack, that is, the projection matrix * element matrix = projection matrix
- In RenderScene, copy the top stack matrix and multiply the observer matrix by the top stack of the model view matrix. The result overwrites the top stack matrix, i.e., projection matrix * view matrix = view projection matrix
- Get the model matrix, multiply the model matrix and the top of stack matrix, and the result overwrites the top of stack matrix, that is, the top of stack = the projection matrix of model view
In the above code, the matrix stack changes as follows
Therefore, in the actual code, the MVP matrix is calculated in the order of PVM. Finally, the MVP matrix is multiplied by the vertex matrix to obtain the vertex and position of the object after transformation.