Microsoft DirectX 8.1 (C++)

ColorVertex Shader

This topic shows the steps necessary to initialize and use a simple vertex shader that uses a position and a diffuse color.

The first step is to declare the structures that hold the position and color as shown in the code example below.

struct XYZBuffer
{
    FLOAT x, y, z;
};

struct ColBuffer
{
    D3DCOLOR color;
};

The next step is to create a vertex shader declaration as shown in the code below.

DWORD decl[] =
{
    D3DVSD_STREAM( 0 ),
    D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3 ),
    D3DVSD_STREAM( 1 ),
    D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR ),
    D3DVSD_END()
};

The next step is to call the IDirect3DDevice8::CreateVertexShader method to create the vertex shader.

g_d3dDevice->CreateVertexShader( decl, NULL, &vShader, 0 );

Passing NULL to the second parameter of CreateVertexShader tells Direct3D that this vertex shader will use a fixed function pipeline.

After creating the vertex buffer and vertex shader, they are ready to use. The code example below shows how to set the vertex shader, set the stream source, and then draw a triangle list that uses the new vertex shader.

g_d3dDevice->SetVertexShader( vShader );
g_d3dDevice->SetStreamSource( 0, xyzbuf, sizeof(XYZBuffer));
g_d3dDevice->SetStreamSource( 1, colbuf, sizeof(ColBuffer));
g_d3dDevice->SetIndices( pIB, 0 );
g_d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, max - min + 1, 0, count / 3 );