/
msAppMesh.cpp
439 lines (362 loc) · 12.9 KB
/
msAppMesh.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// disable MSVC++ warning about long debug symbol names
#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif
#include "msAppMesh.h"
#include "msAppNode.h"
#include "appConfig.h"
#include "appIfl.h"
namespace DTS
{
/// Wrapper to retrieve a milkshape vertex
///
/// @param mesh Mesh to get vertex from
/// @param idx Index of the vertex to retrieve
///
/// @return The x,y,z vertex
Point3D getVert(msMesh *mesh, int idx)
{
msVertex *vtx = msMesh_GetVertexAt(mesh, idx);
return Point3D(MilkshapePoint(vtx->Vertex));
}
/// Wrapper to retrieve a normal of a vertex of a milkshape triangle
///
/// @param mesh Mesh to get normal from
/// @param triIndex Index of the triangle to retrieve
/// @param normIndex Index of the normal to retrieve
///
/// @return The u,v texture vertex
Point3D getNormal(msMesh *mesh, int triIndex, int normIndex)
{
msTriangleEx *triEx = msMesh_GetTriangleExAt(mesh, triIndex);
return Point3D(MilkshapePoint(triEx->Normals[normIndex]));
}
/// Wrapper to retrieve texture mapping from a vertex of a milkshape triangle
///
/// @param mesh Mesh to get vertex from
/// @param triIndex Index of the triangle to retrieve
/// @param vertIndex Index of the vertex to retrieve
///
/// @return The u,v texture vertex
Point2D getTVert(msMesh *mesh, int triIndex, int vertIndex)
{
msTriangleEx *triEx = msMesh_GetTriangleExAt(mesh, triIndex);
return Point2D(triEx->TexCoords[vertIndex][0], triEx->TexCoords[vertIndex][1]);
}
//--------------------------------------------------------------------------
MsAppMesh::MsAppMesh(MilkshapeNode *msNode, AppNode *appNode)
{
mAppNode = appNode;
mMsNode = static_cast<MilkshapeMesh*>(msNode);
}
Matrix<4,4,F32> MsAppMesh::getMeshTransform(const AppTime & time)
{
assert(!mLocked && "Mesh is locked");
S32 frame = (S32)(time.getF32() * AppConfig::AppFramesPerSec() + 0.5);
return mMsNode->getMeshTransform(frame);
}
F32 MsAppMesh::getVisValue(const AppTime & time)
{
assert(!mLocked && "Mesh is locked");
S32 frame = (S32)(time.getF32() * AppConfig::AppFramesPerSec() + 0.5f);
return mMsNode->getVisibility(frame);
}
bool MsAppMesh::generateMaterial(MilkshapeMaterial *msMat, Material &mat)
{
if (!msMat)
return false;
// set defaults
mat.name = msMat->getTextureName();
mat.flags = 0;
// read parameters from material
msMat->getUserPropInt("detail", mat.detail);
msMat->getUserPropInt("bump", mat.bump);
msMat->getUserPropInt("reflectance", mat.reflectance);
msMat->getUserPropFloat("detailScale", mat.detailScale);
msMat->getUserPropFloat("reflection", mat.reflection);
// set flags
bool flag = false;
msMat->getUserPropBool("SWrap", flag);
if (flag)
mat.flags |= Material::SWrap;
flag = false;
msMat->getUserPropBool("TWrap", flag);
if (flag)
mat.flags |= Material::TWrap;
flag = false;
msMat->getUserPropBool("NeverEnvMap", flag);
if (flag)
mat.flags |= Material::NeverEnvMap;
flag = false;
msMat->getUserPropBool("Translucent", flag);
if (flag)
mat.flags |= Material::Translucent;
flag = false;
msMat->getUserPropBool("Additive", flag);
if (flag)
mat.flags |= Material::Additive;
flag = false;
msMat->getUserPropBool("Subtractive", flag);
if (flag)
mat.flags |= Material::Subtractive;
flag = false;
msMat->getUserPropBool("SelfIlluminating", flag);
if (flag)
mat.flags |= Material::SelfIlluminating;
flag = false;
msMat->getUserPropBool("NoMipMap", flag);
if (flag)
mat.flags |= Material::NoMipMap;
flag = false;
msMat->getUserPropBool("MipMapZeroBorder", flag);
if (flag)
mat.flags |= Material::MipMapZeroBorder;
return true;
}
bool MsAppMesh::getMaterial(S32 matIdx, Material &mat, U32 type)
{
// get material object
MilkshapeMaterial *msMat = mMsNode->getMaterial();
if (!msMat)
return false;
// generate material by type requested
switch (type)
{
case Material::DetailMap:
if (generateMaterial(msMat->mDetail, mat))
{
mat.flags |= Material::DetailMap;
return true;
}
return false;
case Material::BumpMap:
if (generateMaterial(msMat->mBump, mat))
{
mat.flags |= Material::BumpMap;
return true;
}
return false;
case Material::ReflectanceMap:
if (generateMaterial(msMat->mReflectance, mat))
{
mat.flags |= Material::ReflectanceMap;
return true;
}
return false;
default:
{
// generate regular material
generateMaterial(msMat, mat);
// if this is an ifl, then create the ifl material if it doesn't
// exist and mark as ifl
char *dot = strchr(const_cast<char*>(mat.name.data()),'_');
if (dot && !stricmp(dot+1,"ifl"))
{
*dot = '.';
mat.flags |= Material::IFLMaterial;
while ((int)mIfls.size() <= matIdx)
mIfls.push_back(NULL);
if (!mIfls[matIdx])
mIfls[matIdx] = new AppIfl(mat.name.c_str());
}
return true;
}
}
}
bool MsAppMesh::animatesFrame(const AppSequenceData & seqData)
{
assert(!mLocked && "Mesh is locked");
/// Morph animation is not supported
return false;
}
bool MsAppMesh::animatesMatFrame(const AppSequenceData & seqData)
{
assert(!mLocked && "Mesh is locked");
/// UV animation is not supported
return false;
}
AppMeshLock MsAppMesh::lockMesh(const AppTime & time, const Matrix<4,4,F32> &objectOffset)
{
msMesh *mesh = mMsNode->getMsMesh();
assert(mesh && "NULL milkshape mesh");
S32 lastMatIdx = -1;
// start lists empty
mFaces.clear();
mVerts.clear();
mTVerts.clear();
mIndices.clear();
mSmooth.clear();
mVertId.clear();
// start out with faces and crop data allocated
mFaces.resize(msMesh_GetTriangleCount(mesh));
S32 vCount = msMesh_GetVertexCount(mesh);
// Transform the vertices by the bounds and scale
std::vector<Point3D> verts(vCount, Point3D());
for (int i = 0; i < vCount; i++)
verts[i] = objectOffset * (getVert(mesh, i) * mMsNode->getScale());
int numTriangles = mFaces.size();
for (int i = 0; i < numTriangles; i++)
{
msTriangle *msFace = msMesh_GetTriangleAt(mesh, i);
Primitive &tsFace = mFaces[i];
// set faces material index
S32 matIndex = msMesh_GetMaterialIndex(mesh);
tsFace.type = (matIndex >= 0) ? matIndex : Primitive::NoMaterial;
tsFace.firstElement = mIndices.size();
tsFace.numElements = 3;
tsFace.type |= Primitive::Triangles | Primitive::Indexed;
// set vertex indices
word vertIndices[3];
msTriangle_GetVertexIndices(msFace, vertIndices);
Point3D vert0 = verts[vertIndices[0]];
Point3D vert1 = verts[vertIndices[1]];
Point3D vert2 = verts[vertIndices[2]];
Point3D norm0 = getNormal(mesh, i, 0);
Point3D norm1 = getNormal(mesh, i, 1);
Point3D norm2 = getNormal(mesh, i, 2);
// set texture vertex indices
Point2D tvert0 = getTVert(mesh, i, 0);
Point2D tvert1 = getTVert(mesh, i, 1);
Point2D tvert2 = getTVert(mesh, i, 2);
// now add indices (switch order to be CW)
mIndices.push_back(addVertex(vert0,norm0,tvert0,vertIndices[0]));
mIndices.push_back(addVertex(vert2,norm2,tvert2,vertIndices[2]));
mIndices.push_back(addVertex(vert1,norm1,tvert1,vertIndices[1]));
// if the material is double-sided, add the backface as well
if (!(tsFace.type & Primitive::NoMaterial))
{
bool doubleSided = false;
MilkshapeMaterial *msMat = mMsNode->getMaterial();
msMat->getUserPropBool("doubleSided", doubleSided);
if (doubleSided)
{
Primitive backface = tsFace;
backface.firstElement = mIndices.size();
mFaces.push_back(backface);
// add verts with order reversed to get the backface
mIndices.push_back(addVertex(vert0,-norm0,tvert0,vertIndices[0]));
mIndices.push_back(addVertex(vert1,-norm1,tvert1,vertIndices[1]));
mIndices.push_back(addVertex(vert2,-norm2,tvert2,vertIndices[2]));
}
}
}
return Parent::lockMesh(time,objectOffset);
}
void MsAppMesh::unlockMesh()
{
Parent::unlockMesh();
// no more cleanup...but if there were some to do, we'd do it here
}
void MsAppMesh::getSkinData()
{
// only generate the skin data once
if (mSkinDataFetched)
return;
mSkinDataFetched = true;
// skinned meshes are attached to more than 1 bone
if (mMsNode->mBoneIndices.size() <= 1)
return;
// need to generate an array of bones that animate this mesh
// Milkshape supports up to 4 bone weights per vertex - all other bones
// are set to weight 0
msModel *model = mMsNode->getModel();
//-----------------------------------------------------------------------
// add all bones attached to this mesh
S32 numBones = mMsNode->mBoneIndices.size();
for (int i = 0; i < numBones;i++)
{
S32 boneIndex = mMsNode->mBoneIndices[i];
assert(boneIndex >= 0 && "Invalid bone index");
MilkshapeNode *node = new MilkshapeBone(boneIndex);
mBones.push_back(new MsAppNode(node, true));
AppConfig::PrintDump(PDPass2,avar("Adding skin object from skin \"%s\" to bone \"%s\" (%i).\r\n",mMsNode->getName(),mBones[i]->getName(),i));
}
//-----------------------------------------------------------------------
// reset all weights to zero
S32 numPoints = msMesh_GetVertexCount(mMsNode->getMsMesh());
mWeights.resize(numBones);
for (int i = 0; i < (int)mWeights.size(); i++)
{
mWeights[i] = new std::vector<F32>;
mWeights[i]->resize(numPoints);
for (int j=0; j<numPoints; j++)
(*mWeights[i])[j] = 0.0f;
}
// set weights for bones that affect vertices in this mesh
for (int j = 0; j < numPoints; j++)
{
S32 indices[MS_BONES_PER_VERTEX_EX];
F32 weights[MS_BONES_PER_VERTEX_EX];
bool attached = false;
// get the bone indices/weights of this vertex - they are stored a bit
// strangely to keep the milkshape file format backwards compatible:
// bone[0] = msVertex.nBoneIndex, weight[0] = msVertexEx.nBoneWeights[0]
// bone[1] = msVertexEx.nBoneIndex[0], weight[1] = msVertexEx.nBoneWeights[1]
// bone[2] = msVertexEx.nBoneIndex[1], weight[2] = msVertexEx.nBoneWeights[2]
// bone[3] = msVertexEx.nBoneIndex[2], weight[3] = 1 - (sum_of_other_weights)
for (int k = 0; k < MS_BONES_PER_VERTEX_EX; k++)
{
msVertexEx *vtx = msMesh_GetVertexExAt(mMsNode->getMsMesh(), j);
// get the bone index
if (k == 0)
{
msVertex *v = msMesh_GetVertexAt(mMsNode->getMsMesh(), j);
indices[k] = msVertex_GetBoneIndex(v);
}
else
{
indices[k] = msVertexEx_GetBoneIndices(vtx, k - 1);
}
// get the bone weight
if (indices[k] >= 0)
{
attached = true;
if (k != (MS_BONES_PER_VERTEX_EX-1))
{
weights[k] = (F32)msVertexEx_GetBoneWeights(vtx, k) / 100.f;
}
else
{
weights[k] = 1.0f - (weights[0] + weights[1] + weights[2]);
}
}
else
weights[k] = 0.0f;
}
// Force verts that are attached to a single bone to have 100% weight
if (attached && (indices[1] == -1) && (weights[0] == 0.0f))
{
weights[0] = 1.0f;
}
// if the vertex is not attached to any bones, attach it to the root bone
if (!attached)
{
S32 boneIndex = mMsNode->getRootBoneIndex();
addBoneWeight(j, boneIndex, 1.0f);
}
else
{
// add weights for each bone
for (int k = 0; k < MS_BONES_PER_VERTEX_EX; k++)
{
if (indices[k] < 0)
continue;
addBoneWeight(j, indices[k], weights[k]);
}
}
}
}
void MsAppMesh::addBoneWeight(S32 vertIndex, S32 boneIndex, F32 weight)
{
// find bone
int i;
for (i = 0; i < (int)mMsNode->mBoneIndices.size(); i++)
if (boneIndex == mMsNode->mBoneIndices[i])
break;
assert(i != mMsNode->mBoneIndices.size() && "Could not find bone");
boneIndex = i;
AppConfig::PrintDump(-1, avar("Adding weight %f for bone %i (\"%s\")\r\n",
weight, boneIndex, mBones[boneIndex]->getName()));
(*mWeights[boneIndex])[vertIndex] = weight;
}
} // namespace DTS