📄 clsmd3loader.cls
字号:
.tElapsed = QPTimer - .tLastFrame
.tFactor = .tElapsed / (.FPS * AnimSpeed)
If .tFactor >= 1 Then
.Frame = .NextFrame
.NextFrame = .NextFrame + 1
If .NextFrame > .EndFrame - 1 Then
.NextFrame = .StartFrame
End If
.tLastFrame = QPTimer
.tFactor = 0
End If
End With
End Sub
Public Property Let SetUpperAni(ByVal Ani As Long)
mUpper.FPS = Anim(Ani).AnFPS
mUpper.StartFrame = Anim(Ani).FirstFrame
mUpper.EndFrame = Anim(Ani).FirstFrame + Anim(Ani).NumFrames
mUpper.NextFrame = mUpper.StartFrame
mUpper.Frame = mUpper.StartFrame
End Property
Public Property Let SetLowerAni(ByVal Ani As Long)
mLower.FPS = Anim(Ani).AnFPS
mLower.StartFrame = Anim(Ani).FirstFrame
mLower.EndFrame = Anim(Ani).FirstFrame + Anim(Ani).NumFrames
mLower.NextFrame = mLower.StartFrame
mLower.Frame = mLower.StartFrame
End Property
Private Sub DoMatrix(ByRef ModelPart As udtMD3Model, ByVal Link As String _
, ByRef MD3 As udtAnimations, ByRef ModelMatrix As D3DMATRIX, Optional IsHead As Boolean)
For x = 0 To ModelPart.MD3Header.NumOfTags - 1
If Link = Mid(ModelPart.MD3Tags(MD3.Frame * ModelPart.MD3Header.NumOfTags + x).TagName, 1, Len(Link)) Then
MatrixFrom = Get4x4Matrix(ModelPart, MD3.Frame * ModelPart.MD3Header.NumOfTags + x)
PosFrom = ModelPart.MD3Tags(MD3.Frame * ModelPart.MD3Header.NumOfTags + x).TagTranslation
End If
If bSmoothBoneAnim Then
If Link = Mid(ModelPart.MD3Tags(MD3.NextFrame * ModelPart.MD3Header.NumOfTags + x).TagName, 1, Len(Link)) Then
MatrixTo = Get4x4Matrix(ModelPart, MD3.NextFrame * ModelPart.MD3Header.NumOfTags + x)
PosTo = ModelPart.MD3Tags(MD3.NextFrame * ModelPart.MD3Header.NumOfTags + x).TagTranslation
End If
End If
Next x
If bSmoothBoneAnim Then
D3DXVec3Lerp PosFinal, PosFrom, PosTo, MD3.tFactor
If IsHead Then
PosFinal.x = PosFinal.x - MD3HeadOffSet(0)
PosFinal.y = PosFinal.y - MD3HeadOffSet(1)
PosFinal.Z = PosFinal.Z - MD3HeadOffSet(2)
End If
D3DXQuaternionRotationMatrix quatFrom, MatrixFrom
D3DXQuaternionRotationMatrix quatTo, MatrixTo
D3DXQuaternionSlerp quatFinal, quatFrom, quatTo, MD3.tFactor
D3DXMatrixRotationQuaternion MatrixFinal, quatFinal
MatrixFinal.m41 = PosFinal.x
MatrixFinal.m42 = PosFinal.y
MatrixFinal.m43 = PosFinal.Z
MatrixFinal.m44 = 1
ModelMatrix = MatrixFinal
Else
If IsHead Then
PosFrom.x = PosFrom.x - MD3HeadOffSet(0)
PosFrom.y = PosFrom.y - MD3HeadOffSet(1)
PosFrom.Z = PosFrom.Z - MD3HeadOffSet(2)
End If
MatrixFrom.m41 = PosFrom.x
MatrixFrom.m42 = PosFrom.y
MatrixFrom.m43 = PosFrom.Z
MatrixFrom.m44 = 1
ModelMatrix = MatrixFrom
End If
End Sub
Private Function Get4x4Matrix(ModelPart As udtMD3Model, TagID As Long) As D3DMATRIX
Dim MatOut As D3DMATRIX
MatOut.m11 = ModelPart.MD3Tags(TagID).TagRotation(0, 0)
MatOut.m21 = ModelPart.MD3Tags(TagID).TagRotation(0, 1)
MatOut.m31 = ModelPart.MD3Tags(TagID).TagRotation(0, 2)
MatOut.m12 = ModelPart.MD3Tags(TagID).TagRotation(1, 0)
MatOut.m22 = ModelPart.MD3Tags(TagID).TagRotation(1, 1)
MatOut.m32 = ModelPart.MD3Tags(TagID).TagRotation(1, 2)
MatOut.m13 = ModelPart.MD3Tags(TagID).TagRotation(2, 0)
MatOut.m23 = ModelPart.MD3Tags(TagID).TagRotation(2, 1)
MatOut.m33 = ModelPart.MD3Tags(TagID).TagRotation(2, 2)
MatOut.m44 = 1
Get4x4Matrix = MatOut
End Function
Public Sub Render(ByRef CurrentWorldMatrix As D3DMATRIX, Update As Boolean)
' IMPORTANT NOTE !
' Please do not change anything in this sub; unless you are a matrix expert.
' The order of the multiplications is CRUCIAL to the correct display of the MD3.
' If you need to translate or rotate the model, apply your transforms to
' the current world matrix before calling the draw routine.
If Update = True Then
BBoxMin.x = 100000
BBoxMin.y = 100000
BBoxMin.Z = 100000
BBoxMax.x = -100000
BBoxMax.y = -100000
BBoxMax.Z = -100000
End If
If bUseTextures = False Then
D3Ddevice.SetTexture 0, Nothing
End If
' Make a copy of the current world matrix
ModelWorld = CurrentWorldMatrix
D3DXMatrixIdentity MatTemp
D3DXMatrixRotationX MatX, -PI / 2
D3DXMatrixRotationY MatY, PI / 2
D3DXMatrixMultiply MatTemp, MatX, MatY
D3DXMatrixMultiply ModelWorld, MatTemp, ModelWorld
D3Ddevice.SetTransform D3DTS_WORLD, ModelWorld
D3Ddevice.SetVertexShader MDLVERTEX_FVF
ModelWorld2 = ModelWorld
If Update Then
UpdateFrames mLower
UpdateFrames mUpper
End If
' Draw lower player model
Draw ModelLower, True, mLower, mLower.Frame, Update
' Draw upper player model
DoMatrix ModelLower, "tag_torso", mLower, matTorsoResult
D3DXMatrixMultiply ModelWorld, matTorsoResult, ModelWorld
D3Ddevice.SetTransform D3DTS_WORLD, ModelWorld
Draw ModelUpper, True, mUpper, mUpper.Frame, Update
' Draw head player model
DoMatrix ModelUpper, "tag_head", mUpper, matHeadResult, True
D3DXMatrixMultiply ModelWorld, matHeadResult, ModelWorld
D3Ddevice.SetTransform D3DTS_WORLD, ModelWorld
Draw ModelHead, False, mUpper, 0, Update
' Draw weapon model
If bWeaponLoaded Then
DoMatrix ModelUpper, "tag_weapon", mUpper, matWeapon, True
D3DXMatrixMultiply ModelWorld, matWeapon, matTorsoResult
D3DXMatrixMultiply ModelWorld, ModelWorld, ModelWorld2
D3Ddevice.SetTransform D3DTS_WORLD, ModelWorld
Draw ModelWeapon, False, mUpper, 0, Update
End If
If bDbgRenderBoundingBox Then
renderBBox BBoxMin, BBoxMax, D3DColorARGB(255, 0, 255, 0), CurrentWorldMatrix
End If
D3Ddevice.SetTransform D3DTS_WORLD, CurrentWorldMatrix
End Sub
Private Sub Draw(ByRef MD3 As udtMD3Model, ByVal UseAnim As Boolean, Animation As udtAnimations, AnimFrame As Long, bUpdate As Boolean)
D3DXVec3TransformCoord tmpVect1, MD3.MD3BoneFrames(AnimFrame).Mins, ModelWorld
If bUpdate Then
If tmpVect1.x > BBoxMax.x Then BBoxMax.x = tmpVect1.x
If tmpVect1.y < BBoxMin.y Then BBoxMin.y = tmpVect1.y
If tmpVect1.Z > BBoxMax.Z Then BBoxMax.Z = tmpVect1.Z
End If
D3DXVec3TransformCoord tmpVect2, MD3.MD3BoneFrames(AnimFrame).Maxs, ModelWorld
If bUpdate Then
If tmpVect2.x < BBoxMin.x Then BBoxMin.x = tmpVect2.x
If tmpVect2.y > BBoxMax.y Then BBoxMax.y = tmpVect2.y
If tmpVect2.Z < BBoxMin.Z Then BBoxMin.Z = tmpVect2.Z
End If
If bDbgRenderHitBoxes Then
renderBBox tmpVect1, tmpVect2, D3DColorARGB(255, 255, 0, 0), IdentMat
End If
' Loop for the number of meshes
For m = 0 To MD3.MD3Header.NumOfMeshes - 1
' Set the texture to use for this mesh
If bUseTextures Then
MemMgr.SetTexture 0, MD3.MD3Meshes(m).MD3SkinNames(0)
End If
If UseAnim And bLinearKeyFrames Then
AnimateLinear MeshResult(0), MD3.MD3Vertices(m).MD3FrameVerts(Animation.Frame).MD3Verts(0), MD3.MD3Vertices(m).MD3FrameVerts(Animation.NextFrame).MD3Verts(0), MD3.MD3Meshes(m).MD3MeshHeader.NumOfTriangle * 3, Animation.tFactor, MakeVector(0, 0, 0), MakeVector(0, 0, 0)
D3Ddevice.DrawPrimitiveUP D3DPT_TRIANGLELIST, MD3.MD3Meshes(m).MD3MeshHeader.NumOfTriangle, MeshResult(0), MDLVERTEX_SIZE
Else
D3Ddevice.DrawPrimitiveUP D3DPT_TRIANGLELIST, MD3.MD3Meshes(m).MD3MeshHeader.NumOfTriangle, MD3.MD3Vertices(m).MD3FrameVerts(AnimFrame).MD3Verts(0), MDLVERTEX_SIZE
End If
Next m
End Sub
Private Sub renderBBox(bbMin As D3DVECTOR, bbMax As D3DVECTOR, color As Long, BoxMatrix As D3DMATRIX)
D3Ddevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
D3Ddevice.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME
D3Ddevice.SetRenderState D3DRS_LIGHTING, 0
D3Ddevice.SetTransform D3DTS_WORLD, BoxMatrix
'front
BoundingBox(0) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
BoundingBox(1) = MakeBBoxVert(bbMin.x, bbMax.y, bbMin.Z, color)
BoundingBox(2) = MakeBBoxVert(bbMax.x, bbMax.y, bbMin.Z, color)
BoundingBox(3) = MakeBBoxVert(bbMax.x, bbMax.y, bbMin.Z, color)
BoundingBox(4) = MakeBBoxVert(bbMax.x, bbMin.y, bbMin.Z, color)
BoundingBox(5) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
'back
BoundingBox(6) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
BoundingBox(7) = MakeBBoxVert(bbMin.x, bbMax.y, bbMax.Z, color)
BoundingBox(8) = MakeBBoxVert(bbMin.x, bbMin.y, bbMax.Z, color)
BoundingBox(9) = MakeBBoxVert(bbMin.x, bbMin.y, bbMax.Z, color)
BoundingBox(10) = MakeBBoxVert(bbMax.x, bbMin.y, bbMax.Z, color)
BoundingBox(11) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
'left
BoundingBox(12) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
BoundingBox(13) = MakeBBoxVert(bbMax.x, bbMin.y, bbMax.Z, color)
BoundingBox(14) = MakeBBoxVert(bbMax.x, bbMin.y, bbMin.Z, color)
BoundingBox(15) = MakeBBoxVert(bbMax.x, bbMin.y, bbMin.Z, color)
BoundingBox(16) = MakeBBoxVert(bbMax.x, bbMax.y, bbMin.Z, color)
BoundingBox(17) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
'right
BoundingBox(18) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
BoundingBox(19) = MakeBBoxVert(bbMin.x, bbMin.y, bbMax.Z, color)
BoundingBox(20) = MakeBBoxVert(bbMin.x, bbMax.y, bbMax.Z, color)
BoundingBox(21) = MakeBBoxVert(bbMin.x, bbMax.y, bbMax.Z, color)
BoundingBox(22) = MakeBBoxVert(bbMin.x, bbMax.y, bbMin.Z, color)
BoundingBox(23) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
'top
BoundingBox(24) = MakeBBoxVert(bbMin.x, bbMax.y, bbMin.Z, color)
BoundingBox(25) = MakeBBoxVert(bbMin.x, bbMax.y, bbMax.Z, color)
BoundingBox(26) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
BoundingBox(27) = MakeBBoxVert(bbMax.x, bbMax.y, bbMax.Z, color)
BoundingBox(28) = MakeBBoxVert(bbMax.x, bbMax.y, bbMin.Z, color)
BoundingBox(29) = MakeBBoxVert(bbMin.x, bbMax.y, bbMin.Z, color)
'bottom
BoundingBox(30) = MakeBBoxVert(bbMax.x, bbMin.y, bbMax.Z, color)
BoundingBox(31) = MakeBBoxVert(bbMin.x, bbMin.y, bbMax.Z, color)
BoundingBox(32) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
BoundingBox(33) = MakeBBoxVert(bbMin.x, bbMin.y, bbMin.Z, color)
BoundingBox(34) = MakeBBoxVert(bbMax.x, bbMin.y, bbMin.Z, color)
BoundingBox(35) = MakeBBoxVert(bbMax.x, bbMin.y, bbMax.Z, color)
D3Ddevice.SetVertexShader BBoxVert_FVF
MemMgr.ClearTexture 0, True
D3Ddevice.DrawPrimitiveUP D3DPT_TRIANGLELIST, 12, BoundingBox(0), Len(BoundingBox(0))
D3Ddevice.SetRenderState D3DRS_LIGHTING, 1
D3Ddevice.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID
D3Ddevice.SetVertexShader MDLVERTEX_FVF
D3Ddevice.SetRenderState D3DRS_CULLMODE, D3DCULL_CCW
D3Ddevice.SetTransform D3DTS_WORLD, ModelWorld
End Sub
Private Sub Class_Initialize()
bUseTextures = True
AnimSpeed = 1
bSmoothBoneAnim = True
bLinearKeyFrames = True
D3DXMatrixIdentity IdentMat
End Sub
Private Function MakeBBoxVert(x As Single, y As Single, Z As Single, col As Long) As BBoxVert
MakeBBoxVert.col = col
MakeBBoxVert.pos.x = x
MakeBBoxVert.pos.y = y
MakeBBoxVert.pos.Z = Z
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -