英文帖子链接http://glasnost.itcarlow.ie/~powerk/GeneralGraphicsNotes/projection/viewport_transformation.html
抽空翻译一下,先记录下来备用
The Viewport Transformation
The final stage of the vertex transformation process is to map the points from the normalized view volume to their final position in the viewport on screen. We want to transform the view volume to the shape and position of the viewport.
The normalised view volume is a cube centred on the origin, bounded by points (−1,−1,−1) and (1,1,1). The u and v coordinates of each point will be transformed to x and y screen coordinates (pixel coordinates) and we will scale the n coordinates (pseudo-depth) to a range between 0 and 1 (scale the front plane to 0 and the back plane to 1), this depth value will be called z.
The x,y sceen coordinates wil be used to draw the pixel in the refresh buffer. The z value will be stored in the depth buffer for hidden surface removal.
The viewport transformation will be represented by a 4x4 matrix. It encapsulates 2 stages
- a scaling to the shape of the viewport
- a transformation to the position of the viewport
The viewport is defined as the rectangle between (vl,vb) and (vr,vt)
The view-volume needs to be scaled to the width and height of the viewport. At this stage we will also scale the pseudo-depth to a range of 0 to 1. The normalised view volume has a height, width and depth of 2.
The width of the viewport is vr−vl and the height of the view port is vt−vb
To scale a value from one range of values to another, we divide by the size of the original range and multiply by the size of the new range.
Therefore the scaling matrix required is;
Finally we need to translate the scaled normalised view volume to the position of the viewport. This is a combination of two translations; first translate to the origin (add vr−vl2 to u and add vt−vb2 to v. Second move from the origin to the position of the viewport (add vl and vb to u and v). The n-coordinate (now in the range {−12⋯12}) needs to be shifted by +12
Note; vr−vl2+vl=vr+vl2, similarly the translation for v is vt+vb2.
So the translation matrix is;
Combining the above two transfomations gives us the Viewport Transformation Matrix;
Aspect Ratio
The aspect ratio is the relationship of the width of the viewport to its height widthheight, this is sometimes written as width:height as in 4:3 or 16:9.
It is important that the aspect ratio of the viewport is the same as the aspect ratio of the viewvolume in order to avoid distortion.
Viewport Transformation in OpenGL
In OpenGL the viewport transformation is controlled by the function glViewPort()which is used to set the size and position of the viewport.