| < Day Day Up > |
We have
Like OpenGL, we need to get the appropriate SDK before we can begin writing software. For DirectX, you can
The first step is to create a new Windows Forms application in VB. We'll use the form for rendering our 3D sphere, which looks very similar to the OpenGL version we have already developed. We can leave the properties of the default form alone and concentrate on the code.
First, add references to Microsoft.DirectX , Microsoft.DirectX.Direct3D , and Microsoft.Direct3DX , and add the following Imports :
Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D
The
Private DX9 As Microsoft.DirectX.Direct3D.Device Private Present As Direct3D.PresentParameters Private Mesh As Direct3D.Mesh Private Material As Direct3D.Material Private Timer1 As Timer
In the Form_Load event, we check for an available adapter, create a device to render to, and initialize Direct3D. At the end of the procedure, we make calls to two additional procedures, which are completed next. Here is the code for this procedure:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "DirectX Light" Dim objAdapters As Direct3D.AdapterInformation objAdapters = Direct3D.Manager.Adapters(0) Present = New Direct3D.PresentParameters() With Present .Windowed = True .SwapEffect = Direct3D.SwapEffect.Discard .BackBufferFormat = objAdapters.CurrentDisplayMode.Format .EnableAutoDepthStencil = True .AutoDepthStencilFormat = Direct3D.DepthFormat.D16 End With DX9 = New Direct3D.Device(objAdapters.Adapter, _ Direct3D.DeviceType.Hardware, Me, _ Direct3D.CreateFlags.HardwareVertexProcessing, Present) AddHandler DX9.DeviceReset, AddressOf Me.OnDeviceReset InitializeDirect3D() StartRefreshCycle() End Sub
The
InitializeDirect3D
procedure basically just makes calls to other procedures, including
CreateMesh
,
CreateMaterial
,
CreateLights
, and
InitializeView
. The
Private Sub InitializeDirect3D() CreateMesh() CreateMaterials() CreateLights() InitializeView() End Sub Private Sub StartRefreshCycle() Timer1 = New Timer() Timer1.Enabled = True Timer1.Interval = 20 AddHandler Timer1.Tick, AddressOf Me.Render Timer1.Start() End Sub
We now create each of the Sub procedures called by the InitializeDirect3D procedure, beginning with CreateMesh . The CreateMesh procedure creates a sphere, which is rendered in the scene much like the sphere we created in the OpenGL version. Here is the code:
Private Sub CreateMesh() Mesh = Direct3D.Mesh.Sphere(DX9, 15, 15, 15) End Sub
The next step is to create the material for the mesh. We can use the
CreateMaterials Sub
procedure, which was called next by
InitializeDirect3D
. We are not going to use a texture for the mesh and will simply use the
Private Sub CreateMaterials() Material = New Direct3D.Material() Material.Diffuse = Color.White End Sub Private Sub CreateLights() Dim Light0 As Direct3D.Light = DX9.Lights(0) Light0.Type = Direct3D.LightType.Directional Light0.Direction = New Vector3(0, -1, 1) Light0.Diffuse = Color.White Light0.Ambient = Color.Gray Light0.Enabled = True Light0.Commit() DX9.RenderState.Lighting = True DX9.RenderState.Ambient = Color.Gray End Sub
The next procedure that gets called is
InitializeView
. Like the
Private Sub InitializeView() Dim eyePosition As New Vector3(0, 0, -75) Dim direction As New Vector3(0, 0, 0) Dim upDirection As New Vector3(0, 1, 0) Dim view As Matrix = Matrix.LookAtLH(eyePosition, direction, upDirection) DX9.SetTransform(Direct3D.TransformType.View, view) Dim fieldOfView As Single = Math.PI / 4 Dim aspectRatio As Single = 1.0 Dim nearPlane As Single = 1.0 Dim farPlane As Single = 500.0 Dim projection As Matrix = _ Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane) DX9.SetTransform(Direct3D.TransformType.Projection, projection) End Sub
The remaining two procedures are Render and OnDeviceReset . OnDeviceReset simply calls InitializeDirect3D , the procedure we looked at earlier. The Render procedure contains the code, which actually causes the mesh to be rendered to the screen. Here is the code for both of them:
Private Sub OnDeviceReset(ByVal Sender As Object, ByVal e As EventArgs) InitializeDirect3D() End Sub Private Sub Render(ByVal sender As Object, ByVal e As EventArgs) DX9.Clear(Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, _ Color.Black.ToArgb(), 1.0, 0) DX9.BeginScene() DX9.Material = Material Mesh.DrawSubset(0) DX9.EndScene() DX9.Present() End Sub
If you were to run the application at this time, you would see a scene rendered something like Figure 29.2.
Figure 29.2:
The scene rendered in DirectX is similar to its OpenGL
| < Day Day Up > |