-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimatedMesh.cpp
241 lines (172 loc) · 7.04 KB
/
AnimatedMesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "AnimatedMesh.h"
AnimatedMesh::AnimatedMesh() : m_speedAdjust(1.0f), m_firstMesh(0), m_currentTrack(0), m_currentTime(0), m_numAnimationSets(0), m_currentAnimationSet(0), m_maxBones(0), m_sphereRadius(0), m_sphereCentre(0,0,0), m_boneMatrices(0)
{
}
AnimatedMesh::~AnimatedMesh()
{
if (m_animController) {
m_animController->Release();
m_animController = 0;
}
if (m_frameRoot) {
AnimatedMeshHierarchy memoryAllocator;
D3DXFrameDestroy(m_frameRoot, &memoryAllocator);
m_frameRoot = 0;
}
if (m_boneMatrices) {
delete[]m_boneMatrices;
m_boneMatrices = 0;
}
}
void AnimatedMesh::UpdateFrameMatrices(const D3DXFRAME * frameBase, const D3DXMATRIX * parentMatrix)
{
D3D::D3DXFRAME_EXTENDED *currentFrame = (D3D::D3DXFRAME_EXTENDED*)frameBase;
if (parentMatrix != NULL)
D3DXMatrixMultiply(¤tFrame->exCombinedTransformationMatrix, ¤tFrame->TransformationMatrix, parentMatrix);
else
currentFrame->exCombinedTransformationMatrix = currentFrame->TransformationMatrix;
if (currentFrame->pFrameSibling != NULL)
UpdateFrameMatrices(currentFrame->pFrameSibling, parentMatrix);
if (currentFrame->pFrameFirstChild != NULL)
UpdateFrameMatrices(currentFrame->pFrameFirstChild, ¤tFrame->exCombinedTransformationMatrix);
}
void AnimatedMesh::DrawFrame(LPD3DXFRAME frame) const
{
LPD3DXMESHCONTAINER meshContainer = frame->pMeshContainer;
while (meshContainer) {
DrawMeshContainer(meshContainer, frame);
meshContainer = meshContainer->pNextMeshContainer;
}
if (frame->pFrameSibling != NULL)
DrawFrame(frame->pFrameSibling);
if (frame->pFrameFirstChild != NULL)
DrawFrame(frame->pFrameFirstChild);
}
void AnimatedMesh::DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase) const
{
D3D::D3DXFRAME_EXTENDED *frame = (D3D::D3DXFRAME_EXTENDED*)frameBase;
D3D::D3DXMESHCONTAINER_EXTENDED *meshContainer = (D3D::D3DXMESHCONTAINER_EXTENDED*)meshContainerBase;
// IDirect3DDevice9* Device = graphics.GetDevice();
Device->SetTransform(D3DTS_WORLD, &frame->exCombinedTransformationMatrix);
for (unsigned int iMaterial = 0; iMaterial < meshContainer->NumMaterials; iMaterial++) {
Device->SetMaterial(&meshContainer->exMaterials[iMaterial]);
Device->SetTexture(0, meshContainer->exTextures[iMaterial]);
LPD3DXMESH pDrawMesh = (meshContainer->pSkinInfo) ? meshContainer->exSkinMesh : meshContainer->MeshData.pMesh;
pDrawMesh->DrawSubset(iMaterial);
}
}
void AnimatedMesh::SetupBoneMatrices(D3D::D3DXFRAME_EXTENDED * pFrame, LPD3DXMATRIX pParentMatrix)
{
D3D::D3DXMESHCONTAINER_EXTENDED* pMesh = (D3D::D3DXMESHCONTAINER_EXTENDED*)pFrame->pMeshContainer;
if (pMesh) {
if (!m_firstMesh)
m_firstMesh = pMesh;
if (pMesh->pSkinInfo && pMesh->MeshData.pMesh) {
D3DVERTEXELEMENT9 Declaration[MAX_FVF_DECL_SIZE];
pMesh->MeshData.pMesh->GetDeclaration(Declaration);
// IDirect3DDevice9* Device = graphics.GetDevice();
pMesh->MeshData.pMesh->CloneMesh(D3DXMESH_MANAGED, Declaration, Device, &pMesh->exSkinMesh);
m_maxBones = max(m_maxBones, (int)pMesh->pSkinInfo->GetNumBones());
for (unsigned int i = 0; i < pMesh->pSkinInfo->GetNumBones(); i++) {
D3D::D3DXFRAME_EXTENDED* pTempFrame = (D3D::D3DXFRAME_EXTENDED*)D3DXFrameFind(m_frameRoot, pMesh->pSkinInfo->GetBoneName(i));
pMesh->exFrameCombinedMatrixPointer[i] = &pTempFrame->exCombinedTransformationMatrix;
}
}
}
if (pFrame->pFrameSibling)
SetupBoneMatrices((D3D::D3DXFRAME_EXTENDED*)pFrame->pFrameSibling, pParentMatrix);
if (pFrame->pFrameFirstChild)
SetupBoneMatrices((D3D:: D3DXFRAME_EXTENDED*)pFrame->pFrameFirstChild, &pFrame->exCombinedTransformationMatrix);
}
bool AnimatedMesh::Load(char * fileName)
{
AnimatedMeshHierarchy *memoryAllocator = new AnimatedMeshHierarchy;
// IDirect3DDevice9* Device = graphics.GetDevice();
D3DXLoadMeshHierarchyFromX(fileName, D3DXMESH_MANAGED, Device, memoryAllocator, NULL, &m_frameRoot, &m_animController);
delete memoryAllocator;
memoryAllocator = 0;
if (m_animController)
m_numAnimationSets = m_animController->GetMaxNumAnimationSets();
if (m_frameRoot) {
SetupBoneMatrices((D3D::D3DXFRAME_EXTENDED*)m_frameRoot, NULL);
m_boneMatrices = new D3DXMATRIX[m_maxBones];
ZeroMemory(m_boneMatrices, sizeof(D3DXMATRIX)*m_maxBones);
D3DXFrameCalculateBoundingSphere(m_frameRoot, &m_sphereCentre, &m_sphereRadius);
}
return true;
}
void AnimatedMesh::FrameMove(float elapsedTime, const D3DXMATRIX * matWorld)
{
elapsedTime /= m_speedAdjust;
if (m_animController != NULL)
m_animController->AdvanceTime(elapsedTime, NULL);
m_currentTime += elapsedTime;
UpdateFrameMatrices(m_frameRoot, matWorld);
D3D::D3DXMESHCONTAINER_EXTENDED* pMesh = m_firstMesh;
if (pMesh && pMesh->pSkinInfo) {
unsigned int Bones = pMesh->pSkinInfo->GetNumBones();
for (unsigned int i = 0; i < Bones; i++) {
D3DXMatrixMultiply(&m_boneMatrices[i], &pMesh->exBoneOffsets[i], pMesh->exFrameCombinedMatrixPointer[i]);
}
void *srcPtr = 0;
pMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&srcPtr);
void *destPtr = 0;
pMesh->exSkinMesh->LockVertexBuffer(0, (void**)&destPtr);
pMesh->pSkinInfo->UpdateSkinnedMesh(m_boneMatrices, NULL, srcPtr, destPtr);
pMesh->exSkinMesh->UnlockVertexBuffer();
pMesh->MeshData.pMesh->UnlockVertexBuffer();
}
}
void AnimatedMesh::Render() const
{
if (m_frameRoot)
DrawFrame(m_frameRoot);
}
void AnimatedMesh::SetAnimationSet(unsigned int index)
{
if (index == m_currentAnimationSet)
return;
if (index >= m_numAnimationSets)
index = 0;
m_currentAnimationSet = index;
LPD3DXANIMATIONSET set;
m_animController->GetAnimationSet(m_currentAnimationSet, &set);
DWORD newTrack = (m_currentTrack == 0 ? 1 : 0);
m_animController->SetTrackAnimationSet(newTrack, set);
set->Release();
m_animController->UnkeyAllTrackEvents(m_currentTrack);
m_animController->UnkeyAllTrackEvents(newTrack);
m_animController->KeyTrackEnable(m_currentTrack, FALSE, m_currentTime + kMoveTransitionTime);
m_animController->KeyTrackSpeed(m_currentTrack, 0.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR);
m_animController->KeyTrackWeight(m_currentTrack, 0.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR);
m_animController->SetTrackEnable(newTrack, TRUE);
m_animController->KeyTrackSpeed(newTrack, 1.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR);
m_animController->KeyTrackWeight(newTrack, 1.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR);
m_currentTrack = newTrack;
}
void AnimatedMesh::NextAnimation()
{
unsigned int newAnimationSet = m_currentAnimationSet + 1;
if (newAnimationSet >= m_numAnimationSets)
newAnimationSet = 0;
SetAnimationSet(newAnimationSet);
}
void AnimatedMesh::AnimateFaster()
{
if (m_speedAdjust > 0.1f)
m_speedAdjust -= 0.1f;
}
void AnimatedMesh::AnimateSlower()
{
m_speedAdjust += 0.1f;
}
std::string AnimatedMesh::GetAnimationSetName(unsigned int index)
{
if (index >= m_numAnimationSets)
return "Error: No set exists";
LPD3DXANIMATIONSET set;
m_animController->GetAnimationSet(m_currentAnimationSet, &set);
std::string nameString(set->GetName());
set->Release();
return nameString;
}