Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class clsEngine
Public BE_Device As Device
Public BE_Map(100, 1000, 100) As Brick
Dim BE_VertexBuffer As VertexBuffer = Nothing
Dim int As Integer
Public Structure Brick
Dim Material As Integer
Dim Bild As Texture
End Structure
Public Function InitializeGraphics() As Boolean
Dim params As New PresentParameters
params.Windowed = True
params.SwapEffect = SwapEffect.Discard
params.EnableAutoDepthStencil = True ' Depth stencil on.
params.AutoDepthStencilFormat = DepthFormat.D16
'1. Renderversuch
Try
BE_Device = New Device(0, DeviceType.Hardware, frmMain.imgRender, _
CreateFlags.HardwareVertexProcessing, params)
Debug.WriteLine("Hardware, HardwareVertexProcessing")
Catch
End Try
'2. Renderversuch
If BE_Device Is Nothing Then
Try
BE_Device = New Device(0, DeviceType.Hardware, frmMain.imgRender, _
CreateFlags.SoftwareVertexProcessing, params)
Debug.WriteLine("Hardware, SoftwareVertexProcessing")
Catch
End Try
End If
'3. Renderversuch
If BE_Device Is Nothing Then
Try
BE_Device = New Device(0, DeviceType.Reference, frmMain.imgRender, _
CreateFlags.SoftwareVertexProcessing, params)
Debug.WriteLine("Reference, SoftwareVertexProcessing")
Catch ex As Exception
'Fehler anzeigen
MessageBox.Show("Error initializing Direct3D" & _
vbCrLf & vbCrLf & ex.Message, _
"Direct3D Error", MessageBoxButtons.OK)
Return False
End Try
End If
BE_Device.RenderState.Lighting = False
BE_Device.RenderState.ZBufferEnable = True
BE_Device.RenderState.CullMode = Cull.CounterClockwise
BE_Device.RenderState.PointSize = 4
BE_Device.RenderState.FillMode = FillMode.Solid
CreateVertexBuffer()
BE_Device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, -10), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
Return True
End Function
'vertex buffer erstellen
Public Sub CreateVertexBuffer()
' Create buffer
BE_VertexBuffer = New VertexBuffer(GetType(CustomVertex.PositionColored), 10000000, BE_Device, 0, CustomVertex.PositionColored.Format, Pool.Default)
Dim vertices As CustomVertex.PositionColored() = CType(BE_VertexBuffer.Lock(0, 0), CustomVertex.PositionColored())
For x As Integer = 0 To BE_Map.GetLength(0) - 1
For y As Integer = 0 To BE_Map.GetLength(1) - 1
For z As Integer = 0 To BE_Map.GetLength(2) - 1
If BE_Map(x, y, z).Material = 0 Then
ElseIf BE_Map(x, y, z).Material = 1 Then
BuildField(vertices, x * 3.2, y * 3.2, z * 3.2)
End If
Next
Next
Next
BE_VertexBuffer.Unlock()
End Sub
#Region "Würfel erstellen"
Private Sub BuildField(ByVal Vertics() As CustomVertex.PositionColored, ByVal LocationX As Single, ByVal LocationY As Single, ByVal LocationZ As Single)
'front
MakeTriangle(Vertics, int, Color.Red, LocationX, LocationY + 3.2, LocationZ, LocationX + 3.2, LocationY, LocationZ, LocationX, LocationY, LocationZ)
MakeTriangle(Vertics, int, Color.Red, LocationX, LocationY + 3.2, LocationZ, LocationX + 3.2, LocationY + 3.2, LocationZ, LocationX + 3.2, LocationY, LocationZ)
'back
MakeTriangle(Vertics, int, Color.Red, LocationX, LocationY, LocationZ + 3.2, LocationX + 3.2, LocationY, LocationZ + 3.2, LocationX, LocationY + 3.2, LocationZ + 3.2)
MakeTriangle(Vertics, int, Color.Red, LocationX + 3.2, LocationY, LocationZ + 3.2, LocationX + 3.2, LocationY + 3.2, LocationZ + 3.2, LocationX, LocationY + 3.2, LocationZ + 3.2)
'left
MakeTriangle(Vertics, int, Color.Green, LocationX, LocationY, LocationZ, LocationX, LocationY, LocationZ + 3.2, LocationX, LocationY + 3.2, LocationZ)
MakeTriangle(Vertics, int, Color.Green, LocationX, LocationY + 3.2, LocationZ, LocationX, LocationY, LocationZ + 3.2, LocationX, LocationY + 3.2, LocationZ + 3.2)
'right
MakeTriangle(Vertics, int, Color.Green, LocationX + 3.2, LocationY, LocationZ, LocationX + 3.2, LocationY + 3.2, LocationZ, LocationX + 3.2, LocationY, LocationZ + 3.2)
MakeTriangle(Vertics, int, Color.Green, LocationX + 3.2, LocationY + 3.2, LocationZ, LocationX + 3.2, LocationY + 3.2, LocationZ + 3.2, LocationX + 3.2, LocationY, LocationZ + 3.2)
'top
MakeTriangle(Vertics, int, Color.Blue, LocationX, LocationY + 3.2, LocationZ, LocationX, LocationY + 3.2, LocationZ + 3.2, LocationX + 3.2, LocationY + 3.2, LocationZ)
MakeTriangle(Vertics, int, Color.Blue, LocationX + 3.2, LocationY + 3.2, LocationZ, LocationX, LocationY + 3.2, LocationZ + 3.2, LocationX + 3.2, LocationY + 3.2, LocationZ + 3.2)
'bottom
MakeTriangle(Vertics, int, Color.Blue, LocationX, LocationY, LocationZ, LocationX + 3.2, LocationY, LocationZ, LocationX + 3.2, LocationY, LocationZ + 3.2)
MakeTriangle(Vertics, int, Color.Blue, LocationX, LocationY, LocationZ, LocationX + 3.2, LocationY, LocationZ + 3.2, LocationX, LocationY, LocationZ + 3.2)
int += 36
End Sub
Private Sub MakeTriangle(ByVal vertices() As CustomVertex.PositionColored, ByRef i As Integer, ByVal clr As Color, _
ByVal x0 As Single, ByVal y0 As Single, ByVal z0 As Single, _
ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, _
ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single)
vertices(i).X = x0
vertices(i).Y = y0
vertices(i).Z = z0
vertices(i).Color = clr.ToArgb
i += 1
vertices(i).X = x1
vertices(i).Y = y1
vertices(i).Z = z1
vertices(i).Color = clr.ToArgb
i += 1
vertices(i).X = x2
vertices(i).Y = y2
vertices(i).Z = z2
vertices(i).Color = clr.ToArgb
i += 1
End Sub
#End Region
Private Sub SetupMatrices()
'Sicht festlegen
BE_Device.Transform.Projection = _
Matrix.PerspectiveFovLH(Math.PI / 4, frmMain.imgRender.Width / frmMain.imgRender.Height, 1, 1000)
End Sub
Public Sub Render()
'Hintergrundfarbe: schwarz
BE_Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Black, 1, 0)
'Szene beginnen
BE_Device.BeginScene()
'Kamera positionieren
SetupMatrices()
'Vertex Buffer zuweisen
BE_Device.SetStreamSource(0, BE_VertexBuffer, 0)
'Format der dreiecke
BE_Device.VertexFormat = CustomVertex.PositionColored.Format
'Keine Ahnung
BE_Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 256)
'Szene abschließen und anzeigen
BE_Device.EndScene()
BE_Device.Present()
End Sub
End Class