Understanding of the Glviewport () function and the Glortho () function

Source: Internet
Author: User

Understanding of the Glviewport () function and the Glortho () function

OpenGL has two more important projection transformation functions, Glviewport and Glortho.
Glortho is the creation of an orthogonal parallel view body. It is generally used for objects that do not cause a change in size because of the distance from the screen. For example, drawings in commonly used projects. Requires a more accurate display. As a result of its confrontation, Glfrustum produces a perspective projection. This is a simulation of real life, people's vision of the actual situation of the observation of objects. For example: Observe two parallel trains, and after a long distance, these two rails will intersect in one place. Also, objects closer to the eye look larger, and distant objects look smaller.

The Glortho (left, right, bottom, top, near, far), which represents the coordinates to the right of the scene body, right represents the right coordinate, and the bottom represents the following, top represents the top. This function is simple to understand, that is, an object is placed there, how do you intercept him. Here, let's just throw away the Glviewport function without looking. First, understand the function of Glortho alone.  Suppose there is a sphere with a radius of 1, the center of the Circle in (0, 0, 0), then we set Glortho (-1.5, 1.5, 1.5, 1.5, -10,10); it means that the sphere is loaded in a box with a wide height of 3. If set Glortho (0.0,1.5,-1.5, 1.5,-10, 10); It means using a box with a width of 1.5 and a height of 3 to load the right side of the whole sphere; if set Glortho (0.0, 1.5, 0.0, 1.5, -10,10) , the upper-right corner of the sphere is loaded in a box with a width and height of 1.5. The above three cases can be seen in the picture:

From the above three cases, we can get a general understanding of the use of the Glortho function.

---glviewport ():
The Glortho function is only responsible for using what kind of scene to intercept the image, and is not responsible for using some sort of rule to render the image on the screen.
Glviewport primarily accomplishes such a function. It is responsible for how the images captured by the scene are displayed to the screen in terms of height and width.
For example: if we use the GLUT library to create a form: Glutinitwindowsize (500, 500), then use Glutreshapefunc (reshape); The reshape code is as follows:

void reshape (int width, int height)
{
Glviewport (0, 0, (Glsizei) width, (glsizei) height);
Glmatrixmodel (gl_projection);
Glloadidentity ();
Glortho (-1.5, 1.5,-1.5, 1.5,-10, 10);

....
}
It is possible to see a normal sphere. However, if we create the form glutinitwindowsize (800,500), the image we see is distorted. The above situation is shown in figure.

< Span style= "color: #494949; Font-family:arial, Helvetica, Sans-serif; line-height:22px; " > because we are using a square cross-sectional view of the image, but stretched to display on the screen, it becomes glviewport (0, 0, 800,500); that is, the display becomes wider, but the display of a square image "live to pull wide." will produce deformation. In this way, we need to adjust our OpenGL display. We can not be so wide as 800, because we are using the square of the view body, so although the form is 800 wide, but we only use 500 of them enough. Modify the program.
void reshape (int width, int height)
{
   glviewport (0, 0, Dis,dis);   

Glmatrixmodel (gl_projection);
Glloadidentity ();
Glortho (-1.5, 1.5,-1.5, 1.5,-10, 10);
.....
}

In addition, we can also use the Glviewport () function to adjust the resolution of the image. For example, to keep the size of the current form unchanged, if we use this size to display only part of the entire object, the resolution of the image will inevitably increase. For example:

< Span style= "color: #494949; Font-family:arial, Helvetica, Sans-serif; line-height:22px; " > 

void reshape (int w, int h)
{
    Glviewport (0, 0, (Glsizei) W, (Glsizei) h);
   glmatrixmode (gl_projection);
   glloadidentity ();
&NBSP;&NBSP;&NBSP;ELSE
        Glortho (0, 1.5* (glfloat) w/(glfloat) h, 0, 1.5, -10.0,10.0);
can expand the resolution by 4 times times.

And if you modify Glviewport (0, 0, 2 * (Glsizei) W, 2 * (Glsizei) h), you can enlarge the resolution by 16 times times.

Complete test procedure:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void init (void)
{
Glfloatmat_specular[] = {1.0, 1.0, 1.0, 1.0};
Glfloatmat_shininess[] = {50.0};
Glfloatlight_position[] = {1.0, 1.0f, 1.0, 0.0};
Glfloatwhite_light[] = {1.0, 1.0, 1.0, 1.0};
Glfloatlmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
Glclearcolor (0.0, 0.0, 0.0, 0.0);
Glshademodel (Gl_smooth);
GLMATERIALFV (Gl_front, Gl_specular, mat_specular);
GLMATERIALFV (Gl_front, gl_shininess, mat_shininess);
GLLIGHTFV (Gl_light0, gl_position, light_position);
GLLIGHTFV (Gl_light0, Gl_diffuse, white_light);
GLLIGHTFV (Gl_light0, Gl_specular, white_light);
GLLIGHTMODELFV (Gl_light_model_ambient, lmodel_ambient);

Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Glenable (gl_depth_test);

}

vo ID display (void)
{
    glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
   glutsolidsphere (1.0, +);
   glflush ();

vo ID reshape (int w, int h)
{
    glviewport (0, 0, (Glsizei) W, (Glsizei) h);
   glmatrixmode (gl_projection);
   glloadidentity ();
   else
        glortho ( -1.5* (glfloat) w/(Glfloat) H, 1.5* (Glfloat) w/(GLfloat) h,-1.5, 1.5,- 10.0, 10.0);

Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

int main (int argc, char **argv)
{
Glutinit (&AMP;ARGC, argv);
Glutinitdisplaymode (Glut_single | Glut_rgb | Glut_depth);
Glutinitwindowsize (500, 500);
Glutinitwindowposition (100, 100);
Glutcreatewindow (Argv[0]);
Init ();
Glutdisplayfunc (display);
Glutreshapefunc (reshape);
Glutmainloop ();
Return0;
}

PROJECT (S5)
Cmake_minimum_required (VERSION 2.6)
Add_executable (S5 Main.cpp)


Find_package (OpenGL)
Find_package (GLUT)

IF (Opengl_found)
Include_directories (${opengl_include_dir})
Target_link_libraries (${project_name}${opengl_libraries})
ELSE (Opengl_found)
MESSAGE (fatal_error "OpenGL NotFound")
ENDIF (Opengl_found)

IF (Glut_found)
Include_directories (${glut_include_dir})
Target_link_libraries (${project_name}${glut_libraries})
ELSE (Glut_found)

ENDIF (Glut_found)

Understanding of the Glviewport () function and the Glortho () function

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.