英文帖子連接http://glasnost.itcarlow.ie/~powerk/GeneralGraphicsNotes/projection/viewport_transformation.htmlhtml
抽空翻譯一下,先記錄下來備用ide
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.ui
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.this
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.atom
The viewport transformation will be represented by a 4x4 matrix. It encapsulates 2 stagesspa
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.orm
The width of the viewport is vr−vl and the height of the view port is vt−vbhtm
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.ip
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;
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.
In OpenGL the viewport transformation is controlled by the function glViewPort()which is used to set the size and position of the viewport.