OpenGL orthographic, Perspective projection

Source: Internet
Author: User
Tags function prototype

In OpenGL, if you want to manipulate the model, it is necessary to multiply the state of the model (the current matrix) by a matrix corresponding to this operation.
If multiplied by the transformation matrix (translation, scaling, rotation), the position of the model is transformed after multiplying;
If multiplied by a projection matrix (projecting a 2D plane), the projection of the model is set when multiplied;
If multiplied by the texture matrix (), the texture mode of the model is set.

And to specify what type of matrix to multiply, that is Glmatrimode (glenum mode);
Glmatrixmode has 3 modes: gl_projection projection, Gl_modelview model view, gl_texture texture.

Therefore, before you manipulate the projection matrix, you need to call the function:
Glmatrixmode (gl_projection); Specify the current matrix as a projection matrix
Then set the matrix to the unit matrix:
Glloadidentity ();

Then call Glfrustum () or gluperspective (), and the resulting matrix will multiply with the current matrix, creating a perspective effect;

1.glFrustum ()

The function prototype is:
void Glfrustum (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far);
Creates a perspective-oriented view body. Its operation is to create a matrix of perspective projections and multiply the matrix by the current matrix. The parameter of this function only defines the three-dimensional space coordinates of the lower-left and upper-right points of the clipping plane, i.e. (left,bottom,-near) and (right,top,-near); The last parameter far is the distance from the viewpoint of the distant clipping plane, The point space coordinates of the lower left and upper right corners are automatically generated by the function based on the perspective projection principle. Near and far represent the distance from the viewpoint, and they are always positive (Near/far must be >0).

2.gluPerspective ()

This function prototype is:
void gluperspective (gldouble fovy,gldouble aspect,gldouble znear, gldouble zfar);
Creates a symmetrical perspective view body, but its parameters are defined in the previous difference, as shown in the figure. Its operation is to create a symmetric perspective projection matrix and multiply the matrix by the current matrix. Parameter Fovy defines the angle of the field of view in the Y-z plane, the range is [0.0, 180.0]; The parameter aspect is the ratio of the projection plane width to the height, and the parameter near and far are the distance from the nearest clipping plane to the viewpoint (along the z negative axis), which are always positive values.
when the above two functions default, the viewpoint is at the origin, and the line of sight is pointing in the negative direction along the z axis.

void Mydisplay (void)
     glmatrixmode (gl_projection);
     loadidentity ();
     frustum (left, right, bottom, top, near, far);

3.glOrtho ()

The prototype of this function is:
Glortho (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far)

Six parameters, the first two are the x-axis minimum and maximum coordinates, the middle two is the y-axis, the last two is the z-axis value
It creates a parallel visual body (which is a rectangular space area).
In fact, the operation of this function is to create a orthographic projection matrix, and multiply this matrix by the current matrix.
Where the near clipping plane is a rectangle, the bottom-left corner of the rectangle is a three-dimensional space coordinate (left,bottom,-near),
The upper right Corner is (right,top,-near), the far clipping plane is also a rectangle, the lower left corner space coordinates are (LEFT,BOTTOM,-FAR), the upper right corner is (Right,top,-far).
Note that all near and far values are either positive or negative at the same time, and the values cannot be the same. If there are no other transformations, the direction of the orthographic projection is parallel to the z axis, and the viewpoint is toward the z negative axis. This means that objects are negative in front of the viewpoint, far and near, and objects are positive at the far and near when they are behind the viewpoint.
Only objects in the viewing body can be displayed.
If the last two values are (0,0), that is, the near and far values are the same, the view body depth is not, the entire scene is pressed into a plane, it will be displayed incorrectly.

Transferred from:

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: 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.