DirectX SDK |
This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.
Another step in setting up a simple scene involves setting the world, view, and projection matrices. The system applies these matrices to the model's geometry in order to place the model in the scene, to adjust for the camera's location and orientation, and to scale the vertex data—making distant objects appear smaller than near objects. (For a conceptual overview, see the Geometry Pipeline.)
To create the world matrix, declare a D3DMATRIX type, matWorld, and create an identity matrix from it. Then, use the Direct3DDevice7.SetTransform method with the D3DTRANSFORMSTATE_WORLD flag to define the matrix for the world view.
' The world matrix controls the position and orientation of the polygons ' in world space. We'll use it later to spin the triangle. Dim matWorld As D3DMATRIX g_dx.IdentityMatrix matWorld g_d3dDevice.SetTransform D3DTRANSFORMSTATE_WORLD, matWorld
To create the view matrix, declare a D3DMATRIX type, matView, and create an identity matrix from it. Then, pass the camera coordinates into the DirectX7.ViewMatrix method to create the view matrix. Use the SetTransform method to define the view matrix for the rendering device.
' The view matrix defines the position and orientation of the camera. ' Here, we are just moving it back along the z-axis by 15 units. Dim matView As D3DMATRIX g_dx.IdentityMatrix matView Call g_dx.ViewMatrix(matView, MakeVector(0, 0, -15), MakeVector(0, 0, 0), _ MakeVector(0, 1, 0), 0) g_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matView
The preceding code uses a private, application-defined function, MakeVector, to transform three points into a D3DVECTOR type:
Private Function MakeVector(a As Double, b As Double, c As Double) As D3DVECTOR Dim vecOut As D3DVECTOR With vecOut .x = a .y = b .z = c End With MakeVector = vecOut End Function
To create the projection matrix, declare a D3DMATRIX type, matProj, and create an identity matrix from it. Then, pass the clipping planes and the field of view angle into the DirectX7.ProjectionMatrix method to create the projection matrix. Use the SetTransform method to define the projection matrix for the rendering device.
' The projection matrix defines how the 3-D scene is "projected" onto the ' 2-D render target (the backbuffer surface). Refer to the docs for more ' info about projection matrices. Dim matProj As D3DMATRIX g_dx.IdentityMatrix matProj Call g_dx.ProjectionMatrix(matProj, 1, 1000, pi / 2) g_d3dDevice.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
In the preceding code, the second parameter passed to ProjectionMatrix represents the distance to the near clipping plane in application-specific units, while the third parameter represents the distance to the far clipping plane in application-specific units. The fourth parameter is the field of view angle in radians.
After you have prepared the materials and configured the transformation matrices, you can set up the geometry for your scene. This step is covered in Step 3.3: Prepare the Geometry.