IRRLoader.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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 IRRLoader.cpp
  35. * @brief Implementation of the Irr importer class
  36. */
  37. #ifndef ASSIMP_BUILD_NO_IRR_IMPORTER
  38. #include "AssetLib/Irr/IRRLoader.h"
  39. #include "Common/Importer.h"
  40. #include <assimp/GenericProperty.h>
  41. #include <assimp/MathFunctions.h>
  42. #include <assimp/ParsingUtils.h>
  43. #include <assimp/SceneCombiner.h>
  44. #include <assimp/StandardShapes.h>
  45. #include <assimp/fast_atof.h>
  46. #include <assimp/importerdesc.h>
  47. #include <assimp/material.h>
  48. #include <assimp/mesh.h>
  49. #include <assimp/postprocess.h>
  50. #include <assimp/scene.h>
  51. #include <assimp/DefaultLogger.hpp>
  52. #include <assimp/IOSystem.hpp>
  53. #include <memory>
  54. using namespace Assimp;
  55. static const aiImporterDesc desc = {
  56. "Irrlicht Scene Reader",
  57. "",
  58. "",
  59. "http://irrlicht.sourceforge.net/",
  60. aiImporterFlags_SupportTextFlavour,
  61. 0,
  62. 0,
  63. 0,
  64. 0,
  65. "irr xml"
  66. };
  67. // ------------------------------------------------------------------------------------------------
  68. // Constructor to be privately used by Importer
  69. IRRImporter::IRRImporter() :
  70. fps(), configSpeedFlag() {
  71. // empty
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. // Destructor, private as well
  75. IRRImporter::~IRRImporter() = default;
  76. // ------------------------------------------------------------------------------------------------
  77. // Returns whether the class can handle the format of the given file.
  78. bool IRRImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  79. static const char *tokens[] = { "irr_scene" };
  80. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. const aiImporterDesc *IRRImporter::GetInfo() const {
  84. return &desc;
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. void IRRImporter::SetupProperties(const Importer *pImp) {
  88. // read the output frame rate of all node animation channels
  89. fps = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_IRR_ANIM_FPS, 100);
  90. if (fps < 10.) {
  91. ASSIMP_LOG_ERROR("IRR: Invalid FPS configuration");
  92. fps = 100;
  93. }
  94. // AI_CONFIG_FAVOUR_SPEED
  95. configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 0));
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Build a mesh that consists of a single squad (a side of a skybox)
  99. aiMesh *IRRImporter::BuildSingleQuadMesh(const SkyboxVertex &v1,
  100. const SkyboxVertex &v2,
  101. const SkyboxVertex &v3,
  102. const SkyboxVertex &v4) {
  103. // allocate and prepare the mesh
  104. aiMesh *out = new aiMesh();
  105. out->mPrimitiveTypes = aiPrimitiveType_POLYGON;
  106. out->mNumFaces = 1;
  107. // build the face
  108. out->mFaces = new aiFace[1];
  109. aiFace &face = out->mFaces[0];
  110. face.mNumIndices = 4;
  111. face.mIndices = new unsigned int[4];
  112. for (unsigned int i = 0; i < 4; ++i)
  113. face.mIndices[i] = i;
  114. out->mNumVertices = 4;
  115. // copy vertex positions
  116. aiVector3D *vec = out->mVertices = new aiVector3D[4];
  117. *vec++ = v1.position;
  118. *vec++ = v2.position;
  119. *vec++ = v3.position;
  120. *vec = v4.position;
  121. // copy vertex normals
  122. vec = out->mNormals = new aiVector3D[4];
  123. *vec++ = v1.normal;
  124. *vec++ = v2.normal;
  125. *vec++ = v3.normal;
  126. *vec = v4.normal;
  127. // copy texture coordinates
  128. vec = out->mTextureCoords[0] = new aiVector3D[4];
  129. *vec++ = v1.uv;
  130. *vec++ = v2.uv;
  131. *vec++ = v3.uv;
  132. *vec = v4.uv;
  133. return out;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. void IRRImporter::BuildSkybox(std::vector<aiMesh *> &meshes, std::vector<aiMaterial *> materials) {
  137. // Update the material of the skybox - replace the name and disable shading for skyboxes.
  138. for (unsigned int i = 0; i < 6; ++i) {
  139. aiMaterial *out = (aiMaterial *)(*(materials.end() - (6 - i)));
  140. aiString s;
  141. s.length = ::ai_snprintf(s.data, MAXLEN, "SkyboxSide_%u", i);
  142. out->AddProperty(&s, AI_MATKEY_NAME);
  143. int shading = aiShadingMode_NoShading;
  144. out->AddProperty(&shading, 1, AI_MATKEY_SHADING_MODEL);
  145. }
  146. // Skyboxes are much more difficult. They are represented
  147. // by six single planes with different textures, so we'll
  148. // need to build six meshes.
  149. const ai_real l = 10.0; // the size used by Irrlicht
  150. // FRONT SIDE
  151. meshes.push_back(BuildSingleQuadMesh(
  152. SkyboxVertex(-l, -l, -l, 0, 0, 1, 1.0, 1.0),
  153. SkyboxVertex(l, -l, -l, 0, 0, 1, 0.0, 1.0),
  154. SkyboxVertex(l, l, -l, 0, 0, 1, 0.0, 0.0),
  155. SkyboxVertex(-l, l, -l, 0, 0, 1, 1.0, 0.0)));
  156. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 6u);
  157. // LEFT SIDE
  158. meshes.push_back(BuildSingleQuadMesh(
  159. SkyboxVertex(l, -l, -l, -1, 0, 0, 1.0, 1.0),
  160. SkyboxVertex(l, -l, l, -1, 0, 0, 0.0, 1.0),
  161. SkyboxVertex(l, l, l, -1, 0, 0, 0.0, 0.0),
  162. SkyboxVertex(l, l, -l, -1, 0, 0, 1.0, 0.0)));
  163. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 5u);
  164. // BACK SIDE
  165. meshes.push_back(BuildSingleQuadMesh(
  166. SkyboxVertex(l, -l, l, 0, 0, -1, 1.0, 1.0),
  167. SkyboxVertex(-l, -l, l, 0, 0, -1, 0.0, 1.0),
  168. SkyboxVertex(-l, l, l, 0, 0, -1, 0.0, 0.0),
  169. SkyboxVertex(l, l, l, 0, 0, -1, 1.0, 0.0)));
  170. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 4u);
  171. // RIGHT SIDE
  172. meshes.push_back(BuildSingleQuadMesh(
  173. SkyboxVertex(-l, -l, l, 1, 0, 0, 1.0, 1.0),
  174. SkyboxVertex(-l, -l, -l, 1, 0, 0, 0.0, 1.0),
  175. SkyboxVertex(-l, l, -l, 1, 0, 0, 0.0, 0.0),
  176. SkyboxVertex(-l, l, l, 1, 0, 0, 1.0, 0.0)));
  177. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 3u);
  178. // TOP SIDE
  179. meshes.push_back(BuildSingleQuadMesh(
  180. SkyboxVertex(l, l, -l, 0, -1, 0, 1.0, 1.0),
  181. SkyboxVertex(l, l, l, 0, -1, 0, 0.0, 1.0),
  182. SkyboxVertex(-l, l, l, 0, -1, 0, 0.0, 0.0),
  183. SkyboxVertex(-l, l, -l, 0, -1, 0, 1.0, 0.0)));
  184. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 2u);
  185. // BOTTOM SIDE
  186. meshes.push_back(BuildSingleQuadMesh(
  187. SkyboxVertex(l, -l, l, 0, 1, 0, 0.0, 0.0),
  188. SkyboxVertex(l, -l, -l, 0, 1, 0, 1.0, 0.0),
  189. SkyboxVertex(-l, -l, -l, 0, 1, 0, 1.0, 1.0),
  190. SkyboxVertex(-l, -l, l, 0, 1, 0, 0.0, 1.0)));
  191. meshes.back()->mMaterialIndex = static_cast<unsigned int>(materials.size() - 1u);
  192. }
  193. // ------------------------------------------------------------------------------------------------
  194. void IRRImporter::CopyMaterial(std::vector<aiMaterial *> &materials,
  195. std::vector<std::pair<aiMaterial *, unsigned int>> &inmaterials,
  196. unsigned int &defMatIdx,
  197. aiMesh *mesh) {
  198. if (inmaterials.empty()) {
  199. // Do we have a default material? If not we need to create one
  200. if (UINT_MAX == defMatIdx) {
  201. defMatIdx = (unsigned int)materials.size();
  202. //TODO: add this materials to someone?
  203. /*aiMaterial* mat = new aiMaterial();
  204. aiString s;
  205. s.Set(AI_DEFAULT_MATERIAL_NAME);
  206. mat->AddProperty(&s,AI_MATKEY_NAME);
  207. aiColor3D c(0.6f,0.6f,0.6f);
  208. mat->AddProperty(&c,1,AI_MATKEY_COLOR_DIFFUSE);*/
  209. }
  210. mesh->mMaterialIndex = defMatIdx;
  211. return;
  212. } else if (inmaterials.size() > 1) {
  213. ASSIMP_LOG_INFO("IRR: Skipping additional materials");
  214. }
  215. mesh->mMaterialIndex = (unsigned int)materials.size();
  216. materials.push_back(inmaterials[0].first);
  217. }
  218. // ------------------------------------------------------------------------------------------------
  219. inline int ClampSpline(int idx, int size) {
  220. return (idx < 0 ? size + idx : (idx >= size ? idx - size : idx));
  221. }
  222. // ------------------------------------------------------------------------------------------------
  223. inline void FindSuitableMultiple(int &angle) {
  224. if (angle < 3)
  225. angle = 3;
  226. else if (angle < 10)
  227. angle = 10;
  228. else if (angle < 20)
  229. angle = 20;
  230. else if (angle < 30)
  231. angle = 30;
  232. }
  233. // ------------------------------------------------------------------------------------------------
  234. void IRRImporter::ComputeAnimations(Node *root, aiNode *real, std::vector<aiNodeAnim *> &anims) {
  235. ai_assert(nullptr != root && nullptr != real);
  236. // XXX totally WIP - doesn't produce proper results, need to evaluate
  237. // whether there's any use for Irrlicht's proprietary scene format
  238. // outside Irrlicht ...
  239. // This also applies to the above function of FindSuitableMultiple and ClampSpline which are
  240. // solely used in this function
  241. if (root->animators.empty()) {
  242. return;
  243. }
  244. unsigned int total(0);
  245. for (std::list<Animator>::iterator it = root->animators.begin(); it != root->animators.end(); ++it) {
  246. if ((*it).type == Animator::UNKNOWN || (*it).type == Animator::OTHER) {
  247. ASSIMP_LOG_WARN("IRR: Skipping unknown or unsupported animator");
  248. continue;
  249. }
  250. ++total;
  251. }
  252. if (!total) {
  253. return;
  254. } else if (1 == total) {
  255. ASSIMP_LOG_WARN("IRR: Adding dummy nodes to simulate multiple animators");
  256. }
  257. // NOTE: 1 tick == i millisecond
  258. unsigned int cur = 0;
  259. for (std::list<Animator>::iterator it = root->animators.begin();
  260. it != root->animators.end(); ++it) {
  261. if ((*it).type == Animator::UNKNOWN || (*it).type == Animator::OTHER) continue;
  262. Animator &in = *it;
  263. aiNodeAnim *anim = new aiNodeAnim();
  264. if (cur != total - 1) {
  265. // Build a new name - a prefix instead of a suffix because it is
  266. // easier to check against
  267. anim->mNodeName.length = ::ai_snprintf(anim->mNodeName.data, MAXLEN,
  268. "$INST_DUMMY_%i_%s", total - 1,
  269. (root->name.length() ? root->name.c_str() : ""));
  270. // we'll also need to insert a dummy in the node hierarchy.
  271. aiNode *dummy = new aiNode();
  272. for (unsigned int i = 0; i < real->mParent->mNumChildren; ++i)
  273. if (real->mParent->mChildren[i] == real)
  274. real->mParent->mChildren[i] = dummy;
  275. dummy->mParent = real->mParent;
  276. dummy->mName = anim->mNodeName;
  277. dummy->mNumChildren = 1;
  278. dummy->mChildren = new aiNode *[dummy->mNumChildren];
  279. dummy->mChildren[0] = real;
  280. // the transformation matrix of the dummy node is the identity
  281. real->mParent = dummy;
  282. } else
  283. anim->mNodeName.Set(root->name);
  284. ++cur;
  285. switch (in.type) {
  286. case Animator::ROTATION: {
  287. // -----------------------------------------------------
  288. // find out how long a full rotation will take
  289. // This is the least common multiple of 360.f and all
  290. // three euler angles. Although we'll surely find a
  291. // possible multiple (haha) it could be somewhat large
  292. // for our purposes. So we need to modify the angles
  293. // here in order to get good results.
  294. // -----------------------------------------------------
  295. int angles[3];
  296. angles[0] = (int)(in.direction.x * 100);
  297. angles[1] = (int)(in.direction.y * 100);
  298. angles[2] = (int)(in.direction.z * 100);
  299. angles[0] %= 360;
  300. angles[1] %= 360;
  301. angles[2] %= 360;
  302. if ((angles[0] * angles[1]) != 0 && (angles[1] * angles[2]) != 0) {
  303. FindSuitableMultiple(angles[0]);
  304. FindSuitableMultiple(angles[1]);
  305. FindSuitableMultiple(angles[2]);
  306. }
  307. int lcm = 360;
  308. if (angles[0])
  309. lcm = Math::lcm(lcm, angles[0]);
  310. if (angles[1])
  311. lcm = Math::lcm(lcm, angles[1]);
  312. if (angles[2])
  313. lcm = Math::lcm(lcm, angles[2]);
  314. if (360 == lcm)
  315. break;
  316. // find out how many time units we'll need for the finest
  317. // track (in seconds) - this defines the number of output
  318. // keys (fps * seconds)
  319. float max = 0.f;
  320. if (angles[0])
  321. max = (float)lcm / angles[0];
  322. if (angles[1])
  323. max = std::max(max, (float)lcm / angles[1]);
  324. if (angles[2])
  325. max = std::max(max, (float)lcm / angles[2]);
  326. anim->mNumRotationKeys = (unsigned int)(max * fps);
  327. anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys];
  328. // begin with a zero angle
  329. aiVector3D angle;
  330. for (unsigned int i = 0; i < anim->mNumRotationKeys; ++i) {
  331. // build the quaternion for the given euler angles
  332. aiQuatKey &q = anim->mRotationKeys[i];
  333. q.mValue = aiQuaternion(angle.x, angle.y, angle.z);
  334. q.mTime = (double)i;
  335. // increase the angle
  336. angle += in.direction;
  337. }
  338. // This animation is repeated and repeated ...
  339. anim->mPostState = anim->mPreState = aiAnimBehaviour_REPEAT;
  340. } break;
  341. case Animator::FLY_CIRCLE: {
  342. // -----------------------------------------------------
  343. // Find out how much time we'll need to perform a
  344. // full circle.
  345. // -----------------------------------------------------
  346. const double seconds = (1. / in.speed) / 1000.;
  347. const double tdelta = 1000. / fps;
  348. anim->mNumPositionKeys = (unsigned int)(fps * seconds);
  349. anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
  350. // from Irrlicht, what else should we do than copying it?
  351. aiVector3D vecU, vecV;
  352. if (in.direction.y) {
  353. vecV = aiVector3D(50, 0, 0) ^ in.direction;
  354. } else
  355. vecV = aiVector3D(0, 50, 00) ^ in.direction;
  356. vecV.Normalize();
  357. vecU = (vecV ^ in.direction).Normalize();
  358. // build the output keys
  359. for (unsigned int i = 0; i < anim->mNumPositionKeys; ++i) {
  360. aiVectorKey &key = anim->mPositionKeys[i];
  361. key.mTime = i * tdelta;
  362. const ai_real t = (ai_real)(in.speed * key.mTime);
  363. key.mValue = in.circleCenter + in.circleRadius * ((vecU * std::cos(t)) + (vecV * std::sin(t)));
  364. }
  365. // This animation is repeated and repeated ...
  366. anim->mPostState = anim->mPreState = aiAnimBehaviour_REPEAT;
  367. } break;
  368. case Animator::FLY_STRAIGHT: {
  369. anim->mPostState = anim->mPreState = (in.loop ? aiAnimBehaviour_REPEAT : aiAnimBehaviour_CONSTANT);
  370. const double seconds = in.timeForWay / 1000.;
  371. const double tdelta = 1000. / fps;
  372. anim->mNumPositionKeys = (unsigned int)(fps * seconds);
  373. anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
  374. aiVector3D diff = in.direction - in.circleCenter;
  375. const ai_real lengthOfWay = diff.Length();
  376. diff.Normalize();
  377. const double timeFactor = lengthOfWay / in.timeForWay;
  378. // build the output keys
  379. for (unsigned int i = 0; i < anim->mNumPositionKeys; ++i) {
  380. aiVectorKey &key = anim->mPositionKeys[i];
  381. key.mTime = i * tdelta;
  382. key.mValue = in.circleCenter + diff * ai_real(timeFactor * key.mTime);
  383. }
  384. } break;
  385. case Animator::FOLLOW_SPLINE: {
  386. // repeat outside the defined time range
  387. anim->mPostState = anim->mPreState = aiAnimBehaviour_REPEAT;
  388. const int size = (int)in.splineKeys.size();
  389. if (!size) {
  390. // We have no point in the spline. That's bad. Really bad.
  391. ASSIMP_LOG_WARN("IRR: Spline animators with no points defined");
  392. delete anim;
  393. anim = nullptr;
  394. break;
  395. } else if (size == 1) {
  396. // We have just one point in the spline so we don't need the full calculation
  397. anim->mNumPositionKeys = 1;
  398. anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
  399. anim->mPositionKeys[0].mValue = in.splineKeys[0].mValue;
  400. anim->mPositionKeys[0].mTime = 0.f;
  401. break;
  402. }
  403. unsigned int ticksPerFull = 15;
  404. anim->mNumPositionKeys = (unsigned int)(ticksPerFull * fps);
  405. anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
  406. for (unsigned int i = 0; i < anim->mNumPositionKeys; ++i) {
  407. aiVectorKey &key = anim->mPositionKeys[i];
  408. const ai_real dt = (i * in.speed * ai_real(0.001));
  409. const ai_real u = dt - std::floor(dt);
  410. const int idx = (int)std::floor(dt) % size;
  411. // get the 4 current points to evaluate the spline
  412. const aiVector3D &p0 = in.splineKeys[ClampSpline(idx - 1, size)].mValue;
  413. const aiVector3D &p1 = in.splineKeys[ClampSpline(idx + 0, size)].mValue;
  414. const aiVector3D &p2 = in.splineKeys[ClampSpline(idx + 1, size)].mValue;
  415. const aiVector3D &p3 = in.splineKeys[ClampSpline(idx + 2, size)].mValue;
  416. // compute polynomials
  417. const ai_real u2 = u * u;
  418. const ai_real u3 = u2 * 2;
  419. const ai_real h1 = ai_real(2.0) * u3 - ai_real(3.0) * u2 + ai_real(1.0);
  420. const ai_real h2 = ai_real(-2.0) * u3 + ai_real(3.0) * u3;
  421. const ai_real h3 = u3 - ai_real(2.0) * u3;
  422. const ai_real h4 = u3 - u2;
  423. // compute the spline tangents
  424. const aiVector3D t1 = (p2 - p0) * in.tightness;
  425. aiVector3D t2 = (p3 - p1) * in.tightness;
  426. // and use them to get the interpolated point
  427. t2 = (h1 * p1 + p2 * h2 + t1 * h3 + h4 * t2);
  428. // build a simple translation matrix from it
  429. key.mValue = t2;
  430. key.mTime = (double)i;
  431. }
  432. } break;
  433. default:
  434. // UNKNOWN , OTHER
  435. break;
  436. };
  437. if (anim) {
  438. anims.push_back(anim);
  439. ++total;
  440. }
  441. }
  442. }
  443. // ------------------------------------------------------------------------------------------------
  444. // This function is maybe more generic than we'd need it here
  445. void SetupMapping(aiMaterial *mat, aiTextureMapping mode, const aiVector3D &axis = aiVector3D(0.f, 0.f, -1.f)) {
  446. if (nullptr == mat) {
  447. return;
  448. }
  449. // Check whether there are texture properties defined - setup
  450. // the desired texture mapping mode for all of them and ignore
  451. // all UV settings we might encounter. WE HAVE NO UVS!
  452. std::vector<aiMaterialProperty *> p;
  453. p.reserve(mat->mNumProperties + 1);
  454. for (unsigned int i = 0; i < mat->mNumProperties; ++i) {
  455. aiMaterialProperty *prop = mat->mProperties[i];
  456. if (!::strcmp(prop->mKey.data, "$tex.file")) {
  457. // Setup the mapping key
  458. aiMaterialProperty *m = new aiMaterialProperty();
  459. m->mKey.Set("$tex.mapping");
  460. m->mIndex = prop->mIndex;
  461. m->mSemantic = prop->mSemantic;
  462. m->mType = aiPTI_Integer;
  463. m->mDataLength = 4;
  464. m->mData = new char[4];
  465. *((int *)m->mData) = mode;
  466. p.push_back(prop);
  467. p.push_back(m);
  468. // Setup the mapping axis
  469. if (mode == aiTextureMapping_CYLINDER || mode == aiTextureMapping_PLANE || mode == aiTextureMapping_SPHERE) {
  470. m = new aiMaterialProperty();
  471. m->mKey.Set("$tex.mapaxis");
  472. m->mIndex = prop->mIndex;
  473. m->mSemantic = prop->mSemantic;
  474. m->mType = aiPTI_Float;
  475. m->mDataLength = 12;
  476. m->mData = new char[12];
  477. *((aiVector3D *)m->mData) = axis;
  478. p.push_back(m);
  479. }
  480. } else if (!::strcmp(prop->mKey.data, "$tex.uvwsrc")) {
  481. delete mat->mProperties[i];
  482. } else
  483. p.push_back(prop);
  484. }
  485. if (p.empty()) return;
  486. // rebuild the output array
  487. if (p.size() > mat->mNumAllocated) {
  488. delete[] mat->mProperties;
  489. mat->mProperties = new aiMaterialProperty *[p.size() * 2];
  490. mat->mNumAllocated = static_cast<unsigned int>(p.size() * 2);
  491. }
  492. mat->mNumProperties = (unsigned int)p.size();
  493. ::memcpy(mat->mProperties, &p[0], sizeof(void *) * mat->mNumProperties);
  494. }
  495. // ------------------------------------------------------------------------------------------------
  496. void IRRImporter::GenerateGraph(Node *root, aiNode *rootOut, aiScene *scene,
  497. BatchLoader &batch,
  498. std::vector<aiMesh *> &meshes,
  499. std::vector<aiNodeAnim *> &anims,
  500. std::vector<AttachmentInfo> &attach,
  501. std::vector<aiMaterial *> &materials,
  502. unsigned int &defMatIdx) {
  503. unsigned int oldMeshSize = (unsigned int)meshes.size();
  504. //unsigned int meshTrafoAssign = 0;
  505. // Now determine the type of the node
  506. switch (root->type) {
  507. case Node::ANIMMESH:
  508. case Node::MESH: {
  509. if (!root->meshPath.length())
  510. break;
  511. // Get the loaded mesh from the scene and add it to
  512. // the list of all scenes to be attached to the
  513. // graph we're currently building
  514. aiScene *localScene = batch.GetImport(root->id);
  515. if (!localScene) {
  516. ASSIMP_LOG_ERROR("IRR: Unable to load external file: ", root->meshPath);
  517. break;
  518. }
  519. attach.emplace_back(localScene, rootOut);
  520. // Now combine the material we've loaded for this mesh
  521. // with the real materials we got from the file. As we
  522. // don't execute any pp-steps on the file, the numbers
  523. // should be equal. If they are not, we can impossibly
  524. // do this ...
  525. if (root->materials.size() != (unsigned int)localScene->mNumMaterials) {
  526. ASSIMP_LOG_WARN("IRR: Failed to match imported materials "
  527. "with the materials found in the IRR scene file");
  528. break;
  529. }
  530. for (unsigned int i = 0; i < localScene->mNumMaterials; ++i) {
  531. // Delete the old material, we don't need it anymore
  532. delete localScene->mMaterials[i];
  533. std::pair<aiMaterial *, unsigned int> &src = root->materials[i];
  534. localScene->mMaterials[i] = src.first;
  535. }
  536. // NOTE: Each mesh should have exactly one material assigned,
  537. // but we do it in a separate loop if this behavior changes
  538. // in future.
  539. for (unsigned int i = 0; i < localScene->mNumMeshes; ++i) {
  540. // Process material flags
  541. aiMesh *mesh = localScene->mMeshes[i];
  542. // If "trans_vertex_alpha" mode is enabled, search all vertex colors
  543. // and check whether they have a common alpha value. This is quite
  544. // often the case so we can simply extract it to a shared oacity
  545. // value.
  546. std::pair<aiMaterial *, unsigned int> &src = root->materials[mesh->mMaterialIndex];
  547. aiMaterial *mat = (aiMaterial *)src.first;
  548. if (mesh->HasVertexColors(0) && src.second & AI_IRRMESH_MAT_trans_vertex_alpha) {
  549. bool bdo = true;
  550. for (unsigned int a = 1; a < mesh->mNumVertices; ++a) {
  551. if (mesh->mColors[0][a].a != mesh->mColors[0][a - 1].a) {
  552. bdo = false;
  553. break;
  554. }
  555. }
  556. if (bdo) {
  557. ASSIMP_LOG_INFO("IRR: Replacing mesh vertex alpha with common opacity");
  558. for (unsigned int a = 0; a < mesh->mNumVertices; ++a)
  559. mesh->mColors[0][a].a = 1.f;
  560. mat->AddProperty(&mesh->mColors[0][0].a, 1, AI_MATKEY_OPACITY);
  561. }
  562. }
  563. // If we have a second texture coordinate set and a second texture
  564. // (either light-map, normal-map, 2layered material) we need to
  565. // setup the correct UV index for it. The texture can either
  566. // be diffuse (light-map & 2layer) or a normal map (normal & parallax)
  567. if (mesh->HasTextureCoords(1)) {
  568. int idx = 1;
  569. if (src.second & (AI_IRRMESH_MAT_solid_2layer | AI_IRRMESH_MAT_lightmap)) {
  570. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_DIFFUSE(0));
  571. } else if (src.second & AI_IRRMESH_MAT_normalmap_solid) {
  572. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_NORMALS(0));
  573. }
  574. }
  575. }
  576. } break;
  577. case Node::LIGHT:
  578. case Node::CAMERA:
  579. // We're already finished with lights and cameras
  580. break;
  581. case Node::SPHERE: {
  582. // Generate the sphere model. Our input parameter to
  583. // the sphere generation algorithm is the number of
  584. // subdivisions of each triangle - but here we have
  585. // the number of polygons on a specific axis. Just
  586. // use some hard-coded limits to approximate this ...
  587. unsigned int mul = root->spherePolyCountX * root->spherePolyCountY;
  588. if (mul < 100)
  589. mul = 2;
  590. else if (mul < 300)
  591. mul = 3;
  592. else
  593. mul = 4;
  594. meshes.push_back(StandardShapes::MakeMesh(mul,
  595. &StandardShapes::MakeSphere));
  596. // Adjust scaling
  597. root->scaling *= root->sphereRadius / 2;
  598. // Copy one output material
  599. CopyMaterial(materials, root->materials, defMatIdx, meshes.back());
  600. // Now adjust this output material - if there is a first texture
  601. // set, setup spherical UV mapping around the Y axis.
  602. SetupMapping((aiMaterial *)materials.back(), aiTextureMapping_SPHERE);
  603. } break;
  604. case Node::CUBE: {
  605. // Generate an unit cube first
  606. meshes.push_back(StandardShapes::MakeMesh(
  607. &StandardShapes::MakeHexahedron));
  608. // Adjust scaling
  609. root->scaling *= root->sphereRadius;
  610. // Copy one output material
  611. CopyMaterial(materials, root->materials, defMatIdx, meshes.back());
  612. // Now adjust this output material - if there is a first texture
  613. // set, setup cubic UV mapping
  614. SetupMapping((aiMaterial *)materials.back(), aiTextureMapping_BOX);
  615. } break;
  616. case Node::SKYBOX: {
  617. // A sky-box is defined by six materials
  618. if (root->materials.size() < 6) {
  619. ASSIMP_LOG_ERROR("IRR: There should be six materials for a skybox");
  620. break;
  621. }
  622. // copy those materials and generate 6 meshes for our new sky-box
  623. materials.reserve(materials.size() + 6);
  624. for (unsigned int i = 0; i < 6; ++i)
  625. materials.insert(materials.end(), root->materials[i].first);
  626. BuildSkybox(meshes, materials);
  627. // *************************************************************
  628. // Skyboxes will require a different code path for rendering,
  629. // so there must be a way for the user to add special support
  630. // for IRR skyboxes. We add a 'IRR.SkyBox_' prefix to the node.
  631. // *************************************************************
  632. root->name = "IRR.SkyBox_" + root->name;
  633. ASSIMP_LOG_INFO("IRR: Loading skybox, this will "
  634. "require special handling to be displayed correctly");
  635. } break;
  636. case Node::TERRAIN: {
  637. // to support terrains, we'd need to have a texture decoder
  638. ASSIMP_LOG_ERROR("IRR: Unsupported node - TERRAIN");
  639. } break;
  640. default:
  641. // DUMMY
  642. break;
  643. };
  644. // Check whether we added a mesh (or more than one ...). In this case
  645. // we'll also need to attach it to the node
  646. if (oldMeshSize != (unsigned int)meshes.size()) {
  647. rootOut->mNumMeshes = (unsigned int)meshes.size() - oldMeshSize;
  648. rootOut->mMeshes = new unsigned int[rootOut->mNumMeshes];
  649. for (unsigned int a = 0; a < rootOut->mNumMeshes; ++a) {
  650. rootOut->mMeshes[a] = oldMeshSize + a;
  651. }
  652. }
  653. // Setup the name of this node
  654. rootOut->mName.Set(root->name);
  655. // Now compute the final local transformation matrix of the
  656. // node from the given translation, rotation and scaling values.
  657. // (the rotation is given in Euler angles, XYZ order)
  658. //std::swap((float&)root->rotation.z,(float&)root->rotation.y);
  659. rootOut->mTransformation.FromEulerAnglesXYZ(AI_DEG_TO_RAD(root->rotation));
  660. // apply scaling
  661. aiMatrix4x4 &mat = rootOut->mTransformation;
  662. mat.a1 *= root->scaling.x;
  663. mat.b1 *= root->scaling.x;
  664. mat.c1 *= root->scaling.x;
  665. mat.a2 *= root->scaling.y;
  666. mat.b2 *= root->scaling.y;
  667. mat.c2 *= root->scaling.y;
  668. mat.a3 *= root->scaling.z;
  669. mat.b3 *= root->scaling.z;
  670. mat.c3 *= root->scaling.z;
  671. // apply translation
  672. mat.a4 += root->position.x;
  673. mat.b4 += root->position.y;
  674. mat.c4 += root->position.z;
  675. // now compute animations for the node
  676. ComputeAnimations(root, rootOut, anims);
  677. // Add all children recursively. First allocate enough storage
  678. // for them, then call us again
  679. rootOut->mNumChildren = (unsigned int)root->children.size();
  680. if (rootOut->mNumChildren) {
  681. rootOut->mChildren = new aiNode *[rootOut->mNumChildren];
  682. for (unsigned int i = 0; i < rootOut->mNumChildren; ++i) {
  683. aiNode *node = rootOut->mChildren[i] = new aiNode();
  684. node->mParent = rootOut;
  685. GenerateGraph(root->children[i], node, scene, batch, meshes,
  686. anims, attach, materials, defMatIdx);
  687. }
  688. }
  689. }
  690. // ------------------------------------------------------------------------------------------------
  691. // Imports the given file into the given scene structure.
  692. void IRRImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  693. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
  694. // Check whether we can read from the file
  695. if (file.get() == nullptr) {
  696. throw DeadlyImportError("Failed to open IRR file ", pFile);
  697. }
  698. // Construct the irrXML parser
  699. XmlParser st;
  700. if (!st.parse( file.get() )) {
  701. throw DeadlyImportError("XML parse error while loading IRR file ", pFile);
  702. }
  703. pugi::xml_node rootElement = st.getRootNode();
  704. // The root node of the scene
  705. Node *root = new Node(Node::DUMMY);
  706. root->parent = nullptr;
  707. root->name = "<IRRSceneRoot>";
  708. // Current node parent
  709. Node *curParent = root;
  710. // Scene-graph node we're currently working on
  711. Node *curNode = nullptr;
  712. // List of output cameras
  713. std::vector<aiCamera *> cameras;
  714. // List of output lights
  715. std::vector<aiLight *> lights;
  716. // Batch loader used to load external models
  717. BatchLoader batch(pIOHandler);
  718. //batch.SetBasePath(pFile);
  719. cameras.reserve(5);
  720. lights.reserve(5);
  721. bool inMaterials = false, inAnimator = false;
  722. unsigned int guessedAnimCnt = 0, guessedMeshCnt = 0, guessedMatCnt = 0;
  723. // Parse the XML file
  724. //while (reader->read()) {
  725. for (pugi::xml_node child : rootElement.children())
  726. switch (child.type()) {
  727. case pugi::node_element:
  728. if (!ASSIMP_stricmp(child.name(), "node")) {
  729. // ***********************************************************************
  730. /* What we're going to do with the node depends
  731. * on its type:
  732. *
  733. * "mesh" - Load a mesh from an external file
  734. * "cube" - Generate a cube
  735. * "skybox" - Generate a skybox
  736. * "light" - A light source
  737. * "sphere" - Generate a sphere mesh
  738. * "animatedMesh" - Load an animated mesh from an external file
  739. * and join its animation channels with ours.
  740. * "empty" - A dummy node
  741. * "camera" - A camera
  742. * "terrain" - a terrain node (data comes from a heightmap)
  743. * "billboard", ""
  744. *
  745. * Each of these nodes can be animated and all can have multiple
  746. * materials assigned (except lights, cameras and dummies, of course).
  747. */
  748. // ***********************************************************************
  749. //const char *sz = reader->getAttributeValueSafe("type");
  750. pugi::xml_attribute attrib = child.attribute("type");
  751. Node *nd;
  752. if (!ASSIMP_stricmp(attrib.name(), "mesh") || !ASSIMP_stricmp(attrib.name(), "octTree")) {
  753. // OctTree's and meshes are treated equally
  754. nd = new Node(Node::MESH);
  755. } else if (!ASSIMP_stricmp(attrib.name(), "cube")) {
  756. nd = new Node(Node::CUBE);
  757. ++guessedMeshCnt;
  758. } else if (!ASSIMP_stricmp(attrib.name(), "skybox")) {
  759. nd = new Node(Node::SKYBOX);
  760. guessedMeshCnt += 6;
  761. } else if (!ASSIMP_stricmp(attrib.name(), "camera")) {
  762. nd = new Node(Node::CAMERA);
  763. // Setup a temporary name for the camera
  764. aiCamera *cam = new aiCamera();
  765. cam->mName.Set(nd->name);
  766. cameras.push_back(cam);
  767. } else if (!ASSIMP_stricmp(attrib.name(), "light")) {
  768. nd = new Node(Node::LIGHT);
  769. // Setup a temporary name for the light
  770. aiLight *cam = new aiLight();
  771. cam->mName.Set(nd->name);
  772. lights.push_back(cam);
  773. } else if (!ASSIMP_stricmp(attrib.name(), "sphere")) {
  774. nd = new Node(Node::SPHERE);
  775. ++guessedMeshCnt;
  776. } else if (!ASSIMP_stricmp(attrib.name(), "animatedMesh")) {
  777. nd = new Node(Node::ANIMMESH);
  778. } else if (!ASSIMP_stricmp(attrib.name(), "empty")) {
  779. nd = new Node(Node::DUMMY);
  780. } else if (!ASSIMP_stricmp(attrib.name(), "terrain")) {
  781. nd = new Node(Node::TERRAIN);
  782. } else if (!ASSIMP_stricmp(attrib.name(), "billBoard")) {
  783. // We don't support billboards, so ignore them
  784. ASSIMP_LOG_ERROR("IRR: Billboards are not supported by Assimp");
  785. nd = new Node(Node::DUMMY);
  786. } else {
  787. ASSIMP_LOG_WARN("IRR: Found unknown node: ", attrib.name());
  788. /* We skip the contents of nodes we don't know.
  789. * We parse the transformation and all animators
  790. * and skip the rest.
  791. */
  792. nd = new Node(Node::DUMMY);
  793. }
  794. /* Attach the newly created node to the scene-graph
  795. */
  796. curNode = nd;
  797. nd->parent = curParent;
  798. curParent->children.push_back(nd);
  799. } else if (!ASSIMP_stricmp(child.name(), "materials")) {
  800. inMaterials = true;
  801. } else if (!ASSIMP_stricmp(child.name(), "animators")) {
  802. inAnimator = true;
  803. } else if (!ASSIMP_stricmp(child.name(), "attributes")) {
  804. // We should have a valid node here
  805. // FIX: no ... the scene root node is also contained in an attributes block
  806. if (!curNode) {
  807. continue;
  808. }
  809. Animator *curAnim = nullptr;
  810. // Materials can occur for nearly any type of node
  811. if (inMaterials && curNode->type != Node::DUMMY) {
  812. // This is a material description - parse it!
  813. curNode->materials.emplace_back();
  814. std::pair<aiMaterial *, unsigned int> &p = curNode->materials.back();
  815. p.first = ParseMaterial(p.second);
  816. ++guessedMatCnt;
  817. continue;
  818. } else if (inAnimator) {
  819. // This is an animation path - add a new animator
  820. // to the list.
  821. curNode->animators.emplace_back();
  822. curAnim = &curNode->animators.back();
  823. ++guessedAnimCnt;
  824. }
  825. /* Parse all elements in the attributes block
  826. * and process them.
  827. */
  828. // while (reader->read()) {
  829. for (pugi::xml_node attrib : child.children()) {
  830. if (attrib.type() == pugi::node_element) {
  831. //if (reader->getNodeType() == EXN_ELEMENT) {
  832. //if (!ASSIMP_stricmp(reader->getNodeName(), "vector3d")) {
  833. if (!ASSIMP_stricmp(attrib.name(), "vector3d")) {
  834. VectorProperty prop;
  835. ReadVectorProperty(prop);
  836. if (inAnimator) {
  837. if (curAnim->type == Animator::ROTATION && prop.name == "Rotation") {
  838. // We store the rotation euler angles in 'direction'
  839. curAnim->direction = prop.value;
  840. } else if (curAnim->type == Animator::FOLLOW_SPLINE) {
  841. // Check whether the vector follows the PointN naming scheme,
  842. // here N is the ONE-based index of the point
  843. if (prop.name.length() >= 6 && prop.name.substr(0, 5) == "Point") {
  844. // Add a new key to the list
  845. curAnim->splineKeys.emplace_back();
  846. aiVectorKey &key = curAnim->splineKeys.back();
  847. // and parse its properties
  848. key.mValue = prop.value;
  849. key.mTime = strtoul10(&prop.name[5]);
  850. }
  851. } else if (curAnim->type == Animator::FLY_CIRCLE) {
  852. if (prop.name == "Center") {
  853. curAnim->circleCenter = prop.value;
  854. } else if (prop.name == "Direction") {
  855. curAnim->direction = prop.value;
  856. // From Irrlicht's source - a workaround for backward compatibility with Irrlicht 1.1
  857. if (curAnim->direction == aiVector3D()) {
  858. curAnim->direction = aiVector3D(0.f, 1.f, 0.f);
  859. } else
  860. curAnim->direction.Normalize();
  861. }
  862. } else if (curAnim->type == Animator::FLY_STRAIGHT) {
  863. if (prop.name == "Start") {
  864. // We reuse the field here
  865. curAnim->circleCenter = prop.value;
  866. } else if (prop.name == "End") {
  867. // We reuse the field here
  868. curAnim->direction = prop.value;
  869. }
  870. }
  871. } else {
  872. if (prop.name == "Position") {
  873. curNode->position = prop.value;
  874. } else if (prop.name == "Rotation") {
  875. curNode->rotation = prop.value;
  876. } else if (prop.name == "Scale") {
  877. curNode->scaling = prop.value;
  878. } else if (Node::CAMERA == curNode->type) {
  879. aiCamera *cam = cameras.back();
  880. if (prop.name == "Target") {
  881. cam->mLookAt = prop.value;
  882. } else if (prop.name == "UpVector") {
  883. cam->mUp = prop.value;
  884. }
  885. }
  886. }
  887. //} else if (!ASSIMP_stricmp(reader->getNodeName(), "bool")) {
  888. } else if (!ASSIMP_stricmp(attrib.name(), "bool")) {
  889. BoolProperty prop;
  890. ReadBoolProperty(prop);
  891. if (inAnimator && curAnim->type == Animator::FLY_CIRCLE && prop.name == "Loop") {
  892. curAnim->loop = prop.value;
  893. }
  894. //} else if (!ASSIMP_stricmp(reader->getNodeName(), "float")) {
  895. } else if (!ASSIMP_stricmp(attrib.name(), "float")) {
  896. FloatProperty prop;
  897. ReadFloatProperty(prop);
  898. if (inAnimator) {
  899. // The speed property exists for several animators
  900. if (prop.name == "Speed") {
  901. curAnim->speed = prop.value;
  902. } else if (curAnim->type == Animator::FLY_CIRCLE && prop.name == "Radius") {
  903. curAnim->circleRadius = prop.value;
  904. } else if (curAnim->type == Animator::FOLLOW_SPLINE && prop.name == "Tightness") {
  905. curAnim->tightness = prop.value;
  906. }
  907. } else {
  908. if (prop.name == "FramesPerSecond" && Node::ANIMMESH == curNode->type) {
  909. curNode->framesPerSecond = prop.value;
  910. } else if (Node::CAMERA == curNode->type) {
  911. /* This is the vertical, not the horizontal FOV.
  912. * We need to compute the right FOV from the
  913. * screen aspect which we don't know yet.
  914. */
  915. if (prop.name == "Fovy") {
  916. cameras.back()->mHorizontalFOV = prop.value;
  917. } else if (prop.name == "Aspect") {
  918. cameras.back()->mAspect = prop.value;
  919. } else if (prop.name == "ZNear") {
  920. cameras.back()->mClipPlaneNear = prop.value;
  921. } else if (prop.name == "ZFar") {
  922. cameras.back()->mClipPlaneFar = prop.value;
  923. }
  924. } else if (Node::LIGHT == curNode->type) {
  925. /* Additional light information
  926. */
  927. if (prop.name == "Attenuation") {
  928. lights.back()->mAttenuationLinear = prop.value;
  929. } else if (prop.name == "OuterCone") {
  930. lights.back()->mAngleOuterCone = AI_DEG_TO_RAD(prop.value);
  931. } else if (prop.name == "InnerCone") {
  932. lights.back()->mAngleInnerCone = AI_DEG_TO_RAD(prop.value);
  933. }
  934. }
  935. // radius of the sphere to be generated -
  936. // or alternatively, size of the cube
  937. else if ((Node::SPHERE == curNode->type && prop.name == "Radius") || (Node::CUBE == curNode->type && prop.name == "Size")) {
  938. curNode->sphereRadius = prop.value;
  939. }
  940. }
  941. //} else if (!ASSIMP_stricmp(reader->getNodeName(), "int")) {
  942. } else if (!ASSIMP_stricmp(attrib.name(), "int")) {
  943. IntProperty prop;
  944. ReadIntProperty(prop);
  945. if (inAnimator) {
  946. if (curAnim->type == Animator::FLY_STRAIGHT && prop.name == "TimeForWay") {
  947. curAnim->timeForWay = prop.value;
  948. }
  949. } else {
  950. // sphere polygon numbers in each direction
  951. if (Node::SPHERE == curNode->type) {
  952. if (prop.name == "PolyCountX") {
  953. curNode->spherePolyCountX = prop.value;
  954. } else if (prop.name == "PolyCountY") {
  955. curNode->spherePolyCountY = prop.value;
  956. }
  957. }
  958. }
  959. //} else if (!ASSIMP_stricmp(reader->getNodeName(), "string") || !ASSIMP_stricmp(reader->getNodeName(), "enum")) {
  960. } else if (!ASSIMP_stricmp(attrib.name(), "string") || !ASSIMP_stricmp(attrib.name(), "enum")) {
  961. StringProperty prop;
  962. ReadStringProperty(prop);
  963. if (prop.value.length()) {
  964. if (prop.name == "Name") {
  965. curNode->name = prop.value;
  966. /* If we're either a camera or a light source
  967. * we need to update the name in the aiLight/
  968. * aiCamera structure, too.
  969. */
  970. if (Node::CAMERA == curNode->type) {
  971. cameras.back()->mName.Set(prop.value);
  972. } else if (Node::LIGHT == curNode->type) {
  973. lights.back()->mName.Set(prop.value);
  974. }
  975. } else if (Node::LIGHT == curNode->type && "LightType" == prop.name) {
  976. if (prop.value == "Spot")
  977. lights.back()->mType = aiLightSource_SPOT;
  978. else if (prop.value == "Point")
  979. lights.back()->mType = aiLightSource_POINT;
  980. else if (prop.value == "Directional")
  981. lights.back()->mType = aiLightSource_DIRECTIONAL;
  982. else {
  983. // We won't pass the validation with aiLightSourceType_UNDEFINED,
  984. // so we remove the light and replace it with a silly dummy node
  985. delete lights.back();
  986. lights.pop_back();
  987. curNode->type = Node::DUMMY;
  988. ASSIMP_LOG_ERROR("Ignoring light of unknown type: ", prop.value);
  989. }
  990. } else if ((prop.name == "Mesh" && Node::MESH == curNode->type) ||
  991. Node::ANIMMESH == curNode->type) {
  992. /* This is the file name of the mesh - either
  993. * animated or not. We need to make sure we setup
  994. * the correct post-processing settings here.
  995. */
  996. unsigned int pp = 0;
  997. BatchLoader::PropertyMap map;
  998. /* If the mesh is a static one remove all animations from the impor data
  999. */
  1000. if (Node::ANIMMESH != curNode->type) {
  1001. pp |= aiProcess_RemoveComponent;
  1002. SetGenericProperty<int>(map.ints, AI_CONFIG_PP_RVC_FLAGS,
  1003. aiComponent_ANIMATIONS | aiComponent_BONEWEIGHTS);
  1004. }
  1005. /* TODO: maybe implement the protection against recursive
  1006. * loading calls directly in BatchLoader? The current
  1007. * implementation is not absolutely safe. A LWS and an IRR
  1008. * file referencing each other *could* cause the system to
  1009. * recurse forever.
  1010. */
  1011. const std::string extension = GetExtension(prop.value);
  1012. if ("irr" == extension) {
  1013. ASSIMP_LOG_ERROR("IRR: Can't load another IRR file recursively");
  1014. } else {
  1015. curNode->id = batch.AddLoadRequest(prop.value, pp, &map);
  1016. curNode->meshPath = prop.value;
  1017. }
  1018. } else if (inAnimator && prop.name == "Type") {
  1019. // type of the animator
  1020. if (prop.value == "rotation") {
  1021. curAnim->type = Animator::ROTATION;
  1022. } else if (prop.value == "flyCircle") {
  1023. curAnim->type = Animator::FLY_CIRCLE;
  1024. } else if (prop.value == "flyStraight") {
  1025. curAnim->type = Animator::FLY_CIRCLE;
  1026. } else if (prop.value == "followSpline") {
  1027. curAnim->type = Animator::FOLLOW_SPLINE;
  1028. } else {
  1029. ASSIMP_LOG_WARN("IRR: Ignoring unknown animator: ", prop.value);
  1030. curAnim->type = Animator::UNKNOWN;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. //} else if (reader->getNodeType() == EXN_ELEMENT_END && !ASSIMP_stricmp(reader->getNodeName(), "attributes")) {
  1036. } else if (attrib.type() == pugi::node_null && !ASSIMP_stricmp(attrib.name(), "attributes")) {
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. break;
  1042. /*case EXN_ELEMENT_END:
  1043. // If we reached the end of a node, we need to continue processing its parent
  1044. if (!ASSIMP_stricmp(reader->getNodeName(), "node")) {
  1045. if (!curNode) {
  1046. // currently is no node set. We need to go
  1047. // back in the node hierarchy
  1048. if (!curParent) {
  1049. curParent = root;
  1050. ASSIMP_LOG_ERROR("IRR: Too many closing <node> elements");
  1051. } else
  1052. curParent = curParent->parent;
  1053. } else
  1054. curNode = nullptr;
  1055. }
  1056. // clear all flags
  1057. else if (!ASSIMP_stricmp(reader->getNodeName(), "materials")) {
  1058. inMaterials = false;
  1059. } else if (!ASSIMP_stricmp(reader->getNodeName(), "animators")) {
  1060. inAnimator = false;
  1061. }
  1062. break;*/
  1063. default:
  1064. // GCC complains that not all enumeration values are handled
  1065. break;
  1066. }
  1067. //}
  1068. // Now iterate through all cameras and compute their final (horizontal) FOV
  1069. for (aiCamera *cam : cameras) {
  1070. // screen aspect could be missing
  1071. if (cam->mAspect) {
  1072. cam->mHorizontalFOV *= cam->mAspect;
  1073. } else {
  1074. ASSIMP_LOG_WARN("IRR: Camera aspect is not given, can't compute horizontal FOV");
  1075. }
  1076. }
  1077. batch.LoadAll();
  1078. // Allocate a temporary scene data structure
  1079. aiScene *tempScene = new aiScene();
  1080. tempScene->mRootNode = new aiNode();
  1081. tempScene->mRootNode->mName.Set("<IRRRoot>");
  1082. // Copy the cameras to the output array
  1083. if (!cameras.empty()) {
  1084. tempScene->mNumCameras = (unsigned int)cameras.size();
  1085. tempScene->mCameras = new aiCamera *[tempScene->mNumCameras];
  1086. ::memcpy(tempScene->mCameras, &cameras[0], sizeof(void *) * tempScene->mNumCameras);
  1087. }
  1088. // Copy the light sources to the output array
  1089. if (!lights.empty()) {
  1090. tempScene->mNumLights = (unsigned int)lights.size();
  1091. tempScene->mLights = new aiLight *[tempScene->mNumLights];
  1092. ::memcpy(tempScene->mLights, &lights[0], sizeof(void *) * tempScene->mNumLights);
  1093. }
  1094. // temporary data
  1095. std::vector<aiNodeAnim *> anims;
  1096. std::vector<aiMaterial *> materials;
  1097. std::vector<AttachmentInfo> attach;
  1098. std::vector<aiMesh *> meshes;
  1099. // try to guess how much storage we'll need
  1100. anims.reserve(guessedAnimCnt + (guessedAnimCnt >> 2));
  1101. meshes.reserve(guessedMeshCnt + (guessedMeshCnt >> 2));
  1102. materials.reserve(guessedMatCnt + (guessedMatCnt >> 2));
  1103. // Now process our scene-graph recursively: generate final
  1104. // meshes and generate animation channels for all nodes.
  1105. unsigned int defMatIdx = UINT_MAX;
  1106. GenerateGraph(root, tempScene->mRootNode, tempScene,
  1107. batch, meshes, anims, attach, materials, defMatIdx);
  1108. if (!anims.empty()) {
  1109. tempScene->mNumAnimations = 1;
  1110. tempScene->mAnimations = new aiAnimation *[tempScene->mNumAnimations];
  1111. aiAnimation *an = tempScene->mAnimations[0] = new aiAnimation();
  1112. // ***********************************************************
  1113. // This is only the global animation channel of the scene.
  1114. // If there are animated models, they will have separate
  1115. // animation channels in the scene. To display IRR scenes
  1116. // correctly, users will need to combine the global anim
  1117. // channel with all the local animations they want to play
  1118. // ***********************************************************
  1119. an->mName.Set("Irr_GlobalAnimChannel");
  1120. // copy all node animation channels to the global channel
  1121. an->mNumChannels = (unsigned int)anims.size();
  1122. an->mChannels = new aiNodeAnim *[an->mNumChannels];
  1123. ::memcpy(an->mChannels, &anims[0], sizeof(void *) * an->mNumChannels);
  1124. }
  1125. if (!meshes.empty()) {
  1126. // copy all meshes to the temporary scene
  1127. tempScene->mNumMeshes = (unsigned int)meshes.size();
  1128. tempScene->mMeshes = new aiMesh *[tempScene->mNumMeshes];
  1129. ::memcpy(tempScene->mMeshes, &meshes[0], tempScene->mNumMeshes * sizeof(void *));
  1130. }
  1131. // Copy all materials to the output array
  1132. if (!materials.empty()) {
  1133. tempScene->mNumMaterials = (unsigned int)materials.size();
  1134. tempScene->mMaterials = new aiMaterial *[tempScene->mNumMaterials];
  1135. ::memcpy(tempScene->mMaterials, &materials[0], sizeof(void *) * tempScene->mNumMaterials);
  1136. }
  1137. // Now merge all sub scenes and attach them to the correct
  1138. // attachment points in the scenegraph.
  1139. SceneCombiner::MergeScenes(&pScene, tempScene, attach,
  1140. AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES | (!configSpeedFlag ? (
  1141. AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY | AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES) :
  1142. 0));
  1143. // If we have no meshes | no materials now set the INCOMPLETE
  1144. // scene flag. This is necessary if we failed to load all
  1145. // models from external files
  1146. if (!pScene->mNumMeshes || !pScene->mNumMaterials) {
  1147. ASSIMP_LOG_WARN("IRR: No meshes loaded, setting AI_SCENE_FLAGS_INCOMPLETE");
  1148. pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  1149. }
  1150. // Finished ... everything destructs automatically and all
  1151. // temporary scenes have already been deleted by MergeScenes()
  1152. delete root;
  1153. }
  1154. #endif // !! ASSIMP_BUILD_NO_IRR_IMPORTER