• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

DirectX vb.net im Notfall auch C++

Firefighter

Mitglied
So ich hab jetzt ewig in Gooogle gesucht, aber nichts gefunden, was mir wirklich weiter hilft.
Mein Problem: Ich will DirectX lernen und finde kein Tutorial, das ich versteh. Ich weiß inzwischen, wie man DirectX einbindet und das Formular konfiguriert. Jetzt will ich auf dieses Formular Würfel mit Texturen zeichnen und zwar möglichst viele mit wenig Aufwand (also als Schleife). Das wäre weiter kein Problem, wenn ich wüsste wie man Dreiecke zeichnet.

Und jetzt nochmal in Kurzform:
Ich möchte wissen wie man Triangles auf die Zeichenfläche zeichnet und mit Bildtexturen füllt und das ganze auf Idiotendeutsch!

Achja, das ganze wenn möglich in vb.net ich kann zwar auch C++, bin darin abernoch ein Anfänger (kann einfache Programme mit Schleifen und Bedingungen schreiben).

Im Voraus schon mal danke
 
Werbung:
Werbung:
Wenn du es einfach haben willst, dann schau dir doch mal XNA.
Ich hab schon von vielen gehört, das dass deutlich einfach und trotzdem gut sein soll.

Gruß KY
 
Ich hab vor einiger Zeit auch mit Directx angefangen (aber mit C#). Die oben genannten Tutorials sind echt in Ordnung, die habe ich auch durchgearbeitet.

Aber am einfachsten (für mich zumindest), war es mit einem Buch zum Thema, welches ich mir geleistet habe. 3D-Spieleprogrammierung mit DirectX 9 und C++.: Amazon.de: David Scherfgen: Bücher (ist zwar mit C++, aber lässt sich leicht übersetzen, das Prinzip bleibt ja gleich)

Also INput noch eine Seite: CodeSampler.com - Direct3D (DirectX 9.0) game programming samples for Windows

Viel Spass und verlier den Mut nicht wenn es mal nicht so klappt =)
 
Ich entschuldige mich schon mal im voraus für den vielen Quellencode!

Also ich hab mir jetzt mit müh und not eine 3d-Klasse zusammen gestellt, die mir basierend auf einer Array beliebig viele Würfel darstellen soll. Das ganze funktioniert auch noch recht gut aber jetzt möchte ich gerne wissen, wie ich statt farben eine Textur auf die Würfel bring.

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
 
Werbung:
Zurück
Oben