IRRLoader.cpp 48 KB

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