This section focuses on the use of projection transformations and viewport transformations.
1, orthographic projection: Glortho
Function 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 view body, the distance between the "high" // Near and far: the front and rear planes of the rectangular viewing body, the distance between the "depth"
void gluortho2d (gldouble left,gldouble right,gldouble bottom,gldouble top);// same as Glortho, Only the object has a z coordinate of 0, and 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
voidMydisplay (void) {glclear (gl_color_buffer_bit); glcolor3f (0,1,0); //Glmatrixmode (gl_projection); The following function automatically makes the matrix transformation, which can not be written hereGlortho (-1,1,-1,1,-1, the);//point of view at the origin, and the object is shifted to 10, in order to be able to see the object, the visual body must contain objects, so set the//Glmatrixmode (Gl_modelview); The following function automatically makes the matrix transformation, which can not be written hereGltranslatef (0,0,-Ten); 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
voidMydisplay (void) {glcolor3f (0.0f,1.0f,0.0f); { //perspective projection, gl_projection the matrix into a projection matrix by parameter, then sets the coefficients, adjusts the projectionGlmatrixmode (gl_projection); 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 whole of the modelGlmatrixmode (Gl_modelview);//can not write, conversion function internal converted matrix ModeGltranslatef (0,0,-1); Glutwiresphere (0.5, -, -); } glflush ();}
(2) Change width and height
Glfrustum (-1,0, -1,11,2); 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,10.5,2); the distance between the near plane is 0.5: because the distance from the previous object to the viewpoint is 1, it can only change between 0~1
Glfrustum (-1,1, -1,10.2,2); the distance between the near plane is 0.2: because the distance from the previous object to the viewpoint is 1, it can only change between 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, z plane open angle, the range of the peripheral vision of the human eye analogy. 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 closest clipping surface of the viewer to the scene // Zfar: Specifies the distance (must be a positive number) of the farthest clipping surface of the viewer to the scene body
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.
voidMydisplay (void) {glcolor3f (0.0f,1.0f,0.0f); { //perspective projection, gl_projection the matrix into a projection matrix by parameter, then sets the coefficients, adjusts the projectionGlmatrixmode (gl_projection); Gluperspective ( Max,1,0, -); //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 whole of the modelGlmatrixmode (Gl_modelview); Gltranslatef (0,0,-1); Glutwiresphere (0.5, -, -); } 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 (1,0, +);
Gluperspective (1,0, +);
Gluperspective (1,0, +);
(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.
OpenGL Zero-based coordinate transformation (bottom)