Can someone please explain to me what the Viewport is. I've read the OpenGL wiki and doc.gl but they give me a definition that I don't understand at all. The definition they gave me is "glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. Let (xnd,ynd) be normalized device coordinates.". I don't understand what that means.
1 Answer
The viewport as specified by glViewport
is just the rectangle in pixels on the screen that you wish to render to. Usually it's from (0,0) to (width, height) in pixels, but you could set it to a smaller region (or larger, for that matter) and it will basically scale the image as it's being rendered.
The "affine transformation from normalized device coordinates" business is saying that the GPU is going to remap from the abstract coordinates in which you define your scene, to the physical pixel coordinates on the actual image you're rendering (which in OpenGL is called window coordinates).
The coordinates of triangle vertices, after going through the projection matrix and so forth, end up in a normalized range from −1 to 1 representing your field of view. For example, x = −1 on the left edge of the camera frustum and x = 1 on the right edge of the frustum (no matter how big the actual field of view of the frustum is). To find which pixels the triangles actually cover on screen, those coordinates get linearly remapped from [−1, 1] to the range of the viewport rectangle in pixels. Technically that kind of mapping is called an "affine transformation".