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

Source: Internet
Author: User

Http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html

There are two more important projection transformation functions in OpenGL, 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 you set Glortho (0.0, 1.5, 0.0, 1.5, 10, 10), it means using a wide The upper-right corner of the sphere is loaded into the box with a 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. 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), then the image we see is distorted. The above situation is shown in figure.

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 is wider, it is displayed when the image of a square "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)

{

int dis = width < height? Width:height;

Glviewport (0, 0, dis, dis); /* Here dis should be 500*/

Glmatrixmodel (gl_projection);

Glloadidentity ();

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

.....

}

Ok. If you can see what I'm writing about. You may have a general understanding of the Glviewport function.

However, we used the above approach, that is, only use a portion of the original screen (width from 501 to 800 we do not use to display images). What if we want to display the image with the entire OpenGL screen but not distort the image?

Then you can only modify the Glortho function. That is, we use a visual body that is the same scale as the form (instead of a square view) to intercept the image. For example, for a form (800, 500), we use Glortho (-1.5 * 800/500, 1.5 * 800/500,-1.5, 1.5,-10, 10)-When we intercept, we use a "flat" view of the body to intercept, then, When the display to the OpenGL screen (800, 500), we just normal to the flat cut image display (flat truncated image refers to the entire captured image, including the black parts around the sphere. Spherical or normal round), it's OK. Such as:

void reshape (int width, int height)

{

Glviewport (width, height); Making OpenGL screens by Form size

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

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 enlarged by 4 times times.

And if you modify Glviewport (0, 0, 2 * (Glsizei) W, 2 * (Glsizei) h); The resolution can be enlarged by 16 times 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);

Glenable (gl_lighting);

Glenable (GL_LIGHT0);

Glenable (gl_depth_test);

}

void display (void)

{

Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);

Glutsolidsphere (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)

{

Glutinit (&ARGC, argv);

Glutinitdisplaymode (Glut_single | Glut_rgb | Glut_depth);

Glutinitwindowsize (500, 500);

Glutinitwindowposition (100, 100);

Glutcreatewindow (Argv[0]);

Init ();

Glutdisplayfunc (display);

Glutreshapefunc (reshape);

Glutmainloop ();

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 (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 (RPM)

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.