OpenGL coordinate conversion (good text-clear version)

Source: Internet
Author: User

The following article details the conversion of coordinates in OpenGL, which is clear and unambiguous. But the so-called rendering pipeline includes only Modelview transformations and projection transformations, which I think is not the case. That's just the angle of the coordinates. For example, what vertex coloring, rasterization, sent to the frame cache are not involved.

Original address: http://blog.csdn.net/zhulinpptor/article/details/5897102

1. OpenGL Rendering Pipeline

OpenGL rendering pipelines are divided into two parts, Model observation Transformations (Modelview transformation) and projection transformations (Projection transformation). As a metaphor, computer graphics development is like taking pictures of us, and the aim is to show the real scene on a photographic paper. Then the process of observing the transformation is like the position of the camera, selecting the object to be photographed, and setting the shape of the object . The projection transformation is like a camera showing a real three-dimensional scene on a photographic paper. The following is a detailed breakdown of these two processes.

1.1 Model Observation Transformation

Let's start by figuring out the rendering pipeline in OpenGL. Pipelines are an abstract concept and are called pipelines because the graphics cards are processed in a fixed order, and strictly in this order . Just as water flows from one end of a pipe to the other, this order cannot be broken. Let's take a look at figure 1 below:

Figure 1 OpenGL rendering pipeline

The main part of the OpenGL graphics pipeline is shown in the figure, and it is often used in graphical programming. A vertex data enters the pipeline from the upper- left corner (MC) of the graph and finally outputs from the lower-right corner (DC) of the graph. The MC is a shorthand for model coordinate, which represents the coordinates of the models . The DC is a shorthand for device coordinate , which represents the equipment coordinates . Of course DC has a lot of, what monitor, printer and so on. Here DC we understand that the screen coordinates are often said to be good. The MC is, of course, the 3D coordinates (note: I'm talking about 3D coordinates, not world coordinates), and this 3D coordinate is the model coordinates, also called local coordinates (relative to world coordinates). The MC undergoes a model transformation (Modeling transformation) to transform to world coordinates, figure 2:

Figure 2 World coordinate system and model coordinate system

Transform to world coordinates the WC coordinate says that the simple point is how to use the world coordinate system to represent the coordinates in the local coordinate system. To speak more clearly, here's a 2D example. As shown in Figure 3:

Fig. 3 Calculation of world coordinate system and model coordinate system

The red coordinate system in the figure is the world coordinate system WC, and the green one is the model coordinate system MC. Now there is a vertex, the coordinates in the model coordinate system are (in), now you want to convert the model coordinates to world coordinates to represent. As you can see from the diagram, the coordinates of the point (3,4) in the world coordinate system are now calculated to get the results we want. First we want to represent the model coordinate system MC in the world coordinate system, using the homogeneous coordinates (homogeneous coordinate) can be expressed as a matrix (note that the matrix used in this tutorial is composed of column vectors): Where the Matrix The first column is the vector representation of the x-axis in the WC in the MC, the second column is the vector representation of the y-axis WC in the MC, and the third column is the coordinates of the origin in the WC in the MC. For those who do not know the coordinates of the secondary coordinate system, please first learn the knowledge of game mathematics. With this model transformation matrix, the coordinates in the world coordinate system can be obtained by multiplying this matrix by the coordinates represented in the MC. So the matrix and the coordinates in the MC are multiplied by:

And that's exactly what we need. Now let's add the camera coordinates too, the camera coordinates are also called the observation coordinates (view coordinate), as shown in Figure 4 and Figure 5.

Figure 4 The three coordinate systems of the Modelview transformation

Figure 5 Modelview Transformation Calculation

Take a look at how points in MC coordinates are represented in camera coordinates. From Figure 5, you can see directly that the points in the MC ( -2,-2) are in the camera coordinate system VC. And the same reasoning above, we can write the camera coordinate system VC in the world standard WC can be expressed as:

So the point in the world coordinate system is converted to a point in the camera coordinate system, we need the inverse matrix of VC:

So the point in the world coordinate system WC (3,4) in the camera coordinate system VC coordinates are:

The above transformation process is that the model coordinates can be transformed into camera coordinates . In OpenGL, when we declare vertices, sometimes we say world coordinates, because the world coordinate system, the model coordinate system and the camera coordinate system are coincident together when initializing. So the model observation transformation is provided in OpenGL, which translates the model coordinate system directly into the camera coordinate system, as shown in Figure 4. Now that we have calculated the VC-1 and MC, if we multiply the VC-1 and MC, we can get the representation of the model coordinates in the camera coordinates. In order to get the representation of coordinates in the model coordinate system in the camera coordinate system, this is the Modelview transformation matrix in OpenGL. This is also the origin of the name of the Modelview transformation, which was obtained through the above two steps. So here, the Modelview transformation matrix M is:

The coordinates in the camera coordinate system can now be obtained by multiplying the model observation matrix m multiplied by the coordinates in the model coordinate system MC . The key to the model observation transformation is to get the coordinates in the camera coordinate system, because the illumination and other calculations are done in this coordinate system. Let's check the actual OpenGL program below. In the program, we use the model in Figure 6 for ease of calculation.

Figure 6 Modelview Transformation Calculation Model

According to the data in the graph, we can write the corresponding MC and VC-1respectively, and obtain the observed transformation matrix M.

Now the program uses the GLGETFLOATV () function to get the current matrix data to check.

[CPP]View plain copy float m[16] = {0};   Used to save the current matrix data Glmatrixmode (Gl_modelview);   Glloadidentity ();    GLGETFLOATV (Gl_modelview_matrix, M);   Camera settings, View transform Glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    GLGETFLOATV (Gl_modelview_matrix, M);   Projection settings Glmatrixmode (gl_projection);   Glloadidentity ();   Glortho ( -10,10,-10,10,-10,10);    Glmatrixmode (Gl_modelview);   Modeling transform Gltranslatef (0, 0,-3);   GLGETFLOATV (Gl_modelview_matrix, M);   Glbegin (gl_points);   glvertex3f (1,1,0); Glend ();

If you set a breakpoint at the last glgetfloatv (Gl_modelview_matrix, M) in the upper section of the program, you can see the data shown in Figure 7.

Figure 7 Modelview transformation matrix data

Here, the whole modelview transformation is complete. The coordinates in the camera coordinate system are obtained by Modelview transformation. A typical calculation in this coordinate system is normal. Now take a look at the next stage.

//////////////////////////////////////////////////////////////

My understanding: Modelview transformation Matrix, is to complete the transformation from the model coordinates to the view coordinates, is a large transformation between the coordinate system. Note that Modelview has both model and view. It's not just a model matrix.

Just the function of translating or rotating the model is a function such as GLTRANSLATEF, which is called the modeling transformation. Its coordinates are based on the model itself, that is, in the model coordinate system class, such as Gltranslatef (0, 0,-3), 3 coordinate values.

Only the function that is set for the view is Glulookat, and its coordinate system is the view coordinate system, such as Glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); The origin of the coordinates inside it is at the origin of the camera coordinate system. See the projection transformation below.

/////////////////////////////////////////////////////////////////


1.2 Projection Transformation

Let's review OpenGL's rendering pipeline first. As you can see in Figure 1, the projection transformation (Projection transformation) is also divided into two parts, the first part is to convert the coordinates obtained from the previous stage to the plane coordinates , the second part is the converted The planar coordinates are normalized and trimmed . In general, three-dimensional coordinates are converted to planar coordinates in two ways: orthographic projection (orthogonal Projection) and perspective projection (Perspective Projection).

1.2.1 Orthogonal projection

Orthogonal projection is very simple, as shown in Figure 8, for three-dimensional space in the coordinate point and a two-dimensional plane, to be projected on the corresponding plane, simply the non-planar points on the coordinate components of the plane to the coordinate values, the rest of the coordinates are unchanged.

Figure 8 Orthographic projection

For example, the point (1,1,5) orthogonal projection to the z=0 plane, then the projected coordinates are (1,1,0). In OpenGL, you can use functions to set orthographic projections:

[CPP]View Plain copy Glortho (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble znear, gldouble Zfar)

The function can set the projection space of the orthogonal projection, and the coordinate points outside the space will not be projected onto the projection plane. The six parameters in the function are the projection space six planes, as shown in Figure 9:

Figure 9 OpenGL orthogonal projection space and projection transformation

In Figure 9, the large projection space is based on the projection space set by these six parameters, OpenGL will automatically normalized the space, that is, the space or cube into a variable length of 1 of the positive hexahedral projection space, and the card Hexahedral center in the camera coordinate system origin. Once Setup uses the Glortho function to set the projection space, OpenGL generates a projection matrix. The function of this matrix is to orthographic the coordinates and formalize the projected coordinates (converting from 1 to 1). It is important to note that OpenGL converts the right-handed coordinate system into a left-handed coordinate system when the matrix is generated. The reason is simple, the z-axis of the right-hand coordinate system is outside the plane, which does not conform to our habits. Matrix derivation of the matrix is not explained in detail here, the students who do not know can refer to the game mathematical data, here only the orthogonal projection matrix.

This matrix seems to be very complex, in fact, the calculation is very simple. For example, the orthogonal projection space Glortho ( -10,10,-10,10,-10,10) is now set, which is a positive hexahedral space with a length of 10. By taking these parameters into the matrix above you can get

Now it's time to check it out in the OpenGL program. Add the following code snippet to the OpenGL program:

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.