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

 Top 10
 -  Mobile Phone Game Programming : Java As a Mobile Game Platform
 -  SQL Azure : Tuning Techniques (part 1) - Dynamic Management Views
 -  Building Android Apps : Web Programming Crash Course
 -  Windows Azure : Static reference data (part 2) - Performance disadvantages of a chatty interface & Caching static data
 -  Using SharePoint 2010 Management PowerShell for Backup and Restore
 -  Managing Exchange Server 2010 : The Exchange Management Console
 -  ASP.NET AJAX : Partial Refreshes (part 1) - A Simple UpdatePanel Test
 -  Enumerate Directories and Files
 -  Programming the Mobile Web : WebKit CSS Extensions (part 4) - Animations
 Most View
 -  IIS 7.0 : Implementing Access Control - Authentication (part 4)
 -  iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)
 -  SQL Server 2008 : Leveraging the Microsoft Sync Framework
 -  Windows Server 2008 : Designing Organizational Unit and Group Structure - Group Policies and OU Design
 -  Deploying the Client for Microsoft Exchange Server 2010 : Understanding Deployment Options
 -  Getting the Most Out of the Microsoft Outlook Client : Using Outlook 2007 (part 2) - Sharing Information with Users Outside the Company
 -  Introducing IIS 7
 -  SQL Server 2008 : Transact-SQL Programming - Common Table Expressions
 -  Managing Remote in Vista
 -  Windows 7 : Getting Help and Giving Others Assistance
 -  Windows Vista Security Guide
 -  Becoming an Excel Programmer : Where's My Code?
 -  Building Android Apps: Introduction to PhoneGap
 -  IIS 7.0 : Performance and Tuning - Memory
 -  Windows 7 : Protecting Your Computer While Browsing (part 3)
 -  Leveraging and Optimizing Search in SharePoint 2010 : Customizing the FAST Search User Interface
 -  Programming Role-Based Security
 -  SharePoint 2010 : Creating and Managing Workflows - Workflows in SharePoint 2010
 -  Windows 7 : Managing the BCD Data Store
 -  Where Windows Malware Hides