Coverage Report

Created: 2025-08-03 06:54

/src/assimp/code/AssetLib/MS3D/MS3DLoader.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2025, assimp team
7
8
9
10
All rights reserved.
11
12
Redistribution and use of this software in source and binary forms,
13
with or without modification, are permitted provided that the following
14
conditions are met:
15
16
* Redistributions of source code must retain the above
17
  copyright notice, this list of conditions and the
18
  following disclaimer.
19
20
* Redistributions in binary form must reproduce the above
21
  copyright notice, this list of conditions and the
22
  following disclaimer in the documentation and/or other
23
  materials provided with the distribution.
24
25
* Neither the name of the assimp team, nor the names of its
26
  contributors may be used to endorse or promote products
27
  derived from this software without specific prior
28
  written permission of the assimp team.
29
30
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
---------------------------------------------------------------------------
42
*/
43
44
/** @file  MS3DLoader.cpp
45
 *  @brief Implementation of the Ms3D importer class.
46
 *  Written against http://chumbalum.swissquake.ch/ms3d/ms3dspec.txt
47
 */
48
49
50
#ifndef ASSIMP_BUILD_NO_MS3D_IMPORTER
51
52
// internal headers
53
#include "MS3DLoader.h"
54
#include <assimp/StreamReader.h>
55
#include <assimp/DefaultLogger.hpp>
56
#include <assimp/scene.h>
57
#include <assimp/IOSystem.hpp>
58
#include <assimp/importerdesc.h>
59
#include <map>
60
61
using namespace Assimp;
62
63
static constexpr aiImporterDesc desc = {
64
    "Milkshape 3D Importer",
65
    "",
66
    "",
67
    "http://chumbalum.swissquake.ch/",
68
    aiImporterFlags_SupportBinaryFlavour,
69
    0,
70
    0,
71
    0,
72
    0,
73
    "ms3d"
74
};
75
76
// ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH
77
//   (enable old code path, which generates extra nodes per mesh while
78
//    the newer code uses aiMesh::mName to express the name of the
79
//    meshes (a.k.a. groups in MS3D))
80
81
// ------------------------------------------------------------------------------------------------
82
// Constructor to be privately used by Importer
83
MS3DImporter::MS3DImporter()
84
    : mScene()
85
414
{}
86
87
// ------------------------------------------------------------------------------------------------
88
// Returns whether the class can handle the format of the given file.
89
bool MS3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const
90
85
{
91
85
    static const char* tokens[] = { "MS3D000000" };
92
85
    return SearchFileHeaderForToken(pIOHandler,pFile,tokens,AI_COUNT_OF(tokens));
93
85
}
94
95
// ------------------------------------------------------------------------------------------------
96
const aiImporterDesc* MS3DImporter::GetInfo () const
97
422
{
98
422
    return &desc;
99
422
}
100
101
// ------------------------------------------------------------------------------------------------
102
void ReadColor(StreamReaderLE& stream, aiColor4D& ambient)
103
0
{
104
    // aiColor4D is packed on gcc, implicit binding to float& fails therefore.
105
0
    stream >> (float&)ambient.r >> (float&)ambient.g >> (float&)ambient.b >> (float&)ambient.a;
106
0
}
107
108
// ------------------------------------------------------------------------------------------------
109
void ReadVector(StreamReaderLE& stream, aiVector3D& pos)
110
0
{
111
    // See note in ReadColor()
112
0
    stream >> (float&)pos.x >> (float&)pos.y >> (float&)pos.z;
113
0
}
114
115
// ------------------------------------------------------------------------------------------------
116
template<typename T>
117
void MS3DImporter :: ReadComments(StreamReaderLE& stream, std::vector<T>& outp)
118
0
{
119
0
    uint16_t cnt;
120
0
    stream >> cnt;
121
122
0
    for(unsigned int i = 0; i < cnt; ++i) {
123
0
        uint32_t index, clength;
124
0
        stream >> index >> clength;
125
126
0
        if(index >= outp.size()) {
127
0
            ASSIMP_LOG_WARN("MS3D: Invalid index in comment section");
128
0
        }
129
0
        else if (clength > stream.GetRemainingSize()) {
130
0
            throw DeadlyImportError("MS3D: Failure reading comment, length field is out of range");
131
0
        }
132
0
        else {
133
0
            outp[index].comment = std::string(reinterpret_cast<char*>(stream.GetPtr()),clength);
134
0
        }
135
0
        stream.IncPtr(clength);
136
0
    }
137
0
}
Unexecuted instantiation: void Assimp::MS3DImporter::ReadComments<Assimp::MS3DImporter::TempGroup>(Assimp::StreamReader<false, false>&, std::__1::vector<Assimp::MS3DImporter::TempGroup, std::__1::allocator<Assimp::MS3DImporter::TempGroup> >&)
Unexecuted instantiation: void Assimp::MS3DImporter::ReadComments<Assimp::MS3DImporter::TempMaterial>(Assimp::StreamReader<false, false>&, std::__1::vector<Assimp::MS3DImporter::TempMaterial, std::__1::allocator<Assimp::MS3DImporter::TempMaterial> >&)
Unexecuted instantiation: void Assimp::MS3DImporter::ReadComments<Assimp::MS3DImporter::TempJoint>(Assimp::StreamReader<false, false>&, std::__1::vector<Assimp::MS3DImporter::TempJoint, std::__1::allocator<Assimp::MS3DImporter::TempJoint> >&)
138
139
// ------------------------------------------------------------------------------------------------
140
template <typename T, typename T2, typename T3> bool inrange(const T& in, const T2& lower, const T3& higher)
141
0
{
142
0
    return in > lower && in <= higher;
143
0
}
144
145
// ------------------------------------------------------------------------------------------------
146
void MS3DImporter :: CollectChildJoints(const std::vector<TempJoint>& joints,
147
    std::vector<bool>& hadit,
148
    aiNode* nd,
149
    const aiMatrix4x4& absTrafo)
150
0
{
151
0
    unsigned int cnt = 0;
152
0
    for(size_t i = 0; i < joints.size(); ++i) {
153
0
        if (!hadit[i] && !strcmp(joints[i].parentName,nd->mName.data)) {
154
0
            ++cnt;
155
0
        }
156
0
    }
157
158
0
    nd->mChildren = new aiNode*[nd->mNumChildren = cnt];
159
0
    cnt = 0;
160
0
    for(size_t i = 0; i < joints.size(); ++i) {
161
0
        if (!hadit[i] && !strcmp(joints[i].parentName,nd->mName.data)) {
162
0
            aiNode* ch = nd->mChildren[cnt++] = new aiNode(joints[i].name);
163
0
            ch->mParent = nd;
164
165
0
            ch->mTransformation = aiMatrix4x4::Translation(joints[i].position,aiMatrix4x4()=aiMatrix4x4())*
166
0
                aiMatrix4x4().FromEulerAnglesXYZ(joints[i].rotation);
167
168
0
            const aiMatrix4x4 abs = absTrafo*ch->mTransformation;
169
0
            for(unsigned int a = 0; a < mScene->mNumMeshes; ++a) {
170
0
                aiMesh* const msh = mScene->mMeshes[a];
171
0
                for(unsigned int n = 0; n < msh->mNumBones; ++n) {
172
0
                    aiBone* const bone = msh->mBones[n];
173
174
0
                    if(bone->mName == ch->mName) {
175
0
                        bone->mOffsetMatrix = aiMatrix4x4(abs).Inverse();
176
0
                    }
177
0
                }
178
0
            }
179
180
0
            hadit[i] = true;
181
0
            CollectChildJoints(joints,hadit,ch,abs);
182
0
        }
183
0
    }
184
0
}
185
186
// ------------------------------------------------------------------------------------------------
187
void MS3DImporter :: CollectChildJoints(const std::vector<TempJoint>& joints, aiNode* nd)
188
0
{
189
0
     std::vector<bool> hadit(joints.size(),false);
190
0
     aiMatrix4x4 trafo;
191
192
0
     CollectChildJoints(joints,hadit,nd,trafo);
193
0
}
194
195
// ------------------------------------------------------------------------------------------------
196
// Imports the given file into the given scene structure.
197
void MS3DImporter::InternReadFile( const std::string& pFile,
198
    aiScene* pScene, IOSystem* pIOHandler)
199
0
{
200
201
0
    auto file = pIOHandler->Open(pFile, "rb");
202
0
    if (!file)
203
0
        throw DeadlyImportError("MS3D: Could not open ", pFile);
204
205
0
    StreamReaderLE stream(file);
206
207
    // CanRead() should have done this already
208
0
    char head[10];
209
0
    int32_t version;
210
211
0
    mScene = pScene;
212
213
214
    // 1 ------------ read into temporary data structures mirroring the original file
215
216
0
    stream.CopyAndAdvance(head,10);
217
0
    stream >> version;
218
0
    if (strncmp(head,"MS3D000000",10)) {
219
0
        throw DeadlyImportError("Not a MS3D file, magic string MS3D000000 not found: ", pFile);
220
0
    }
221
222
0
    if (version != 4) {
223
0
        throw DeadlyImportError("MS3D: Unsupported file format version, 4 was expected");
224
0
    }
225
226
0
    uint16_t verts;
227
0
    stream >> verts;
228
229
0
    std::vector<TempVertex> vertices(verts);
230
0
    for (unsigned int i = 0; i < verts; ++i) {
231
0
        TempVertex& v = vertices[i];
232
233
0
        stream.IncPtr(1);
234
0
        ReadVector(stream,v.pos);
235
0
        v.bone_id[0] = stream.GetI1();
236
0
        v.ref_cnt = stream.GetI1();
237
238
0
        v.bone_id[1] = v.bone_id[2] = v.bone_id[3] = UINT_MAX;
239
0
        v.weights[1] = v.weights[2] = v.weights[3] = 0.f;
240
0
        v.weights[0] = 1.f;
241
0
    }
242
243
0
    uint16_t tris;
244
0
    stream >> tris;
245
246
0
    std::vector<TempTriangle> triangles(tris);
247
0
    for (unsigned int i = 0;i < tris; ++i) {
248
0
        TempTriangle& t = triangles[i];
249
250
0
        stream.IncPtr(2);
251
0
        for (unsigned int j = 0; j < 3; ++j) {
252
0
            t.indices[j] = stream.GetI2();
253
0
        }
254
255
0
        for (unsigned int j = 0; j < 3; ++j) {
256
0
            ReadVector(stream,t.normals[j]);
257
0
        }
258
259
0
        for (unsigned int j = 0; j < 3; ++j) {
260
0
            stream >> (float&)(t.uv[j].x); // see note in ReadColor()
261
0
        }
262
0
        for (unsigned int j = 0; j < 3; ++j) {
263
0
            stream >> (float&)(t.uv[j].y);
264
0
        }
265
266
0
        t.sg    = stream.GetI1();
267
0
        t.group = stream.GetI1();
268
0
    }
269
270
0
    uint16_t grp;
271
0
    stream >> grp;
272
273
0
    bool need_default = false;
274
0
    std::vector<TempGroup> groups(grp);
275
0
    for (unsigned int i = 0;i < grp; ++i) {
276
0
        TempGroup& t = groups[i];
277
278
0
        stream.IncPtr(1);
279
0
        stream.CopyAndAdvance(t.name,32);
280
281
0
        t.name[32] = '\0';
282
0
        uint16_t num;
283
0
        stream >> num;
284
285
0
        t.triangles.resize(num);
286
0
        for (unsigned int j = 0; j < num; ++j) {
287
0
            t.triangles[j] = stream.GetI2();
288
0
        }
289
0
        t.mat = stream.GetI1();
290
0
        if (t.mat == UINT_MAX) {
291
0
            need_default = true;
292
0
        }
293
0
    }
294
295
0
    uint16_t mat;
296
0
    stream >> mat;
297
298
0
    std::vector<TempMaterial> materials(mat);
299
0
    for (unsigned int j = 0;j < mat; ++j) {
300
0
        TempMaterial& t = materials[j];
301
302
0
        stream.CopyAndAdvance(t.name,32);
303
0
        t.name[32] = '\0';
304
305
0
        ReadColor(stream,t.ambient);
306
0
        ReadColor(stream,t.diffuse);
307
0
        ReadColor(stream,t.specular);
308
0
        ReadColor(stream,t.emissive);
309
0
        stream >> t.shininess  >> t.transparency;
310
311
0
        stream.IncPtr(1);
312
313
0
        stream.CopyAndAdvance(t.texture,128);
314
0
        t.texture[128] = '\0';
315
316
0
        stream.CopyAndAdvance(t.alphamap,128);
317
0
        t.alphamap[128] = '\0';
318
0
    }
319
320
0
    float animfps, currenttime;
321
0
    uint32_t totalframes;
322
0
    stream >> animfps >> currenttime >> totalframes;
323
324
0
    uint16_t joint;
325
0
    stream >> joint;
326
327
0
    std::vector<TempJoint> joints(joint);
328
0
    for(unsigned int ii = 0; ii < joint; ++ii) {
329
0
        TempJoint& j = joints[ii];
330
331
0
        stream.IncPtr(1);
332
0
        stream.CopyAndAdvance(j.name,32);
333
0
        j.name[32] = '\0';
334
335
0
        stream.CopyAndAdvance(j.parentName,32);
336
0
        j.parentName[32] = '\0';
337
338
0
        ReadVector(stream,j.rotation);
339
0
        ReadVector(stream,j.position);
340
341
0
        j.rotFrames.resize(stream.GetI2());
342
0
        j.posFrames.resize(stream.GetI2());
343
344
0
        for(unsigned int a = 0; a < j.rotFrames.size(); ++a) {
345
0
            TempKeyFrame& kf = j.rotFrames[a];
346
0
            stream >> kf.time;
347
0
            ReadVector(stream,kf.value);
348
0
        }
349
0
        for(unsigned int a = 0; a < j.posFrames.size(); ++a) {
350
0
            TempKeyFrame& kf = j.posFrames[a];
351
0
            stream >> kf.time;
352
0
            ReadVector(stream,kf.value);
353
0
        }
354
0
    }
355
356
0
    if(stream.GetRemainingSize() > 4) {
357
0
        uint32_t subversion;
358
0
        stream >> subversion;
359
0
        if (subversion == 1) {
360
0
            ReadComments<TempGroup>(stream,groups);
361
0
            ReadComments<TempMaterial>(stream,materials);
362
0
            ReadComments<TempJoint>(stream,joints);
363
364
            // model comment - print it for we have such a nice log.
365
0
            if (stream.GetI4()) {
366
0
                const size_t len = static_cast<size_t>(stream.GetI4());
367
0
                if (len > stream.GetRemainingSize()) {
368
0
                    throw DeadlyImportError("MS3D: Model comment is too long");
369
0
                }
370
371
0
                const std::string& s = std::string(reinterpret_cast<char*>(stream.GetPtr()),len);
372
0
                ASSIMP_LOG_DEBUG("MS3D: Model comment: ", s);
373
0
            }
374
375
0
            if(stream.GetRemainingSize() > 4 && inrange((stream >> subversion,subversion),1u,3u)) {
376
0
                for(unsigned int i = 0; i < verts; ++i) {
377
0
                    TempVertex& v = vertices[i];
378
0
                    v.weights[3]=1.f;
379
0
                    for(unsigned int n = 0; n < 3; v.weights[3]-=v.weights[n++]) {
380
0
                        v.bone_id[n+1] = stream.GetI1();
381
0
                        v.weights[n] = static_cast<float>(static_cast<unsigned int>(stream.GetI1()))/255.f;
382
0
                    }
383
0
                    stream.IncPtr((subversion-1)<<2u);
384
0
                }
385
386
                // even further extra data is not of interest for us, at least now now.
387
0
            }
388
0
        }
389
0
    }
390
391
    // 2 ------------ convert to proper aiXX data structures -----------------------------------
392
393
0
    if (need_default && materials.size()) {
394
0
        ASSIMP_LOG_WARN("MS3D: Found group with no material assigned, spawning default material");
395
        // if one of the groups has no material assigned, but there are other
396
        // groups with materials, a default material needs to be added (
397
        // scenepreprocessor adds a default material only if nummat==0).
398
0
        materials.emplace_back();
399
0
        TempMaterial& m = materials.back();
400
401
0
        strcpy(m.name,"<MS3D_DefaultMat>");
402
0
        m.diffuse = aiColor4D(0.6f,0.6f,0.6f,1.0);
403
0
        m.transparency = 1.f;
404
0
        m.shininess = 0.f;
405
406
        // this is because these TempXXX struct's have no c'tors.
407
0
        m.texture[0] = m.alphamap[0] = '\0';
408
409
0
        for (unsigned int i = 0; i < groups.size(); ++i) {
410
0
            TempGroup& g = groups[i];
411
0
            if (g.mat == UINT_MAX) {
412
0
                g.mat = static_cast<unsigned int>(materials.size()-1);
413
0
            }
414
0
        }
415
0
    }
416
417
    // convert materials to our generic key-value dict-alike
418
0
    if (materials.size()) {
419
0
        pScene->mMaterials = new aiMaterial*[materials.size()];
420
0
        for (size_t i = 0; i < materials.size(); ++i) {
421
422
0
            aiMaterial* mo = new aiMaterial();
423
0
            pScene->mMaterials[pScene->mNumMaterials++] = mo;
424
425
0
            const TempMaterial& mi = materials[i];
426
427
0
            aiString tmp;
428
0
            if (0[mi.alphamap]) {
429
0
                tmp = aiString(mi.alphamap);
430
0
                mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_OPACITY(0));
431
0
            }
432
0
            if (0[mi.texture]) {
433
0
                tmp = aiString(mi.texture);
434
0
                mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_DIFFUSE(0));
435
0
            }
436
0
            if (0[mi.name]) {
437
0
                tmp = aiString(mi.name);
438
0
                mo->AddProperty(&tmp,AI_MATKEY_NAME);
439
0
            }
440
441
0
            mo->AddProperty(&mi.ambient,1,AI_MATKEY_COLOR_AMBIENT);
442
0
            mo->AddProperty(&mi.diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
443
0
            mo->AddProperty(&mi.specular,1,AI_MATKEY_COLOR_SPECULAR);
444
0
            mo->AddProperty(&mi.emissive,1,AI_MATKEY_COLOR_EMISSIVE);
445
446
0
            mo->AddProperty(&mi.shininess,1,AI_MATKEY_SHININESS);
447
0
            mo->AddProperty(&mi.transparency,1,AI_MATKEY_OPACITY);
448
449
0
            const int sm = mi.shininess>0.f?aiShadingMode_Phong:aiShadingMode_Gouraud;
450
0
            mo->AddProperty(&sm,1,AI_MATKEY_SHADING_MODEL);
451
0
        }
452
0
    }
453
454
    // convert groups to meshes
455
0
    if (groups.empty()) {
456
0
        throw DeadlyImportError("MS3D: Didn't get any group records, file is malformed");
457
0
    }
458
459
0
    pScene->mMeshes = new aiMesh*[pScene->mNumMeshes=static_cast<unsigned int>(groups.size())]();
460
0
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
461
462
0
        aiMesh* m = pScene->mMeshes[i] = new aiMesh();
463
0
        const TempGroup& g = groups[i];
464
465
0
        if (pScene->mNumMaterials && g.mat > pScene->mNumMaterials) {
466
0
            throw DeadlyImportError("MS3D: Encountered invalid material index, file is malformed");
467
0
        } // no error if no materials at all - scenepreprocessor adds one then
468
469
0
        m->mMaterialIndex  = g.mat;
470
0
        m->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
471
472
0
        m->mFaces = new aiFace[m->mNumFaces = static_cast<unsigned int>(g.triangles.size())];
473
0
        m->mNumVertices = m->mNumFaces*3;
474
475
        // storage for vertices - verbose format, as requested by the postprocessing pipeline
476
0
        m->mVertices = new aiVector3D[m->mNumVertices];
477
0
        m->mNormals  = new aiVector3D[m->mNumVertices];
478
0
        m->mTextureCoords[0] = new aiVector3D[m->mNumVertices];
479
0
        m->mNumUVComponents[0] = 2;
480
481
0
        typedef std::map<unsigned int,unsigned int> BoneSet;
482
0
        BoneSet mybones;
483
484
0
        for (unsigned int j = 0,n = 0; j < m->mNumFaces; ++j) {
485
0
            aiFace& f = m->mFaces[j];
486
0
            if (g.triangles[j] >= triangles.size()) {
487
0
                throw DeadlyImportError("MS3D: Encountered invalid triangle index, file is malformed");
488
0
            }
489
490
0
            TempTriangle& t = triangles[g.triangles[j]];
491
0
            f.mIndices = new unsigned int[f.mNumIndices=3];
492
493
0
            for (unsigned int k = 0; k < 3; ++k,++n) {
494
0
                if (t.indices[k] >= vertices.size()) {
495
0
                    throw DeadlyImportError("MS3D: Encountered invalid vertex index, file is malformed");
496
0
                }
497
498
0
                const TempVertex& v = vertices[t.indices[k]];
499
0
                for(unsigned int a = 0; a < 4; ++a) {
500
0
                    if (v.bone_id[a] != UINT_MAX) {
501
0
                        if (v.bone_id[a] >= joints.size()) {
502
0
                            throw DeadlyImportError("MS3D: Encountered invalid bone index, file is malformed");
503
0
                        }
504
0
                        if (mybones.find(v.bone_id[a]) == mybones.end()) {
505
0
                             mybones[v.bone_id[a]] = 1;
506
0
                        }
507
0
                        else ++mybones[v.bone_id[a]];
508
0
                    }
509
0
                }
510
511
                // collect vertex components
512
0
                m->mVertices[n] = v.pos;
513
514
0
                m->mNormals[n] = t.normals[k];
515
0
                m->mTextureCoords[0][n] = aiVector3D(t.uv[k].x,1.f-t.uv[k].y,0.0);
516
0
                f.mIndices[k] = n;
517
0
            }
518
0
        }
519
520
        // allocate storage for bones
521
0
        if(!mybones.empty()) {
522
0
            std::vector<unsigned int> bmap(joints.size());
523
0
            m->mBones = new aiBone*[mybones.size()]();
524
0
            for(BoneSet::const_iterator it = mybones.begin(); it != mybones.end(); ++it) {
525
0
                aiBone* const bn = m->mBones[m->mNumBones] = new aiBone();
526
0
                const TempJoint& jnt = joints[(*it).first];
527
528
0
                bn->mName.Set(jnt.name);
529
0
                bn->mWeights = new aiVertexWeight[(*it).second];
530
531
0
                bmap[(*it).first] = m->mNumBones++;
532
0
            }
533
534
            // .. and collect bone weights
535
0
            for (unsigned int j = 0,n = 0; j < m->mNumFaces; ++j) {
536
0
                TempTriangle& t = triangles[g.triangles[j]];
537
538
0
                for (unsigned int k = 0; k < 3; ++k,++n) {
539
0
                    const TempVertex& v = vertices[t.indices[k]];
540
0
                    for(unsigned int a = 0; a < 4; ++a) {
541
0
                        const unsigned int bone = v.bone_id[a];
542
0
                        if(bone==UINT_MAX){
543
0
                            continue;
544
0
                        }
545
546
0
                        aiBone* const outbone = m->mBones[bmap[bone]];
547
0
                        aiVertexWeight& outwght = outbone->mWeights[outbone->mNumWeights++];
548
549
0
                        outwght.mVertexId = n;
550
0
                        outwght.mWeight = v.weights[a];
551
0
                    }
552
0
                }
553
0
            }
554
0
        }
555
0
    }
556
557
    // ... add dummy nodes under a single root, each holding a reference to one
558
    // mesh. If we didn't do this, we'd lose the group name.
559
0
    aiNode* rt = pScene->mRootNode = new aiNode("<MS3DRoot>");
560
561
#ifdef ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH
562
    rt->mChildren = new aiNode*[rt->mNumChildren=pScene->mNumMeshes+(joints.size()?1:0)]();
563
564
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
565
        aiNode* nd = rt->mChildren[i] = new aiNode();
566
567
        const TempGroup& g = groups[i];
568
569
        // we need to generate an unique name for all mesh nodes.
570
        // since we want to keep the group name, a prefix is
571
        // prepended.
572
        nd->mName = aiString("<MS3DMesh>_");
573
        nd->mName.Append(g.name);
574
        nd->mParent = rt;
575
576
        nd->mMeshes = new unsigned int[nd->mNumMeshes = 1];
577
        nd->mMeshes[0] = i;
578
    }
579
#else
580
0
    rt->mMeshes = new unsigned int[pScene->mNumMeshes];
581
0
    for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
582
0
        rt->mMeshes[rt->mNumMeshes++] = i;
583
0
    }
584
0
#endif
585
586
    // convert animations as well
587
0
    if(joints.size()) {
588
0
#ifndef ASSIMP_BUILD_MS3D_ONE_NODE_PER_MESH
589
0
        rt->mChildren = new aiNode*[1]();
590
0
        rt->mNumChildren = 1;
591
592
0
        aiNode* jt = rt->mChildren[0] = new aiNode();
593
#else
594
        aiNode* jt = rt->mChildren[pScene->mNumMeshes] = new aiNode();
595
#endif
596
0
        jt->mParent = rt;
597
0
        CollectChildJoints(joints,jt);
598
0
        jt->mName.Set("<MS3DJointRoot>");
599
600
0
        pScene->mAnimations = new aiAnimation*[ pScene->mNumAnimations = 1 ];
601
0
        aiAnimation* const anim = pScene->mAnimations[0] = new aiAnimation();
602
603
0
        anim->mName.Set("<MS3DMasterAnim>");
604
605
        // carry the fps info to the user by scaling all times with it
606
0
        anim->mTicksPerSecond = animfps;
607
608
        // leave duration at its default, so ScenePreprocessor will fill an appropriate
609
        // value (the values taken from some MS3D files seem to be too unreliable
610
        // to pass the validation)
611
        // anim->mDuration = totalframes/animfps;
612
613
0
        anim->mChannels = new aiNodeAnim*[joints.size()]();
614
0
        for(std::vector<TempJoint>::const_iterator it = joints.begin(); it != joints.end(); ++it) {
615
0
            if ((*it).rotFrames.empty() && (*it).posFrames.empty()) {
616
0
                continue;
617
0
            }
618
619
0
            aiNodeAnim* nd = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
620
0
            nd->mNodeName.Set((*it).name);
621
622
0
            if ((*it).rotFrames.size()) {
623
0
                nd->mRotationKeys = new aiQuatKey[(*it).rotFrames.size()];
624
0
                for(std::vector<TempKeyFrame>::const_iterator rot = (*it).rotFrames.begin(); rot != (*it).rotFrames.end(); ++rot) {
625
0
                    aiQuatKey& q = nd->mRotationKeys[nd->mNumRotationKeys++];
626
627
0
                    q.mTime = (*rot).time*animfps;
628
0
                    q.mValue = aiQuaternion(aiMatrix3x3(aiMatrix4x4().FromEulerAnglesXYZ((*it).rotation)*
629
0
                        aiMatrix4x4().FromEulerAnglesXYZ((*rot).value)));
630
0
                }
631
0
            }
632
633
0
            if ((*it).posFrames.size()) {
634
0
                nd->mPositionKeys = new aiVectorKey[(*it).posFrames.size()];
635
636
0
                aiQuatKey* qu = nd->mRotationKeys;
637
0
                for(std::vector<TempKeyFrame>::const_iterator pos = (*it).posFrames.begin(); pos != (*it).posFrames.end(); ++pos,++qu) {
638
0
                    aiVectorKey& v = nd->mPositionKeys[nd->mNumPositionKeys++];
639
640
0
                    v.mTime = (*pos).time*animfps;
641
0
                    v.mValue = (*it).position + (*pos).value;
642
0
                }
643
0
            }
644
0
        }
645
        // fixup to pass the validation if not a single animation channel is non-trivial
646
0
        if (!anim->mNumChannels) {
647
0
            anim->mChannels = nullptr;
648
0
        }
649
0
    }
650
0
}
651
652
#endif