Microsoft DirectX 8.1 (Visual Basic)

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.

Private Type XYZBuffer
    x As Single
    y As Single
    z As Single
End Type

Private Type ColBuffer
    color As Long
End Type

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

Dim decl(5) As Long

decl(0) = D3DVSD_STREAM(0)
decl(1) = D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3)
decl(2) = D3DVSD_STREAM(1)
decl(3) = D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_UBYTE)
decl(4) = D3DVSD_END()

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

Call m_D3DDevice.CreateVertexShader(decl, ByVal 0, vShader, 0)

Passing ByVal 0 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.

Call m_D3DDevice.SetVertexShader(vShader)
Call m_D3DDevice.SetStreamSource(0, xyzbuf, len(XYZBuffer))
Call m_D3DDevice.SetStreamSource(1, colbuf, len(ColBuffer))
Call m_D3DDevice.SetIndices(IB, 0)
Call m_D3DDevice.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, max - min + 1, 0, count / 3)