No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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