Glviewport () and glortho () functions)

Source: Internet
Author: User

There are two important projection transformation functions in OpenGL: glviewport and glortho.

Glortho is used to create an Orthogonal Parallel visual object. It is generally used when an object does not change its size because it is far from the screen. For example, commonly used engineering drawings. More accurate display is required. As its opposition, glfrustum generates a perspective projection. This is a simulation of the real situation in which people observe objects from the perspective of vision. For example, when two parallel trains arrive, after a long distance, the two rails will be in one place. Also, objects close to the eyes look larger and objects farther away look smaller.

Glortho (left, right, bottom, top, near, far), left indicates the coordinates of the left side of the visual object, right indicates the coordinates of the right side, bottom indicates the following, and top indicates the above. In a simple sense, this function is where an object is located. How do you intercept it. Here, let's skip the glviewport function. First, understand the functions of glortho. Suppose there is a sphere with a radius of 1 and the center is (0, 0, 0). Then, we set glortho (-1.5, 1.5,-1.5, 1.5,-10, 10); indicates that the entire sphere is installed in a frame with a width and height of 3. If glortho (0.0, 1.5,-1.5, 1.5,-10, 10) is set, a frame with a width of 1.5 and a height of 3 is used to bring in the right of the entire sphere; if glortho (0.0, 1.5, 0.0, 1.5,-10, 10) is set, the upper right corner of the sphere is installed in a frame with a width and height of 1.5. The preceding three situations can be shown in the following figure:

 

 

 

From the above three cases, we can get a general idea of the glortho function usage. The glortho function is only responsible for capturing images using visual objects, rather than using certain rules to present images on the screen.

Glviewport mainly implements such a function. It is responsible for displaying the height and width of the captured image on the screen.

For example, if we use the glut library to create a form: gluinitwindowsize (500,500), and then use the glureshapefunc (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 );

....

}

In this way, you can see a normal sphere. However, if we create a form with a value of 800,500, the image is distorted. See the preceding figure.

 

Because we use a square cross-section visual image, but when stretched to the screen display, it becomes glviewport (0, 0,800,500); that is, the display width, however, when the image is displayed, a square image is "actually scaled out ". Deformation occurs. In this way, we need to adjust our OpenGL display. We don't need to be as wide as 800, because we use a square visual object, so although the form is 800 wide, we only use 500 of it. Modify the program.

Void reshape (INT width, int height)

{

Int Dis = width

Glviewport (0, 0, DIS, DIS);/* Dis should be 500 */

Glmatrixmodel (gl_projection );

Glloadidentity ();

Glortho (-1.5, 1.5,-1.5, 1.5,-10, 10 );

.....

}

OK. If you can understand what I wrote. You may have a general understanding of the glviewport function.

However, the above method is to use only a part of the original screen (from 501 to 800 in width we are not used to display the image ). What if we want to use the entire OpenGL screen to display images without deformation?

Then you can only modify the glortho function. That is to say, we use a visual object with the same proportion as the form (instead of a square Visual Object) to intercept the image. For example, for the form (800,500), we use glortho (-1.5*800/500, 1.5*800/500,-1.5, 1.5,-10, 10) to intercept the form, we use a "flat" scene to intercept the image. When the OpenGL screen is displayed (800,500 ), we only need to normally display the flat captured image (the flat captured image refers to the entire captured image, including the black part around the sphere. Spherical or normal circle. For example:

Void reshape (INT width, int height)

{

Glviewport (width, height); // creates an OpenGL screen based on the size of the form.

Glmatrixmode (gl_projection );

Glloadidentity ();

If (width <= height)

Glortho (-1.5, 1.5,-1.5 * (glfloat) height/(glfloat) width, 1.5 * (glfloat) height/(glfloat) width,-10.0, 10.0 );

Else

Glortho (-1.5 * (glfloat) width/(glfloat) height, 1.5 * (glfloat) W/(glfloat) h,-1.5, 1.5,-10.0, 10.0 );

....

}

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

Void reshape (int w, int H)

{

Glviewport (0, 0, (glsizei) W, (glsizei) H );

Glmatrixmode (gl_projection );

Glloadidentity ();

If (W <= H)

Glortho (0, 1.5, 0, 1.5 * (glfloat) h/(glfloat) W,-10.0, 10.0 );

Else

Glortho (0, 1.5 * (glfloat) W/(glfloat) h, 0, 1.5,-10.0, 10.0 );

}

The resolution can be increased by 4 times.

If you modify glviewport (0, 0, 2 * (glsizei) W, 2 * (glsizei) H), you can increase the resolution by 16 times.

Complete Test procedure:

/* Build on Ubuntu 9.04 */

# Include <Gl/Gl. h>

# Include <Gl/Glu. h>

# Include <Gl/glut. h>

Void Init (void)

{

Glfloat mat_specular [] = {1.0, 1.0, 1.0, 1.0 };

Glfloat mat_shininess [] ={ 50.0 };

Glfloat light_position [] = {1.0, 1.0f, 1.0, 0.0 };

Glfloat white_light [] = {1.0, 1.0, 1.0, 1.0 };

Glfloat lmodel_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 );

Glable (gl_lighting );

Glable (gl_light0 );

Glenable (gl_depth_test );

}

Void display (void)

{

Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );

Glusolidsphere (1.0, 20, 16 );

Glflush ();

}

Void reshape (int w, int H)

{

Glviewport (0, 0, (glsizei) W, (glsizei) H );

Glmatrixmode (gl_projection );

Glloadidentity ();

If (W <= H)

Glortho (-1.5, 1.5,-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) W,-10.0, 10.0 );

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)

{

Gluinit (& argc, argv );

Fig );

Gluinitwindowsize (500,500 );

Gluinitwindowposition (100,100 );

Ngcreatewindow (argv [0]);

Init ();

Gludisplayfunc (Display );

Glureshapefunc (reshape );

Glumainloop ();

Return 0;

}

/* Cmakelists.txt */

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 not found ")

Endif (opengl_found)

If (glu_found)

Include_directories ($ {glu_include_dir })

Target_link_libraries ($ {project_name }$ {glu_libraries })

Else (glu_found)

Endif (glu_found)

Related Article

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.