OpenGL-GLUT tutorial (iii) glut window settings

Source: Internet
Author: User

GLUTTutorial

Prepare for transformation window

Download the following vcpus and run the program (glu0.zip) (this is the project in the previous section ). You will see two windows: a console window and an OpenGL window. Change the window size so that the height and width are no longer equal, and the triangle is deformed. This occurs because you have not correctly set the projection matrix. The default value is the Perspective Projection Matrix and the aspect ratio is 1. Therefore, the height-to-aspect ratio changes, and the projection will be deformed. Therefore, as long as the aspect ratio changes, the projection should be recalculated.

 

GLUT defines which function should be called when the window size changes. In addition, this function will be called when the window is created for the first time to ensure that the rendering will not be deformed when the initialization window is not a square.

 

This function is glureshapefunc ().

Void glureshapefunc (void (* func) (INT width, int height ));

Parameters:

FUNC: the name of the function responsible for setting the correct projection.

Therefore, the first thing we must do is return to the main () function. In the code in the previous chapter, add the call to the glureshapefunc (). Let's call the function responsible for the window size changesize. The current Code is as follows.

void main(int argc, char **argv) {

         glutInit(&argc, argv);

         glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

         glutInitWindowPosition(100,100);

         glutInitWindowSize(320,320);

         glutCreateWindow("3D Tech- GLUT Tutorial");

         glutDisplayFunc(renderScene);

         

         // Here is our new entry in the main function 

         glutReshapeFunc(changeSize);

         

         glutMainLoop();

}

What we need to do below is to define the function changesize (). The Declaration of the glureshapefunc () function shows that the changsize () function has two parameters .. These two parameters represent the new window height and width.

void changeSize(int w, int h) {

 

         // Prevents the divisor from being 0 in height

         // (You can set the window width to 0 ).

         if(h == 0)

                 h = 1;

 

         float ratio = 1.0* w / h;

 

         // Unit projection matrix.

         glMatrixMode(GL_PROJECTION);

         glLoadIdentity();

         

         // Set the window size to increase the window size.

         glViewport(0, 0, w, h);

 

         // Set the correct projection matrix

         gluPerspective(45,ratio,1,1000);

        //The following figure shows how to set the 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
  );

}

We introduced some functions in the previous code. Let's explain it in detail to avoid confusion.

The first step is to calculate the aspect ratio (wight/height ). Note that in order to calculate correctly, we must ensure that the height is not 0.

 

Next, we set the current matrix as the projection matrix, which defines a visual space (Viewing Volume ).

We call a matrix of units to initialize the projection matrix. Then we use the glviewport function to set the window as the whole window. You can also set different values. The function (0, 0) specifies the lower left corner of the window, and (w, h) specifies the size of the window rectangle. Note that the coordinates are related to the customer region, not the screen coordinates ..

 

The gluperspective function is a function in other OpenGL libraries. (For details about the parameters of the gluperspective function, see other books. I don't want to add some other OpenGL content to the glut explanation .)

 

The last step is to set the model observation matrix. Call gl_modelview to set the current matrix as the model observation matrix. Glulookat () is also a function in the Glu library. For detailed parameter settings, see other books.

 

The following section describes the vcss project (glu1.zip). You can set the size and change the parameter value. You must understand how the program runs.

(My translation skills are limited. I hope to correct the mistakes. Thank you)
Source: http://www.lighthouse3d.com/opengl/glut/index.php? 3

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.