DirectX SDK Tutorial 3 Mod 1

From SwinBrain

Tutorial 3 from the DirectX 9.0c SDK (June 2006) samples is a good starting point for creating polygons. The modifications described here create an open ended box. These modifications create a program that is intermediate between Tutorial 3 (which draws a single triangle with vertex colours) and Tutorial 4 (which creates a strip of many triangles to make a polygon and uses lighting instead of vertex colours).

Adding the Z-buffer

The following changes add the z-buffer so that faces behind other faces are not displayed. Note that they appear in the subsequent tutorial on lighting.

//In InitD3D(), add the following to the presentation parameters
d3dpp.EnableAutoDepthStencil = TRUE;		//enable the depth stencil...
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;	//...and determine its format
 
//Also in InitD3D(), add the following to enable the z-buffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
 
//In Render(), change the third argument to Clear so that the z-buffer 
//is also cleared each time we render the scene
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

Now, the interesting changes...

//In InitGeometry(), instead of a boring single triangle, 
//try the following definition for g_Vertices:
	{ -1.0f,-1.0f, -1.0f, 0xffff0000, }, //front
	{ -1.0f, 1.0f, -1.0f, 0xffffaa00, },
        {  1.0f,-1.0f, -1.0f, 0xffffff00, },
        {  1.0f, 1.0f, -1.0f, 0xff00ff00, },
	{  1.0f,-1.0f,  1.0f, 0xff0000ff, }, //right side
	{  1.0f, 1.0f,  1.0f, 0xff5500aa, },
	{ -1.0f,-1.0f,  1.0f, 0xff0000aa, }, //rear side
	{ -1.0f, 1.0f,  1.0f, 0xff00ff00, },
    	{ -1.0f,-1.0f, -1.0f, 0xffff0000, }, //left side
	{ -1.0f, 1.0f, -1.0f, 0xffffaa00, },

Further Changes

  • Make sure you adjust the size of the vertex buffer accordingly
  • In Render(), change the number of primitives to render in the call to DrawPrimitive. Note that only one call to the function is needed because of the way it uses the vertices when told to render a TRIANGLESTRIP. Look in the D3D documentation to see how each alternative D3DDRAWPRIMITIVETYPE is interpreted
  • You may also want to change the speed of the animation to fully appreciate your new artwork

The image on the left is what you should see after making the changes. Can you alter your program to produce output like the second image?

Image:DirectX_SDK_tutorial3_mod_Spinning_box1.jpg Image:DirectX_SDK_tutorial3_mod_Spinning_box2.jpg


Links

More information is coming on this topic...