MULTIMEDIA

Programming with DirectX : Projection Transformations

1/10/2011 11:28:09 AM
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
);

Other  
 
Most View
LG Intuition Review - Skirts The Line Between Smartphone And Tablet (Part 3)
Mac Mini 2012 Review (Part 2)
Grado GR10 In-Ear Headphones Review
The Best Apps and Gear of 2012 (Part 4)
Wake Up Your Wi-Fi (Part 1)
IP Camera Eyespy247 EXT+ - Cloud-Based Home And Office Video-Monitoring System
Hauppauge HD PVR 2 Gaming Edition
Samsung NX300 Camera – Large Touchscreen And Wonderful Image Quality (Part 2)
Windows 8 : Managing Windows Update (part 4) - Viewing update history, Rolling back updates
HTC Desire C - Does It Have Anything Good?
Top 10
The Maserati Ghibli S – The Latin Conquistador (Part 2)
The Maserati Ghibli S – The Latin Conquistador (Part 1)
The Lexus CT200h F-Sport – A Good Deal Easier To Live With
The Mercedes-Benz C-Class Estate – A Practical And Comfortable Choice Of Family Car
The Mercedes-Benz CLS 220 BlueTec AMG Line – Smooth And Impressive
Sharepoint 2013 : SharePoint Publishing Infrastructure (part 6) - Check In/Out, Versioning, and Content Approval
Sharepoint 2013 : SharePoint Publishing Infrastructure (part 5) - Content Management - Putting It All Together
Sharepoint 2013 : SharePoint Publishing Infrastructure (part 4) - Content Management - Master Pages,Page Layouts
Sharepoint 2013 : SharePoint Publishing Infrastructure (part 3) - Content Management - Site Columns, Content Types
Sharepoint 2013 : SharePoint Publishing Infrastructure (part 2) - Publishing Features