Related contents:

  1. OpenGL- Introduction to the Graphics API
  2. OpenGL– An explanation of related terms
  3. OpenGL– Environment configuration
  4. OpenGL- Case 1- Draw a triangle
  5. OpenGL- Case 2- Draw a square
  6. OpenGL- Image tear
  7. OpenGL–3D mathematical correlation (Vectors and matrices)
  8. OpenGL- Matrix transformations and matrix stacks

vector

What is a vector?

Let’s start by understanding what a vector is. A vector is just a bunch of numbers, and it exists to distinguish it from a scalar quantity. In general, a vector has both a magnitude and a direction in the coordinate system (” know the coordinates “, “know the length “,” know the direction “), just as velocity is a vector, and distance is a scalar.

The figure above shows the 2D vector and 3D vector respectively. The vector has no position, only direction and length.

Representation of vectors

So now that we know what vectors are, let’s look at the operations on vectors, let’s look at the representations of vectors

In the figure above, now that we know the coordinates of A and B (in two dimensions), how do we represent vector A and vector B?

Representation of vector A:

B – A = (x1-x2, y1-y2);

Representation of vector B:

A – B = (x2-x1, y2-y1);

You can see that in order to represent a vector, you have to know the coordinates of the two points on the vector, and then you have to determine the order of subtraction based on the direction of the vector. (The above example is in two dimensional coordinates; if it is a 3D vector, it adds one dimensional component plus or minus.)

Vector operations and special vectors

1. The zero vector

For any vector x, x plus y is equal to x, then x is called the zero vector. For example, the 3D zero vector is [0 0 0]. The zero vector is special because it’s the only vector that has magnitude zero, and it’s the only vector that has no direction.

2. Negative vector

For the vector x, if x plus minus x is equal to 0, then minus x is the negative vector. Negative vector means you’re going to get a vector that’s equal in magnitude and opposite in direction.

3. Magnitude of the vector

The modulo of the vector is the magnitude or the length of the vector.

In linear algebra, vector model is usually used in vector and two vertical lines on each side way, said such as | | v | |, said vector v mode. The calculation formula of vector modulus is as follows:

  

For 2D, the 3D vector is as follows

  

4. Normalized vectors

Let’s start with unit vectors. Unit vectors are vectors of length 1 (sometimes called normals). To normalize a vector is to scale a non-unit vector to 1. This process is called normalization (also called normalization).

For any non-zero vector V, you can compute a unit vector n that goes in the same direction as V. This process is called “normalization” of a vector. To normalize a vector, you divide the vector by its magnitude.

  

5. Scalar and vector operations

You can’t add or subtract between a scalar and a vector, but you can multiply and divide, so multiplying a vector by a scalar or dividing by a scalar is the same thing as scaling the length of the vector by factors.

  

For 2D, the 3D vector is as follows

  

6. Vector addition and subtraction

If two vectors have the same dimension, then they can add and subtract, and the resulting vector has the same dimension as the original vector. Addition is equal to the addition of the components of two vectors, and subtraction is equivalent to adding a negative vector.

The addition and subtraction of vectors gives us the triangle rule, which says if you add the first part of the vector you get the addition.

Dot and cross products of vectors

Dot product

For 2D, for 3D

A dot b = axbx + ayby

A ·b = axbx + ayby+ azbz

Note: x,y and z are Angle marks, which are “·”, not “*” oh

Scalars can be multiplied by vectors, vectors can be multiplied by vectors, and this is called the dot product (also called the inner product). In addition, you can’t write “dot” when you’re multiplying a scalar with a vector, you have to write “dot” when you’re multiplying a vector with a vector, and dot takes precedence over addition or subtraction. The dot product of the vector is a scalar

Let’s take a look at the vector dot product with a graph:

cross-product

The cross product of two vectors gives you a new vector that is perpendicular to the original vectors. The cross product of a vector can only be used in 3D vectors.

Cross (a, b) = | | | | * b * sin (alpha)

The order of cross products

The two vectors that you’re taking the cross product with are sequentially related, and you’re going to get different results depending on the order. As for the judgment of the result, the right hand can be judged by the right hand, the so-called right hand rule is to make the right hand, excluding the thumb of the four fingers to the direction of the cross product (such as b x a, from b vector to a vector) fist, the thumb is pointing in the direction of the cross product vertical vector. You can see the following picture:

Vector in OpenGL

How to define a vector

OpenGL gives us an API in Math3D for vectors.

M3DVector3f can represent a three-dimensional vector (x, y, z)

M3Dvector4f can represent a thought vector (x, y, z, w)

In a typical case, the w coordinate is set to 1.0. The x, y and z values are scaled by dividing by w.

Code reference:

// Three dimensional vector/thought vector declaration
typedef float M3DVector3f[3];
typedef float M3Dvector4f[4];
// Declare a three-dimensional vector M3DVector3f: type vVector: variable name
M3DVector3f vVector;
// Declare a four-dimensional vector and initialize a four-dimensional vector
M3DVector4f vVertex = {0.0.1.1};
// Declare a three-component vertex array. For example, generate a triangle
M3DVector3f vVerts[] = {
    0.5 f.0.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.0.5 f.0.0 f
};
Copy the code

Dot product

The Math3D library provides an API for the dot product:

// 1. M3dDotProduct3 function obtains the dot product result between two vectors;
float m3dDotProduct3(const M3DVector3f u,const M3DVector3f v);

/ / (2) of the Angle between two vectors is m3dGetAngleBetweenVector3 radian value;
float m3dGetAngleBetweenVector3(const M3DVector3f u,const M3DVector3f v);
Copy the code

cross-product

The Math3D library provides an API for the cross product:

// 1. M3dCrossProduct3 function obtains the result of the cross product between two vectors to obtain a new vector
void m3dCrossProduct3(M3DVector3f result,const M3DVector3f  u ,const M3DVector3f v);
Copy the code

matrix

define

Matrix notation

In linear algebra, a matrix is a rectangular block of numbers in the form of rows and columns (a vector is an array of scalars and a matrix is an array of vectors).

Matrices are usually represented by open brackets, or they can be represented by vertical lines, and they are usually represented by capital letters. When we use the components of a matrix, and we use subscripts to represent the components of a matrix, notice that the components of a matrix start at 1, not 0.

square

A matrix with the same number of rows and columns is called a square matrix. The concept of square matrices is very important, and the matrices that are commonly used in 3D are 2×2, 3×3, 4×4 squares.

The diagonal elements of a square matrix are the elements whose components have the same row and column numbers. For example, m11, M22, and M33 in a 3×3 matrix are the diagonal elements, and the rest are non-diagonal elements.

  

Diagonal matrix

A square matrix is called a diagonal matrix if the off-diagonal elements are all 0. For example,

Unit matrix

In a diagonal matrix, if the diagonal elements are all 1, the diagonal matrix is the identity matrix. The identity matrix is a special kind of diagonal matrix, the identity matrix multiplied by any matrix is the original matrix. For example, the 3D identity matrix is as follows

  

Transposed matrix

So the transpose of a matrix is the transformation of the rows into the column elements, the column elements into the row elements of the matrix, and the transpose of M is called MT.

  

  • The transpose of a row vector is a column vector, and the transpose of a column vector is a row vector.

  • For any diagonal matrix D, there’s a transpose that’s equal to itself, and the element matrix is the same.

  • For any matrix D, transpose twice is equal to itself.

Operation of matrix

1. Multiplication of scalar and matrix

The scalar k multiplied by the matrix M will give you a matrix N that has the same dimension as the original matrix, and every element of the matrix N is equal to every element of the matrix M multiplied by the scalar K, and the formula is as follows.

  

2. Multiplication of matrix and matrix

Matrix multiplication is defined as multiplying an r x n matrix A times an n x c matrix B, and you get an r x c matrix AB. For example, when you multiply A 4x 2 matrix A by A 2 x5 matrix B, you get A 4×5 matrix AB.

So for our new matrix AB, let’s call it C, then each member Cij of C is equal to the dot product of the ith row vector of A with the JTH column vector of B. The sample is as follows

  

The element C24 of C is going to be equal to the dot product of the second row vector of A with the fourth row vector of B.

3. Vector and matrix multiplication (important)

Because vectors are also matrices, you have to satisfy the matrix to matrix multiplication rule, so for row vectors, you multiply the row vectors left by the matrix, and for column vectors, you multiply the column vectors right by the matrix. Row vectors and column vectors when they’re multiplied by the same matrix are going to give different results, so we have to be careful that this is one of the differences between row vectors and column vectors, especially when we’re doing matrix operations.

As an example, for row vectors and column vectors, the results are different.

  

V: row vector

H: column

In DX3 we use row vectors, but in OpenGL we use column vectors, so you have to take the transpose of the row vectors and the column vectors before you do the operation so that you don’t have any problems with the computation.

Matrix in OpenGL

Definition and initialization of matrix

Definition 1.

// Three dimensional matrix/thought matrix declaration
typedef float M3DMatrix33f[9];
typedef float M3DMatrix44f[16];
Copy the code

Here in the other becomes standard, many matrix definitions are used as two-dimensional arrays. But in OpenGL conventions, it’s more of a one-bit array. The reason is that OpenGL uses the column-major sorting convention (i.e., the transpose mentioned above).

In addition, the matrices in are all 4×4, and each column is a vector composed of four elements, as shown in the figure below:

The last row of the matrix is 0, only the last entry is 1.

2. The initialization

There are three ways to initialize a cell matrix in OpenGL:

  • throughGLFloatClass to create a one-bit array
GLFloat m[] = {
  1.0.0.0.//X Column
  0.1.0.0.//Y Column
  0.0.1.0.//Z Column
  0.0.0.1  // Translation 
}
Copy the code
  • throughM3DMatrix44fCreate a cell matrix
M3DMatrix44f m = {
  1.0.0.0.//X Column
  0.1.0.0.//Y Column
  0.0.1.0.//Z Column
  0.0.0.1 // Translation
}
Copy the code
  • Through the method ofm3dLoadIdentity44fCreating a cell matrix
void m3dLoadIdentity44f(M3DMatrix44f m);
Copy the code

Matrix transformations in OpenGL

1. Change the order

OpenGL uses a camera model, which compares view transformation to the process of taking a photo with a camera. Here are the steps (with a few changes from the Little Red Book) :

  1. Moves the object to be shot to the specified position in the scene. (Model Transform)
  2. Move the camera into position and point it in a certain direction. (View Transform)
  3. Set the focal length of the camera, or adjust the zoom. Projection Transform
  4. Take a photo and transform it to the desired image size. (This is the viewport transformation, we will not discuss here)

The first three of the four steps are different in linear algebra and OpenGL if implemented using matrices, so let’s look at each of them:

2. Linear algebra Angle

In the mathematical dimension of linear algebra, for the sake of writing. So the coordinates are calculated from left to right. As follows:

To understand the calculation of MVP matrix from a mathematical point of view, since vertices are row vectors, to satisfy the stipulated conditions of matrix multiplication, the MVP matrix must be placed on the right side, which belongs to right multiplication. (ps: The vector and matrix multiplication above is also mentioned)

  • After transformation, the vertex vector is V_local * M_model * M_view * M_pro
  • Transformed vertex vector = vertex * Model matrix * Observation matrix * Projection matrix

3. OpenGL Angle

Matrices in OpenGL are dominated by columns, so vertices are represented as column vectors.

To understand the calculation of MVP matrix from the perspective of OpenGL, since vertices are column vectors, if the terms are subjected to matrix rules, it is necessary to satisfy the conditions of matrix multiplication. It is necessary to reverse the order of MVP matrix to PVM and put it on the left side of column vectors, which belongs to left multiplication

  • Transform vertex vector = M_pro * M_view * M_model * V_local

  • Transformation vertex vector = projection matrix * View transformation matrix * Model matrix * vertex

Let A be the matrix du of Baimp and B be the matrix OF PN, then the matrix C of m*n is called the product of the matrix A and B, denoted as C=AB and called A left times B.

DaoA = mp, and B = pn, then we call m*n C=A product of B, and we call it C=AB, and we call B =A.

Note: Matrix multiplication generally does not satisfy the commutative law

The last

The above is all the content of this time, mainly involving the vector and matrix related content, in addition about the transformation of matrix may not be very comprehensive, will be supplemented later. If you don’t understand or incorrect, welcome to point out, thank you ~

Study harmoniously, don’t be impatient ~