Public Class AnimatedMesh
Public cmesh() As CustomMesh
Public tMesh As CustomMesh
Public Key1 As Int16
Public Key2 As Int16
Public Dtime As Single
Public Const tChange As Single = 0.05
Public KOT As Int16
Public KeyOrder() As Integer
Public Structure vertexlist
Public vertexs() As Vertex
End Structure
Dim vertexlists() As vertexlist
Public Function LoadAnimatedMesh(ByVal anmFile As String, ByVal fFolder As String, ByVal device As Direct3D.Device, ByVal Texturefolder As String)
Try
Dim icount As Int16
Dim icount2 As Int16
Dim keys As Int16
Dim stemp As String
Dim Gs As GraphicsStream
FileOpen(1, fFolder & anmFile, OpenMode.Input)
Input(1, keys)
ReDim cmesh(keys)
ReDim vertexlists(keys)
Do Until EOF(1)
Input(1, stemp)
cmesh(icount) = New CustomMesh
vertexlists(icount) = New vertexlist
cmesh(icount).LoadMesh(fFolder, stemp, DX9.D3DDevice, Texturefolder)
icount += 1
Loop
FileClose(1)
tMesh = cmesh(0)
For icount = 0 To keys - 1
ReDim vertexlists(icount).vertexs(cmesh(icount).xMesh.NumberVertices)
Gs = cmesh(icount).xMesh.LockVertexBuffer(LockFlags.None)
For icount2 = 0 To cmesh(icount).xMesh.NumberVertices - 1
vertexlists(icount).vertexs(icount2).p.X = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).p.Y = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).p.Z = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).n.X = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).n.Y = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).n.Z = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).tu = Gs.Read(GetType(Single))
vertexlists(icount).vertexs(icount2).tv = Gs.Read(GetType(Single))
Next
cmesh(icount).xMesh.UnlockVertexBuffer()
Key2 = 1
Next
Catch ex As Exception
Debug.WriteLine("Load Annimated Mesh:" & ex.ToString)
ExitGame()
End Try
End Function
Public Function DrawAnimatedMesh(ByVal automatic As Boolean, ByVal d3ddevice As Direct3D.Device)
Dim icount As Int16
Try
If automatic Then
Dtime += tChange
If Dtime > 1 Then
Dtime = tChange
KOT += 1
If KOT >= KeyOrder.Length Then
KOT = 0
End If
Key1 = Key2
Key2 = KeyOrder(KOT)
End If
End If
Dim gs As GraphicsStream
gs = tMesh.xMesh.LockVertexBuffer(LockFlags.None)
With vertexlists(Key2)
For icount = 0 To vertexlists(0).vertexs.Length - 2
gs.Write((lerp(vertexlists(Key1).vertexs(icount).p.X, .vertexs(icount).p.X, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).p.Y, .vertexs(icount).p.Y, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).p.Z, .vertexs(icount).p.Z, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).n.X, .vertexs(icount).n.X, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).n.Y, .vertexs(icount).n.Y, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).n.Z, .vertexs(icount).n.Z, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).tu, .vertexs(icount).tu, Dtime)))
gs.Write((lerp(vertexlists(Key1).vertexs(icount).tv, .vertexs(icount).tv, Dtime)))
Next
End With
tMesh.xMesh.UnlockVertexBuffer()
tMesh.DrawMesh(DX9.D3DDevice)
Catch ex As Exception
Debug.WriteLine(Key1 & " " & Key2 & " " & Dtime & " " & icount & "/" & vertexlists(0).vertexs.Length - 1)
Debug.WriteLine("Draw Animated Mesh:" & ex.ToString)
ExitGame()
End Try
End Function
Public Function lerp(ByVal n1 As Single, ByVal n2 As Single, ByVal time As Single) As Single
Return ((n2 - n1) * time) + n1
End Function
End Class