The Direct3D Transformation Pipeline

本文为Direct3D开发者详细解释了如何直接操作Direct3D矩阵来设置Direct3D转换管线参数。转换管线包括世界变换、视图变换和投影变换,用于将3D模型坐标转换为屏幕坐标。文章介绍了每个变换的作用、Direct3D矩阵的使用以及转换过程中的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

This white paper provides a technical explanation for Direct3D application developers on how to set the parameters of the Direct3D Transformation Pipeline by the direct manipulation of Direct3D matrices.

Overview

Direct3D uses three transformations to change your 3D model coordinates into pixel coordinates (screen space). These transformations are world transform, view transform, and projection transform.

World transform controls how model coordinates are transformed into world coordinates. World transform can include translations, rotations, and scalings, but it does not apply to lights. For more information on working with world transforms, see World Transform.

View transform controls the transition from world coordinates into "camera space," determining camera position in the world. For an example of working with view transforms, see View Transform.

Projection transform changes the geometry from camera space into "clip space" and applies perspective distortion. The term "clip space" refers to how the geometry is clipped to the view volume during this transform. For an example of working with projection transforms, see Projection Transform.

Finally, the geometry in clip space is transformed into pixel coordinates (screen space). This transformation is controlled by the viewport settings.

Clipping and transforming vertices must take place in homogenous space (simply put, space in which the coordinate system includes a fourth element), but the final result for most applications needs to be non-homogenous three-dimensional (3D) coordinates defined in "screen space." This means that both the input vertices and the clipping volume must be translated into homogenous space to perform the clipping and then translated back into non-homogenous space to be displayed.

The three Direct3D transformations-world, view, and projection transform-are defined by Direct3D matrices. A Direct3D matrix is a 4x4 homogenous matrix defined by a D3DMATRIX structure. Although Direct3D matrices are not standard objects-they are not represented by a COM interface-you can create and set them just as you would any other Direct3D object. For more information on Direct3D matrices, see Transforms.

The Transformation Pipeline

If a vertex in the model coordinate is given by Pm = (Xm, Ym, Zm, 1), then the transformations shown in the following figure are applied to compute screen coordinates Ps = (Xs, Ys, Zs, Ws).

Ee418867.d3dxfrm61(en-us,VS.85).gif

The seven stages are as follows:

  1. World matrix Mworld transforms vertices from the model space to the world space. This matrix is set by:

    d3dDevice->SetTransform (D3DTRANSFORMSTATE_WORLD, matrix address)

    Direct3D implementation assumes that the last column of this matrix is (0, 0, 0, 1). No error is returned if the user specifies a matrix with a different last column, but the lighting and fog will be incorrect.

  2. View matrix Mview transforms vertices from the world space to the camera space. This matrix is set by:

    d3dDevice->SetTransform (D3DTRANSFORMSTATE_VIEW, matrix address)

    Direct3D implementation assumes that the last column of this matrix is (0, 0, 0, 1). No error is returned if the user specifies a matrix with different last column, but the lighting and fog will be incorrect.

  3. Projection matrix Mproj transforms vertices from the camera space to the projection space. This matrix is set by:

    d3dDevice->SetTransform (D3DTRANSFORMSTATE_PROJECTION, matrix address)

    The last column of the projection matrix should be (0, 0, 1, 0), or (0, 0, a, 0) for correct fog and lighting effects; (0, 0, 1, 0) form is preferred.

    Clipping volume for all points Xp = (Xp, Yp, Zp, Wp) in the projection space is defined as:

    -Wp < Xp <= Wp

    -Wp < Yp <= Wp

    0 < Zp <= Wp

    All points that do not satisfy these equations will be clipped.

    If a view volume is defined as:

    Sw-screen window width in camera space in near clipping plane

    Sh-screen window height in camera space in near clipping plane

    Zn-distance to the near clipping plane along Z axes in camera space

    Zf-distance to the far clipping plane along Z axes in camera space

    then a perspective projection matrix could be written as follows:

    Ee418867.d3dxfrm62(en-us,VS.85).gif

    where Mij are members of Mproj.

    For the orthogonal projection we have:

    Ee418867.d3dxfrm63(en-us,VS.85).gif

    Direct3D assumes that the perspective projection matrix has the form:

    Ee418867.d3dxfrm64(en-us,VS.85).gif

    If the perspective projection matrix does not have this form, there will be some artifacts. For example, table fog will not work.

  4. Direct3D allows the user to change the clip volume as follows:

    Ee418867.d3dxfrm65(en-us,VS.85).gif

    This can be rewritten as:

    Ee418867.d3dxfrm66(en-us,VS.85).gif

    where:

    Cx, Cy - dvClipX, dvClipY from D3DVIEWPORT9

    Cw, Ch - dvClipWidth, dvClipHeight from D3DVIEWPORT9

    Zmin, Zmax - dvMinZ, dvMaxZ from D3DVIEWPORT9

    This transformation can provide increased precision and is equivalent to scaling and shifting the clipping volume.

    The corresponding Mclip matrix is:

    Ee418867.d3dxfrm67(en-us,VS.85).gif

    A vertex is transformed by:

    (Xc, Yc, Zc, Wc) = (Xp, Yp, Zp, Wp) * Mclip

    If you do not want to scale the clip volume, you can set viewport parameters to default values:

    dvClipWidth = 2

    dvClipHeight = 2

    dvClipX = -1

    dvClipY = 1

    dvMinZ = 0

    dvMaxZ = 1

  5. The clipping stage is optional if the user specifies the D3DDP_DONOTCLIP flag in a DrawPrimitive call. In this case, all matrices (including Mvs) can be combined.

  6. The viewport scale matrix Mvs scales coordinates to be within the viewport window and flips the Y axis from up to down:

    Ee418867.d3dxfrm68(en-us,VS.85).gif

    where:

    dwX, dwY - viewport offsets in pixels from D3DVIEWPORT9

    dwWidth, dwHeight - viewport width and height in pixels from D3DVIEWPORT9

  7. Finally, screen coordinates are computed and passed to the rasterizer:

    Ee418867.d3dxfrm69(en-us,VS.85).gif

Usage Tips

  1. The last column of the world and view matrices should be (0, 0, 0, 1), or the lighting will be incorrect.

  2. Set the viewport parameters to build an identity Mclip matrix, unless you understand exactly what it is needed for.

    dvClipWidth = 2

    dvClipHeight = 2

    dvClipX = -1

    dvClipY = 1

    dvMinZ = 0

    dvMaxZ = 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值