forked from scott-t/tlj-residual
-
Notifications
You must be signed in to change notification settings - Fork 1
/
skeleton.cpp
128 lines (106 loc) · 3.44 KB
/
skeleton.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
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
#include "engines/stark/skeleton.h"
#include "engines/stark/skeleton_anim.h"
#include "engines/stark/gfx/coordinate.h"
#include "engines/stark/stark.h"
#include "common/stream.h"
namespace Stark {
Skeleton::Skeleton() : _anim(NULL), _lastTime(0), _maxTime(0) {
}
Skeleton::~Skeleton() {
for (Common::Array<BoneNode *>::iterator it = _bones.begin(); it != _bones.end(); ++it)
delete *it;
}
bool Skeleton::readFromStream(Common::ReadStream *stream) {
uint32 numBones = stream->readUint32LE();
for (uint32 i = 0; i < numBones; ++i) {
BoneNode *node = new BoneNode();
uint32 len = stream->readUint32LE();
char *ptr = new char[len + 4];
stream->read(ptr, len + 4);
node->_name = Common::String(ptr, len);
node->_u1 = get_float(ptr + len);
delete[] ptr;
len = stream->readUint32LE();
for (uint32 j = 0; j < len; ++j)
node->_children.push_back(stream->readUint32LE());
node->_idx = _bones.size();
_bones.push_back(node);
}
for (uint32 i = 0; i < numBones; ++i) {
BoneNode *node = _bones[i];
for (int j = 0; j < node->_children.size(); ++j) {
_bones[node->_children[j]]->_parent = i;
}
}
return true;
}
bool Skeleton::setAnim(Common::ReadStream *stream) {
SkeletonAnim *_oldAnim = _anim;
_anim = new SkeletonAnim();
if (_anim->createFromStream(stream)) {
_maxTime = _anim->getLength();
delete _oldAnim;
} else {
_anim = _oldAnim;
return false;
}
return true;
}
void Skeleton::setNode(uint32 time, BoneNode *bone, const Coordinate &parentCoord) {
Coordinate animCoord = _anim->getCoordForBone(time, bone->_idx);
animCoord.rotate(parentCoord);
bone->_animPos = parentCoord + animCoord;
for (int i = 0; i < bone->_children.size(); ++i) {
setNode(time, _bones[bone->_children[i]], bone->_animPos);
}
}
bool Skeleton::animate(uint32 delta) {
// Start at root bone
// For each child
// - Set childs animation coordinate
// - Process that childs children
if (_maxTime > 0) {
_lastTime += delta;
while (_lastTime > _maxTime)
_lastTime -= _maxTime;
}
setNode(_lastTime, _bones[0], Coordinate());
/*
Graphics::Vector3d b1 = (*face)->_verts[vertIdx]->_pos1;
idx = (*face)->_verts[vertIdx]->_bone1;
BoneNode *bone;
do {
bone = bones[idx];
key1 = anims[idx]->_keys[0];
Graphics::Vector3d tmp = key1->_pos;
float tmpRot[] = _Q_MAT(-key1->_rotW, key1->_rot.x(), key1->_rot.y(), key1->_rot.z()); // - is LH to RH
_VECT_ROTATE(b1, tmpRot);
b1 += tmp;
idx = bone->_parent;
} while (idx != -1);
*/
return true;
}
} // End of namespace Stark