-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion.h
339 lines (258 loc) · 11.1 KB
/
motion.h
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#pragma once
#include "mgs/mtar/mtar.h"
#include "mgs/mtar/mtcm.h"
#include "mgs/mtar/mtex.h"
#include "noesis/plugin/pluginshare.h"
#include "include/half/half.h"
const double g_mgspw_PI = acos(-1);
const float g_mgspw_GAME_FRAMERATE = 30.0f;
struct MoveAnimation {
int keyframe;
float x;
float y;
float z;
};
struct RotAnimation {
int keyframe;
float x;
float y;
float z;
float w;
};
inline
float shiftRadix(const float& f, const int& exponent) {
return f * pow(2, exponent);
}
inline
float short2Half(const uint16_t& n) {
half x;
x.setBits(n);
float r = x;
return r;
}
inline
float short2HalfExp(const uint16_t& n) {
half x;
x.setBits(n);
float r = x;
return shiftRadix(r, 7);
}
inline
bool checkMagic(BYTE* motionFile) {
MtarHeader* header = (MtarHeader*)motionFile;
return header->magic == 0x7261744D;
}
inline
BYTE* openMotion(noeRAPI_t* rapi) {
int len;
char out[MAX_NOESIS_PATH];
BYTE* mtarFile = rapi->Noesis_LoadPairedFile("load mtar", ".mtar", len, out);
if (!mtarFile) return NULL;
return checkMagic(mtarFile) ? mtarFile : NULL;
}
inline
RichQuat angleAxisToQuat(const float& angle, RichVec3 axis) {
RichQuat quat;
axis.Normalize();
double sinAngle = sin(angle / 2.0f);
quat[0] = axis[0] * sinAngle;
quat[1] = axis[1] * sinAngle;
quat[2] = axis[2] * sinAngle;
quat[3] = cos(angle / 2.0f);
return quat;
}
inline
std::vector<RotAnimation> readRotBitstream(uint16_t* rotBitStream, const int& size, const int& quatLength, const uint32_t& numFrames) {
int keyFrame = 0;
std::vector<RotAnimation> ra;
RichBitStream bs = RichBitStream(rotBitStream, size * 2);
while (keyFrame < numFrames) {
keyFrame += bs.ReadBits(8);
float theta = shiftRadix(bs.ReadBits(quatLength), -quatLength) * g_mgspw_PI;
float axisX = shiftRadix(bs.ReadBits(quatLength), -quatLength);
float axisY = shiftRadix(bs.ReadBits(quatLength), -quatLength);
bool negCheck = bs.ReadBits(1);
bool negCheck2 = bs.ReadBits(1);
bool negCheck3 = bs.ReadBits(1);
float axisZ = 1.0f - axisX - axisY;
if (negCheck ) { axisX = -axisX; }
if (negCheck2) { axisY = -axisY; }
if (negCheck3) { axisZ = -axisZ; }
RichQuat qt = angleAxisToQuat(theta, { axisX, axisY, axisZ });
ra.push_back({keyFrame, qt[0], qt[1], qt[2], qt[3] });
}
return ra;
}
inline
std::vector<MoveAnimation> readMoveBitstream(uint16_t* moveBitStream, const int& size, const uint32_t& numFrames) {
int keyFrame = 0;
std::vector<MoveAnimation> ma;
RichBitStream bs = RichBitStream(moveBitStream, size * 2);
while (keyFrame < numFrames) {
float x = short2HalfExp(bs.ReadBits(16));
float y = short2HalfExp(bs.ReadBits(16));
float z = short2HalfExp(bs.ReadBits(16));
keyFrame += bs.ReadBits(8);
ma.push_back({ keyFrame, x , y, z });
}
return ma;
}
inline
int determineArchiveSize(uint8_t* mtcm, int i) {
MtcmHeader* mtcmHeader = (MtcmHeader*)mtcm;
int rotOffset = mtcmHeader->quatOffset[i];
int nextOffset = (i == mtcmHeader->numJoints - 1) ? mtcmHeader->rootOffset : mtcmHeader->quatOffset[i + 1];
int size = nextOffset - rotOffset;
return size <= 0 ? mtcmHeader->rootOffset - rotOffset : size;
}
inline
noeKeyFrameData_t createTransKFData(const MoveAnimation& trans, std::vector<float>& aniData) {
noeKeyFrameData_t data = {};
data.dataIndex = aniData.size();
data.time = trans.keyframe / g_mgspw_GAME_FRAMERATE;
aniData.push_back(trans.x);
aniData.push_back(trans.y);
aniData.push_back(trans.z);
return data;
}
inline
noeKeyFrameData_t createRotKFData(const RotAnimation& rot, std::vector<float>& aniData) {
noeKeyFrameData_t data = {};
data.dataIndex = aniData.size();
data.time = rot.keyframe / g_mgspw_GAME_FRAMERATE;
RichQuat quat = { rot.x, rot.y, rot.z, rot.w };
quat.Transpose();
aniData.push_back(quat[0]);
aniData.push_back(quat[1]);
aniData.push_back(quat[2]);
aniData.push_back(quat[3]);
return data;
}
inline
std::vector<noeKeyFrameData_t> createKFData(const std::vector<MoveAnimation>& trans, const std::vector<RotAnimation>& rot, const std::vector<MoveAnimation>& scale, std::vector<float>& aniData) {
std::vector<noeKeyFrameData_t> kfData;
for (int i = 0; i < trans.size(); i++) {
noeKeyFrameData_t transData = createTransKFData(trans[i], aniData);
kfData.push_back(transData);
}
for (int i = 0; i < rot.size(); i++) {
noeKeyFrameData_t rotData = createRotKFData(rot[i], aniData);
kfData.push_back(rotData);
}
for (int i = 0; i < scale.size(); i++) {
noeKeyFrameData_t scaleData = createTransKFData(scale[i], aniData);
kfData.push_back(scaleData);
}
return kfData;
}
inline
noeKeyFramedBone_t createKFBone(uint32_t boneID, modelBone_t* noeBones, int numBones, int numFrames, const std::vector<MoveAnimation>& trans, const std::vector<RotAnimation>& rot, const std::vector<MoveAnimation>& scale, std::vector<float>& aniData, std::vector<std::vector<noeKeyFrameData_t>>& kfData) {
noeKeyFramedBone_t kfBone = {};
std::string boneStrcode = intToHexString(boneID);
int boneIdx = findBoneIdx((char*)boneStrcode.c_str(), noeBones, numBones);
if (boneIdx == -1) return kfBone;
kfBone.boneIndex = boneIdx;
kfBone.scaleInterpolation = NOEKF_INTERPOLATE_LINEAR;
kfBone.rotationInterpolation = NOEKF_INTERPOLATE_LINEAR;
kfBone.translationInterpolation = NOEKF_INTERPOLATE_LINEAR;
kfBone.scaleType = NOEKF_SCALE_VECTOR_3;
kfBone.rotationType = NOEKF_ROTATION_QUATERNION_4;
kfBone.translationType = NOEKF_TRANSLATION_VECTOR_3;
kfBone.numScaleKeys = scale.size();
kfBone.numRotationKeys = rot.size();
kfBone.numTranslationKeys = trans.size();
kfBone.maxTime = numFrames / g_mgspw_GAME_FRAMERATE;
std::vector<noeKeyFrameData_t> nkfData = createKFData(trans, rot, scale, aniData);
kfData.push_back(nkfData);
int transPos = 0;
int rotPos = (trans.size());
int scalePos = (trans.size() + rot.size());
if (kfBone.numScaleKeys) kfBone.scaleKeys = &kfData[kfData.size()-1][scalePos];
if (kfBone.numRotationKeys) kfBone.rotationKeys = &kfData[kfData.size()-1][rotPos ];
if (kfBone.numTranslationKeys) kfBone.translationKeys = &kfData[kfData.size()-1][transPos];
return kfBone;
}
inline
noeKeyFramedAnim_t createKFAnim(char* animName, modelBone_t* noeBones, int numBones, std::vector<noeKeyFramedBone_t>& kfBones, std::vector<float>& data) {
noeKeyFramedAnim_t kfAnim = {};
kfAnim.name = animName;
kfAnim.numKfBones = kfBones.size();
kfAnim.kfBones = kfBones.data();
kfAnim.numBones = numBones;
kfAnim.framesPerSecond = g_mgspw_GAME_FRAMERATE;
kfAnim.data = data.data();
kfAnim.numDataFloats = data.size();
return kfAnim;
}
inline
void bindMtex(uint8_t* mtex, modelBone_t* noeBones, int numBones, std::vector <std::vector<noeKeyFrameData_t>>& kfData, std::vector<noeKeyFramedBone_t>& kfBones, std::vector<float>& aniData) {
MtexHeader* mtexHeader = (MtexHeader*)mtex;
for (int i = 0; i < mtexHeader->numMTP; i++) {
MtpData* mtp = (MtpData*)&mtexHeader->mtpData;
std::vector<RotAnimation> rot;
std::vector<MoveAnimation> trans;
std::vector<MoveAnimation> scale;
int movSize = mtp[i].rotOffset - mtp[i].moveOffset;
int moveOffset = mtp[i].moveOffset * 2;
uint16_t* moveBitstream = (uint16_t*)&mtex[moveOffset];
trans = readMoveBitstream(moveBitstream, movSize, mtexHeader->numFrames);
int rotSize = (i == mtexHeader->numMTP - 1) ? mtexHeader->dataSize - mtp[i].rotOffset : mtp[i + 1].moveOffset - mtp[i].rotOffset;
int rotOffset = mtp[i].rotOffset * 2;
uint16_t* rotBitstream = (uint16_t*)&mtex[rotOffset];
rot = readRotBitstream(rotBitstream, rotSize, mtexHeader->quatBits, mtexHeader->numFrames);
noeKeyFramedBone_t kfBone = createKFBone(mtp[i].boneName, noeBones, numBones, mtexHeader->numFrames, trans, rot, scale, aniData, kfData);
kfBones.push_back(kfBone);
}
}
inline
noesisAnim_t* bindMtcm(uint8_t* mtcm, uint8_t* mtex, uint32_t* boneTable, noeRAPI_t* rapi, modelBone_t* noeBones, int numBones) {
MtcmHeader* mtcmHeader = (MtcmHeader*)mtcm;
uint8_t* mtcmArchive = &mtcm[mtcmHeader->archiveOffset];
uint8_t* jointIndicies = &mtcm[mtcmHeader->indiciesOffset];
uint8_t* jointIndicies2 = &mtcm[mtcmHeader->indiciesOffset + mtcmHeader->numJoints];
std::vector<float> aniData;
std::vector<noeKeyFramedBone_t> kfBones;
std::vector <std::vector<noeKeyFrameData_t>> kfData;
RichBitStream bs = RichBitStream(&mtcmHeader->bitCheck0, 128);
for (int j = 0; j < mtcmHeader->numJoints; j++) {
int check = bs.ReadBits(1);
std::vector<RotAnimation> rot;
std::vector<MoveAnimation> trans;
std::vector<MoveAnimation> scale;
if (j == 0) {
uint16_t* moveBitstream = (uint16_t*)mtcmArchive;
trans = readMoveBitstream(moveBitstream, mtcmHeader->quatOffset[0], mtcmHeader->numFrames);
}
int boneID = boneTable[jointIndicies[j]];
int size = determineArchiveSize(mtcm, j);
int rotOffset = mtcmHeader->quatOffset[j];
uint16_t* rotBitstream = (uint16_t*)&mtcmArchive[rotOffset * 2];
int quatLength = check ? mtcmHeader->quatHighBit : mtcmHeader->quatLowBit;
rot = readRotBitstream(rotBitstream, size, quatLength, mtcmHeader->numFrames);
noeKeyFramedBone_t kfBone = createKFBone(boneID, noeBones, numBones, mtcmHeader->numFrames, trans, rot, scale, aniData, kfData);
kfBones.push_back(kfBone);
}
//if (mtex) bindMtex(mtex, noeBones, numBones, kfData, kfBones, aniData);
if (aniData.empty()) return NULL;
std::string animName = intToHexString(mtcmHeader->name);
noeKeyFramedAnim_t kfAnim = createKFAnim((char*)animName.c_str(), noeBones, numBones, kfBones, aniData);
noesisAnim_t* anim = rapi->Noesis_AnimFromBonesAndKeyFramedAnim(noeBones, numBones, &kfAnim, true);
return anim;
}
inline
void loadMotion(noeRAPI_t* rapi, BYTE* motionFile, modelBone_t* noeBones, int numBones) {
MtarHeader* mtarHeader = (MtarHeader*) motionFile;
uint8_t* mtcm = (uint8_t* )&motionFile[mtarHeader->mtcmOffset];
uint8_t* mtex = (uint8_t* )&motionFile[mtarHeader->mtexOffset];
MtarData* mtcmTable = (MtarData* )&motionFile[mtarHeader->dataTableOffset];
uint32_t* boneTable = (uint32_t* )&motionFile[mtarHeader->boneNameTableOffset];
CArrayList<noesisAnim_t*> animList;
for (int i = 0; i < mtarHeader->numMotion; i++) {
uint8_t* thisMtcm = &mtcm[mtcmTable[i].mtcmOffset];
uint8_t* thisMtex = mtcmTable[i].mtexSize ? &mtex[mtcmTable[i].mtexOffset] : NULL;
noesisAnim_t* anim = bindMtcm(thisMtcm, thisMtex, boneTable, rapi, noeBones, numBones);
if (anim) animList.Append(anim);
}
noesisAnim_t* anims = rapi->Noesis_AnimFromAnimsList(animList, animList.Num());
rapi->rpgSetExData_AnimsNum(anims, 1);
}