This document provides an overview of basic Direct3D 9 concepts and code samples for rendering 3D primitives. It discusses compiling a tutorial project, defining a custom vertex type, setting up a vertex buffer, and rendering a triangle. It also describes different primitive types like point lists, line lists, triangle lists, and how to render them using DrawPrimitive. Code examples are provided for each primitive type.
6. Library Directory
C:DxSdk_200806Libx86; must be located at last of library
directory string.
– $(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);C:DxSdk_2008
06Libx86;
6
7. Tutorial 02: Rendering Vertices
The Vertices sample project creates the simplest shape, a
triangle, and renders it to the display.
– Step 1 - Defining a Custom Vertex Type
– Step 2 - Setting Up the Vertex Buffer
– Step 3 - Rendering the Display
7
8. Step 1 – Defining a Custom Vertex Type
The Vertices sample project renders a 2D triangle by using
three vertices.
– struct CUSTOMVERTEX
– {
– FLOAT x, y, z, rhw; // The transformed position for the vertex.
– DWORD color; // The vertex color.
– };
The next step is to define the FVF that describes the contents
of the vertices in the vertex buffer.
– #define D3DFVF_CUSTOMVERTEX
(D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
8
9. Step 2 – Setting Up the Vertex Buffer
The following code fragment initializes the values for three
custom vertices.
– CUSTOMVERTEX vertices[] =
– {
– { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
– { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
– { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
– };
The next step is to call IDirect3DDevice9::CreateVertexBuffer to
create a vertex buffer as shown in the following code
fragment.
– if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
– 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) )
)
– return E_FAIL;
9
10. After creating a vertex buffer, it is filled with data from the
custom vertices.
– VOID* pVertices;
– if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
– return E_FAIL;
– memcpy( pVertices, vertices, sizeof(vertices) );
– g_pVB->Unlock();
10
11. Step 3 – Rendering the Display
Rendering the display starts by clearing the back buffer to a
blue color and then calling BeginScene.
– g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f,
0L );
– g_pd3dDevice->BeginScene();
You need to set the stream source; in this case, use stream 0.
– g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
The next step is to call IDirect3DDevice9::SetFVF to identify
the fixed function vertex shader.
– g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
The next step is to use IDirect3DDevice9::DrawPrimitive to
render the vertices in the vertex buffer.
– g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
11
12. The last steps are to end the scene and then present the back
buffer to the front buffer.
– g_pd3dDevice->EndScene();
– g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
12
13. Primitives in Direct3D 9
A 3D primitive is a collection of vertices that form a single 3D
entity.
– The simplest primitive is a collection of points in a 3D coordinate
system, which is called a point list.
Direct3D devices can create and manipulate the following
types of primitives.
– Point Lists (Direct3D 9)
– Line Lists (Direct3D 9)
– Line Strips (Direct3D 9)
– Triangle Lists (Direct3D 9)
– Triangle Strips (Direct3D 9)
– Triangle Fans (Direct3D 9)
13
15. Line Lists
CUSTOMVERTEX Vertices[] =
{
{-5.0, -5.0, 0.0},
{ 0.0, 5.0, 0.0},
{ 5.0, -5.0, 0.0},
{10.0, 5.0, 0.0},
{15.0, -5.0, 0.0},
{20.0, 5.0, 0.0}
};
The code example below shows how to use
IDirect3DDevice9::DrawPrimitive to render this line list.
//
// It is assumed that d3dDevice is a valid
// pointer to a IDirect3DDevice9 interface.
//
d3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 3 );
15
16. Line Strips
CUSTOMVERTEX Vertices[] =
{
{-5.0, -5.0, 0.0},
{ 0.0, 5.0, 0.0},
{ 5.0, -5.0, 0.0},
{10.0, 5.0, 0.0},
{15.0, -5.0, 0.0},
{20.0, 5.0, 0.0}
};
The code example below shows how to use
IDirect3DDevice9::DrawPrimitive to render this line strip.
d3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, 5 );
16
17. Triangle Lists
CUSTOMVERTEX Vertices[] =
{
{-5.0, -5.0, 0.0},
{ 0.0, 5.0, 0.0},
{ 5.0, -5.0, 0.0},
{10.0, 5.0, 0.0},
{15.0, -5.0, 0.0},
{20.0, 5.0, 0.0}
};
The code example below shows how to use
IDirect3DDevice9::DrawPrimitive to render this triangle list.
//
// It is assumed that d3dDevice is a valid
// pointer to a IDirect3DDevice9 interface.
//
d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
17
18. Triangle Strips
A triangle strip is a series of connected triangles.
– Because the triangles are connected, the application does not need to
repeatedly specify all three vertices for each triangle.
The system uses vertices v1, v2, and v3 to draw the first
triangle; v2, v4, and v3 to draw the second triangle.
18
19. CUSTOMVERTEX Vertices[] =
{
{-5.0, -5.0, 0.0},
{ 0.0, 5.0, 0.0},
{ 5.0, -5.0, 0.0},
{10.0, 5.0, 0.0},
{15.0, -5.0, 0.0},
{20.0, 5.0, 0.0}
};
The code example below shows how to use
IDirect3DDevice9::DrawPrimitive to render this triangle strip.
d3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 4);
19
20. Triangle Fans
A triangle fan is similar to a triangle strip, except that all the
triangles share one vertex.
20
21. CUSTOMVERTEX Vertices[] =
{
{ 0.0, 0.0, 0.0},
{-5.0, 5.0, 0.0},
{-3.0, 7.0, 0.0},
{ 0.0, 10.0, 0.0},
{ 3.0, 7.0, 0.0},
{ 5.0, 5.0, 0.0},
};
The code example below shows how to use
IDirect3DDevice9::DrawPrimitive to render this triangle fan.
d3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 4 );
21