IRRLoader.cpp 42 KB

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