Section 3.5. The Viewport


3.5. The Viewport

In the final stage of the transformation pipeline, OpenGL takes the 1.0 to 1.0 normalized device-coordinate cube and maps it into window coordinates. The x and y window-coordinate values correspond to window subpixel locations with the origin in the bottom-left corner. Window-coordinate z values correspond to depth buffer values in which values increase with distance from the viewpoint.

Applications can control the mapping from normalized device coordinates to window coordinates with the glViewport() command.


void glViewport( GLint x, GLint y, GLsizei w, GLsizei h );


Determines the xy mapping from normalized device coordinates to window coordinates. Vertices in normalized device coordinates are mapped to an area of the window with the bottom-left corner at (x, y), width w, and height h. x and y can be negative, but w and h must be positive.

There is a similar function, glDepthRange(), for controlling the mapping of normalized device-coordinate z values into window-space depth buffer values. By default, the full range of normalized device-coordinate z values map to the full range of window-space depth buffer values, which is sufficient for most rendering cases. Because glDepthRange() is not typically used, it's not covered in this book. See OpenGL® Reference Manual for more information.

The default viewport setting maps the entire 1.0 to 1.0 normalized device-coordinate cube to the full window. If the window is resized, by default the viewport values remain unchanged, and OpenGL continues to map normalized device coordinates to the previous window size. As a result, some parts of the new window could be blank, or some of the geometry might be clipped outside the window boundary. To prevent this, call glViewport() when the window size changes.

Although OpenGL follows the Cartesian coordinate system first-quadrant standard of placing the origin in the bottom-left corner, many computing platforms place the origin in the top-left corner, with positive y pixel values proceeding downward. Applications need to convert mouse xy positions before interpreting them as OpenGL window coordinates. Given an operating system window coordinate (osX, osY), the following code assumes a top-left-corner origin and converts the values to OpenGL window coordinates (openglWinX, openglWinY ):

 GLint vp[4]; glGetIntegerv( GL_VIEWPORT, vp ); GLfloat openglWinX = osX; GLfloat openglWinY = vp[3]  osY + vp[1]; 


After OpenGL transforms vertices into window space, transformation is complete, and the next stage of the rendering process is rasterization, as discussed in Chapter 1.




OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net