Search code examples
c++directxvertex

What is RHW in DirectX Vertex


I know RHW is Reciprocal Homogeneous W, but could someone explain how it can be used and what it does ?


Solution

  • Explanation on gamedev forum post and msdn.

    to quote the answer from gamedev, OP jpetrie:

    RHW is the reciprocal of the homogeneous (clip space) w coordinate of a vertex (e.g., 1/w).

    Recall that we must expand our 3D vectors to 4D vectors in order to be able to multiply them with 4x4 matrices (which we do because 4x4 matrices allow us to completely encode both rotational, translational, and scaling terms). In doing so we often set (or assume) the expanded fourth (w) component is 1 for model vertices, and the nature of the transforms we use to bring model-space vertices into world or view space doesn't include any terms that alter the w component.

    However, the typical perspective projection transformation often takes the follow general form

    A 0 0 0
    0 B 0 0
    0 0 C D
    0 0 E 0
    

    which, when multiplied with a general view-space vertex vector (x,y,z,1), gives you a vector like (Ax, By, Cz + E, zD) -- note that the resulting vertex has a w component that is proportional to the original input's z component. Also note that the space you're in after multiplication by this matrix is called clip space, because the nature of the transform has distorted the viewing frustum into a cuboid, the edges of which are much easier to perform clipping against.

    After clipping, the graphics pipeline divided by the w component to scale things based on distance to give you the perspective effect. This division cannot be encoded in a matrix transform.

    Now, division by w is the same as multiplication by the reciprocal of w, and the reason that you are required to give the reciprocal of the clip space w coordinate is probably a throwback to the time when division was significantly slower than multiplication. When you use pre-transformed vertices, the transformation from model to world to view to clip space is skipped, but the rest of the pipeline (division by w, conversion to window coordinates, and rasterization, obviously) must still happen. Since the projection transform was skipped, there is no basis for the pipeline to determine the w coordinate to divide by, so it asks you for it.

    There are some interesting uses for the value, but for the most part people use pretransformed vertices when doing 2D rendering, and in that case its most useful to set the RHW value to 1, which effectively causes the divide to be a no-op.