M3DImporter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. Copyright (c) 2019 bzt
  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
  9. following 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. #ifndef ASSIMP_BUILD_NO_M3D_IMPORTER
  35. #define M3D_IMPLEMENTATION
  36. #define M3D_NONORMALS /* leave the post-processing to Assimp */
  37. #define M3D_NOWEIGHTS
  38. #define M3D_NOANIMATION
  39. #include <assimp/DefaultIOSystem.h>
  40. #include <assimp/IOStreamBuffer.h>
  41. #include <assimp/ai_assert.h>
  42. #include <assimp/importerdesc.h>
  43. #include <assimp/scene.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/Importer.hpp>
  46. #include <memory>
  47. #include "M3DImporter.h"
  48. #include "M3DMaterials.h"
  49. #include "M3DWrapper.h"
  50. // RESOURCES:
  51. // https://gitlab.com/bztsrc/model3d/blob/master/docs/m3d_format.md
  52. // https://gitlab.com/bztsrc/model3d/blob/master/docs/a3d_format.md
  53. /*
  54. Unfortunately aiNode has bone structures and meshes too, yet we can't assign
  55. the mesh to a bone aiNode as a skin may refer to several aiNodes. Therefore
  56. I've decided to import into this structure:
  57. aiScene->mRootNode
  58. | |->mMeshes (all the meshes)
  59. | \->children (empty if there's no skeleton imported, no meshes)
  60. | \->skeleton root aiNode*
  61. | |->bone aiNode
  62. | | \->subbone aiNode
  63. | |->bone aiNode
  64. | | ...
  65. | \->bone aiNode
  66. \->mMeshes[]
  67. \->aiBone, referencing mesh-less aiNodes from above
  68. * - normally one, but if a model has several skeleton roots, then all of them
  69. are listed in aiScene->mRootNode->children, but all without meshes
  70. */
  71. static const aiImporterDesc desc = {
  72. "Model 3D Importer",
  73. "",
  74. "",
  75. "",
  76. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
  77. 0,
  78. 0,
  79. 0,
  80. 0,
  81. #ifdef M3D_ASCII
  82. "m3d a3d"
  83. #else
  84. "m3d"
  85. #endif
  86. };
  87. namespace Assimp {
  88. using namespace std;
  89. // ------------------------------------------------------------------------------------------------
  90. // Default constructor
  91. M3DImporter::M3DImporter() :
  92. mScene(nullptr) {
  93. // empty
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Returns true, if file is a binary or ASCII Model 3D file.
  97. bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  98. const std::string extension = GetExtension(pFile);
  99. if (extension == "m3d"
  100. #ifdef M3D_ASCII
  101. || extension == "a3d"
  102. #endif
  103. )
  104. return true;
  105. else if (!extension.length() || checkSig) {
  106. if (!pIOHandler) {
  107. return true;
  108. }
  109. /*
  110. * don't use CheckMagicToken because that checks with swapped bytes too, leading to false
  111. * positives. This magic is not uint32_t, but char[4], so memcmp is the best way
  112. const char* tokens[] = {"3DMO", "3dmo"};
  113. return CheckMagicToken(pIOHandler,pFile,tokens,2,0,4);
  114. */
  115. std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb"));
  116. unsigned char data[4];
  117. if (4 != pStream->Read(data, 1, 4)) {
  118. return false;
  119. }
  120. return !memcmp(data, "3DMO", 4) /* bin */
  121. #ifdef M3D_ASCII
  122. || !memcmp(data, "3dmo", 4) /* ASCII */
  123. #endif
  124. ;
  125. }
  126. return false;
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. const aiImporterDesc *M3DImporter::GetInfo() const {
  130. return &desc;
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. // Model 3D import implementation
  134. void M3DImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
  135. // Read file into memory
  136. std::unique_ptr<IOStream> pStream(pIOHandler->Open(file, "rb"));
  137. if (!pStream.get()) {
  138. throw DeadlyImportError("Failed to open file " + file + ".");
  139. }
  140. // Get the file-size and validate it, throwing an exception when fails
  141. size_t fileSize = pStream->FileSize();
  142. if (fileSize < 8) {
  143. throw DeadlyImportError("M3D-file " + file + " is too small.");
  144. }
  145. std::vector<unsigned char> buffer(fileSize);
  146. if (fileSize != pStream->Read(buffer.data(), 1, fileSize)) {
  147. throw DeadlyImportError("Failed to read the file " + file + ".");
  148. }
  149. // extra check for binary format's first 8 bytes. Not done for the ASCII variant
  150. if (!memcmp(buffer.data(), "3DMO", 4) && memcmp(buffer.data() + 4, &fileSize, 4)) {
  151. throw DeadlyImportError("Bad binary header in file " + file + ".");
  152. }
  153. #ifdef M3D_ASCII
  154. // make sure there's a terminator zero character, as input must be ASCIIZ
  155. if (!memcmp(buffer.data(), "3dmo", 4)) {
  156. buffer.push_back(0);
  157. }
  158. #endif
  159. // Get the path for external assets
  160. std::string folderName("./");
  161. std::string::size_type pos = file.find_last_of("\\/");
  162. if (pos != std::string::npos) {
  163. folderName = file.substr(0, pos);
  164. if (!folderName.empty()) {
  165. pIOHandler->PushDirectory(folderName);
  166. }
  167. }
  168. //DefaultLogger::create("/dev/stderr", Logger::VERBOSE);
  169. ASSIMP_LOG_DEBUG_F("M3D: loading ", file);
  170. // let the C SDK do the hard work for us
  171. M3DWrapper m3d(pIOHandler, buffer);
  172. if (!m3d) {
  173. throw DeadlyImportError("Unable to parse " + file + " as M3D.");
  174. }
  175. // create the root node
  176. pScene->mRootNode = new aiNode;
  177. pScene->mRootNode->mName = aiString(m3d.Name());
  178. pScene->mRootNode->mTransformation = aiMatrix4x4();
  179. pScene->mRootNode->mNumChildren = 0;
  180. mScene = pScene;
  181. ASSIMP_LOG_DEBUG("M3D: root node " + m3d.Name());
  182. // now we just have to fill up the Assimp structures in pScene
  183. importMaterials(m3d);
  184. importTextures(m3d);
  185. importBones(m3d, M3D_NOTDEFINED, pScene->mRootNode);
  186. importMeshes(m3d);
  187. importAnimations(m3d);
  188. // Pop directory stack
  189. if (pIOHandler->StackSize() > 0) {
  190. pIOHandler->PopDirectory();
  191. }
  192. }
  193. // ------------------------------------------------------------------------------------------------
  194. // convert materials. properties are converted using a static table in M3DMaterials.h
  195. void M3DImporter::importMaterials(const M3DWrapper &m3d) {
  196. unsigned int i, j, k, l, n;
  197. m3dm_t *m;
  198. aiString name = aiString(AI_DEFAULT_MATERIAL_NAME);
  199. aiColor4D c;
  200. ai_real f;
  201. ai_assert(mScene != nullptr);
  202. ai_assert(m3d);
  203. mScene->mNumMaterials = m3d->nummaterial + 1;
  204. mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials];
  205. ASSIMP_LOG_DEBUG_F("M3D: importMaterials ", mScene->mNumMaterials);
  206. // add a default material as first
  207. aiMaterial *mat = new aiMaterial;
  208. mat->AddProperty(&name, AI_MATKEY_NAME);
  209. c.a = 1.0f;
  210. c.b = c.g = c.r = 0.6f;
  211. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE);
  212. mScene->mMaterials[0] = mat;
  213. if (!m3d->nummaterial || !m3d->material) {
  214. return;
  215. }
  216. for (i = 0; i < m3d->nummaterial; i++) {
  217. m = &m3d->material[i];
  218. aiMaterial *newMat = new aiMaterial;
  219. name.Set(std::string(m->name));
  220. newMat->AddProperty(&name, AI_MATKEY_NAME);
  221. for (j = 0; j < m->numprop; j++) {
  222. // look up property type
  223. // 0 - 127 scalar values,
  224. // 128 - 255 the same properties but for texture maps
  225. k = 256;
  226. for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++)
  227. if (m->prop[j].type == m3d_propertytypes[l].id ||
  228. m->prop[j].type == m3d_propertytypes[l].id + 128) {
  229. k = l;
  230. break;
  231. }
  232. // should never happen, but be safe than sorry
  233. if (k == 256)
  234. continue;
  235. // scalar properties
  236. if (m->prop[j].type < 128 && aiProps[k].pKey) {
  237. switch (m3d_propertytypes[k].format) {
  238. case m3dpf_color:
  239. c = mkColor(m->prop[j].value.color);
  240. newMat->AddProperty(&c, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
  241. break;
  242. case m3dpf_float:
  243. f = m->prop[j].value.fnum;
  244. newMat->AddProperty(&f, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
  245. break;
  246. default:
  247. n = m->prop[j].value.num;
  248. if (m->prop[j].type == m3dp_il) {
  249. switch (n) {
  250. case 0:
  251. n = aiShadingMode_NoShading;
  252. break;
  253. case 2:
  254. n = aiShadingMode_Phong;
  255. break;
  256. default:
  257. n = aiShadingMode_Gouraud;
  258. break;
  259. }
  260. }
  261. newMat->AddProperty(&n, 1, aiProps[k].pKey, aiProps[k].type, aiProps[k].index);
  262. break;
  263. }
  264. }
  265. // texture map properties
  266. if (m->prop[j].type >= 128 && aiTxProps[k].pKey &&
  267. // extra check, should never happen, do we have the refered texture?
  268. m->prop[j].value.textureid < m3d->numtexture &&
  269. m3d->texture[m->prop[j].value.textureid].name) {
  270. name.Set(std::string(std::string(m3d->texture[m->prop[j].value.textureid].name) + ".png"));
  271. mat->AddProperty(&name, aiTxProps[k].pKey, aiTxProps[k].type, aiTxProps[k].index);
  272. n = 0;
  273. mat->AddProperty(&n, 1, _AI_MATKEY_UVWSRC_BASE, aiProps[k].type, aiProps[k].index);
  274. }
  275. }
  276. mScene->mMaterials[i + 1] = mat;
  277. }
  278. }
  279. // ------------------------------------------------------------------------------------------------
  280. // import textures, this is the simplest of all
  281. void M3DImporter::importTextures(const M3DWrapper &m3d) {
  282. unsigned int i;
  283. const char *formatHint[] = {
  284. "rgba0800",
  285. "rgba0808",
  286. "rgba8880",
  287. "rgba8888"
  288. };
  289. m3dtx_t *t;
  290. ai_assert(mScene != nullptr);
  291. ai_assert(m3d);
  292. mScene->mNumTextures = m3d->numtexture;
  293. ASSIMP_LOG_DEBUG_F("M3D: importTextures ", mScene->mNumTextures);
  294. if (!m3d->numtexture || !m3d->texture) {
  295. return;
  296. }
  297. mScene->mTextures = new aiTexture *[m3d->numtexture];
  298. for (i = 0; i < m3d->numtexture; i++) {
  299. unsigned int j, k;
  300. t = &m3d->texture[i];
  301. aiTexture *tx = new aiTexture;
  302. tx->mFilename = aiString(std::string(t->name) + ".png");
  303. if (!t->w || !t->h || !t->f || !t->d) {
  304. /* without ASSIMP_USE_M3D_READFILECB, we only have the filename, but no texture data ever */
  305. tx->mWidth = 0;
  306. tx->mHeight = 0;
  307. memcpy(tx->achFormatHint, "png\000", 4);
  308. tx->pcData = nullptr;
  309. } else {
  310. /* if we have the texture loaded, set format hint and pcData too */
  311. tx->mWidth = t->w;
  312. tx->mHeight = t->h;
  313. strcpy(tx->achFormatHint, formatHint[t->f - 1]);
  314. tx->pcData = new aiTexel[tx->mWidth * tx->mHeight];
  315. for (j = k = 0; j < tx->mWidth * tx->mHeight; j++) {
  316. switch (t->f) {
  317. case 1: tx->pcData[j].g = t->d[k++]; break;
  318. case 2:
  319. tx->pcData[j].g = t->d[k++];
  320. tx->pcData[j].a = t->d[k++];
  321. break;
  322. case 3:
  323. tx->pcData[j].r = t->d[k++];
  324. tx->pcData[j].g = t->d[k++];
  325. tx->pcData[j].b = t->d[k++];
  326. tx->pcData[j].a = 255;
  327. break;
  328. case 4:
  329. tx->pcData[j].r = t->d[k++];
  330. tx->pcData[j].g = t->d[k++];
  331. tx->pcData[j].b = t->d[k++];
  332. tx->pcData[j].a = t->d[k++];
  333. break;
  334. }
  335. }
  336. }
  337. mScene->mTextures[i] = tx;
  338. }
  339. }
  340. // ------------------------------------------------------------------------------------------------
  341. // this is tricky. M3D has a global vertex and UV list, and faces are indexing them
  342. // individually. In assimp there're per mesh vertex and UV lists, and they must be
  343. // indexed simultaneously.
  344. void M3DImporter::importMeshes(const M3DWrapper &m3d) {
  345. ASSIMP_LOG_DEBUG_F("M3D: importMeshes ", m3d->numface);
  346. if (!m3d->numface || !m3d->face || !m3d->numvertex || !m3d->vertex) {
  347. return;
  348. }
  349. unsigned int i, j, k, l, numpoly = 3, lastMat = M3D_INDEXMAX;
  350. std::vector<aiMesh *> *meshes = new std::vector<aiMesh *>();
  351. std::vector<aiFace> *faces = nullptr;
  352. std::vector<aiVector3D> *vertices = nullptr;
  353. std::vector<aiVector3D> *normals = nullptr;
  354. std::vector<aiVector3D> *texcoords = nullptr;
  355. std::vector<aiColor4D> *colors = nullptr;
  356. std::vector<unsigned int> *vertexids = nullptr;
  357. aiMesh *pMesh = nullptr;
  358. ai_assert(mScene != nullptr);
  359. ai_assert(m3d);
  360. ai_assert(mScene->mRootNode != nullptr);
  361. for (i = 0; i < m3d->numface; i++) {
  362. // we must switch mesh if material changes
  363. if (lastMat != m3d->face[i].materialid) {
  364. lastMat = m3d->face[i].materialid;
  365. if (pMesh && vertices && vertices->size() && faces && faces->size()) {
  366. populateMesh(m3d, pMesh, faces, vertices, normals, texcoords, colors, vertexids);
  367. meshes->push_back(pMesh);
  368. delete faces;
  369. delete vertices;
  370. delete normals;
  371. delete texcoords;
  372. delete colors;
  373. delete vertexids; // this is not stored in pMesh, just to collect bone vertices
  374. }
  375. pMesh = new aiMesh;
  376. pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  377. pMesh->mMaterialIndex = lastMat + 1;
  378. faces = new std::vector<aiFace>();
  379. vertices = new std::vector<aiVector3D>();
  380. normals = new std::vector<aiVector3D>();
  381. texcoords = new std::vector<aiVector3D>();
  382. colors = new std::vector<aiColor4D>();
  383. vertexids = new std::vector<unsigned int>();
  384. }
  385. // add a face to temporary vector
  386. aiFace *pFace = new aiFace;
  387. pFace->mNumIndices = numpoly;
  388. pFace->mIndices = new unsigned int[numpoly];
  389. for (j = 0; j < numpoly; j++) {
  390. aiVector3D pos, uv, norm;
  391. k = static_cast<unsigned int>(vertices->size());
  392. pFace->mIndices[j] = k;
  393. l = m3d->face[i].vertex[j];
  394. if (l >= m3d->numvertex) continue;
  395. pos.x = m3d->vertex[l].x;
  396. pos.y = m3d->vertex[l].y;
  397. pos.z = m3d->vertex[l].z;
  398. vertices->push_back(pos);
  399. colors->push_back(mkColor(m3d->vertex[l].color));
  400. // add a bone to temporary vector
  401. if (m3d->vertex[l].skinid != M3D_UNDEF && m3d->vertex[l].skinid != M3D_INDEXMAX && m3d->skin && m3d->bone) {
  402. // this is complicated, because M3D stores a list of bone id / weight pairs per
  403. // vertex but assimp uses lists of local vertex id/weight pairs per local bone list
  404. vertexids->push_back(l);
  405. }
  406. l = m3d->face[i].texcoord[j];
  407. if (l != M3D_UNDEF && l < m3d->numtmap) {
  408. uv.x = m3d->tmap[l].u;
  409. uv.y = m3d->tmap[l].v;
  410. uv.z = 0.0;
  411. texcoords->push_back(uv);
  412. }
  413. l = m3d->face[i].normal[j];
  414. if (l != M3D_UNDEF && l < m3d->numvertex) {
  415. norm.x = m3d->vertex[l].x;
  416. norm.y = m3d->vertex[l].y;
  417. norm.z = m3d->vertex[l].z;
  418. normals->push_back(norm);
  419. }
  420. }
  421. faces->push_back(*pFace);
  422. delete pFace;
  423. }
  424. // if there's data left in the temporary vectors, flush them
  425. if (pMesh && vertices->size() && faces->size()) {
  426. populateMesh(m3d, pMesh, faces, vertices, normals, texcoords, colors, vertexids);
  427. meshes->push_back(pMesh);
  428. }
  429. // create global mesh list in scene
  430. mScene->mNumMeshes = static_cast<unsigned int>(meshes->size());
  431. mScene->mMeshes = new aiMesh *[mScene->mNumMeshes];
  432. std::copy(meshes->begin(), meshes->end(), mScene->mMeshes);
  433. // create mesh indeces in root node
  434. mScene->mRootNode->mNumMeshes = static_cast<unsigned int>(meshes->size());
  435. mScene->mRootNode->mMeshes = new unsigned int[meshes->size()];
  436. for (i = 0; i < meshes->size(); i++) {
  437. mScene->mRootNode->mMeshes[i] = i;
  438. }
  439. delete meshes;
  440. if (faces) delete faces;
  441. if (vertices) delete vertices;
  442. if (normals) delete normals;
  443. if (texcoords) delete texcoords;
  444. if (colors) delete colors;
  445. if (vertexids) delete vertexids;
  446. }
  447. // ------------------------------------------------------------------------------------------------
  448. // a reentrant node parser. Otherwise this is simple
  449. void M3DImporter::importBones(const M3DWrapper &m3d, unsigned int parentid, aiNode *pParent) {
  450. unsigned int i, n;
  451. ai_assert(pParent != nullptr);
  452. ai_assert(mScene != nullptr);
  453. ai_assert(m3d);
  454. ASSIMP_LOG_DEBUG_F("M3D: importBones ", m3d->numbone, " parentid ", (int)parentid);
  455. if (!m3d->numbone || !m3d->bone) {
  456. return;
  457. }
  458. for (n = 0, i = parentid + 1; i < m3d->numbone; i++) {
  459. if (m3d->bone[i].parent == parentid) {
  460. n++;
  461. }
  462. }
  463. pParent->mChildren = new aiNode *[n];
  464. for (i = parentid + 1; i < m3d->numbone; i++) {
  465. if (m3d->bone[i].parent == parentid) {
  466. aiNode *pChild = new aiNode;
  467. pChild->mParent = pParent;
  468. pChild->mName = aiString(std::string(m3d->bone[i].name));
  469. convertPose(m3d, &pChild->mTransformation, m3d->bone[i].pos, m3d->bone[i].ori);
  470. pChild->mNumChildren = 0;
  471. pParent->mChildren[pParent->mNumChildren] = pChild;
  472. pParent->mNumChildren++;
  473. importBones(m3d, i, pChild);
  474. }
  475. }
  476. }
  477. // ------------------------------------------------------------------------------------------------
  478. // this is another headache. M3D stores list of changed bone id/position/orientation triplets and
  479. // a timestamp per frame, but assimp needs timestamp and lists of position, orientation lists per
  480. // bone, so we have to convert between the two conceptually different representation forms
  481. void M3DImporter::importAnimations(const M3DWrapper &m3d) {
  482. unsigned int i, j, k, l, pos, ori;
  483. double t;
  484. m3da_t *a;
  485. ai_assert(mScene != nullptr);
  486. ai_assert(m3d);
  487. mScene->mNumAnimations = m3d->numaction;
  488. ASSIMP_LOG_DEBUG_F("M3D: importAnimations ", mScene->mNumAnimations);
  489. if (!m3d->numaction || !m3d->action || !m3d->numbone || !m3d->bone || !m3d->vertex) {
  490. return;
  491. }
  492. mScene->mAnimations = new aiAnimation *[m3d->numaction];
  493. for (i = 0; i < m3d->numaction; i++) {
  494. a = &m3d->action[i];
  495. aiAnimation *pAnim = new aiAnimation;
  496. pAnim->mName = aiString(std::string(a->name));
  497. pAnim->mDuration = ((double)a->durationmsec) / 10;
  498. pAnim->mTicksPerSecond = 100;
  499. // now we know how many bones are referenced in this animation
  500. pAnim->mNumChannels = m3d->numbone;
  501. pAnim->mChannels = new aiNodeAnim *[pAnim->mNumChannels];
  502. for (l = 0; l < m3d->numbone; l++) {
  503. unsigned int n;
  504. pAnim->mChannels[l] = new aiNodeAnim;
  505. pAnim->mChannels[l]->mNodeName = aiString(std::string(m3d->bone[l].name));
  506. // now n is the size of positions / orientations arrays
  507. pAnim->mChannels[l]->mNumPositionKeys = pAnim->mChannels[l]->mNumRotationKeys = a->numframe;
  508. pAnim->mChannels[l]->mPositionKeys = new aiVectorKey[a->numframe];
  509. pAnim->mChannels[l]->mRotationKeys = new aiQuatKey[a->numframe];
  510. pos = m3d->bone[l].pos;
  511. ori = m3d->bone[l].ori;
  512. for (j = n = 0; j < a->numframe; j++) {
  513. t = ((double)a->frame[j].msec) / 10;
  514. for (k = 0; k < a->frame[j].numtransform; k++) {
  515. if (a->frame[j].transform[k].boneid == l) {
  516. pos = a->frame[j].transform[k].pos;
  517. ori = a->frame[j].transform[k].ori;
  518. }
  519. }
  520. if (pos >= m3d->numvertex || ori >= m3d->numvertex) continue;
  521. m3dv_t *v = &m3d->vertex[pos];
  522. m3dv_t *q = &m3d->vertex[ori];
  523. pAnim->mChannels[l]->mPositionKeys[j].mTime = t;
  524. pAnim->mChannels[l]->mPositionKeys[j].mValue.x = v->x;
  525. pAnim->mChannels[l]->mPositionKeys[j].mValue.y = v->y;
  526. pAnim->mChannels[l]->mPositionKeys[j].mValue.z = v->z;
  527. pAnim->mChannels[l]->mRotationKeys[j].mTime = t;
  528. pAnim->mChannels[l]->mRotationKeys[j].mValue.w = q->w;
  529. pAnim->mChannels[l]->mRotationKeys[j].mValue.x = q->x;
  530. pAnim->mChannels[l]->mRotationKeys[j].mValue.y = q->y;
  531. pAnim->mChannels[l]->mRotationKeys[j].mValue.z = q->z;
  532. } // foreach frame
  533. } // foreach bones
  534. mScene->mAnimations[i] = pAnim;
  535. }
  536. }
  537. // ------------------------------------------------------------------------------------------------
  538. // convert uint32_t into aiColor4D
  539. aiColor4D M3DImporter::mkColor(uint32_t c) {
  540. aiColor4D color;
  541. color.a = ((float)((c >> 24) & 0xff)) / 255;
  542. color.b = ((float)((c >> 16) & 0xff)) / 255;
  543. color.g = ((float)((c >> 8) & 0xff)) / 255;
  544. color.r = ((float)((c >> 0) & 0xff)) / 255;
  545. return color;
  546. }
  547. // ------------------------------------------------------------------------------------------------
  548. // convert a position id and orientation id into a 4 x 4 transformation matrix
  549. void M3DImporter::convertPose(const M3DWrapper &m3d, aiMatrix4x4 *m, unsigned int posid, unsigned int orientid) {
  550. ai_assert(m != nullptr);
  551. ai_assert(m3d);
  552. ai_assert(posid != M3D_UNDEF);
  553. ai_assert(posid < m3d->numvertex);
  554. ai_assert(orientid != M3D_UNDEF);
  555. ai_assert(orientid < m3d->numvertex);
  556. if (!m3d->numvertex || !m3d->vertex)
  557. return;
  558. m3dv_t *p = &m3d->vertex[posid];
  559. m3dv_t *q = &m3d->vertex[orientid];
  560. /* quaternion to matrix. Do NOT use aiQuaternion to aiMatrix3x3, gives bad results */
  561. if (q->x == 0.0 && q->y == 0.0 && q->z >= 0.7071065 && q->z <= 0.7071075 && q->w == 0.0) {
  562. m->a2 = m->a3 = m->b1 = m->b3 = m->c1 = m->c2 = 0.0;
  563. m->a1 = m->b2 = m->c3 = -1.0;
  564. } else {
  565. m->a1 = 1 - 2 * (q->y * q->y + q->z * q->z);
  566. if (m->a1 > -M3D_EPSILON && m->a1 < M3D_EPSILON) m->a1 = 0.0;
  567. m->a2 = 2 * (q->x * q->y - q->z * q->w);
  568. if (m->a2 > -M3D_EPSILON && m->a2 < M3D_EPSILON) m->a2 = 0.0;
  569. m->a3 = 2 * (q->x * q->z + q->y * q->w);
  570. if (m->a3 > -M3D_EPSILON && m->a3 < M3D_EPSILON) m->a3 = 0.0;
  571. m->b1 = 2 * (q->x * q->y + q->z * q->w);
  572. if (m->b1 > -M3D_EPSILON && m->b1 < M3D_EPSILON) m->b1 = 0.0;
  573. m->b2 = 1 - 2 * (q->x * q->x + q->z * q->z);
  574. if (m->b2 > -M3D_EPSILON && m->b2 < M3D_EPSILON) m->b2 = 0.0;
  575. m->b3 = 2 * (q->y * q->z - q->x * q->w);
  576. if (m->b3 > -M3D_EPSILON && m->b3 < M3D_EPSILON) m->b3 = 0.0;
  577. m->c1 = 2 * (q->x * q->z - q->y * q->w);
  578. if (m->c1 > -M3D_EPSILON && m->c1 < M3D_EPSILON) m->c1 = 0.0;
  579. m->c2 = 2 * (q->y * q->z + q->x * q->w);
  580. if (m->c2 > -M3D_EPSILON && m->c2 < M3D_EPSILON) m->c2 = 0.0;
  581. m->c3 = 1 - 2 * (q->x * q->x + q->y * q->y);
  582. if (m->c3 > -M3D_EPSILON && m->c3 < M3D_EPSILON) m->c3 = 0.0;
  583. }
  584. /* set translation */
  585. m->a4 = p->x;
  586. m->b4 = p->y;
  587. m->c4 = p->z;
  588. m->d1 = 0;
  589. m->d2 = 0;
  590. m->d3 = 0;
  591. m->d4 = 1;
  592. }
  593. // ------------------------------------------------------------------------------------------------
  594. // find a node by name
  595. aiNode *M3DImporter::findNode(aiNode *pNode, aiString name) {
  596. ai_assert(pNode != nullptr);
  597. ai_assert(mScene != nullptr);
  598. if (pNode->mName == name) {
  599. return pNode;
  600. }
  601. for (unsigned int i = 0; i < pNode->mNumChildren; i++) {
  602. aiNode *pChild = findNode(pNode->mChildren[i], name);
  603. if (pChild) {
  604. return pChild;
  605. }
  606. }
  607. return nullptr;
  608. }
  609. // ------------------------------------------------------------------------------------------------
  610. // fills up offsetmatrix in mBones
  611. void M3DImporter::calculateOffsetMatrix(aiNode *pNode, aiMatrix4x4 *m) {
  612. ai_assert(pNode != nullptr);
  613. ai_assert(mScene != nullptr);
  614. if (pNode->mParent) {
  615. calculateOffsetMatrix(pNode->mParent, m);
  616. *m *= pNode->mTransformation;
  617. } else {
  618. *m = pNode->mTransformation;
  619. }
  620. }
  621. // ------------------------------------------------------------------------------------------------
  622. // because M3D has a global mesh, global vertex ids and stores materialid on the face, we need
  623. // temporary lists to collect data for an aiMesh, which requires local arrays and local indeces
  624. // this function fills up an aiMesh with those temporary lists
  625. void M3DImporter::populateMesh(const M3DWrapper &m3d, aiMesh *pMesh, std::vector<aiFace> *faces, std::vector<aiVector3D> *vertices,
  626. std::vector<aiVector3D> *normals, std::vector<aiVector3D> *texcoords, std::vector<aiColor4D> *colors,
  627. std::vector<unsigned int> *vertexids) {
  628. ai_assert(pMesh != nullptr);
  629. ai_assert(faces != nullptr);
  630. ai_assert(vertices != nullptr);
  631. ai_assert(normals != nullptr);
  632. ai_assert(texcoords != nullptr);
  633. ai_assert(colors != nullptr);
  634. ai_assert(vertexids != nullptr);
  635. ai_assert(m3d);
  636. ASSIMP_LOG_DEBUG_F("M3D: populateMesh numvertices ", vertices->size(), " numfaces ", faces->size(),
  637. " numnormals ", normals->size(), " numtexcoord ", texcoords->size(), " numbones ", m3d->numbone);
  638. if (vertices->size() && faces->size()) {
  639. pMesh->mNumFaces = static_cast<unsigned int>(faces->size());
  640. pMesh->mFaces = new aiFace[pMesh->mNumFaces];
  641. std::copy(faces->begin(), faces->end(), pMesh->mFaces);
  642. pMesh->mNumVertices = static_cast<unsigned int>(vertices->size());
  643. pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
  644. std::copy(vertices->begin(), vertices->end(), pMesh->mVertices);
  645. if (normals->size() == vertices->size()) {
  646. pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
  647. std::copy(normals->begin(), normals->end(), pMesh->mNormals);
  648. }
  649. if (texcoords->size() == vertices->size()) {
  650. pMesh->mTextureCoords[0] = new aiVector3D[pMesh->mNumVertices];
  651. std::copy(texcoords->begin(), texcoords->end(), pMesh->mTextureCoords[0]);
  652. pMesh->mNumUVComponents[0] = 2;
  653. }
  654. if (colors->size() == vertices->size()) {
  655. pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
  656. std::copy(colors->begin(), colors->end(), pMesh->mColors[0]);
  657. }
  658. // this is complicated, because M3D stores a list of bone id / weight pairs per
  659. // vertex but assimp uses lists of local vertex id/weight pairs per local bone list
  660. pMesh->mNumBones = m3d->numbone;
  661. // we need aiBone with mOffsetMatrix for bones without weights as well
  662. if (pMesh->mNumBones && m3d->numbone && m3d->bone) {
  663. pMesh->mBones = new aiBone *[pMesh->mNumBones];
  664. for (unsigned int i = 0; i < m3d->numbone; i++) {
  665. aiNode *pNode;
  666. pMesh->mBones[i] = new aiBone;
  667. pMesh->mBones[i]->mName = aiString(std::string(m3d->bone[i].name));
  668. pMesh->mBones[i]->mNumWeights = 0;
  669. pNode = findNode(mScene->mRootNode, pMesh->mBones[i]->mName);
  670. if (pNode) {
  671. calculateOffsetMatrix(pNode, &pMesh->mBones[i]->mOffsetMatrix);
  672. pMesh->mBones[i]->mOffsetMatrix.Inverse();
  673. } else
  674. pMesh->mBones[i]->mOffsetMatrix = aiMatrix4x4();
  675. }
  676. if (vertexids->size() && m3d->numvertex && m3d->vertex && m3d->numskin && m3d->skin) {
  677. unsigned int i, j;
  678. // first count how many vertices we have per bone
  679. for (i = 0; i < vertexids->size(); i++) {
  680. if (vertexids->at(i) >= m3d->numvertex) {
  681. continue;
  682. }
  683. unsigned int s = m3d->vertex[vertexids->at(i)].skinid;
  684. if (s != M3D_UNDEF && s != M3D_INDEXMAX) {
  685. for (unsigned int k = 0; k < M3D_NUMBONE && m3d->skin[s].weight[k] > 0.0; k++) {
  686. aiString name = aiString(std::string(m3d->bone[m3d->skin[s].boneid[k]].name));
  687. for (j = 0; j < pMesh->mNumBones; j++) {
  688. if (pMesh->mBones[j]->mName == name) {
  689. pMesh->mBones[j]->mNumWeights++;
  690. break;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. // allocate mWeights
  697. for (j = 0; j < pMesh->mNumBones; j++) {
  698. aiBone *pBone = pMesh->mBones[j];
  699. if (pBone->mNumWeights) {
  700. pBone->mWeights = new aiVertexWeight[pBone->mNumWeights];
  701. pBone->mNumWeights = 0;
  702. }
  703. }
  704. // fill up with data
  705. for (i = 0; i < vertexids->size(); i++) {
  706. if (vertexids->at(i) >= m3d->numvertex) continue;
  707. unsigned int s = m3d->vertex[vertexids->at(i)].skinid;
  708. if (s != M3D_UNDEF && s != M3D_INDEXMAX && s < m3d->numskin) {
  709. for (unsigned int k = 0; k < M3D_NUMBONE && m3d->skin[s].weight[k] > 0.0; k++) {
  710. if (m3d->skin[s].boneid[k] >= m3d->numbone) continue;
  711. aiString name = aiString(std::string(m3d->bone[m3d->skin[s].boneid[k]].name));
  712. for (j = 0; j < pMesh->mNumBones; j++) {
  713. if (pMesh->mBones[j]->mName == name) {
  714. aiBone *pBone = pMesh->mBones[j];
  715. pBone->mWeights[pBone->mNumWeights].mVertexId = i;
  716. pBone->mWeights[pBone->mNumWeights].mWeight = m3d->skin[s].weight[k];
  717. pBone->mNumWeights++;
  718. break;
  719. }
  720. }
  721. } // foreach skin
  722. }
  723. } // foreach vertexids
  724. }
  725. }
  726. }
  727. }
  728. // ------------------------------------------------------------------------------------------------
  729. } // Namespace Assimp
  730. #endif // !! ASSIMP_BUILD_NO_M3D_IMPORTER