Вот листинг программы:
- Код: Выделить всё
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class ZBuffer
#Region " Объявление переменных Direct3D "
Private Direct3D_Device As Direct3D.Device
Private Direct3D_PresentParameters As New Direct3D.PresentParameters
Private vertexBuffer As VertexBuffer = Nothing
Private indexBuffer As IndexBuffer = Nothing
#End Region
Dim angle As Single
Private Shared ReadOnly indices() As Short = {0, 1, 2, _
1, 3, 2, _
4, 5, 6, _
6, 5, 7, _
0, 5, 4, _
0, 2, 5, _
1, 6, 7, _
1, 7, 3, _
0, 6, 1, _
4, 6, 0, _
2, 3, 7, _
5, 2, 7}
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.ClientSize = New System.Drawing.Size(300, 300)
Me.Text = "Direct3D Tutorial 9 - Z буфер(глубины)"
Me.SetStyle(ControlStyles.AllPaintingInWmPaint & ControlStyles.Opaque, True)
Direct3D_PresentParameters.Windowed = True
Direct3D_PresentParameters.SwapEffect = SwapEffect.Discard
Direct3D_PresentParameters.EnableAutoDepthStencil = True
Direct3D_PresentParameters.AutoDepthStencilFormat = DepthFormat.D16
Direct3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_PresentParameters)
OnCreateDevice(Direct3D_Device, Nothing)
End Sub
Public Sub OnCreateDevice(ByVal sender As Object, ByVal e As EventArgs)
Dim dev As Device = CType(sender, Device)
vertexBuffer = New VertexBuffer(GetType(CustomVertex.PositionColored), 8, dev, Usage.Dynamic + Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default)
AddHandler vertexBuffer.Created, AddressOf Me.OnCreateVertexBuffer
Me.OnCreateVertexBuffer(vertexBuffer, Nothing)
indexBuffer = New IndexBuffer(GetType(Short), indices.Length, dev, Usage.WriteOnly, Pool.Default)
Me.OnCreateIndexBuffer(indexBuffer, Nothing)
End Sub
Public Sub OnCreateVertexBuffer(ByVal sender As Object, ByVal e As EventArgs)
Dim vb As VertexBuffer = CType(sender, VertexBuffer)
Dim verts As CustomVertex.PositionColored() = CType(vb.Lock(0, 0), CustomVertex.PositionColored())
verts(0) = New CustomVertex.PositionColored(-1, 1, 1, System.Drawing.Color.Purple.ToArgb())
verts(1) = New CustomVertex.PositionColored(-1, -1, 1, System.Drawing.Color.Red.ToArgb())
verts(2) = New CustomVertex.PositionColored(1, 1, 1, System.Drawing.Color.Blue.ToArgb())
verts(3) = New CustomVertex.PositionColored(1, -1, 1, System.Drawing.Color.Yellow.ToArgb())
verts(4) = New CustomVertex.PositionColored(-1, 1, -1, System.Drawing.Color.Gold.ToArgb())
verts(5) = New CustomVertex.PositionColored(1, 1, -1, System.Drawing.Color.Green.ToArgb())
verts(6) = New CustomVertex.PositionColored(-1, -1, -1, System.Drawing.Color.Black.ToArgb())
verts(7) = New CustomVertex.PositionColored(1, -1, -1, System.Drawing.Color.WhiteSmoke.ToArgb())
vb.SetData(verts, 0, LockFlags.None)
vb.Unlock()
End Sub
Public Sub OnCreateIndexBuffer(ByVal sender As Object, ByVal e As EventArgs)
Dim ib As IndexBuffer = CType(sender, IndexBuffer)
ib.SetData(indices, 0, LockFlags.None)
End Sub
Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Direct3D_Device.Clear(ClearFlags.Target + ClearFlags.ZBuffer, System.Drawing.Color.CornflowerBlue, 1, 0)
SetupCamera()
Me.Invalidate()
Direct3D_Device.BeginScene()
Direct3D_Device.SetStreamSource(0, vertexBuffer, 0)
Direct3D_Device.Indices = indexBuffer
Direct3D_Device.VertexFormat = CustomVertex.PositionColored.Format
DrawBox(angle / Math.PI, angle / Math.PI * 2, angle / Math.PI / 4, 0, 0, 0)
DrawBox(angle / Math.PI, angle / Math.PI * 3, angle / Math.PI / 4, 5, 0, 0)
DrawBox(angle / Math.PI, angle / Math.PI * 4, angle / Math.PI / 4, -5, 0, 0)
DrawBox(angle / Math.PI, angle / Math.PI * 2, angle / Math.PI / 4, 0, Math.Cos(angle) * 5, Math.Sin(angle) * 5)
Direct3D_Device.EndScene()
Direct3D_Device.Present()
End Sub
Private Sub SetupCamera()
Direct3D_Device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI) / 4, Me.Width / Me.Height, 0, 100)
Direct3D_Device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 18), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
Direct3D_Device.RenderState.Lighting = False
End Sub
Private Sub DrawBox(ByVal yaw As Single, ByVal pitch As Single, ByVal roll As Single, ByVal x As Single, ByVal y As Single, ByVal z As Single)
angle += 0.01
Direct3D_Device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z)
Direct3D_Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, indices.Length / 3)
End Sub
End Class