Programming with DirectX : Projection Transformations

Projection transformations affect how a rendered scene looks when displayed to the screen. The two main types of projections are orthogonal projections and perspective projections, both of which are supported by Direct3D. A projection is a matrix that stores projection information. To apply the projection to the geometry in the scene, we multiply the projection matrix and vertices of the geometry, which is a process known as transformation. A projection is a representation of how objects are viewed when rendered. The second type, orthogonal projection, will be discussed next.

Orthogonal Projection

Orthogonal projection causes all objects to be rendered to the screen at the same size regardless of how far away an object is. In the real world, as objects move farther away from you, they appear smaller. An example of this is shown in Figure 1.

Figure 1. An example of objects getting smaller as they move farther away.

 

In orthogonal projection, the size of the objects does not change due to distance. Many times, this effect is desired, but for most 3D scenes in modern video games it is often important to have a different type of projection. Orthogonal projection can be a great type of projection for 2D elements such as menus, heads-up displays, and any other type of rendering where the geometry is not to change in size with distance. An example of orthogonal projection is shown in Figure 2.

Figure 2. An example of orthogonal projection.

 

In Direct3D there are four different functions for creating an orthogonal matrix. The first two functions are D3DXMatrixOrthoLH() and D3DXMatrixOrthoRH(). The LH version creates a left-handed projection matrix, while RH creates a right-handed projection matrix. Their function prototypes are as follows.

D3DXMATRIX * D3DXMatrixOrthoLH(
   D3DXMATRIX *pOut,
   FLOAT w,
   FLOAT h,
   FLOAT zn,
   FLOAT zf
);

D3DXMATRIX * D3DXMatrixOrthoRH(
   D3DXMATRIX *pOut,
   FLOAT w,
   FLOAT h,
   FLOAT zn,
   FLOAT zf
);
 

The parameters of the functions start with the D3DXMATRIX object, which is the structure that represents matrices in Direct3D, which will be created from the function call, the width and height of the desired view volume, and the near and far plane. The near plane determines how close to the viewer an object can be before it is seen, while the far plane determines how far away an object can be before it disappears. The width and height, which is normally the width and height of the screen or rendering area, in addition to the near and far values collectively represent the view volume. The view volume is an area in which objects are visible. (see Figure 3)

Figure 3. An example of a view volume.

 

The left- and right-handedness of the functions refer to coordinate systems. A coordinate system essentially tells the graphics API which direction, left or right, the positive X axis travels and which direction, toward or away, the positive Z axis travels. An illustration is shown in Figure 4.

Figure 4. Left- and right-handed coordinate systems.


OpenGL uses a right-handed coordinate system, while Direct3D traditionally used a left-handed system.

 

Direct3D allows developers to use either left- or right-hand coordinates. Using a right-handed system allows developers to use the same data in OpenGL and Direct3D applications without modification of the geometry’s X and Z axes. This is the reason behind the multiple versions of the orthogonal projection functions. The last two orthogonal projection functions are as follows.

D3DXMATRIX * D3DXMatrixOrthoOffCenterLH(
   D3DXMATRIX *pOut,
   FLOAT l,
   FLOAT r,
   FLOAT b,
   FLOAT t,
   FLOAT zn,
   FLOAT zf
);
D3DXMATRIX * D3DXMatrixOrthoOffCenterRH(
   D3DXMATRIX *pOut,
   FLOAT l,
   FLOAT r,
   FLOAT b,
   FLOAT t,
   FLOAT zn,
   FLOAT zf
);
 

The l and r parameters represent the minimum and maximum width, while b and t represent the minimum and maximum height. zn and zf are the near and far values. The D3DXMatrixOrthoLH() and D3DXMatrixOrthoRH() functions are special cases of D3DXMatrixOrthoOffCenterLH() and D3DXMatrixOrthoOffCenterRH(). The off center functions allow more customizability than the other two seen earlier in this section.

Perspective Projection

The other type of projection is perspective projection. This type of projection adds perspective to scenes. Perspective projection allows objects to shrink as they move farther away from the viewer. Objects also distort as they are viewed at an angle. Figure 5 shows an example of perspective projection. Perspective projection is a type of projection that can be seen in all modern 3D video games.

Figure 5. Perspective projection.


Perspective projection is the same as the idea behind perspective in drawing art.

 

The perspective projection matrix functions are as follows, where the parameters match those of the orthogonal counterparts.

D3DXMATRIX * D3DXMatrixPerspectiveLH(
   D3DXMATRIX *pOut,
   FLOAT w,
   FLOAT h,
   FLOAT zn,
   FLOAT zf
);

D3DXMATRIX * D3DXMatrixPerspectiveRH(
   D3DXMATRIX *pOut,
   FLOAT w,
   FLOAT h,
   FLOAT zn,
   FLOAT zf
);

D3DXMATRIX * D3DXMatrixPerspectiveOffCenterLH(
   D3DXMATRIX *pOut,
   FLOAT l,
   FLOAT r,
   FLOAT b,
   FLOAT t,
   FLOAT zn,
   FLOAT zf
);

D3DXMATRIX * D3DXMatrixPerspectiveOffCenterRH(
   D3DXMATRIX *pOut,
   FLOAT l,
   FLOAT r,
   FLOAT b,
   FLOAT t,
   FLOAT zn,
   FLOAT zf
);