The coordinate transformation of OpenGL learning (bottom)

Source: Internet
Author: User
Tags in degrees

This section focuses on the use of projection transformations and viewport transformations.

1, orthographic projection: Glorthofunction Prototypes:
void Glortho (gldouble left,gldouble right,gldouble bottom,gldouble top, gldouble near,gldouble FAR);// Left and right: a rectangular view of the plane, the distance between the "wide"//bottom and top: the upper and lower plane of the rectangular visual body, between the distance between the "high"//near and far: the front and rear plane of the rectangular visual body, between the distance is "depth"
void gluortho2d (gldouble left,gldouble right,gldouble bottom,gldouble top);//Same as Glortho, except that the object has a z-axis coordinate of 0, the default near-far plane distance is 1 and 1

* Note: The meaning of the near-far plane distance from the glfrustum is different:

The near-far plane distance in the glfrustum: the distance from the viewpoint to the near and far plane, only considering the distance, regardless of the direction, so must be positive .

The near-far plane distance in the Glortho: the distance from the viewpoint before and after, that is, the viewpoint can be inside the scene (not inside the object), ( -1,10), near the plane in the point of view behind 1 units distance, far plane in the front of the viewpoint 10 unit distance

void Mydisplay (void) {    glclear (gl_color_buffer_bit);    glcolor3f (0,1,0);    Glmatrixmode (gl_projection); The following function automatically makes the matrix transformation, which can not write    Glortho ( -1,1,-1,1,-1,15);//The viewpoint is at the origin, and the object is shifted to 10, in order to be able to see the object, The visual body must contain the object, so set    the//glmatrixmode (Gl_modelview); The following function automatically does the matrix transformation, here can not write    Gltranslatef (0,0,-10);    Glutwireteapot (1);    Glflush ();}

2. Perspective projection: Glfrustum

Function Prototypes:

void Glfrustum (gldouble left,gldouble right,gldouble bottom,gldouble top, gldouble near,gldouble far);

Left, right: the distance between the sides of the crop window, the distance between the edges of the viewport and the object line: the centerline. The left and right borders determine the width of the viewport.

Bottom, top: The plane distance from the top edge of the clipping window, and the distance (the point of view and the object line: centerline) up and down. The upper and lower bounds determine the height of the viewport.

Znear, Zfar: The distance from the viewpoint to the near and far plane must be positive.

Because the use of a few, so directly see the effect of transformation.

(1) The near-plane distance is 0

void Mydisplay (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    {        //perspective projection, by parameter gl_projection, the matrix is transformed into a projection matrix, then the coefficients are set and the projection        Glmatrixmode (gl_projection) is adjusted;        Glfrustum ( -1,1, -1,1, 0,2);//The first four variables control the width and height of the bevel, Width: 2, Height: 2, and the last two adjust the distance.        //model transformations, because the default viewpoints and models are all at the origin, so if you do not set the model movement, the viewpoint is at the center of the model, and the projection cannot see the overall glmatrixmode of the model        (Gl_modelview);//Can not write, conversion function Internal transformed matrix mode        Gltranslatef (0,0,-1);        Glutwiresphere (0.5,20,20);    }    Glflush ();}

(2) Change width and height

Glfrustum ( -1,0,  -1,1,);//width: 1, High: 2, "the right half does not show = = Line of sight left 1", if the distance of the near plane is 0, when the image is fixed, other parameter adjustment does not work.
The right half does not show = = line of sight shift left 1

(3) Adjust the distance

Glfrustum ( -1,1,  -1,1, 0.5,2);//The distance of the near plane is 0.5: since the distance of the object from the viewpoint is 1, it can only change between the 0~1
Glfrustum ( -1,1,  -1,1, 0.2,2);//The distance of the near plane is 0.2: since the distance of the object from the viewpoint is 1, it can only change between the 0~1


3. Perspective projection: gluperspective

Function Prototypes:

void Gluperspective (gldouble fovy,gldouble aspect,gldouble znear, gldouble Zfar),//fovy: Specifies the angle of view of the scene  body, in degrees, X, The angle of the Z-plane opens, which is a metaphor for the range of the human eye. When the angle becomes smaller than the squint, the field of view shrinks to the front, at which point the object becomes smaller and the distance pulls away. Aspect: Specifies the aspect ratio of your scene, affecting the aspect ratio of the projected image//znear: Specifies the distance (positive) of the nearest cropped face of the viewer to the scene//zfar:  Specifies the distance (positive) of the farthest clipping surface of the viewer to the scene
Personal Understanding: Projection imaging and image is a reverse process, the picture is to zoom out to the screen, while the visual is the reverse process, aggregation imaging.

void Mydisplay (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    {        //perspective projection, by parameter gl_projection, the matrix is transformed into a projection matrix, then the coefficients are set and the projection        Glmatrixmode (gl_projection) is adjusted;        Gluperspective (150,1,0,100);        The model transforms, because the default viewpoints and models are all at the origin, so if the model movement is not set, the viewpoint is at the center of the model, and the projection cannot see the overall        Glmatrixmode (Gl_modelview) of the model;        Gltranslatef (0,0,-1);        Glutwiresphere (0.5,20,20);    }    Glflush ();}

(1) First look at the effect of angle change

Directly modify the Gluperspective (150,1,0,100) in the above code, the first parameter represents the angle of the implementation opening.

Borrow an understanding of this section in a blog post: http://blog.csdn.net/shuaihj/article/details/7228575

When we squint, the angle of view is small, the object in front of us will be clearer, similar to the feeling of keyhole imaging, telescope or camera focusing , the object of the image will be narrowed and displayed relatively large. When we open our eyes, our vision becomes larger and divergent, the object becomes smaller in the field of vision, or the eye is-_-.

Gluperspective (55,1,0,100);
Gluperspective (15,1,0,100);
Gluperspective (170,1,0,100);

(Left: 55°, Middle: 15° Right: 170°)

(2) Look at the effect of distance change

When the position of the object is not moving, the imaging result of the projection is determined by the angle and the angle of view is fixed, then the result is invariable. This is not the same as glfrustum, more focused on the use of angle to control imaging.

It is important to note that the nearest plane: Znear, must be between the viewpoint and the object, otherwise it cannot be imaged.






The coordinate transformation of OpenGL learning (bottom)

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.