Platform SDK: DirectX |
This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.
The Triangle tutorial code initializes the geometry in the application-defined InitGeometry subroutine by defining four D3DVECTOR types; a vector type is used for each of the three points used to define the triangle, and for the one point used to define the normal in 3-D space. Technically, you are not required to set up the geometry at this time—you can do it anytime prior to calling the rendering methods:
Private Sub InitGeometry() ' Set up the geometry for the triangle. Dim p1 As D3DVECTOR, _ p2 As D3DVECTOR, _ p3 As D3DVECTOR, _ vNormal As D3DVECTOR ' Set values for three points used to define a triangle. p1.x = 0#: p1.y = 5#: p1.z = 0# p2.x = 5#: p2.y = -5#: p2.z = 0# p3.x = -5#: p3.y = -5#: p3.z = 0# ' Create a normal vector--shared by the points of the ' triangle--that points toward the viewpoint. Note that ' we reverse the Z coordinate for the normal of the points ' that define the backside of the triangle. vNormal.x = 0#: vNormal.y = 0#: vNormal.z = -1#
The DirectX7.CreateD3DVertex method uses the members of a D3DVECTOR type to fill D3DVERTEX types by using the individual values that represent discrete vertex components. Fill the global D3DVERTEX type array, g_TriangleVert, with the triangle's vertex information:
' Create the 3 vertices for the front of the triangle. g_dx.CreateD3DVertex p1.x, p1.y, p1.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, _ g_TriangleVert(0) g_dx.CreateD3DVertex p2.x, p2.y, p2.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, _ g_TriangleVert(1) g_dx.CreateD3DVertex p3.x, p3.y, p3.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, _ g_TriangleVert(2) ' Now do the same for the back of the triangle. g_dx.CreateD3DVertex p3.x, p3.y, p3.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, _ g_TriangleVert(3) g_dx.CreateD3DVertex p2.x, p2.y, p2.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, _ g_TriangleVert(4) g_dx.CreateD3DVertex p1.x, p1.y, p1.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, _ g_TriangleVert(5)
The preceding code fragment locates three points in 3-D space that define a triangle standing upright in the z=0 plane. After creating all the Direct3D-related objects, initializing the scene, and defining the geometry for your scene, you can render your scene as described in Step 4: Execute the Rendering Loop.