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  
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Filling the Wireframe with Triangles
  •  iPhone 3D Programming : Adding Depth and Realism - Creating and Using the Depth Buffer
  •  iPhone 3D Programming : Adding Depth and Realism - Examining the Depth Buffer
  •  iPhone 3D Programming : HelloCone with Fixed Function
  •  iPhone 3D Programming : Vector Beautification with C++
  •  jQuery 1.3 : An image carousel
  •  jQuery 1.3 : Headline rotator
  •  Silverlight : Print a Document
  •  Silverlight : Capture a Webcam
  •  Silverlight : Make Your Application Run out of the Browser
  •  Silverlight : Put Content into a 3D Perspective
  •  Silverlight : Response to Timer Events on the UI Thread
  •  Silverlight : Build a Download and Playback Progress Bar
  •  Silverlight : Play a Video
  •  C# 4.0 : Add a Static Constructor and Initialization
  •  C# 4.0 : Add a Constructor
  •  
    Most View
    Nvidia GeForce GTX 680 2GB - Nvidia’s Top-End single-GPU Card
    Overview On UPnP Network Protocol (Part 2)
    Toshiba THNSNF512GCSS 512GB SSD – Take Advantage Of 19nm MLC NAND
    Lenovo IdeaPad Y480 Review – Laptop With Excellent Construction Quality And Ergonomic Design
    Canon ImageFormula DR-C130 - Fine Choice For Small-Office Scanner
    The next Nexus (Part 2)
    Group Test: Android Tablets – November 2012 (Part 1) - Acer Iconia A110, Acer Iconia A210
    Pre/ Power Amplifier - Nagra Jazz/ MSA (Part 1)
    Sony Vaio T11 Hands On - Now You’re Talking About ‘Ultranets’
    Keep Kids Online Safely (Part 1)
    Top 10
    G.Skill DDR3 SDRAM 32GB Review (Part 6)
    G.Skill DDR3 SDRAM 32GB Review (Part 5)
    G.Skill DDR3 SDRAM 32GB Review (Part 4)
    G.Skill DDR3 SDRAM 32GB Review (Part 3)
    G.Skill DDR3 SDRAM 32GB Review (Part 2)
    G.Skill DDR3 SDRAM 32GB Review (Part 1)
    Macbook Pro With 13in Retina Display Review (Part 3)
    Macbook Pro With 13in Retina Display Review (Part 2)
    Macbook Pro With 13in Retina Display Review (Part 1)
    Kensington Folio Trio For iPad