Some of this article is used to refer to the OpenGL Programming Guide (8th Edition), and interested students can join together to see. This is a consolidation supplement.
OpenGL uses the camera model, that is, the view transformation operation analogy to use the camera to take pictures of the process, the following steps (here and red Book some changes):
- Moves the object you are preparing to shoot to the specified position in the scene. (Model Transform)
- Move the camera to the location where you want to shoot and aim it in a direction. (View transform, Transform)
- Set the camera's focal length, or adjust the zoom ratio. (Projection transform, Projection Transform)
- Take a photo and change it to the desired image size. (This is the viewport transform, we don't discuss it here.)
In these four steps, the first three can be implemented using a transformation matrix. After reading this article basic can use their own matrix to replace OpenGL inside the various transformation operations. As for why to follow the above sequence can refer to the following code, interested readers can change the position to try, where the projection transformation matrix must be placed on the leftmost.
/*project: Projection transformation Matrix view: View transformation matrix Model: Matrix */modelviewproject = Project * View * model;
The following need to know some matrix and homogeneous coordinates of the knowledge, online has a lot of information, if you do not know the next can be understood.
It is necessary to explain in depth the meaning of the homogeneous coordinates. Assuming a 13-D vector x, we linearly transform the vector to Tx, but no matter if T is any 3*3 matrix, the translation of the vector cannot be done (e.g. x= (0,0,0),Tx It is impossible to change to (1,0,0)anyway. Here we need to use the homogeneous coordinates, the purpose of using homogeneous coordinates is to add additional values to complete the translation of the vector. With OpenGL already known, the three-dimensional data is implanted into the thought coordinate space. is to transform x= (0,0,0) into x= (0,0,0,1). T is also transformed into a 4*4 matrix.
Let's discuss one of the properties of the homogeneous coordinates, which is actually the direction rather than the position. For example, x1= (1,2,3,1) and x2= (2,4,6,2) actually represent the same position in a 3-D coordinate system. In addition, the larger the last component W, the homogeneous coordinates will be farther away. When OpenGL prepares to display the collection body, it uses the last component of the left divided by the first three components to re-transform the second coordinate into the 3-dimensional coordinate system, so that the larger the W object will show the smaller. When W is 0.0 , because the coordinates are in an infinitely close position, the display may appear infinitely large. This concept is very important because the following projection matrix is the use of this concept!
We'll show you the next three transformations . which matrices are specifically included in the. For a vertex, we perform a model transformation of the its first, with view transformations and projection transformations. We subdivide these transformations to get the following code:
/*s: Zoom R: Rotate T: Pan */modelviewproject = Project * VIEWR * VIEWT * ModelS * MODELR * modeltgl_position = M Odelviewproject * VERTEX;
below I will be in order to introduce each of the matrix, first introduce the model transformation matrix, this Part I do not elaborate, understand the people should be a lot.
(1) Modelt translation matrix
This matrix is the simplest matrix, here's an example. Let's say we're going to translate the v= ( x , y, z) in the positive direction of 2.5, as follows:
Figure 1
(2) MODELR rotation matrix
This matrix is more diverse, my previous blog about this matrix has a lot of analysis, here is not much to say. The following is a list of the corresponding rotation matrices (and most commonly used) for the three Euler angles:
Figure 2
(3) ModelS scaling matrix
This matrix is also relatively simple, just put the scaling factor factor on the diagonal line of the matrix. The specific expression is as follows:
Figure 3
These three matrices make up the model transformation matrix, which can be arbitrarily combined with the translation rotation scale matrix according to the specific requirement, and not necessarily in the order of the above code.
Here we introduce the view matrix, which we can imagine as setting the position of the camera or the human eye (view). Setting up the camera normally takes only two steps, first moving the camera to a fixed position and then converting the current coordinate system into a camera coordinate system. Unlike model transformations, which can be rotated more than once, it requires only 1 panning and a rotation operation, and does not need to be scaled.
(1) Viewt translation matrix
This matrix and Modelt are exactly the opposite. We can understand that if we want to set the camera in a world coordinate (x, Y, z) position, then we move the camera back to the world coordinate system where the displacement of the origin is (-x,-y,-z). So when we take the camera as the coordinates origin, all the objects in the original coordinate system should be added with this negative translation component. Then the translation matrix is as follows:
Figure 4
(2) VIEWR rotation matrix
We turned the original coordinate system into a camera coordinate system, not only to pan to the camera position, but also to rotate to the camera orientation. As shown in 5, we want to transform the blue coordinate system into a red camera coordinate system by rotating it. Since the three base vectors of a coordinate system are all units, the simplest way is to multiply. The practice is to point to the camera coordinate system of three base vectors, see figure 6 of the formula.
Figure 5
Figure 6
Where V points to the y axis of the camera coordinate system,U points to the x axis of the camera coordinate system, andN points to the z axis of the camera coordinate system.
the two matrices form the view transformation matrix, The matrix of the view multiplied by the model, which is referred to in the OpenGL fixed pipeline as a matrix, can be set by Glmatrixmode (Gl_modelview) .
Detailed description of various transformations in OpenGL (projection transformations, model transformations, view transformations) (i)