Tipp 0184 Würfel erstellen und rotieren
Autor/Einsender:
Datum:
  Thomas Becker
20.09.2008
Entwicklungsumgebung: VB.Net 2005
DirectX-Version:   DirectX 9
Der Tipp zeigt, wie man mittels Transformation ein Objekt mit einfachen Mitteln zur Rotation bringen kann. Hier findet beispielhaft Matrix.RotationYawPitchRoll Anwendung, um den Würfel zu drehen.
 
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public Class Form1

Private ReadOnly m_DataNormals As Single(,) = New Single(,) _
  {{0.0!, 0.0!, 1.0!}, {0.0!, 0.0!, 1.0!}, {0.0!, 0.0!, -1.0!}, _
  {1.0!, 0.0!, -1.0!}, {0.0!, 1.0!, 0.0!}, {0.0!, 1.0!, 0.0!}, _
  {0.0!, -1.0!, 0.0!}, {0.0!, -1.0!, 0.0!}, {-1.0!, 0.0!, 0.0!}, _
  {-1.0!, 0.0!, 0.0!}, {1.0!, 0.0!, 0.0!}, {1.0!, 0.0!, 0.0!}}

Private ReadOnly m_DataTriangle As Single(,) = New Single(,) _
  {{-1.0!, 1.0!, 1.0!}, {-1.0!, -1.0!, 1.0!}, {1.0!, 1.0!, 1.0!}, _
  {-1.0!, -1.0!, 1.0!}, {1.0!, -1.0!, 1.0!}, {1.0!, 1.0!, 1.0!}, _
  {-1.0!, 1.0!, -1.0!}, {1.0!, 1.0!, -1.0!}, {-1.0!, -1.0!, _
  -1.0!}, {-1.0!, -1.0!, -1.0!}, {1.0!, 1.0!, -1.0!}, {1.0!, _
  -1.0!, -1.0!}, {-1.0!, 1.0!, 1.0!}, {1.0!, 1.0!, -1.0!}, _
  {-1.0!, 1.0!, -1.0!}, {-1.0!, 1.0!, 1.0!}, {1.0!, 1.0!, 1.0!}, _
  {1.0!, 1.0!, -1.0!}, {-1.0!, -1.0!, 1.0!}, {-1.0!, -1.0!, _
  -1.0!}, {1.0!, -1.0!, -1.0!}, {-1.0!, -1.0!, 1.0!}, {1.0!, _
  -1.0!, -1.0!}, {1.0!, -1.0!, 1.0!}, {-1.0!, 1.0!, 1.0!}, _
  {-1.0!, -1.0!, -1.0!}, {-1.0!, -1.0!, 1.0!}, {-1.0!, 1.0!, _
  -1.0!}, {-1.0!, -1.0!, -1.0!}, {-1.0!, 1.0!, 1.0!}, {1.0!, _
  1.0!, 1.0!}, {1.0!, -1.0!, 1.0!}, {1.0!, -1.0!, -1.0!}, {1.0!, _
  1.0!, -1.0!}, {1.0!, 1.0!, 1.0!}, {1.0!, -1.0!, -1.0!}}

Private _pp As PresentParameters
Private _dev As Device, _angel As Single

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
  _pp = New PresentParameters

  With _pp
    .Windowed = True
    .SwapEffect = SwapEffect.Discard
    .EnableAutoDepthStencil = True
    .AutoDepthStencilFormat = DepthFormat.D16
  End With

  _dev = New Device(0, DeviceType.Hardware, Panel1.Handle, _
         CreateFlags.SoftwareVertexProcessing, _pp)
  Timer1.Enabled = True

  Button1.Enabled = False
  Button2.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button2.Click
  If Button2.Text = "Anhalten" Then
    Button2.Text = "Weiter"
    Timer1.Enabled = False
  Else
    Button2.Text = "Anhalten"
    Timer1.Enabled = True
  End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
  Me.Close()
End Sub

Private Function GetPositionColored() As _
   CustomVertex.PositionNormalColored()
  Dim verts As CustomVertex.PositionNormalColored() = _
      New CustomVertex.PositionNormalColored(36) {}

  For vertCounter As Integer = 0 To 35
    verts(vertCounter) = New
    CustomVertex.PositionNormalColored(m_DataTriangle(vertCounter, _
    0), m_DataTriangle(vertCounter, 1), _
    m_DataTriangle(vertCounter, 2), _
    m_DataNormals((vertCounter \ 3), 0), _
    m_DataNormals((vertCounter \ 3), 1), _
    m_DataNormals((vertCounter \ 3), 2), 0)
  Next
  Return verts
End Function

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e _
    As System.EventArgs) Handles Timer1.Tick

  With _dev
    .Clear(ClearFlags.Target Or ClearFlags.ZBuffer, _
     Color.LightBlue, 1, 0)
    .Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 4, _
     1, 1, 10)
    .Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 5), _
     New Vector3(), New Vector3(0, 1, 0))
    .RenderState.Lighting = False
    .BeginScene()
    .Transform.World = Matrix.RotationYawPitchRoll(_angel, _
     _angel, _angel)
    _angel = _angel + 0.2!

    .VertexFormat = CustomVertex.PositionColored.Format
    .DrawUserPrimitives(PrimitiveType.TriangleList, 12, _
     GetPositionColored)
    .EndScene()
    .Present()
  End With
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.FormClosingEventArgs) _
    Handles Me.FormClosing
   ' Prozedur zum Formschließen
  Timer1.Enabled = False
   ' Device aus dem Speicher löschen
  _dev.Dispose()
End Sub
End Class
 
Hinweis
Um diesen Tipp ausführen zu können, wird die DirectX 9 for Managed Code Runtime benötigt.

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (13,7 kB) Downloads bisher: [ 641 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 31. Dezember 2011