preface
As a person who has worked in the game industry, I found that the old knowledge has been useless for a long time and then I gradually forgot it, so I decided to write an article to record it and share it with you.
Rendering 3d objects is similar to Chrome rendering in that there are clearly divided steps to produce the final product. Graphics call this process the rendering pipeline
Let’s start with the data. First, just like a browser, drawing 3d objects requires data. In browsers, HTML, CSS, JS and so on are used as data. Based on these data, a web page is drawn and presented to the user. And the simplest data we have in graphics is vertices, so what is a vertex? The vertices represent three points in space and their coordinates are represented by vectors (x,y,z)
A typical rendering pipeline that wants to draw n vertices as data into a 3D object requires the MVP step
Preliminary knowledge
Let’s talk about a few things before we get started
Left – handed and right – handed
The left hand coordinate system is X to the right, Y to the up, and Z to the forward. The right hand coordinate system’s Z axis is the opposite, pointing to “oneself”. In computers, the left hand coordinate system is usually used, while in mathematics, the right hand coordinate system is usually used. In fact, a lot of computers also use right-handed coordinate system, this is just based on the actual application, I didn’t say which one is better.
A matrix times a vector
The Model phase
In 3d rendering, cartesian coordinate system is used to represent the position in 3d space, including rotation Angle.
So what do you do in the Model phase? This involves world coordinates and local coordinates, which can also be understood as absolute coordinates and relative coordinates. The point below is when I put the object in space (0,0,0), the absolute coordinates of the cube are shown below. If I now move the cube up 1, then all of the points made of the cube are going to increase by 1. But the positions of the eight vertices relative to the center of the square remain unchanged, that is, their relative coordinates remain unchanged. For the sake of abstraction, we don’t want to change the eight vertices we want to change the square coordinates to (0,1,0) and then we’ll have a formula that will handle all the vertices, the same thing with rotation. That’s what the Model phase does.
Mobile model Translation Matrices
So let’s say we have positions 10,10,10,1, or let’s call them vectors, or let’s call them vectors. I’m going to move him 10 to the right
Rotate the model over matrices
Here is an example of a left-handed coordinate system.
The following equations rotate the point (x, y, z) about the X-axis to produce the new point (x’, y’, z’).
The following equation rotates this point about the Y-axis.
The following equation rotates the point about the z axis.
Scaling the model
Let’s say we have the x,y,z,w vector. We need to make him twice as big
Combine all matrices
ModelMatrix =TranslationMatrix * RotationMatrix * ScaleMatrix;
This matrix is used to obtain absolute coordinates from relative coordinates.