The point product of a vector has commutative and associative laws.
The vector points are multiplied by coordinates:
Applications of vector dot multiplication are:
Another multiplication of vectors is cross multiplication
The direction of the cross product of the vector follows the right-hand rule. Assuming $\ vec {a}\ times\ vec {b} $, then the direction of the result is that the four fingers of the right hand turn from the direction of the vector a to the direction of b, clench tightly, and then give a thumbs up is, the direction of the thumb. That is, the result of the cross product is perpendicular to the plane where a and b are located
Then the length of the cross product is actually $|\ vec {a}\ times\ vec {b }||=||\ vec {a} | | | |\ vec {b} | | sin\ theta $
The cross product of vectors does not support associativity. To be precise, the result of the order of commutative cross products is reversed, that is, the direction is reversed: $\ vec {a}\ times\ vec {b} = -\ vec {b}\ times\ vec {a} $
The cross product of the vector is represented by a matrix:
Cross product can determine whether a vector is on the left or right of another vector. This is easier to understand. When the right-hand rule rotates, it rotates clockwise or counterclockwise, and the result is the opposite.
Another function is to determine whether a point is inside a triangle
In the figure above, if the symbols of vector BC cross-multiplied by vector BP, vector CA cross-multiplied by vector CP, and vector AB cross-multiplied by vector AP are all the same, then it means that the P point is inside
A matrix is a two-dimensional array of m rows and n columns.
The premise that two matrices can be multiplied is that the number of columns in the first matrix and the number of rows in the second matrix are the same.
That is, a matrix with M rows and N columns can be multiplied by a matrix with N rows and P columns, resulting in a matrix with M rows and P columns.
Suppose A and B are multiplied by two matrices to a matrix C, where each term of A is $a_ {ij} $, each term of B is $B_ {ij} $, and each term of C is $c_ {ij} $, then $c_ {ij} =\ sum_ {k = 0} ^ {k = N} a_ {ik} b_ {kj} $
The important point here is how to write the equations of coordinate transformation in the form of matrices
For example, how to say that points in a two-dimensional coordinate system are symmetrical according to the y-axis
Just write it in a system of equations
Write it in matrix form
Each matrix has its own transpose matrix and inverse matrix.
The transpose matrix of A is written as $A_T $, and $ (AB) ^ T = B ^ TA ^ T $
The Inverse Matrix of A is written as $A ^ {-1} $, and $AA ^ {-1} = I $, where I is the identity matrix, and any matrix multiplied by the identity matrix equals nothing, that is, any matrix multiplied by A The change produced by the matrix can be restored by multiplying the Inverse Matrix of A
When the image is scaled by s times, it is represented by the equation
The corresponding scaling matrix is:
Step 4 Rotate
So far, all our transformations can be expressed in matrix form, because our previous transformations can be expressed in the following equation:
Expressed as a matrix is
But the problem is that you can’t represent translation in this way, because translation can’t be written in this form
The system of equations for translation is like this:
If scaling, rotation, and translation are all represented by matrices, they should be as follows:
So at this time we have to introduce homogeneous coordinates, that is, add w. At this time, the 2D point coordinates are represented by (x, y, 1), and the 2D vector is represented by (x, y, 0)
When representing a point, w is 1, when representing a vector, w is 0, and there is a very magical place like this, that is, if two points are subtracted, w will become 0, which happens to be a vector, point and vector Adding, w is 1, which is also a point
Using homogeneous coordinates, we can uniformly rotate, scale, and translate into a matrix
The translation is expressed in homogeneous coordinates as:
We can now use homogeneous coordinates to represent rotation, translation, and scaling respectively
So how do we mix these operations?
As you can see in the figure above, we now translate and then rotate. The result of translation is different from that of rotation first, because our rotation matrix is rotated around the origin
Therefore, we generally specify the order to perform mixing operations. The first operation is multiplied left by the coordinates of the current point, and after obtaining the new point, it is multiplied left by the next operation matrix.
Then, although our order cannot be changed because matrix multiplication has no commutativity, matrix multiplication has associativity, that is:
So we can achieve the result of multiplying the rotation, scaling, and translation matrices as the transformation matrix, and then multiplying it left with each point
There is actually another problem here, which is, what if we just want a point to rotate around its lower left corner?
It’s simple, translate the bottom left corner to the origin, then rotate, and finally translate the bottom left corner back:
The transformation of 3D is actually no different from 2D, except that homogeneous coordinates have four dimensions
The transformation matrix is as follows:
The scaling matrix can be written as;
The translation matrix can be expressed as:
Rotation is more complicated because it can be divided into rotation around different axes
There is also a formula to multiply any rotation matrix Factorization by the rotation matrix in three axis directions. The following way represents the rotation angle of $\ theta $around the $\ vec {n} $axis.
When we are doing game development and writing Shader, we often use something called an MVP matrix to change the coordinates of points on the model to the coordinates on the screen.
The MVP here refers to Model, View, and Projection, which means model transformation, view transformation, and projection transformation
Model transformation is to change the coordinates from the coordinate system of the model itself to the coordinates of the game world coordinate system
View transformation is to change the coordinates on the world coordinate system into the coordinates of the observation space
The projection transformation is to change the coordinates of the observation space into the clipping space. In fact, this step does not do the operation of projecting to the two-dimensional plane. The specific projection operation is written in the GPU in the rendering pipeline, and is generally not processed in the Shader.
The observation transformation we are talking about here is just a step in the rendering pipeline. At the beginning, it is operated vertex-by-vertex in the vertex shader. We get the coordinates of the point in the clipping space. In the next rendering pipeline, we have to go through grating., sampling, chip-by-chip shader output color, depth test, etc. will finally be projected onto a two-dimensional plane.
Tired than the usual way we take pictures, the Model matrix is like we find a suitable camera position, the View matrix is to use the camera to find an angle
We don’t talk about the Model matrix here, because it is the same as the View matrix, which changes from one coordinate system to another, and the Projection matrix is different in that the observation space is a box with a side length of 1 cube, we need to consider scaling
View matrix is the point from the world coordinates into the observation space, i.e., from the origin of the world coordinate system relative to the camera position becomes coordinates.
After the Model transformation, we have the coordinates of the point in space, now we need to define the position and orientation of the camera in space:
The coordinates and orientation of the camera here are relative to the world coordinate system. And our object coordinates are currently relative to the world coordinate system.
Now all we have to do is change the object coordinates relative to the world coordinate system to relative to the camera coordinates.
Here we introduce a common physical concept - relative motion, that is, if the same transformation operation is performed on the camera and the object, the relative position of the two remains unchanged.
Then we can now try to move the camera to the origin. The observation direction of the camera is towards the negative direction of the z-axis of the world coordinate system, find the matrix of this transformation, and then apply this matrix to each point, which is equivalent to moving the object to the observation space. Although from the point of view of the object coordinates, a certain movement is made in the world coordinate system, this movement does not change the relative position of the object and the camera, and also successfully moves the camera to the origin of the world coordinates, so the result is equal to moving the object to the observation space.
So how do you get this view matrix?
This method is more complicated
We can use a better property here, that is, the rotation matrix is actually an orthogonal matrix, the Inverse Matrix and the transpose matrix of the orthogonal matrix are the same, that is, we can find the matrix of the world coordinate axis transformed into the camera coordinate axis, and then find his transpose matrix, that is, the Inverse Matrix, which is the matrix of the camera coordinate axis transformed into the world coordinate axis
Just through the View matrix, the relative position of our camera and the object as a whole remains unchanged and moves to the position of the camera at the origin of the world coordinate system.
What is the purpose of this? Of course, there is an advantage that it is easy to understand, but in fact, it does not make sense for a calculator, because it is all multiplied by a matrix, and the amount of calculation will not make a difference.
Another advantage of this is to reduce the calculation of the projection matrix.
Our projection matrix is divided into two types, one is parallel projection and the other is orthogonal projection:
Let’s first look at the relatively simple parallel projection
A relatively simple way to understand this projection is to just throw away the z-axis, which is the coordinate of the final point on the screen, and then both the x and y directions are translated and scaled between [-1, 1]. The reason why we can just throw away z here is that our camera is moved to the origin and in the negative direction of the z-axis.
However, dropping the z-axis cannot be done yet. We still need the information of z to do in-depth tests later. What we need to do now is to normalize x, y, z to the cube of $[-1,1] ^ 3 $
As shown in the figure below, in the parallel projection, we start with a cube in the observation space. We need to move the center of this cube to the origin and scale it to a cube with side length 1
The difference between orthogonal projection and parallel projection is that there is a near-large and far-small effect, and its initial observation space is not a cube, but a ladder
We are looking for this ladder normalization matrix to be divided into two steps:
So we’re focusing on the first step right now
And because our current camera is facing the negative direction of z, so the zoom bottom will not affect the z coordinate, we only need to focus on x and y, we take the y coordinate for example:
Similarly, the left side of x becomes $x '=\ frac {n} {z} x $
Then at this time, our transformation matrix can be written. First, the transformation of the orthogonal projection space into the parallel projection space
Then the formal orthogonal projection View matrix is $M_ {press-ortho} $multiplied by the parallel projection View matrix
At this point, we have completed the transformation of a point from model space to normalized space in the world coordinate system