Current location - Education and Training Encyclopedia - University ranking - Shader Notes -3D Mathematical Basis
Shader Notes -3D Mathematical Basis
In the 3D world, coordinate representation is needed to determine the positions of different vertices. The values of two coordinates are located based on a fixed reference point, which is the origin of the coordinates.

Usually, the coordinates of the origin are (0,0).

If all the coordinates are managed together, a coordinate system is formed. A complete coordinate system will contain the origin, direction and coordinates.

There are many kinds of coordinate systems involved in 3D, which can be distinguished from each other in size. All coordinate systems can be divided into plane rectangular coordinate system and space rectangular coordinate system.

In the spatial coordinate system, according to the different coordinate directions, it can be divided into left-handed coordinate system and right-handed coordinate system.

If the thumb is set to the X axis, the index finger is set to the Y axis, and the middle finger is set to the Z axis, the coordinate system of the left and right hands is as follows:

In mathematics, coordinates are scalars, which only indicate size, not direction.

A vector that can express both length and direction information is called a vector, or "vector". Vertex normal vector, tangent vector, etc. It's all vectors.

Assuming that the starting point of two-dimensional vector A is A and the ending point is B, vector A can be calculated by subtracting the coordinates of A from the coordinates of B. ..

The calculation formula of two-dimensional vector A is as follows:

If the formula is extended to three-dimensional space, assuming the starting point A and the ending point B of three-dimensional vector A, the calculation formula of three-dimensional vector A is as follows:

If two vectors are equal in length but opposite in direction, they are opposite in direction.

The inverse vector of a zero vector is itself.

From a geometric point of view, taking the starting point of a vector as the end point and the original end point as the starting point, the final vector is the inverse of the original vector.

Suppose that the starting point A and the ending point B of vector A, and the opposite vector B of a, are expressed as follows:

A vector contains a length and a direction, and the length of the vector is called the module of the vector. For example, the modulus of vector a is expressed as | a |.

As shown in the figure, through Pythagorean theorem, we can get

The formula for calculating the modulus length of two-dimensional vector A is:

As shown in the figure, through the quadratic pythagorean theorem, we get

"Unit vector" refers to a vector with a module length of 1.

In many cases, the direction of the vector is more worthy of attention than the length of the vector, such as the direction of light irradiation and the viewing direction of the camera. For the convenience of calculation, this vector can be transformed into a unit vector, and the process of this transformation is called vector normalization.

From the algebraic point of view, a non-zero vector divided by its own length can scale its own length to 1. The length of the zero vector is 0, which is meaningless in mathematics as a dividend.

Therefore, the normalized vector of non-zero vector a is:

Compared with the original vector, the unit vector only changes the length and the direction remains the same.

From a geometric point of view, the addition of vectors satisfies the triangle rule.

The addition of vectors can be extended to the addition of multiple quantities. The final result is from the beginning of the first vector to the end of the last vector, and the length is the distance from the beginning to the end.

From the algebraic point of view, the addition of vectors is the addition of the same components.

The subtraction of vectors can be understood as the addition of the opposite vector of one vector and another vector, which also satisfies the triangle rule.

The subtraction of vectors a and b is transformed into the addition of opposite vectors;

When two vectors with the same starting point are subtracted, the vector obtained is that the end point of the second vector points to the end point of the first vector, and the length is the distance between the two ends.

From the algebraic point of view, the subtraction of vectors is to subtract the same components. The calculation formula is as follows:

Multiplying a vector by a scalar can produce the effect of vector scaling. As shown in the figure:

Assuming that the vector a =(x, y, z), the formula for scaling the vector k times is:

The dot product of vectors a and b is denoted as a.b.

In algebra, dot product is also called inner product, and the result of dot product of two vectors is the sum of multiplication of all corresponding components. The result of dot product is a numerical value.

The calculation formula of dot product is:

In geometry, the result of the dot product of two vectors is the product of the projection length of one vector on the other vector and the length of this vector.

The calculation formula of dot product is:

The cross product of a vector is also called the outer product, and the result of the cross product is a vector. The cross product of vectors a and b is written as a× b.

The calculation formula of cross product is:

In geometry, the vector obtained by cross product is perpendicular to the plane where A and B are located, and its length is equal to the area of the parallelogram composed of vectors A and B, which is called normal vector. As shown in the figure:

Assuming that the two vectors of the cross product are reversed, the direction of the vector obtained by the cross product of B and A will be downward.

Commonly used vector algorithms are as follows:

A vector can be represented by a horizontally arranged array. Assuming that arrays with the same dimension are arranged vertically, the final array is a matrix.

The dimension of a vector indicates the number of numbers contained in the vector. Similarly, the dimensions of a matrix represent the number of rows and columns contained in the matrix. Usually, R (acronym for R (RAW)) is used to represent the number of rows, and C (acronym for column) is used to represent the number of columns, while the matrix itself is represented by black italic capital letters, such as M.

A 3×3 matrix m and all corresponding components can be expressed as follows:

A matrix with equal number of rows and columns is called a square matrix.

Components with equal number of rows and columns in a square matrix are called diagonal elements, and other components are called off-diagonal elements. A matrix whose off-diagonal elements are all zero is called a diagonal matrix.

In a diagonal matrix, a matrix whose diagonal elements are all 1 is called a identity matrix.

Multiply any matrix by identity matrix, and the final result is the same as the original matrix. Identity matrix has the same effect on matrices as 1 on scalars.

Assuming that a matrix m of r×c is diagonalized, the new matrix obtained is called the transposed matrix of matrix m.

The important rule of transpose matrix: transpose a matrix and then transpose it, and the obtained matrix is the same as the original matrix, and the formula is as follows:

Similar to vectors, matrices can also be multiplied by scalars, and there is no need to write operational symbols in the middle. The result of multiplication is the same as the dimension of the original matrix, and then each component is multiplied by this scalar. The formula is as follows:

When the number of columns in the first matrix is equal to the number of rows in the second matrix, the two matrices can be multiplied, and the number of rows in the matrix is equal to the number of rows in the first matrix and the number of columns in the second matrix.

In shaders, vectors can also be multiplied by matrices. When multiplying, the vector can be regarded as a matrix of 1 row or 1 column.

The vector (x, y, z) can be written horizontally as a matrix of 1×3, which is called a row vector:

It can also be written as a matrix of 3× 1, which is called column vector:

The geometric meaning of multiplication of vector and matrix is to realize the spatial transformation of vector.

In 3D, all transformations are completed by matrices, including translation, rotation and scaling. In addition, the transformation between coordinate spaces is also completed by matrix.

Suppose the vector v = (x, y, z), which can be expressed as

Assume that the two coordinate vectors of the plane coordinate system are p = (1, 0) and q = (0, 1) respectively, and rotate counterclockwise around the origin, as shown in the following figure:

The rotation directions in the left-handed and right-handed coordinate systems are completely different. The way to judge the direction of rotation in two coordinate systems is to extend the hand of the corresponding coordinate system, hold the rotating shaft and keep the orientation of the thumb consistent with the positive direction of the rotating shaft, and the orientation of the other four fingers is the positive direction of rotation. As shown in the figure, the direction indicated by the arc arrow is the positive rotation direction of the right hand coordinate system.

The rotation directions corresponding to the left and right coordinate systems are summarized as follows:

Only the rotation around the coordinate axis is explained here.

Rotate the spatial coordinate system around the X axis, and the rotation direction in the spatial coordinate system is as shown in the following figure:

All coordinates on the x axis will not change. Observing the coordinate system from the positive direction of the X axis to the negative direction, the rotation of the Y axis and the Z axis is exactly the same as that of the plane coordinate system, as shown in the figure:

The transformation matrix of the final rotation angle around the x axis is:

The rotation direction of the spatial coordinate system around the Y axis is shown in the following figure:

Observing the coordinate system from the positive direction of Y axis to the negative direction, the rotation of X axis and Z axis is as shown in the following figure:

The rotation direction of the spatial coordinate system around the Z axis is shown in the following figure:

Observing the coordinate system from the positive direction of Z axis to the negative direction, the rotation of X axis and Y axis is shown in the following figure: