Public Class MeshPlus
Implements IDisposable
Public MasterMesh As Mesh
Public MMMtrls() As Material
Public MMTxtrs() As Texture
Public ReadOnly SubsetsEx As Integer
Public Sub New(ByVal Filename As String, ByVal Options As MeshFlags, ByVal D As Device)
Dim Ems() As ExtendedMaterial, LV As Integer
'Merely holds a material and a filename for a texture.
'MasterMesh = Mesh.FromFile(Application.StartupPath & "\mball.x", MeshFlags.SoftwareProcessing, D9)
'You can use the one above if you have a material and texture that you will be supplying to the mesh.
'However, the material and texture can be included within the .x file.
MasterMesh = Mesh.FromFile(Filename, Options, D, Ems)
'Loads the mesh from file. .x data goes into the Extended Material array.
'The number of entries corresponds to the number of materials and textures.
SubsetsEx = Ems.GetUpperBound(0)
MMMtrls = New Material(SubsetsEx) {}
MMTxtrs = New Texture(SubsetsEx) {}
For LV = 0 To SubsetsEx
MMMtrls(LV) = Ems(LV).Material3D
'Set the material. 3D means DX3D.
MMMtrls(LV).Ambient = MMMtrls(LV).Diffuse
'Ambient property is lost within the loader or in the .x file.
'But anyway, this property must be set in VB.
'Not every mesh will have a texture, so meshes without textures will have Texture = Nothing
If Not (Ems(LV).TextureFilename Is Nothing) Then
MMTxtrs(LV) = TextureLoader.FromFile(D, Ems(LV).TextureFilename)
End If
Next
'All of this information will be used when drawing the mesh.
End Sub
Public Sub DrawAllSubsets(ByVal D As Device)
'Each subset must be drawn separately and is what we extracted data from.
Dim LV As Integer
For LV = 0 To SubsetsEx
D.Material = MMMtrls(LV)
'Set the appropriate material for this subset.
If MMTxtrs(LV) Is Nothing Then
D.SetTexture(0, Nothing)
'Clear unset texture.
Else
D.SetTexture(0, MMTxtrs(LV))
'And the appropriate texture.
End If
'Then draw the mesh subset.
MasterMesh.DrawSubset(LV)
Next
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
Dim LV As Integer
For LV = 0 To SubsetsEx
MMTxtrs(LV).Dispose()
Next
MasterMesh.Dispose()
End Sub
End Class |