Computer graphics and OpenGL learning six (two-dimensional observation 2.OpenGL two-dimensional observation function)

OpenGL two-dimensional observation function

This section contains a lot of conceptual content. In order to facilitate understanding and memory, you can view the function operations in the examples of the previous articles.

1. OpenGL projection mode

Before choosing the OpenGL clipping window and viewport, the appropriate mode must be established to construct the matrix for the transformation from the world coordinate system to the screen coordinate system. In OpenGL, the parameters of the clipping window must be set as part of the projection transformation. Therefore, the projection mode must be selected first. We can do this using a function that sets the modeled viewing mode in the geometric transformation.

glMatrixMode (GL_PROJECTION);
glLoadIdentity();

The glMatrixMode() function is actually a declaration of what to do next, that is, before I do the next step, I tell the computer that I want to operate on "what". This "what" is an option in the "()" of glMatrixMode (parameters) Yes, GL_PROJECTION, GL_MODELVIEW and GL_TEXTURE; if the parameter is GL_PROJECTION, this is the meaning of projection, that is, to operate the projection correlation, that is, to project the object onto a plane, just like we take a picture, the 3-dimensional object Projected to a 2-dimensional plane. (Here refers to the blog https://blog.csdn.net/jiangdf/article/details/8460012)

glLoadIdentity() is to get the identity matrix to ensure that after we switch the scene, the new observation parameters will not be mixed with the previous observation parameters.


2. GLU clipping window function

 Defining a 2D clipping window uses the following OpenGL functions:

gluOrtho2D(xwmin,xwmax,ywmin,ywmax);

The coordinate position of the clipping boundary is given as a double precision floating point number. This function gives the orthographic projection that maps the scene to the screen. For a 3D scene, this means projecting objects along parallel lines perpendicular to the 2D xy plane. But in 2D projection, the object is defined in the 2D xy plane. Therefore, an orthographic projection has no effect on a 2D scene other than transforming object positions to a normalized coordinate system . OpenGL clipping functions use a normalized coordinate range of -1 to 1.


3. OpenGL viewport function

Use the following functions to specify the parameters of the viewport

glvVewport(xvmin,yvmin,vpWidth,vpHeight);

All parameters here are given in integer screen coordinates corresponding to the display window. xvmin, yvmin specifies the position of the lower left corner of the viewport, corresponding to the lower left corner of the display window. vpWidth, vpHeight are used to set the width and height pixels of the viewport. If we do not use glViewports in our program, the default viewport size and position is the same as the display window.

Finally, the pixel colors of the primitives in the viewport are loaded into the refresh buffer at the specified screen location.


4. Create a GLUT display window

Use the following function to initialize.

glutInit(&argc,argv);

And use the following three GLUT functions to define the display window and choose its size and position:

glutInitWindowPosition(xTopLeft, yTopLeft);//Set the initial position of the upper left corner of the display window
   glutInitWindowSize(dwWidth,dwHeight);//Set the window size, the parameters are width and height respectively
    glutCreateWindow("Title");//Title

5. Set the mode and color of the GLUT display window

glutInitDisplayMode(mode);

This function is used to select the color mode (RGB or index number) and different buffer combinations, such as:

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

To display the background color of the window use:

glClearColor(red,green,blue,alpha);

In color-indexed mode, we use:

glClearIndex(index)

6. GLUT display window identification

Because an application can create multiple display windows, the display integer window identifier can be obtained using the following function:

windowID=glutCreateWindow("Title");

7. Delete the GLUT display window

Use the following function to delete a window

glutDestroyWindow(windowID);

8. Current GLUT display window

Any specified display window operation is performed on the current display window, that is, the current display window created last, or the display window specified with the following command:

glutSetWindow(windowId);

At any time, you can determine which window is currently displayed by querying the function as follows:

currentWindowId=glutGetWindow();

9. Modify the position and size of the GLUT display window

To change the position of the currently displayed window, use

glutPositionWindow(xNewTopLeft,yNewTopLeft);

The following function sets the size of the display window

glutReshapeWindow(dwNewWidth,dwNewHeight);

Use the function below to expand the current display window to the entire screen

glutFullScreen();

Whenever the reality window is changed, it is possible to change the aspect ratio of the window and deform the object. Changes to the display window can be adjusted using the following statement:

glutReshapeFunc(winReshapeFcn);

winReshapeFcn is a "callback function" for a "reshape event".



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519373&siteId=291194637
Recommended