-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimation.cpp
241 lines (222 loc) · 6.6 KB
/
animation.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 "types.h"
#include "util.h"
void XAnim::read_translations(const std::string& tag)
{
u16 numtrans = m_reader->read<u16>();
if (numtrans == 0)
return;
vec3 v;
if (numtrans == 1)
{
v = m_reader->read<vec3>();
//printf("\tnumtrans 1 for '%s' %f,%f,%f\n", tag.c_str(), v.x, v.y, v.z);
m_animframes[0].trans[tag] = v;
return;
}
std::vector<int> frames;
if (numtrans == 1 || numtrans == m_numframes)
{
for (int i = 0; i < numtrans; ++i)
frames.push_back(i);
}
else if (m_numframes > 0xff)
{
for (int i = 0; i < numtrans; ++i)
frames.push_back(m_reader->read<u16>());
}
else
{
for (int i = 0; i < numtrans; ++i)
frames.push_back(m_reader->read<u8>());
}
for (int i = 0; i < numtrans; ++i)
{
v = m_reader->read<vec3>();
//printf("trans frame %d -> %f,%f,%f\n", frames[i], v.x, v.y, v.z);
m_animframes[frames[i]].trans[tag] = v;
}
}
void XAnim::read_rotations(const std::string& tag, bool flipquat, bool simplequat)
{
u16 numrot = m_reader->read<u16>();
//printf("numrot=%d\n", numrot);
if (numrot == 0)
return;
std::vector<int> frames;
if (numrot == 1 || numrot == m_numframes)
{
for (int i = 0; i < numrot; ++i)
frames.push_back(i);
}
else if (m_numframes > 0xff)
{
for (int i = 0; i < numrot; ++i)
frames.push_back(m_reader->read<u16>());
}
else
{
for (int i = 0; i < numrot; ++i)
frames.push_back(m_reader->read<u8>());
}
for (int i = 0; i < numrot; ++i)
{
glm::quat q = m_reader->read_quat(flipquat, simplequat);
//printf("frame %d '%s' -> q = %f,%f,%f,%f flip=%d,simple=%d\n", frames[i], tag.c_str(), q.x, q.y, q.z, q.w, flipquat, simplequat);
if (!flipquat)
{
m_animframes[frames[i]].quats[tag] = q;
}
}
}
bool XAnim::read_xanim_file(BinaryReader &rd)
{
m_reader = &rd;
m_version = rd.read<u16>();
if (m_version != 0xe)
return rd.set_error_message("expected xanim version 0xe, got %x\n", m_version);
m_numframes = rd.read<u16>();
m_numparts = rd.read<u16>();
m_flags = rd.read<u8>();
m_framerate = rd.read<u16>();
bool looping = (m_flags & 0x1) == 0x1;
bool delta = (m_flags & 0x2) == 0x2;
m_frequency = ((float)m_framerate) / ((float)m_numframes);
if (delta)
{
read_rotations("tag_origin", false, true);
read_translations("tag_origin");
}
if (looping)
++m_numframes;
std::vector<std::string> partnames;
int boneflagssize = ((m_numparts - 1) >> 3) + 1;
std::vector<u8> flipflags = rd.read_typed_buffer_to_vector<u8>(boneflagssize);
std::vector<u8> simpleflags = rd.read_typed_buffer_to_vector<u8>(boneflagssize);
for (int i = 0; i < m_numparts; ++i)
{
std::string partname;
if (!rd.read_null_terminated_string(partname))
break;
partnames.push_back(partname);
}
for (int i = 0; i < m_numparts; ++i)
{
bool flipquat = ((1 << (i & 7)) & flipflags[i >> 3]) != 0;
bool simplequat = ((1 << (i & 7)) & simpleflags[i >> 3]) != 0;
read_rotations(partnames[i], flipquat, simplequat);
read_translations(partnames[i]);
}
std::unordered_map<std::string, Bone> lastpose;
for (int i = 0; i < m_numframes; ++i)
{
auto fnd = m_animframes.find(i);
XAnimFrame* curframe = NULL;
if (fnd == m_animframes.end())
{
//printf("skipping frame %d\n", i);
continue;
}
else
{
curframe = &m_animframes[i];
}
std::vector<Bone> refframe;
refframe.resize(m_reference->parts.bones.size());
int refboneindex = 0;
for (auto& refbone : m_reference->parts.bones)
{
Bone xb = refbone;
if (lastpose.find(refbone.name) != lastpose.end())
{
xb = lastpose[refbone.name];
}
//xb.trans = glm::rotate(xb.rot, animframe_iterator.second.parts[refbone.name].trans) + xb.trans;
//xb.rot = xb.rot * animframe_iterator.second.parts[refbone.name].rot;
//additive animation
//TODO: FIXME add other relative and global
//do we have a known good rotation?
if (curframe->quats.find(refbone.name) != curframe->quats.end())
{
//xb.rot = glm::slerp(xb.rot, curframe->quats[refbone.name], 0.5f);
xb.transform.rotation = curframe->quats[refbone.name];
}
//do we have a known good translation?
if (curframe->trans.find(refbone.name) != curframe->trans.end())
{
//xb.trans = (xb.trans + curframe->trans[refbone.name]) / 2.f;
if (refbone.name == "tag_origin") //ignore any translations for tag_origin, so the animation moves on the spot
;
else
xb.transform.translation = curframe->trans[refbone.name];
}
lastpose[refbone.name] = xb;
refframe[refboneindex] = xb;
++refboneindex;
}
m_refframes.push_back(refframe);
}
u8 notify_count = rd.read<u8>();
for (int i = 0; i < notify_count; ++i)
{
//TODO: write this to xanim_export
std::string notify_string;
rd.read_null_terminated_string(notify_string);
u16 notify_time_value = rd.read<u16>();
printf("notify string: %s:%f, frame: %d\n", notify_string.c_str(), ((float)notify_time_value / ((float)m_numframes)), notify_time_value);
}
return true;
}
bool XAnim::export_file(const std::string& filename)
{
FILE* fp = NULL;
std::string fullfilename = filename + ".xanim_export";
fopen_s(&fp, fullfilename.c_str(), "w");
if (!fp)
return false;
fprintf(fp, "// This was file generated with https://github.com/riicchhaarrd/xmodelconverter\n");
fprintf(fp, "ANIMATION\n");
fprintf(fp, "VERSION 3\n");
fprintf(fp, "\n");
fprintf(fp, "NUMPARTS %d\n", m_reference->parts.bones.size());
int refboneidx = 0;
for (auto& it : m_reference->parts.bones)
{
fprintf(fp, "PART %d \"%s\"\n", refboneidx++, it.name.c_str());
}
fprintf(fp, "\n");
fprintf(fp, "FRAMERATE %d\n", m_framerate);
fprintf(fp, "NUMFRAMES %d\n", m_refframes.size());
fprintf(fp, "\n");
int frameno = 0;
for (auto& refframe : m_refframes)
{
fprintf(fp, "FRAME %d\n", frameno++);
refboneidx = 0;
for (auto& xb : refframe)
{
//mat4 mat = refframe[refboneidx].bp;// getWorldMatrix(refframe, refboneidx);
auto mat = util::get_world_transform(refframe, refboneidx).get_matrix();
vec3 offset = util::get_translation_component_from_matrix(mat);
fprintf(fp, "PART %d\n", refboneidx);
fprintf(fp, "OFFSET %f, %f, %f\n", offset.x, offset.y, offset.z);
fprintf(fp, "SCALE 1.000000, 1.000000, 1.000000\n");
vec3 x, y, z;
util::get_xyz_components_from_matrix(mat, x, y, z);
fprintf(fp, "X %f, %f, %f\n", x.x, x.y, x.z);
fprintf(fp, "Y %f, %f, %f\n", y.x, y.y, y.z);
fprintf(fp, "Z %f, %f, %f\n", z.x, z.y, z.z);
fprintf(fp, "\n");
++refboneidx;
}
}
fprintf(fp, "NOTETRACKS\n");
refboneidx = 0;
for (auto& it : m_reference->parts.bones)
{
fprintf(fp, "PART %d\n", refboneidx++);
fprintf(fp, "NUMTRACKS 0\n");
fprintf(fp, "\n");
}
fclose(fp);
return true;
}