Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've been worling on this class for a while, and while it's not finished it works pretty good and should be a great starting point for anyone looking to build d3d apps.

 

Class Code:

 

Imports System
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX

Public Class clsMesh

   'Variable Declerations
   Private Device As Device                    'Reference do the main Device
   Private v3Position As New Vector3(0, 0, 0)  'Location of our Mesh in 3D space
   Private Mesh As Mesh                        'Our mesh file
   Private Materials() As Material             'Mesh Materials
   Private Textures() As Texture               'Mesh Textures


   Private pitchRadians As Single = 0
   Private yawRadians As Single = 0


   'Amount to increase the pitch by
   Public Sub Pitch(ByVal radians As Single)
       'Device.Transform.World = Matrix.RotationAxis(New Vector3(0, 1, 0), radians)
       pitchRadians += radians
   End Sub

   'Amount to increase yaw by
   Public Sub Yaw(ByVal radians As Single)
       yawRadians += radians
   End Sub


   Sub New(ByRef ldev As Device, ByVal path As String)
       xNew(ldev, path, New Vector3(0, 0, 0))
   End Sub
   Sub New(ByRef ldev As Device, ByVal path As String, ByVal v3pos As Vector3)
       xNew(ldev, path, v3pos)
   End Sub

   'All other 'new' routines call this one, this one has all of the parapaters
   Private Sub xNew(ByRef ldev As Device, ByVal path As String, ByVal v3 As Vector3)
       'Set our local referance to the main device
       Device = ldev

       'Create the mesh file from the path to the .X file
       CreateMesh(path)

       'set our position in 3D Space
       Position = v3
   End Sub

   Public Sub Move(ByVal pos As Vector3)
       'Set our mesh's position so next time we don't have to call the Move sub
       Position = pos
   End Sub


   Public Sub Update()
       'Transform the matrix world to the location we want our
       'mesh to be at, additionally adjust the pitch and yaw of our mesh

       Dim FinalMatrix As Matrix
       FinalMatrix = Matrix.Multiply(Matrix.RotationAxis(New Vector3(0, 1, 0), pitchRadians), Matrix.RotationAxis(New Vector3(1, 0, 0), yawRadians))
       FinalMatrix = Matrix.Multiply(FinalMatrix, Matrix.Translation(Position))
       Device.Transform.World = FinalMatrix
   End Sub

   Public Sub DrawMesh()
       DrawMesh(v3Position)
   End Sub
   Public Sub DrawMesh(ByVal v3 As Vector3)
       'Are we moving to the same location?
       If v3 <> v3Position Then
           'If not move to the location provided.
           Move(v3)
       End If
       'Loop through the materials and set the textures
       For i As Integer = 0 To Materials.Length - 1
           'If the texture isn't null
           If Not Textures(i) Is System.DBNull.Value Then
               'Set the device texture
               Device.SetTexture(0, Textures(i))
           End If

           'set the materials
           Device.Material = Materials(i)

           'draw the mesh
           Mesh.DrawSubset(i)
       Next i
   End Sub

   Private Sub CreateMesh(ByVal path As String)
       'Define extended materials
       Dim extendedMaterials() As ExtendedMaterial = Nothing

       'Actually load our mesh from a file
       Mesh = Direct3D.Mesh.FromFile(path, MeshFlags.SystemMemory, Device, extendedMaterials)

       If Not Textures Is System.DBNull.Value Then
           'we should dispose textures at this point
           'functionallity needs to be written.
       End If

       'Begin setting up our textures
       Textures = New Texture(extendedMaterials.Length) {}

       'Begin setting up the materials
       Materials = New Material(extendedMaterials.Length) {}

       'Loop through the materials
       For i As Integer = 0 To extendedMaterials.Length - 1

           'If the texture file name is nothing skip this section
           If extendedMaterials(i).TextureFilename <> "" Then

               'Define variable
               Dim texturePath As String

               'set the texture path via standard .NET methods
               texturePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), extendedMaterials(i).TextureFilename)

               'Now we load the texutre into it's respective place in the array
               Textures(i) = TextureLoader.FromFile(Device, texturePath)
           End If
           Materials(i) = extendedMaterials(i).Material3D
           Materials(i).Ambient = Materials(i).Diffuse
       Next i
   End Sub

   Public Property Position() As Vector3
       Get
           Return v3Position
       End Get
       Set(ByVal value As Vector3)
           v3Position = value
       End Set
   End Property
End Class

 

Usage:

Dim x as clsMesh

x = new clsMesh(dxDevice,"pathtomesh.x")

Main Loop
   dxDevice.beginscene()
   x.DrawMesh()
   dxDevice.endscene()
Loop

 

The usage code is untested, but should be pretty simple to get working, the class code should compile aginst the 2005 October SDK. The December SDK may work as well, but I haven't tested with it as much.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...