Microsoft DirectX 8.1 (Visual Basic)

Volume Texture Resources

Volume textures are three-dimensional collections of pixels (texels) that can be used to paint a two-dimensional primitive such as a triangle or a line. Three-element texture coordinates are required for each vertex of a primitive that is to be textured with a volume. As the primitive is drawn, each contained pixel is filled with the color value from some pixel within the volume, in accordance with rules analogous to the two-dimensional texture case. Volumes are not rendered directly because there are no three-dimensional primitives that can be painted with them.

You can use volume textures for special effects such as patchy fog, explosions, and so on.

Volumes are organized into slices and can be thought of as width x height 2-D surfaces stacked to make a width x height x depth volume. Each slice is a single row. Volumes can have subsequent levels, in which the dimensions of each level are truncated to half the dimensions of the previous level. The illustration below gives an idea of what a volume texture with multiple levels looks like.

Creating a Volume Texture

The code examples below show the steps required to use a volume texture.

First, specify a custom vertex type that has three texture coordinates for each vertex, as shown in this code example.

Private Type VOLUMEVERTEX
    Dim x As Single
    Dim y As Single
    Dim z As Single
    Dim color As Long
    Dim tu As Single
    Dim tv As Single
    Dim tw As Single
};

Const D3DFVF_VOLUMEVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or _
                             D3DFVF_TEX1 Or D3DFVF_TEXCOORDSIZE3_0)

Next, fill the vertices with data.

Dim Vertices(3) As VOLUMEVERTEX

With Vertices(0):
    .x = 1.0: .y = 1.0: .z = 0.0: .color = &HFFFFFFFF .tu = 1.0: .tv = 1.0: .tw = 0.0
End With

With Vertices(1):
    .x = -1.0: .y = 1.0: .z = 0.0: .color = &HFFFFFFFF .tu = 0.0: .tv = 1.0: .tw = 0.0
End With

With Vertices(2):
    .x = 1.0: .y = -1.0: .z = 0.0: .color = &HFFFFFFFF .tu = 1.0: .tv = 0.0: .tw = 0.0
End With

With Vertices(3):
    .x = -1.0: .y = -1.0: .z = 0.0: .color = &HFFFFFFFF .tu = 0.0: .tv = 0.0: .tw = 0.0
End With

Now, create a vertex buffer and fill it with data from the vertices.

The next step is to use the Direct3DDevice8.CreateVolumeTexture method to create a volume texture, as shown in the code example below.

Dim volTexture As Direct3DVolumeTexture8

Set volTexture = m_D3DDevice.CreateVolumeTexture(8, 4, 4, 1, 0, _
                                                 D3DFMT_R8G8B8, _
                                                 D3DPOOL_MANAGED)

Before rendering the primitive, set the current texture to the volume texture created above. The code example below shows the entire rendering process for a strip of triangles.

d3dDevice.BeginScene

// Draw the quad, with the volume texture.
Call d3dDevice.SetTexture(0, volTexture)
Call d3dDevice.SetVertexShader(D3DFVF_VOLUMEVERTEX)
Call d3dDevice.SetStreamSource(0, VB, len(Vertices(0))
Call d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2)

// End the scene.
d3dDevice.EndScene