Public Class MeshPlus
Implements IDisposable
Const Pi As Single = 3.1415926535897931
Const PiHalf As Single = Pi / 2
Public MasterMesh As Mesh
Public MMMtrls() As Material
Public MMTxtrs() As Texture
Public ReadOnly SubsetsEx As Integer 'Number of subsets - 1.
Public RSTMatrix As Matrix 'Rotation, scale, and translate matrix.
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 New(ByVal Filename As String, ByVal Options As MeshFlags, ByVal D As Device, ByVal Mx As Matrix)
Dim Ems() As ExtendedMaterial, LV As Integer
MasterMesh = Mesh.FromFile(Filename, Options, D, Ems)
SubsetsEx = Ems.GetUpperBound(0)
MMMtrls = New Material(SubsetsEx) {}
MMTxtrs = New Texture(SubsetsEx) {}
For LV = 0 To SubsetsEx
MMMtrls(LV) = Ems(LV).Material3D
MMMtrls(LV).Ambient = MMMtrls(LV).Diffuse
If Not (Ems(LV).TextureFilename Is Nothing) Then
MMTxtrs(LV) = TextureLoader.FromFile(D, Ems(LV).TextureFilename)
End If
Next
Me.RSTMatrix = Mx
'Viewpoint matrix.
'All of this information will be used when drawing the mesh.
End Sub
Public Sub New(ByVal Filename As String, ByVal Options As MeshFlags, ByVal D As Device, ByVal RotateYZ As Single, ByVal RotateXY As Single, ByVal SX As Single, ByVal SY As Single, ByVal SZ As Single, ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
Dim Ems() As ExtendedMaterial, LV As Integer
MasterMesh = Mesh.FromFile(Filename, Options, D, Ems)
SubsetsEx = Ems.GetUpperBound(0)
MMMtrls = New Material(SubsetsEx) {}
MMTxtrs = New Texture(SubsetsEx) {}
For LV = 0 To SubsetsEx
MMMtrls(LV) = Ems(LV).Material3D
MMMtrls(LV).Ambient = MMMtrls(LV).Diffuse
If Not (Ems(LV).TextureFilename Is Nothing) Then
MMTxtrs(LV) = TextureLoader.FromFile(D, Ems(LV).TextureFilename)
End If
Next
'First, we apply the rotations:
RSTMatrix = Matrix.RotationX(RotateYZ)
RSTMatrix.Multiply(Matrix.RotationZ(RotateXY))
'Next, we do the scaling:
RSTMatrix.Multiply(Matrix.Scaling(SX, SY, SZ))
'Then, we do the translation:
RSTMatrix.Multiply(Matrix.Translation(X, Y, Z))
End Sub
Public Sub New(ByVal Filename As String, ByVal Options As MeshFlags, ByVal D As Device, ByVal RotateYZ As Single, ByVal RotateXY As Single, ByVal S As Vector3, ByVal P As Vector3)
Dim Ems() As ExtendedMaterial, LV As Integer
MasterMesh = Mesh.FromFile(Filename, Options, D, Ems)
SubsetsEx = Ems.GetUpperBound(0)
MMMtrls = New Material(SubsetsEx) {}
MMTxtrs = New Texture(SubsetsEx) {}
For LV = 0 To SubsetsEx
MMMtrls(LV) = Ems(LV).Material3D
MMMtrls(LV).Ambient = MMMtrls(LV).Diffuse
If Not (Ems(LV).TextureFilename Is Nothing) Then
MMTxtrs(LV) = TextureLoader.FromFile(D, Ems(LV).TextureFilename)
End If
Next
'First, we apply the rotations:
RSTMatrix = Matrix.RotationX(RotateYZ)
RSTMatrix.Multiply(Matrix.RotationZ(RotateXY))
'Next, we do the scaling:
RSTMatrix.Multiply(Matrix.Scaling(S))
'Then, we do the translation:
RSTMatrix.Multiply(Matrix.Translation(P))
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 DrawAllSubsetsXF(ByVal D As Device)
'Each subset must be drawn separately and is what we extracted data from.
Dim LV As Integer
D.Transform.World = RSTMatrix
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
|