-
Notifications
You must be signed in to change notification settings - Fork 5
/
KAIMyEntitySaba.cpp
381 lines (321 loc) · 9.54 KB
/
KAIMyEntitySaba.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "KAIMyEntitySaba.h"
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#include <stb_image.h>
#include <Saba/Base/Time.h>
using namespace KAIMyEntitySaba;
const int FPS = 30;
const float PHYSICS_ELAPSED = 1.0f / FPS;
Model *KAIMyEntitySaba::LoadModelPMX(const char *filename, const char *dir, size_t layerCount)
{
auto model = std::make_unique<saba::PMXModel>();
if (!model->Load(filename, dir))
{
model.reset();
return nullptr;
}
model->InitializeAnimation();
model->ResetPhysics();
auto vmdAnims = new saba::VMDAnimation *[layerCount];
for (size_t i = 0; i < layerCount; ++i)
vmdAnims[i] = nullptr;
auto animTimes = new double[layerCount];
for (int i = 0; i < layerCount; ++i)
animTimes[i] = 0.0;
Model *result = new Model;
result->mmdModel = std::move(model);
result->vmdAnims = vmdAnims;
result->vmdAnimCount = layerCount;
result->animTimes = animTimes;
result->prevSabaTime = saba::GetTime();
return result;
}
Model *KAIMyEntitySaba::LoadModelPMD(const char *filename, const char *dir, size_t layerCount)
{
auto model = std::make_unique<saba::PMDModel>();
if (!model->Load(filename, dir))
{
model.reset();
return nullptr;
}
model->InitializeAnimation();
model->ResetPhysics();
auto vmdAnims = new saba::VMDAnimation *[layerCount];
for (int i = 0; i < layerCount; ++i)
vmdAnims[i] = nullptr;
auto animTimes = new double[layerCount];
for (int i = 0; i < layerCount; ++i)
animTimes[i] = 0.0;
Model *result = new Model;
result->mmdModel = std::move(model);
result->vmdAnims = vmdAnims;
result->vmdAnimCount = layerCount;
result->animTimes = animTimes;
result->prevSabaTime = saba::GetTime();
return result;
}
void KAIMyEntitySaba::DeleteModel(Model *model)
{
delete[] model->animTimes;
delete[] model->vmdAnims;
model->mmdModel.reset();
delete model;
}
void KAIMyEntitySaba::UpdateModel(Model *model)
{
int maxAnim = 0;
double deltaTime = saba::GetTime() - model->prevSabaTime;
model->prevSabaTime = saba::GetTime();
double elapsed = deltaTime;
if (elapsed > PHYSICS_ELAPSED)
elapsed = PHYSICS_ELAPSED;
for (size_t i = 0; i < model->vmdAnimCount; ++i)
{
auto vmdAnim = model->vmdAnims[i];
if (vmdAnim != nullptr)
{
maxAnim = i;
}
}
model->mmdModel->BeginAnimation();
for (size_t i = 0; i < model->vmdAnimCount; ++i)
{
auto vmdAnim = model->vmdAnims[i];
if (vmdAnim != nullptr)
{
double animTime = model->animTimes[i] + deltaTime;
if (animTime * FPS > vmdAnim->GetMaxKeyTime())
animTime = 0.0;
model->animTimes[i] = animTime;
if (i == maxAnim)
{
model->mmdModel->UpdateAllAnimation(vmdAnim, animTime * FPS, elapsed);
}
else
{
float vmdFrame = animTime * FPS;
if (vmdAnim != nullptr)
{
vmdAnim->Evaluate(vmdFrame);
}
model->mmdModel->UpdateMorphAnimation();
model->mmdModel->UpdateNodeAnimation(false);
}
}
}
model->mmdModel->EndAnimation();
model->mmdModel->Update();
}
size_t KAIMyEntitySaba::GetVertexCount(Model *model)
{
return model->mmdModel->GetVertexCount();
}
const void *KAIMyEntitySaba::GetPoss(Model *model)
{
return model->mmdModel->GetUpdatePositions();
}
const void *KAIMyEntitySaba::GetNormals(Model *model)
{
return model->mmdModel->GetUpdateNormals();
}
const void *KAIMyEntitySaba::GetUVs(Model *model)
{
return model->mmdModel->GetUpdateUVs();
}
size_t KAIMyEntitySaba::GetIndexElementSize(Model *model)
{
return model->mmdModel->GetIndexElementSize();
}
size_t KAIMyEntitySaba::GetIndexCount(Model *model)
{
return model->mmdModel->GetIndexCount();
}
const void *KAIMyEntitySaba::GetIndices(Model *model)
{
return model->mmdModel->GetIndices();
}
size_t KAIMyEntitySaba::GetMaterialCount(Model *model)
{
return model->mmdModel->GetMaterialCount();
}
const char *KAIMyEntitySaba::GetMaterialTex(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_texture.c_str();
}
const char *KAIMyEntitySaba::GetMaterialSpTex(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_spTexture.c_str();
}
const char *KAIMyEntitySaba::GetMaterialToonTex(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_toonTexture.c_str();
}
const void *KAIMyEntitySaba::GetMaterialAmbient(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_ambient);
}
const void *KAIMyEntitySaba::GetMaterialDiffuse(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_diffuse);
}
const void *KAIMyEntitySaba::GetMaterialSpecular(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_specular);
}
float KAIMyEntitySaba::GetMaterialSpecularPower(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_specularPower;
}
float KAIMyEntitySaba::GetMaterialAlpha(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_alpha;
}
const void *KAIMyEntitySaba::GetMaterialTextureMulFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_textureMulFactor);
}
const void *KAIMyEntitySaba::GetMaterialTextureAddFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_textureAddFactor);
}
int KAIMyEntitySaba::GetMaterialSpTextureMode(Model *model, size_t pos)
{
return (int)model->mmdModel->GetMaterials()[pos].m_spTextureMode;
}
const void *KAIMyEntitySaba::GetMaterialSpTextureMulFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_spTextureMulFactor);
}
const void *KAIMyEntitySaba::GetMaterialSpTextureAddFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_spTextureAddFactor);
}
const void *KAIMyEntitySaba::GetMaterialToonTextureMulFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_toonTextureMulFactor);
}
const void *KAIMyEntitySaba::GetMaterialToonTextureAddFactor(Model *model, size_t pos)
{
return &(model->mmdModel->GetMaterials()[pos].m_toonTextureAddFactor);
}
bool KAIMyEntitySaba::GetMaterialBothFace(Model *model, size_t pos)
{
return model->mmdModel->GetMaterials()[pos].m_bothFace;
}
size_t KAIMyEntitySaba::GetSubMeshCount(Model *model)
{
return model->mmdModel->GetSubMeshCount();
}
int KAIMyEntitySaba::GetSubMeshMaterialID(Model *model, size_t pos)
{
return model->mmdModel->GetSubMeshes()[pos].m_materialID;
}
int KAIMyEntitySaba::GetSubMeshBeginIndex(Model *model, size_t pos)
{
return model->mmdModel->GetSubMeshes()[pos].m_beginIndex;
}
int KAIMyEntitySaba::GetSubMeshVertexCount(Model *model, size_t pos)
{
return model->mmdModel->GetSubMeshes()[pos].m_vertexCount;
}
void KAIMyEntitySaba::ChangeModelAnim(Model *model, saba::VMDAnimation *anim, size_t layer)
{
model->mmdModel->InitializeAnimation();
model->vmdAnims[layer] = anim;
model->prevSabaTime = saba::GetTime();
model->animTimes[layer] = 0.0;
}
void KAIMyEntitySaba::ResetModelPhysics(Model *model)
{
model->mmdModel->ResetPhysics();
}
glm::mat4 *KAIMyEntitySaba::CreateMat()
{
glm::mat4 *result = new glm::mat4;
return result;
}
void KAIMyEntitySaba::DeleteMat(glm::mat4 *mat)
{
delete mat;
}
void KAIMyEntitySaba::GetRightHandMat(Model *model, glm::mat4 *mat)
{
const char name[] = {static_cast<char>(0xE5), static_cast<char>(0x8F), static_cast<char>(0xB3), static_cast<char>(0xE4), static_cast<char>(0xB8), static_cast<char>(0xAD), static_cast<char>(0xE6), static_cast<char>(0x8C), static_cast<char>(0x87), static_cast<char>(0xEF), static_cast<char>(0xBC), static_cast<char>(0x91), 0x00}; //Japanese name of bone by UTF8
saba::MMDNode *node = model->mmdModel->GetNodeManager()->GetMMDNode(name);
if (node == nullptr)
return;
*mat = node->GetGlobalTransform();
}
void KAIMyEntitySaba::GetLeftHandMat(Model *model, glm::mat4 *mat)
{
const char name[] = {static_cast<char>(0xE5), static_cast<char>(0xB7), static_cast<char>(0xA6), static_cast<char>(0xE4), static_cast<char>(0xB8), static_cast<char>(0xAD), static_cast<char>(0xE6), static_cast<char>(0x8C), static_cast<char>(0x87), static_cast<char>(0xEF), static_cast<char>(0xBC), static_cast<char>(0x91), 0x00}; //Japanese name of bone by UTF8
saba::MMDNode *node = model->mmdModel->GetNodeManager()->GetMMDNode(name);
if (node == nullptr)
return;
*mat = node->GetGlobalTransform();
}
Texture *KAIMyEntitySaba::LoadTexture(const char *filename)
{
saba::File file;
if (!file.Open(filename))
return nullptr;
stbi_set_flip_vertically_on_load(true);
int x, y, comp;
int ret = stbi_info_from_file(file.GetFilePointer(), &x, &y, &comp);
if (ret == 0)
return nullptr;
uint8_t *data;
bool hasAlpha;
if (comp != 4)
{
data = stbi_load_from_file(file.GetFilePointer(), &x, &y, &comp, STBI_rgb);
hasAlpha = false;
}
else
{
data = stbi_load_from_file(file.GetFilePointer(), &x, &y, &comp, STBI_rgb_alpha);
hasAlpha = true;
}
Texture *result = new Texture;
result->data = data;
result->x = x;
result->y = y;
result->hasAlpha = hasAlpha;
return result;
}
void KAIMyEntitySaba::DeleteTexture(Texture *tex)
{
stbi_image_free(tex->data);
delete tex;
}
int KAIMyEntitySaba::GetTextureX(Texture *tex)
{
return tex->x;
}
int KAIMyEntitySaba::GetTextureY(Texture *tex)
{
return tex->y;
}
const void *KAIMyEntitySaba::GetTextureData(Texture *tex)
{
return tex->data;
}
bool KAIMyEntitySaba::TextureHasAlpha(Texture *tex)
{
return tex->hasAlpha;
}
saba::VMDAnimation *KAIMyEntitySaba::LoadAnimation(Model *model, const char *filename)
{
saba::VMDFile vmd;
if (!saba::ReadVMDFile(&vmd, filename))
return nullptr;
saba::VMDAnimation *result = new saba::VMDAnimation;
result->Create(model->mmdModel);
result->Add(vmd);
result->SyncPhysics(0.0f, FPS);
return result;
}
void KAIMyEntitySaba::DeleteAnimation(saba::VMDAnimation *anim)
{
delete anim;
}