The direct3d transformation Pipeline

Source: Internet
Author: User

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 between des a fourth element ), but the final result for most applications needs to be non-homogenous three-dimenates (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 wowould 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 ).

The seven stages are as follows:

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

    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:

    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:

    D3ddevice-> settransform (d3dtransformstate_projection, matrix address)

    The last column of the projection matrix shoshould 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:

    -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:

    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 cocould be written as follows:

    Where mij are members of mproj.

    For the orthogonal projection we have:

    Direct3d assumes that the Perspective Projection Matrix has the form:

    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:

    This can be rewritten:

    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:

    A vertex is transformed:

    (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:

    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:

Usage tips
  1. The last column of the world and view matrices shoshould 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.

    Dvclipwidth = 2

    Dvclipheight = 2

    Dvclipx =-1

    Dvclipy = 1

    Dvminz = 0

    Dvmaxz = 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.