OpenGL path (2) setting the observation matrix and avoiding Drawing Deformation

Source: Internet
Author: User

After the environment is configured successfully, run the sample code. You must find that the triangle is deformed as the window is stretched. The problem is solved below.

The Code is as follows:

# Include <Gl/glut. h> # include <Gl/Glu. h> # include <Gl/GL. h> # pragma comment (Lib, "opengl32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glut. lib ") void renderscene (void) {glclear (gl_color_buffer_bit | callback); glbegin (gl_triangles); glvertex3f (0.0, 0.0, 0.0); glvertex3f (0.5, 0.0, 0.0 ); glvertex3f (0.0, 0.5, 0.0); glend (); glbegin (gl_quads); glvertex3 F (1.0, 1.0, 0.0); glvertex3f (2.0, 1.0, 0.0); glvertex3f (2.0, 0.0, 0.0); glvertex3f (1.0, 0.0, 0.0); glend (); gluswapbuffers ();} void changesize (int w, int h) {// prevent the divisor (that is, the height is 0) // (you can set the window width to 0 ). if (H = 0) H = 1; float ratio = 1.0 * W/H; // unitized projection matrix. Glmatrixmode (gl_projection); glloadidentity (); // you can specify the window size as glviewport (0, 0, W, h). // set the correct projection matrix gluperspective (45, ratio, 1, 1000); // The following is the set model view matrix glmatrixmode (gl_modelview); glloadidentity (); glulookat (0.0, 0.0, 5.0, 0.0, 0.0,-1.0, 0.0f, 1.0f, 0.0f); // set the observation point} int main (INT argc, char * argv []) {gluinit (& argc, argv ); fig ); // call the function glureshapefunc (changesize) when the specified program is idle; // specify the callback function glable (gl_depth_test) when the window shape changes; glumainloop (); Return 0 ;}


OpenGL path (2) setting the observation matrix and avoiding Drawing Deformation

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.