OpenGL Learning Note 3 model transformation, view transformation, projection transformation, viewport transformation Introduction

Source: Internet
Author: User

Introduction to model transformations, view transformations, projection transformations, viewport transformations

There are four kinds of transformations in OpenGL, namely Model transformation, view transformation, projection transformation and viewport transformation. These four kinds of transformations are the basic operation of graphics rendering, essentially these four transformations are represented by matrix multiplication (these operations are done by a 4*4 matrix), through the transformation, we can see a variety of display effects, the simplest effect is to let the elements in a certain direction of transformation (zoom, zoom out, Flip, etc.) or crop the entity that you want to display. We'll go over the four transformations and how we relate to each other in detail next.


To display an object with three-dimensional coordinates on the screen, we need the following steps in general:

1. The model, the view transformation (not all need to transform, according to their own drawing situation to determine what kind of transformation), the effect is: rotation, translation, zooming and so on. In general, there are several transformations that need to be combined when drawing a scene.

2. Objects (or parts) that are located outside the window (i.e., the visual body, temporarily understood as a window) need to be cut off, which is equal to the last displayed figure and there are no objects outside the window. That is: "Projection transformation".

3. Set the corresponding relationship between the transformed coordinates and the pixel of the screen. This process is called viewport transformation.


We now use the action of "photographer to take the graduation picture" to explain the effect of the four transformations in the target Scene view transformation.

1. View transform: We need to hold the camera on the tripod when taking the picture, and let the lens align with the object (graphics called: scene, we will use "scene" to represent the object).

2. Model transformation: We need to arrange the objects in the scene, similar to our graduation photo, the photographer always plans for the person who takes the photograph (: The tall man stands back, the man to the left, the one to the right). The purpose of this is to make the object position in the scene what we expect.

3. Projection transformation: When the lens of the person looking very small (that is, far away from the camera), the cameraman to push the lens, so that the scene (photo) of the people to become larger, spot scene (photo) in a lot of people, no coverage, we need to pull the lens, put all the people into the scene (photo).

Note: Photos can be understood as the scene we are drawing.

4. Viewport transform: When the photographer gives us a good graduation photo, we need to make laboratory photos, we may enlarge the photo as needed (ps:12 inch to 32 inch) or reduce the photo, this process is called the viewport transform

Note: When the viewport is transformed, the laboratory is the final result of the scene we draw on the screen.

Note: The scene transformation sequence above is strictly consistent, but the view transform must appear before the model transformation, and the projection and viewport transformations can be placed anywhere before the program (drawing) ends.

the principle of four Scene view Transformations (PS: These principles can be understood, we only need to invoke OpenGL in the corresponding function to achieve the above functions): in order to transform the various views in the scene, we need to create a 4*4 matrix M, and then multiply it with the coordinates of each vertex v in the scene to achieve the transformation of the coordinates, that is, V ' =m*v. The parameters in the M matrix represent what transformations we make. The coordinates of the object in the vertex are multiplied by M into V ', and then drawn, it becomes the result we want to see.

Note: The vertex of the object has four coordinates (X,Y,Z,W), where w defaults to 1, so we write the program only the first three coordinates, that is, X, Y, Z.

Note: When we use four transformations to change the object in the scene, the normal of the object's surface is also transformed (so that the vertex coordinates of the transformed object have the correct correspondence with the normal of the plane in which it is located). PS: Normal is very important in our drawing scene.

Effects of four scene view transformations

[1] The View and model transformations together form a "Model View Matrix", which is multiplied by the world coordinates of the object, resulting in corresponding visual coordinates.

[2] If a projection matrix is defined in the program, the projection matrix will produce "clipping coordinates", which together define the "visual Body" (PS: Temporarily understood as a box, the object in this box can be displayed), the action on the scene outside the object will be cut off, Will not appear in the final scene (understood as: flushed out photos).

[3] Subsequently, the OpenGL program occurs in "Perspective division", which divides the coordinates of the object by W (w default is 1), resulting in "device coordinates" (OpenGL automatically do this step).

[4] Next, the "viewport transformation", the role of the previous step of the coordinates into "window coordinates", we can control the size of the viewport to make the scene of the object becomes larger or smaller.

The above steps for drawing a scene are summed up in a passage:

After the necessary transformations are specified in the program, we can draw the scene (which is understood to be: ready to take pictures),

(1) OpenGL transforms each vertex of an object in the scene through a model and view transformation. (2) Each vertex is then transformed according to our own projection transformation, and if the vertex bit is outside the scene, it is clipped out. (3) The remaining vertices of the transformation are divided by W (Perspective Division) and then mapped to the viewport.

Note: Someone must ask? The object shown on our computer screen needs only x, Y coordinates (the screen is two-dimensional), why is there a z-coordinate?

Z the function of the coordinates is to reflect the depth of the object's vertex (that is, from the vertex to the screen distance), because we see in the life of the scene is "near big far small", when two objects placed before us, because of the previous object occlusion, we can only see the latter object of the local (very obvious reason), Z coordinate is to achieve the above effect, when two objects in the scene X , Y coordinates, and Z coordinates are different, OpenGL You can use this information to determine which surface is obscured by another surface to avoid drawing the masked surface (which makes the program render more efficient), a technique known as "hidden surface removal technology."

This section provides a brief introduction to the concept of four scene transformations in OpenGL, and the next chapter details how the transformations are used in each section.

OpenGL Learning Note 3 model transformation, view transformation, projection transformation, viewport transformation Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.