?/TD>
Microsoft DirectX 9.0

Vertex Color


This example applies the vertex color from the vertex data to the object. The vertex data contains position data as well as diffuse color data. These are shown following:

struct CUSTOMVERTEX_POS_COLOR
{
    float       x, y, z;
    DWORD       diffuseColor;
};

// Create vertex data with position and texture coordinates.
CUSTOMVERTEX_POS_COLOR g_Vertices[]=
{
    //  x      y     z     diffuse   
    { -1.0f, 0.25f, 0.0f, 0xffff0000,  },	//  lower right - red
    {  0.0f, 0.25f, 0.0f, 0xff00ff00,  },	//  lower left - green
    {  0.0f, 1.25f, 0.0f, 0xff0000ff,  },	//  upper left - blue
    { -1.0f, 1.25f, 0.0f, 0xffffffff,  },	//  upper right - white
};

The vertex shader declaration needs to reflect the position and color data also.

// Create the shader declaration.
D3DVERTEXELEMENT9 decl[] = 
{
	{ 0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
	{ 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
	D3DDECL_END()
};

One way for the shader to get the transformation matrix is from a constant register. This is done by calling SetVertexShaderConstant.

D3DXMATRIX mat;
D3DXMatrixMultiply( &mat, &m_matView, &m_matProj );
D3DXMatrixTranspose( &mat, &mat );
hr = m_pd3dDevice->SetVertexShaderConstantF( 1, (float*)&mat, 4 );
if(FAILED(hr)) 
    return hr;

This declaration declares one stream of data, which contains the position and the color data. The color data is assigned to vertex register 0. Here is the shader.

vs_1_1              ; version instruction
m4x4 oPos, v0, c0   ; transform vertices by view/projection matrix
mov oD0, v1         ; load color from register 1 to diffuse color

It contains three instructions. The first is always the version instruction. The second instruction transforms the vertices. The third instruction moves the color in the vertex register to the output diffuse color register. The result is output vertices using the vertex color data.

The resulting output looks like the following:

Output vertices



© 2002 Microsoft Corporation. All rights reserved.