OpenGL Edge Learning------2 OpenGL state, viewport settings

Source: Internet
Author: User

    • The state management mechanism of OpenGL
    • Viewport and viewport coordinate system concepts
    • Test Viewport settings
      • 1 Moving the Viewport
      • 2 Multi-viewport
    • Viewport summary

1 status management mechanism for OpenGL

As we can see from the simplest examples above, we have almost no configuration of colors and coordinate systems, and OpenGL has been able to implement rendering. This is because OpenGL itself manages many of the state data that is required for rendering, and it automatically sets a reasonable default value when it is initialized. For example, the default clear screen color is black, which is why we see the window client area black.

OpenGL rendering requires a lot of state data for its use, and if all of this data is passed as a parameter to the render function, the function looks like this.

glXXX(渲染上下文,颜色,坐标,视口,光照,雾,.....);

This is a nightmare for client use! Because most of the rendering behavior requires these state data, O Peng GL simply makes all the data used by these people into global variables for use by all render functions. This allows the rendering function to be simplified into a

glXXX(坐标);

Of course, the global variable also brings the problem of non-reentrant rendering function, fortunately, in OpenGL, the function re-entry is not considered important. It is customary to call this a state machine that uses a large number of global state data.

In fact, many libraries have taken a state machine mechanism, such as Windows GDI.

2 viewport and viewport coordinate system concepts

The essence of computer graphics is to create two-dimensional images of three-dimensional objects , which involve transformation between multiple coordinate systems. This article starts with the simplest viewport coordinate system . The viewport (Viewport) is the destination where the final rendering results are displayed. It is a rectangular area with pixels in length and the position and size of the viewport defined in the viewport coordinate system . The viewport coordinate system is a standard cartesian rectangular coordinate system whose origin is in the lower-left corner of the client area of the render environment window, where the horizontal axis (x) is positive and the vertical (Y) is positive. As shown in the following:

Note: The viewport coordinate system is different from Windows's window coordinate system.
The functions OpenGL uses to set Viewports are:

void WINAPI glViewport(   GLint   x,   GLint   y,   GLsizei width,   GLsizei height);

function parameters are for the viewport coordinate system. For example

glViewport(100,50200150);

Set the viewport location and size as shown:

The execution of each rendering (Glbegin () and Glend () the middle of the code) is the current viewport as the final output destination. The viewport is also one of the state variables that OpenGL maintains internally, which can be changed multiple times in one frame of rendering, and OpenGL uses the current viewport when performing rendering. The window client area can be split into multiple viewports, but only one viewport takes effect at the same time.

when initializing the OpenGL window environment, the viewport is set to fill the entire customer area .

3 Test Viewport Settings 3.1 Move viewport

In the response to the wm_lbuttondown event, we set the lower-left corner coordinates of the viewport to the position of the mouse, the width to 200 pixels, and the height to 150 pixels. Then re-render the OpenGL.

void OnLeftButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    RECT clientRect;    ::GetClientRect(hWnd, &clientRect);    int/* 需要包含 <windowsx.h> */    int yPos = GET_Y_LPARAM(lParam);    int x = xPos;                   /* 把窗口坐标系坐标转换为视口坐标系坐标 */    int y = clientRect.bottom - yPos;    200150);    InvalidateRect(hWnd, NULL, TRUE);}

In wm_paint event handling, the current viewport position and size is marked by drawing a rectangle that fills the entire visual space. Note: The default color state (white) is used when we draw.

void OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    glClear(GL_COLOR_BUFFER_BIT);    glBegin(GL_QUADS);    glVertex2d(-1, -1);    glVertex2d(1, -1);    glVertex2d(11);    glVertex2d(-11);    glEnd();    HDC hdc = ::GetDC(hWnd);    ::SwapBuffers(hdc);    ::ReleaseDC(hWnd, hdc);}

This way, the current viewport area is labeled whenever a mouse is clicked in the client area.


SOURCE download

3.2 Multi-viewport

OpenGL Viewports are also one of the many state variables that are maintained internally, and can only be output using the current viewport in a single OpenGL rendering (the rendering code between Glbegin () and Glend (). However, a frame image can contain any number of OpenGL renderings, that is, Glbengin () and Glend () can be called multiple times in the program. For example, the following pseudo-code:

glViewport(A);glBegin();渲染在房子里面看到的场景glEnd();glViewport(B);渲染在外面看房子的场景glBegin();glEnd();

So we get two different viewports, a, B. Eventually A and b make up a full frame of the image.

Our program is not yet able to render such a complex scene, then it is represented by a simple color rectangle.
Modify the render function as follows:

voidOnPaint (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {glclear (gl_color_buffer_bit); GLCOLOR3F (1,0,0); Glviewport (0,0, -, -);    Glbegin (gl_quads); Glvertex2d (-1, -1); Glvertex2d (1, -1); Glvertex2d (1,1); Glvertex2d (-1,1);    Glend (); GLCOLOR3F (0,1,0); Glviewport ( -, -, -, -);    Glbegin (gl_quads); Glvertex2d (-1, -1); Glvertex2d (1, -1); Glvertex2d (1,1); Glvertex2d (-1,1);    Glend ();    HDC HDC =:: GetDC (HWND);    :: Swapbuffers (HDC); :: ReleaseDC (HWnd, HDC);}


SOURCE download

4 Viewport Summary

Viewports are the simplest concept in OpenGL, and the viewport coordinate system is the only coordinate system related to the window environment. Most programs, using one viewport is enough, the most likely case is to automatically adjust the viewport size in the Wm_size event response so that it always fills the entire window client area.

void OnSize(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    int cx = LOWORD(lParam);    int cy = HIWORD(lParam);    glViewport(00, cx, cy);}

OpenGL Edge Learning------2 OpenGL state, viewport settings

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.