Tutorial 3 – The Third Dimension

Now that we’ve covered the (quite lengthy) setup in the previous 2 tutorials, we can start to go step by step and do some more interesting things with our DirectX code. In this tutorial we’ll learn how to move from rendering flat 2D images to generating a 3D scene.

Projections Revisited

Last tutorial we mentioned both orthographic and perspective projection matrices, but we only used the former. In this tutorial we will be using the perspective projection matrix to make distant objects appear smaller than those closer to us. To set this up we make the following call:

D3DXMatrixPerspectiveFovRH(&projectionMat,
                           D3DXToRadian(45.0f),
                           (float) m_width / (float) m_height,
                           0.1f,
                           20.0f);

This creates a right handed perspective matrix with a 45 degree field of view, an aspect ratio matching our window dimensions and able to display anything between 0.1 and 20 units in front of us.

The Camera & Motion

Because we’re now displaying our scene in 3 dimensions, we need to describe where our camera is in this 3D world and how it is oriented. To do this we create a camera matrix using the following call:

D3DXMatrixLookAtRH(&viewMat,
                   new D3DXVECTOR3(0.0f, 0.0f, 4.0f),
                   new D3DXVECTOR3(0.0f, 0.0f, 0.0f),
                   new D3DXVECTOR3(0.0f, 1.0f, 0.0f));

This creates a view matrix to represent a camera looking from 4 units out in the z-axis, looking at the world’s origin with the positive y-axis used as the camera’s “up” direction.

We also want to make our triangle spin around on the y-axis. The following call creates a rotation matrix which will transform our world to create just that effect:

D3DXMatrixRotationY(&worldMat, D3DXToRadian(rotation));

Polygons In Depth

The only other difference in this code compared to the last tutorial is that fact that we’re describing our vertices using x, y AND z coordinates. That means that our input description used on initialisation needs to take this into account by using 3 floating point values (DXGI_FORMAT_R32G32B32_FLOAT) for the “Position”.

Here’s the results of the code:

The full code can be downloaded here:

DirectX Tutorial 3

  1. No comments yet.
  1. No trackbacks yet.

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: