Microsoft DirectX 8.1 (Visual Basic) |
One of the requirements of using lights is that each surface has a normal. To do this, the Lights sample project uses a different custom vertex type. The new custom vertex format has a 3-D position and a surface normal. The surface normal is used internally by Microsoft® Direct3D for lighting calculations.
' A structure for the custom vertex type. Private Type CUSTOMVERTEX postion As D3DVECTOR '3d position for vertex normal As D3DVECTOR 'surface normal for vertex End Type ' The custom FVF, which describes the custom vertex structure. Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL)
Now that the correct vector format is defined, the Lights sample project calls InitGeometry, which is an application-defined function that creates a cylinder. The first step is to fill the vertex buffer with the points of the cylinder. Note that in the following code fragment, each point is defined by a position and a normal.
For i = 0 To 49 theta = (2 * g_pi * i) / (50 - 1) Vertices(2 * i + 0).postion = vec3(Sin(theta), -1, Cos(theta)) Vertices(2 * i + 0).normal = vec3(Sin(theta), 0, Cos(theta)) Vertices(2 * i + 1).postion = vec3(Sin(theta), 1, Cos(theta)) Vertices(2 * i + 1).normal = vec3(Sin(theta), 0, Cos(theta)) Next
The second step is to create a vertex buffer that stores the points of the cylinder as shown in the following code fragment.
Set g_VB = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 50 * 2, _ 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT) If g_VB Is Nothing Then Exit Function
Now fill the vertex buffer with the vertices for the cylinder and the vertex buffer is ready for rendering. But first, the material and light for this scene must be set up before rendering the cylinder, as described in Step 2: Setting up Material and Light.