MS3DLoader.cpp 23 KB

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