Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Managed DirectX Demo - GameEngine.vb

GameEngine.vb

Caricato da: GN
Scarica il programma completo

  1. Imports Microsoft.DirectX
  2. Imports Microsoft.DirectX.Direct3D
  3. Imports Microsoft.DirectX.DirectInput
  4. Imports Microsoft.DirectX.DirectSound
  5.  
  6. Public Class GameEngine
  7.     Dim DispGrafico As Direct3D.Device
  8.     Dim Tastiera As DirectInput.Device
  9.     Dim Mouse As DirectInput.Device
  10.     Dim Suoni As DirectSound.Device
  11.     Dim ListaOgg As New List(Of String)
  12.     Dim Pos As New Vector3(0, 0, 0)
  13.     Dim Rot As New Vector3(0, 0, 0)
  14.     Dim MeshCorr As oggX
  15.     Dim TempoPassatoMouse As Integer
  16.     Dim Suono1 As SecondaryBuffer
  17.     Dim Suono2 As SecondaryBuffer
  18.     Private _OggCorr As Integer
  19.  
  20.     Property OggCorr() As Integer
  21.         Get
  22.             Return _OggCorr
  23.         End Get
  24.         Set(ByVal value As Integer)
  25.             If value <> ListaOgg.Count And value <> -1 Then
  26.                 _OggCorr = value
  27.             ElseIf value = ListaOgg.Count Then
  28.                 _OggCorr = 0
  29.             ElseIf value = -1 Then
  30.                 _OggCorr = ListaOgg.Count - 1
  31.             End If
  32.             MeshCorr = Me.CaricaMesh(ListaOgg.Item(_OggCorr) & "\Mesh.x", True, True, ListaOgg.Item(OggCorr), Pos, Rot)
  33.         End Set
  34.     End Property
  35.  
  36.     Private Sub GameEngine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  37.         MsgBox("Managed DirectX Demo: esempio di utilizzo delle DirectX in VB.NET. Sviluppato da GN." & Chr(13) _
  38.         & "E' possibile aggiungere altri oggetti 3D inserendo sottocartelle con un file Mesh.x e le textures nella cartella Oggetti." & Chr(13) & Chr(13) _
  39.         & "Comandi della visualizzazzione 3D:" & Chr(13) _
  40.         & "Frecce direzionali: ruota sugli assi X e Y;" & Chr(13) _
  41.         & "PagSu\PagGiù: ruota sull'asse Z;" & Chr(13) _
  42.         & "Maiusc + Frecce direzionali: muovi sugli assi X e Y;" & Chr(13) _
  43.         & "Maiusc + PagSu\PagGiù: muovi sull'asse Z;" & Chr(13) _
  44.         & "Barra spaziatrice: resetta posizione;" & Chr(13) _
  45.         & "Clic sinistro: oggetto successivo;" & Chr(13) _
  46.         & "Clic destro: oggetto precedente;" & Chr(13) _
  47.         & "Esc: esci." & Chr(13) & Chr(13) _
  48.         & "Fare clic su OK per iniziare.")
  49.         Dim ParametriGrafici As New PresentParameters()
  50.         If MsgBox("Schermo intero?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  51.             ParametriGrafici.BackBufferCount = 1
  52.             ParametriGrafici.AutoDepthStencilFormat = DepthFormat.D16
  53.             ParametriGrafici.EnableAutoDepthStencil = True
  54.             ParametriGrafici.DeviceWindowHandle = Me.Handle
  55.             ParametriGrafici.SwapEffect = SwapEffect.Flip
  56.             ParametriGrafici.Windowed = False
  57.             ParametriGrafici.BackBufferWidth = My.Computer.Screen.Bounds.Width
  58.             ParametriGrafici.BackBufferHeight = My.Computer.Screen.Bounds.Height
  59.             ParametriGrafici.BackBufferFormat = Format.X8R8G8B8
  60.         Else
  61.             ParametriGrafici.BackBufferCount = 1
  62.             ParametriGrafici.AutoDepthStencilFormat = DepthFormat.D16
  63.             ParametriGrafici.EnableAutoDepthStencil = True
  64.             ParametriGrafici.DeviceWindowHandle = Me.Handle
  65.             ParametriGrafici.SwapEffect = SwapEffect.Flip
  66.             ParametriGrafici.Windowed = True
  67.         End If
  68.         DispGrafico = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, ParametriGrafici)
  69.         Tastiera = New DirectInput.Device(SystemGuid.Keyboard)
  70.         Tastiera.SetDataFormat(DeviceDataFormat.Keyboard)
  71.         Tastiera.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
  72.         Tastiera.Acquire()
  73.         Mouse = New DirectInput.Device(SystemGuid.Mouse)
  74.         Mouse.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
  75.         Mouse.SetDataFormat(DeviceDataFormat.Mouse)
  76.         Mouse.Acquire()
  77.         Suoni = New DirectSound.Device
  78.         Suoni.SetCooperativeLevel(Me, CooperativeLevel.Priority)
  79.         Me.Show()
  80.         Me.Focus()
  81.         For Each CartellaCorr As String In My.Computer.FileSystem.GetDirectories(Application.StartupPath & "\Oggetti")
  82.             ListaOgg.Add(CartellaCorr)
  83.         Next
  84.         OggCorr = 0
  85.         Suono1 = CaricaSuono(Application.StartupPath & "\Suoni\1.wav")
  86.         Suono2 = CaricaSuono(Application.StartupPath & "\Suoni\2.wav")
  87.         Me.MainLoop()
  88.     End Sub
  89.  
  90.     Sub MainLoop()
  91.         Do
  92.             Tastiera.Poll()
  93.             Mouse.Poll()
  94.             DispGrafico.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Blue, 1, 0)
  95.             DispGrafico.BeginScene()
  96.             DispGrafico.SamplerState(0).MinFilter = TextureFilter.Linear
  97.             DispGrafico.Transform.View = Matrix.LookAtLH(New Vector3(10, 10, 10), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
  98.             DispGrafico.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / 16), 4 / 3, 1, 2000)
  99.             DispGrafico.RenderState.Lighting = True
  100.             DispGrafico.RenderState.Ambient = Color.White
  101.             Dim Modifica As New Vector3(0, 0, 0)
  102.             If Tastiera.GetCurrentKeyboardState.Item(Key.LeftShift) = True Then
  103.                 Modifica = Pos
  104.             Else
  105.                 Modifica = Rot
  106.             End If
  107.             If Tastiera.GetCurrentKeyboardState.Item(Key.Left) = True Then
  108.                 Modifica.X += 0.1
  109.             ElseIf Tastiera.GetCurrentKeyboardState.Item(Key.Right) = True Then
  110.                 Modifica.X -= 0.1
  111.             End If
  112.             If Tastiera.GetCurrentKeyboardState.Item(Key.Up) = True Then
  113.                 Modifica.Y += 0.1
  114.             ElseIf Tastiera.GetCurrentKeyboardState.Item(Key.Down) = True Then
  115.                 Modifica.Y -= 0.1
  116.             End If
  117.             If Tastiera.GetCurrentKeyboardState.Item(Key.PageUp) = True Then
  118.                 Modifica.Z += 0.1
  119.             ElseIf Tastiera.GetCurrentKeyboardState.Item(Key.PageDown) = True Then
  120.                 Modifica.Z -= 0.1
  121.             End If
  122.             If Tastiera.GetCurrentKeyboardState.Item(Key.LeftShift) = True Then
  123.                 Pos = Modifica
  124.             Else
  125.                 Rot = Modifica
  126.             End If
  127.             If Tastiera.GetCurrentKeyboardState.Item(Key.Escape) Then
  128.                 Application.Exit()
  129.             End If
  130.             If Tastiera.GetCurrentKeyboardState.Item(Key.Space) = True Then
  131.                 Pos = New Vector3(0, 0, 0)
  132.                 Rot = New Vector3(0, 0, 0)
  133.             End If
  134.             If Mouse.CurrentMouseState.GetMouseButtons(0) > 0 Then
  135.                 If TempoPassatoMouse = 20 Then
  136.                     OggCorr += 1
  137.                     TempoPassatoMouse = 0
  138.                     Suono1.Play(0, BufferPlayFlags.Default)
  139.                 End If
  140.             End If
  141.             If Mouse.CurrentMouseState.GetMouseButtons(1) > 0 Then
  142.                 If TempoPassatoMouse = 20 Then
  143.                     OggCorr -= 1
  144.                     TempoPassatoMouse = 0
  145.                     Suono2.Play(0, BufferPlayFlags.Default)
  146.                 End If
  147.             End If
  148.             If TempoPassatoMouse <> 20 Then
  149.                 TempoPassatoMouse += 1
  150.             End If
  151.             MeshCorr.pos = Pos
  152.             MeshCorr.rot = Rot
  153.             Me.AggiungiMesh(MeshCorr)
  154.             DispGrafico.EndScene()
  155.             DispGrafico.Present()
  156.             Application.DoEvents()
  157.         Loop
  158.     End Sub
  159.  
  160.     Function CaricaMesh(ByVal fileSrc As String, ByVal materialiOn As Boolean, ByVal textureOn As Boolean, ByVal TexPath As String, ByVal pos As Vector3, ByVal rot As Vector3) As oggX
  161.         With CaricaMesh
  162.             Dim materiali() As ExtendedMaterial = Nothing
  163.             .mesh = Mesh.FromFile(fileSrc, MeshFlags.Dynamic, DispGrafico, materiali)
  164.             .numX = UBound(materiali)
  165.             ReDim .tex(.numX)
  166.             ReDim .mat(.numX)
  167.             Dim i As Integer
  168.             For i = 0 To .numX
  169.                 If textureOn Then
  170.                     If materiali(i).TextureFilename <> "" Then
  171.                         .tex(i) = TextureLoader.FromFile(DispGrafico, TexPath & "\" & materiali(i).TextureFilename)
  172.                     End If
  173.                 End If
  174.                 If materialiOn Then
  175.                     .mat(i) = materiali(i).Material3D
  176.                     .mat(i).Ambient = .mat(i).Diffuse
  177.                 End If
  178.             Next
  179.             .pos = pos
  180.             .rot = rot
  181.         End With
  182.     End Function
  183.  
  184.     Sub AggiungiMesh(ByVal Modello As oggX)
  185.         Dim pos As Matrix = Matrix.Translation(Modello.pos)
  186.         Dim rot As Matrix = Matrix.RotationYawPitchRoll(Modello.rot.Y, Modello.rot.X, Modello.rot.Z)
  187.         DispGrafico.Transform.World = Matrix.Multiply(pos, rot)
  188.         For i As Integer = 0 To Modello.numX
  189.             DispGrafico.Material = Modello.mat(i)
  190.             DispGrafico.SetTexture(0, Modello.tex(i))
  191.             Modello.mesh.DrawSubset(i)
  192.         Next
  193.     End Sub
  194.  
  195.     Function CaricaSuono(ByVal src As String) As SecondaryBuffer
  196.         Dim d As New BufferDescription()
  197.         d.Flags = BufferDescriptionFlags.ControlPan Or BufferDescriptionFlags.ControlFrequency Or BufferDescriptionFlags.ControlVolume
  198.         Return New SecondaryBuffer(src, d, Suoni)
  199.     End Function
  200.  
  201.     Private Sub GameEngine_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  202.         End
  203.     End Sub
  204. End Class