OpenGL projection transformation Summary

Source: Internet
Author: User

Projection transformation is a key graph transformation. OpenGL only provides two projection modes: normal projection and Perspective Projection. No matter which projection function is called, in order to avoid unnecessary transformations, you must add the following two sentences before it:

Glmatrixmode (gl_projection );

Glloadidentity ();

In fact, the purpose of projection transformation is to define a visual object so that the excess part of the visual object can be cropped out. The final image is only the relevant part of the visual body. This section describes the concept and usage of projection transformation in detail.

1. Orthographic Projection)

Normal projection, also called Parallel projection. The projected visual object is a parallel pipe of a rectangle, as shown in. The biggest feature of normal projection is that the size of the projected object remains unchanged no matter how far the object is from the camera. This projection is usually used in the drawing of building blueprints and computer-aided design. These industries require that the size and angle of the projected objects remain unchanged, so that the proportion of the objects during construction or manufacturing is correct.

In this mode, you do not need to set the camera location, direction, and viewpoint location, that is, you do not need the glulookat function.

OpenGL has two normal projection functions.

A function is:

Void glortho (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far)

It creates a parallel visual object. In fact, this function is used to create a normal projection matrix and multiply it by the current matrix. The near-crop plane is a rectangle, the three-dimensional coordinate of the point in the lower left corner of the rectangle is (left, bottom,-near), and the point in the upper right corner is (right, top,-near ); the far crop plane is also a rectangle. the coordinate of the point space in the lower left corner is (left, bottom,-far), and the point in the upper right corner is (right, top,-far ). All near and far values are both positive and negative. If there are no other transformations, the direction of the projection is parallel to the Z axis, and the viewpoint is oriented to the Z negative axis. This means that far and near are both negative values before the object's viewpoint, and far and near are both positive values after the object's viewpoint.

 

Another function is:

Void gluortho2d (gldouble left, gldouble right, gldouble bottom, gldouble top)

It is a special normal projection function, which is mainly used for the projection of two-dimensional images to two-dimensional screens. The default values of near and far are-1.0 and 1.0, respectively. The zcoordinates of all two-dimensional objects are 0.0. Therefore, the cropping area is a rectangle with the (left, bottom) and (right, top) points in the lower left corner.

 

2. Perspective Projection)

Perspective Projection is in line with people's psychological habits, that is, objects close to the viewpoint are large, objects far from the viewpoint are small, and objects far from the pole disappear and become the extinction point. Its visual body is similar to an edges removed from the top and bottom, that is, a pyramid. This projection is usually used for animation, visual simulation, and many other aspects with authenticity reflection. There are also two OpenGL perspective projection functions, the glfrustum () function ().

In this case, you need to use glulookat to set the camera position, camera direction (generally set to (0, 1, 0), and viewpoint position.

This function is prototype:

Void glfrustum (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far );

It creates a perspective visual body. The operation is to create a pivot projection matrix and multiply it by the current matrix. The parameters of this function only define the three-dimensional space coordinates of the lower-left and upper-right vertices of the near-cropping plane, namely (left, bottom,-near) and (right, top,-near ); the final far parameter is the Z negative value of the far-cropping plane. The coordinate between the point space in the lower-left corner and the upper-right corner is automatically generated by the function based on the perspective projection principle. Near and far indicate the distance from the viewpoint. They are always positive values.

Another function is:

Void gluperspective (gldouble fovy, gldouble aspect, gldouble znear, gldouble zfar );

It also creates a symmetric perspective visual body, but its parameters are defined differently, as shown in. The operation is to create a symmetric Perspective Projection Matrix and multiply it by the current matrix. The fovy parameter defines the angle of the field of view on the X-Z plane in the range of [0.0, 180.0]; The aspect parameter is the ratio of the projection plane width to height; the znear and far parameters are the distance from the cropping surface to the eyes. They are always positive values.

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.