Reprint: http://www.cnblogs.com/graphics/archive/2009/10/13/1582773.html
Viewport transformation after the projection transformation, the role of the viewport transformation is to convert the point on the projection plane to a point on the screen, which is a scaling process.
The function of the projection transform is to project the scene in the viewing frustum (visual body) to a plane, which is the projection plane, in DirectX, using the near clipping plane as the projection plane (z=1), The matrix solution of perspective projection, refer to the perspective projection detailed
The viewport transformation transforms the object in the cuboid into the viewport, as shown in the following figure.
Where the cuboid coordinate range is
The coordinate range of the viewport is
Note: From the figure above, the viewport starts with (X, Y), the width height is the width and the height,x axis to the right, the Y axis is positive, and the Y axis is opposite to the three-dimensional coordinates. The viewport is a 2D plane, but in the viewport transformation, the z-coordinate is also transformed, but not reflected in this figure.
First column of the transformation matrix is evaluated first
The upper-left corner of the cuboid (-1, 1, 0, 1) maps to the starting point in viewport (X, Y, MinZ, 1),
The upper-right corner of the cuboid (1, 1, 0, 1) maps to points in viewport (X+width, Y, MinZ, 1),
Suppose that the first column of the transformation matrix [x ', y ', z ', 1]t matrix multiplication has
[-1, 1, 0, 1]* [x ', y ', z ', 1]t = x
[1, 1, 0, 1]* [x ', y ', z ', 1]t = X+width
The corresponding two equations are
-1*x ' + 1*y ' + 0*z ' + 1*w ' = X
1*x ' + 1*y ' + 0*z ' + 1*w ' = X+width
The solution.
X ' = WIDTH/2
Y ' = 0
Z ' = 0
W ' = x + WIDTH/2
Second column, please.
The column equation (omitted here x ', Z ', but the result is unchanged, the same as the same)
Y ' + 1*w ' =y
-1*y ' + 1*w ' =y+height
The solution.
Y ' =-HEIGHT/2
W ' = Y + HEIGHT/2
Finally, we ask for the third column
Column equation
0*z ' + 1*w ' = MinZ
1*z ' + 1*w ' = MaxZ
The solution.
Z ' = Maxz–minz
W ' = MinZ
Combine the above columns to get the viewport transformation matrix