IRRLoader.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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::GenerateGraph(Node* root,aiNode* rootOut ,aiScene* scene,
  86. BatchLoader& batch,
  87. std::vector<aiMesh*>& meshes,
  88. std::vector<aiNodeAnim*>& anims,
  89. std::vector<AttachmentInfo>& attach)
  90. {
  91. // Setup the name of this node
  92. rootOut->mName.Set(root->name);
  93. unsigned int oldMeshSize = (unsigned int)meshes.size();
  94. // Now determine the type of the node
  95. switch (root->type)
  96. {
  97. case Node::ANIMMESH:
  98. case Node::MESH:
  99. {
  100. // get the loaded mesh from the scene and add it to
  101. // the list of all scenes to be attached to the
  102. // graph we're currently building
  103. aiScene* scene = batch.GetImport(root->meshPath);
  104. if (!scene)
  105. {
  106. DefaultLogger::get()->error("IRR: Unable to load external file: " + root->meshPath);
  107. break;
  108. }
  109. attach.push_back(AttachmentInfo(scene,rootOut));
  110. // now combine the material we've loaded for this mesh
  111. // with the real meshes we got from the file. As we
  112. // don't execute any pp-steps on the file, the numbers
  113. // should be equal. If they are not, we can impossibly
  114. // do this ...
  115. if (root->materials.size() != (unsigned int)scene->mNumMaterials)
  116. {
  117. DefaultLogger::get()->warn("IRR: Failed to match imported materials "
  118. "with the materials found in the IRR scene file");
  119. break;
  120. }
  121. for (unsigned int i = 0; i < scene->mNumMaterials;++i)
  122. {
  123. // delete the old material
  124. delete scene->mMaterials[i];
  125. std::pair<aiMaterial*, unsigned int>& src = root->materials[i];
  126. scene->mMaterials[i] = src.first;
  127. // Process material flags (e.g. lightmapping)
  128. }
  129. }
  130. break;
  131. case Node::LIGHT:
  132. case Node::CAMERA:
  133. // We're already finished with lights and cameras
  134. break;
  135. case Node::SPHERE:
  136. {
  137. // generate the sphere model. Our input parameter to
  138. // the sphere generation algorithm is the number of
  139. // subdivisions of each triangle - but here we have
  140. // the number of poylgons on a specific axis. Just
  141. // use some limits ...
  142. unsigned int mul = root->spherePolyCountX*root->spherePolyCountY;
  143. if (mul < 100)mul = 2;
  144. else if (mul < 300)mul = 3;
  145. else mul = 4;
  146. meshes.push_back(StandardShapes::MakeMesh(mul,&StandardShapes::MakeSphere));
  147. // Adjust scaling
  148. root->scaling *= root->sphereRadius;
  149. }
  150. break;
  151. case Node::CUBE:
  152. case Node::SKYBOX:
  153. {
  154. // Skyboxes and normal cubes - generate the cube first
  155. meshes.push_back(StandardShapes::MakeMesh(&StandardShapes::MakeHexahedron));
  156. // Adjust scaling
  157. root->scaling *= root->sphereRadius;
  158. }
  159. break;
  160. case Node::TERRAIN:
  161. {
  162. }
  163. break;
  164. };
  165. // Check whether we added a mesh. In this case we'll also
  166. // need to attach it to the node
  167. if (oldMeshSize != (unsigned int) meshes.size())
  168. {
  169. rootOut->mNumMeshes = 1;
  170. rootOut->mMeshes = new unsigned int[1];
  171. rootOut->mMeshes[0] = oldMeshSize;
  172. }
  173. // Now compute the final local transformation matrix of the
  174. // node from the given translation, rotation and scaling values.
  175. // (the rotation is given in Euler angles, XYZ order)
  176. aiMatrix4x4 m;
  177. rootOut->mTransformation = aiMatrix4x4::RotationX(AI_DEG_TO_RAD(root->rotation.x),m)
  178. * aiMatrix4x4::RotationY(AI_DEG_TO_RAD(root->rotation.y),m)
  179. * aiMatrix4x4::RotationZ(AI_DEG_TO_RAD(root->rotation.z),m);
  180. // apply scaling
  181. aiMatrix4x4& mat = rootOut->mTransformation;
  182. mat.a1 *= root->scaling.x;
  183. mat.b1 *= root->scaling.x;
  184. mat.c1 *= root->scaling.x;
  185. mat.a2 *= root->scaling.y;
  186. mat.b2 *= root->scaling.y;
  187. mat.c2 *= root->scaling.y;
  188. mat.a3 *= root->scaling.z;
  189. mat.b3 *= root->scaling.z;
  190. mat.c3 *= root->scaling.z;
  191. // apply translation
  192. mat.a4 = root->position.x;
  193. mat.b4 = root->position.y;
  194. mat.c4 = root->position.z;
  195. // Add all children recursively. First allocate enough storage
  196. // for them, then call us again
  197. rootOut->mNumChildren = (unsigned int)root->children.size();
  198. if (rootOut->mNumChildren)
  199. {
  200. rootOut->mChildren = new aiNode*[rootOut->mNumChildren];
  201. for (unsigned int i = 0; i < rootOut->mNumChildren;++i)
  202. {
  203. aiNode* node = rootOut->mChildren[i] = new aiNode();
  204. node->mParent = rootOut;
  205. GenerateGraph(root->children[i],node,scene,batch,meshes,anims,attach);
  206. }
  207. }
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. // Imports the given file into the given scene structure.
  211. void IRRImporter::InternReadFile( const std::string& pFile,
  212. aiScene* pScene, IOSystem* pIOHandler)
  213. {
  214. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  215. // Check whether we can read from the file
  216. if( file.get() == NULL)
  217. throw new ImportErrorException( "Failed to open IRR file " + pFile + "");
  218. // Construct the irrXML parser
  219. CIrrXML_IOStreamReader st(file.get());
  220. reader = createIrrXMLReader((IFileReadCallBack*) &st);
  221. // The root node of the scene
  222. Node* root = new Node(Node::DUMMY);
  223. root->parent = NULL;
  224. // Current node parent
  225. Node* curParent = root;
  226. // Scenegraph node we're currently working on
  227. Node* curNode = NULL;
  228. // List of output cameras
  229. std::vector<aiCamera*> cameras;
  230. // List of output lights
  231. std::vector<aiLight*> lights;
  232. // Batch loader used to load external models
  233. BatchLoader batch(pIOHandler);
  234. cameras.reserve(5);
  235. lights.reserve(5);
  236. bool inMaterials = false, inAnimator = false;
  237. unsigned int guessedAnimCnt = 0, guessedMeshCnt = 0;
  238. // Parse the XML file
  239. while (reader->read())
  240. {
  241. switch (reader->getNodeType())
  242. {
  243. case EXN_ELEMENT:
  244. if (!ASSIMP_stricmp(reader->getNodeName(),"node"))
  245. {
  246. /* What we're going to do with the node depends
  247. * on its type:
  248. *
  249. * "mesh" - Load a mesh from an external file
  250. * "cube" - Generate a cube
  251. * "skybox" - Generate a skybox
  252. * "light" - A light source
  253. * "sphere" - Generate a sphere mesh
  254. * "animatedMesh" - Load an animated mesh from an external file
  255. * and join its animation channels with ours.
  256. * "empty" - A dummy node
  257. * "camera" - A camera
  258. *
  259. * Each of these nodes can be animated.
  260. */
  261. const char* sz = reader->getAttributeValueSafe("type");
  262. Node* nd;
  263. if (!ASSIMP_stricmp(sz,"mesh"))
  264. {
  265. nd = new Node(Node::MESH);
  266. }
  267. else if (!ASSIMP_stricmp(sz,"cube"))
  268. {
  269. nd = new Node(Node::CUBE);
  270. ++guessedMeshCnt;
  271. // meshes.push_back(StandardShapes::MakeMesh(&StandardShapes::MakeHexahedron));
  272. }
  273. else if (!ASSIMP_stricmp(sz,"skybox"))
  274. {
  275. nd = new Node(Node::SKYBOX);
  276. ++guessedMeshCnt;
  277. }
  278. else if (!ASSIMP_stricmp(sz,"camera"))
  279. {
  280. nd = new Node(Node::CAMERA);
  281. // Setup a temporary name for the camera
  282. aiCamera* cam = new aiCamera();
  283. cam->mName.Set( nd->name );
  284. cameras.push_back(cam);
  285. }
  286. else if (!ASSIMP_stricmp(sz,"light"))
  287. {
  288. nd = new Node(Node::LIGHT);
  289. // Setup a temporary name for the light
  290. aiLight* cam = new aiLight();
  291. cam->mName.Set( nd->name );
  292. lights.push_back(cam);
  293. }
  294. else if (!ASSIMP_stricmp(sz,"sphere"))
  295. {
  296. nd = new Node(Node::SPHERE);
  297. ++guessedMeshCnt;
  298. }
  299. else if (!ASSIMP_stricmp(sz,"animatedMesh"))
  300. {
  301. nd = new Node(Node::ANIMMESH);
  302. }
  303. else if (!ASSIMP_stricmp(sz,"empty"))
  304. {
  305. nd = new Node(Node::DUMMY);
  306. }
  307. else
  308. {
  309. DefaultLogger::get()->warn("IRR: Found unknown node: " + std::string(sz));
  310. /* We skip the contents of nodes we don't know.
  311. * We parse the transformation and all animators
  312. * and skip the rest.
  313. */
  314. nd = new Node(Node::DUMMY);
  315. }
  316. /* Attach the newly created node to the scenegraph
  317. */
  318. curNode = nd;
  319. nd->parent = curParent;
  320. curParent->children.push_back(nd);
  321. }
  322. else if (!ASSIMP_stricmp(reader->getNodeName(),"materials"))
  323. {
  324. inMaterials = true;
  325. }
  326. else if (!ASSIMP_stricmp(reader->getNodeName(),"animators"))
  327. {
  328. inAnimator = true;
  329. }
  330. else if (!ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  331. {
  332. /* We should have a valid node here
  333. */
  334. if (!curNode)
  335. {
  336. DefaultLogger::get()->error("IRR: Encountered <attributes> element, but "
  337. "there is no node active");
  338. continue;
  339. }
  340. Animator* curAnim = NULL;
  341. if (inMaterials && curNode->type == Node::ANIMMESH ||
  342. curNode->type == Node::MESH )
  343. {
  344. /* This is a material description - parse it!
  345. */
  346. curNode->materials.push_back(std::pair< aiMaterial*, unsigned int > () );
  347. std::pair< aiMaterial*, unsigned int >& p = curNode->materials.back();
  348. p.first = ParseMaterial(p.second);
  349. continue;
  350. }
  351. else if (inAnimator)
  352. {
  353. /* This is an animation path - add a new animator
  354. * to the list.
  355. */
  356. curNode->animators.push_back(Animator());
  357. curAnim = & curNode->animators.back();
  358. ++guessedAnimCnt;
  359. }
  360. /* Parse all elements in the attributes block
  361. * and process them.
  362. */
  363. while (reader->read())
  364. {
  365. if (reader->getNodeType() == EXN_ELEMENT)
  366. {
  367. if (!ASSIMP_stricmp(reader->getNodeName(),"vector3d"))
  368. {
  369. VectorProperty prop;
  370. ReadVectorProperty(prop);
  371. // Convert to our coordinate system
  372. std::swap( (float&)prop.value.z, (float&)prop.value.y );
  373. prop.value.y *= -1.f;
  374. if (inAnimator)
  375. {
  376. if (curAnim->type == Animator::ROTATION && prop.name == "Rotation")
  377. {
  378. // We store the rotation euler angles in 'direction'
  379. curAnim->direction = prop.value;
  380. }
  381. else if (curAnim->type == Animator::FOLLOW_SPLINE)
  382. {
  383. // Check whether the vector follows the PointN naming scheme,
  384. // here N is the ONE-based index of the point
  385. if (prop.name.length() >= 6 && prop.name.substr(0,5) == "Point")
  386. {
  387. // Add a new key to the list
  388. curAnim->splineKeys.push_back(aiVectorKey());
  389. aiVectorKey& key = curAnim->splineKeys.back();
  390. // and parse its properties
  391. key.mValue = prop.value;
  392. key.mTime = strtol10(&prop.name.c_str()[5]);
  393. }
  394. }
  395. else if (curAnim->type == Animator::FLY_CIRCLE)
  396. {
  397. if (prop.name == "Center")
  398. {
  399. curAnim->circleCenter = prop.value;
  400. }
  401. else if (prop.name == "Direction")
  402. {
  403. curAnim->direction = prop.value;
  404. }
  405. }
  406. else if (curAnim->type == Animator::FLY_STRAIGHT)
  407. {
  408. if (prop.name == "Start")
  409. {
  410. // We reuse the field here
  411. curAnim->circleCenter = prop.value;
  412. }
  413. else if (prop.name == "End")
  414. {
  415. // We reuse the field here
  416. curAnim->direction = prop.value;
  417. }
  418. }
  419. }
  420. else
  421. {
  422. if (prop.name == "Position")
  423. {
  424. curNode->position = prop.value;
  425. }
  426. else if (prop.name == "Rotation")
  427. {
  428. curNode->rotation = prop.value;
  429. }
  430. else if (prop.name == "Scale")
  431. {
  432. curNode->scaling = prop.value;
  433. }
  434. else if (Node::CAMERA == curNode->type)
  435. {
  436. aiCamera* cam = cameras.back();
  437. if (prop.name == "Target")
  438. {
  439. cam->mLookAt = prop.value;
  440. }
  441. else if (prop.name == "UpVector")
  442. {
  443. cam->mUp = prop.value;
  444. }
  445. }
  446. }
  447. }
  448. else if (!ASSIMP_stricmp(reader->getNodeName(),"bool"))
  449. {
  450. BoolProperty prop;
  451. ReadBoolProperty(prop);
  452. if (inAnimator && curAnim->type == Animator::FLY_CIRCLE && prop.name == "Loop")
  453. {
  454. curAnim->loop = prop.value;
  455. }
  456. }
  457. else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
  458. {
  459. FloatProperty prop;
  460. ReadFloatProperty(prop);
  461. if (inAnimator)
  462. {
  463. // The speed property exists for several animators
  464. if (prop.name == "Speed")
  465. {
  466. curAnim->speed = prop.value;
  467. }
  468. else if (curAnim->type == Animator::FLY_CIRCLE && prop.name == "Radius")
  469. {
  470. curAnim->circleRadius = prop.value;
  471. }
  472. else if (curAnim->type == Animator::FOLLOW_SPLINE && prop.name == "Tightness")
  473. {
  474. curAnim->tightness = prop.value;
  475. }
  476. }
  477. else
  478. {
  479. if (prop.name == "FramesPerSecond" &&
  480. Node::ANIMMESH == curNode->type)
  481. {
  482. curNode->framesPerSecond = prop.value;
  483. }
  484. else if (Node::CAMERA == curNode->type)
  485. {
  486. /* This is the vertical, not the horizontal FOV.
  487. * We need to compute the right FOV from the
  488. * screen aspect which we don't know yet.
  489. */
  490. if (prop.name == "Fovy")
  491. {
  492. cameras.back()->mHorizontalFOV = prop.value;
  493. }
  494. else if (prop.name == "Aspect")
  495. {
  496. cameras.back()->mAspect = prop.value;
  497. }
  498. else if (prop.name == "ZNear")
  499. {
  500. cameras.back()->mClipPlaneNear = prop.value;
  501. }
  502. else if (prop.name == "ZFar")
  503. {
  504. cameras.back()->mClipPlaneFar = prop.value;
  505. }
  506. }
  507. else if (Node::LIGHT == curNode->type)
  508. {
  509. /* Additional light information
  510. */
  511. if (prop.name == "Attenuation")
  512. {
  513. lights.back()->mAttenuationLinear = prop.value;
  514. }
  515. else if (prop.name == "OuterCone")
  516. {
  517. lights.back()->mAngleOuterCone = AI_DEG_TO_RAD( prop.value );
  518. }
  519. else if (prop.name == "InnerCone")
  520. {
  521. lights.back()->mAngleInnerCone = AI_DEG_TO_RAD( prop.value );
  522. }
  523. }
  524. // radius of the sphere to be generated -
  525. // or alternatively, size of the cube
  526. else if (Node::SPHERE == curNode->type && prop.name == "Radius" ||
  527. Node::CUBE == curNode->type && prop.name == "Size" )
  528. {
  529. curNode->sphereRadius = prop.value;
  530. }
  531. }
  532. }
  533. else if (!ASSIMP_stricmp(reader->getNodeName(),"int"))
  534. {
  535. IntProperty prop;
  536. ReadIntProperty(prop);
  537. if (inAnimator)
  538. {
  539. if (curAnim->type == Animator::FLY_STRAIGHT && prop.name == "TimeForWay")
  540. {
  541. curAnim->timeForWay = prop.value;
  542. }
  543. }
  544. else
  545. {
  546. // sphere polgon numbers in each direction
  547. if (Node::SPHERE == curNode->type)
  548. {
  549. if (prop.name == "PolyCountX")
  550. {
  551. curNode->spherePolyCountX = prop.value;
  552. }
  553. else if (prop.name == "PolyCountY")
  554. {
  555. curNode->spherePolyCountY = prop.value;
  556. }
  557. }
  558. }
  559. }
  560. else if (!ASSIMP_stricmp(reader->getNodeName(),"string") ||
  561. !ASSIMP_stricmp(reader->getNodeName(),"enum"))
  562. {
  563. StringProperty prop;
  564. ReadStringProperty(prop);
  565. if (prop.value.length())
  566. {
  567. if (prop.name == "Name")
  568. {
  569. curNode->name = prop.value;
  570. /* If we're either a camera or a light source
  571. * we need to update the name in the aiLight/
  572. * aiCamera structure, too.
  573. */
  574. if (Node::CAMERA == curNode->type)
  575. {
  576. cameras.back()->mName.Set(prop.value);
  577. }
  578. else if (Node::LIGHT == curNode->type)
  579. {
  580. lights.back()->mName.Set(prop.value);
  581. }
  582. }
  583. else if (Node::LIGHT == curNode->type && "LightType" == prop.name)
  584. {
  585. }
  586. else if (prop.name == "Mesh" && Node::MESH == curNode->type ||
  587. Node::ANIMMESH == curNode->type)
  588. {
  589. /* This is the file name of the mesh - either
  590. * animated or not. We need to make sure we setup
  591. * the correct postprocessing settings here.
  592. */
  593. unsigned int pp = 0;
  594. BatchLoader::PropertyMap map;
  595. /* If the mesh is a static one remove all animations
  596. */
  597. if (Node::ANIMMESH != curNode->type)
  598. {
  599. pp |= aiProcess_RemoveComponent;
  600. SetGenericProperty<int>(map.ints,AI_CONFIG_PP_RVC_FLAGS,
  601. aiComponent_ANIMATIONS | aiComponent_BONEWEIGHTS);
  602. }
  603. batch.AddLoadRequest(prop.value,pp,&map);
  604. curNode->meshPath = prop.value;
  605. }
  606. else if (inAnimator && prop.name == "Type")
  607. {
  608. // type of the animator
  609. if (prop.value == "rotation")
  610. {
  611. curAnim->type = Animator::ROTATION;
  612. }
  613. else if (prop.value == "flyCircle")
  614. {
  615. curAnim->type = Animator::FLY_CIRCLE;
  616. }
  617. else if (prop.value == "flyStraight")
  618. {
  619. curAnim->type = Animator::FLY_CIRCLE;
  620. }
  621. else if (prop.value == "followSpline")
  622. {
  623. curAnim->type = Animator::FOLLOW_SPLINE;
  624. }
  625. else
  626. {
  627. DefaultLogger::get()->warn("IRR: Ignoring unknown animator: "
  628. + prop.value);
  629. curAnim->type = Animator::UNKNOWN;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. else if (reader->getNodeType() == EXN_ELEMENT_END &&
  636. !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  637. {
  638. break;
  639. }
  640. }
  641. }
  642. break;
  643. case EXN_ELEMENT_END:
  644. // If we reached the end of a node, we need to continue processing its parent
  645. if (!ASSIMP_stricmp(reader->getNodeName(),"node"))
  646. {
  647. if (!curNode)
  648. {
  649. // currently is no node set. We need to go
  650. // back in the node hierarchy
  651. curParent = curParent->parent;
  652. if (!curParent)
  653. {
  654. curParent = root;
  655. DefaultLogger::get()->error("IRR: Too many closing <node> elements");
  656. }
  657. }
  658. else curNode = NULL;
  659. }
  660. // clear all flags
  661. else if (!ASSIMP_stricmp(reader->getNodeName(),"materials"))
  662. {
  663. inMaterials = false;
  664. }
  665. else if (!ASSIMP_stricmp(reader->getNodeName(),"animators"))
  666. {
  667. inAnimator = false;
  668. }
  669. break;
  670. default:
  671. // GCC complains that not all enumeration values are handled
  672. break;
  673. }
  674. }
  675. /* Now iterate through all cameras and compute their final (horizontal) FOV
  676. */
  677. for (std::vector<aiCamera*>::iterator it = cameras.begin(), end = cameras.end();
  678. it != end; ++it)
  679. {
  680. aiCamera* cam = *it;
  681. if (cam->mAspect) // screen aspect could be missing
  682. {
  683. cam->mHorizontalFOV *= cam->mAspect;
  684. }
  685. else DefaultLogger::get()->warn("IRR: Camera aspect is not given, can't compute horizontal FOV");
  686. }
  687. /* Allocate a tempoary scene data structure
  688. */
  689. aiScene* tempScene = new aiScene();
  690. tempScene->mRootNode = new aiNode();
  691. tempScene->mRootNode->mName.Set("<IRRRoot>");
  692. /* Copy the cameras to the output array
  693. */
  694. tempScene->mNumCameras = (unsigned int)cameras.size();
  695. tempScene->mCameras = new aiCamera*[tempScene->mNumCameras];
  696. ::memcpy(tempScene->mCameras,&cameras[0],sizeof(void*)*tempScene->mNumCameras);
  697. /* Copy the light sources to the output array
  698. */
  699. tempScene->mNumLights = (unsigned int)lights.size();
  700. tempScene->mLights = new aiLight*[tempScene->mNumLights];
  701. ::memcpy(tempScene->mLights,&lights[0],sizeof(void*)*tempScene->mNumLights);
  702. // temporary data
  703. std::vector< aiNodeAnim*> anims;
  704. std::vector< AttachmentInfo > attach;
  705. std::vector<aiMesh*> meshes;
  706. anims.reserve(guessedAnimCnt + (guessedAnimCnt >> 2));
  707. meshes.reserve(guessedMeshCnt + (guessedMeshCnt >> 2));
  708. /* Now process our scenegraph recursively: generate final
  709. * meshes and generate animation channels for all nodes.
  710. */
  711. GenerateGraph(root,tempScene->mRootNode, tempScene,
  712. batch, meshes, anims, attach);
  713. if (!anims.empty())
  714. {
  715. tempScene->mNumAnimations = 1;
  716. tempScene->mAnimations = new aiAnimation*[tempScene->mNumAnimations];
  717. aiAnimation* an = tempScene->mAnimations[0] = new aiAnimation();
  718. // ***********************************************************
  719. // This is only the global animation channel of the scene.
  720. // If there are animated models, they will have separate
  721. // animation channels in the scene. To display IRR scenes
  722. // correctly, users will need to combine the global anim
  723. // channel with all the local animations they want to play
  724. // ***********************************************************
  725. an->mName.Set("Irr_GlobalAnimChannel");
  726. // copy all node animation channels to the global channel
  727. an->mNumChannels = (unsigned int)anims.size();
  728. an->mChannels = new aiNodeAnim*[an->mNumChannels];
  729. ::memcpy(an->mChannels, & anims [0], sizeof(void*)*an->mNumChannels);
  730. }
  731. if (meshes.empty())
  732. {
  733. // There are no meshes in the scene - the scene is incomplete
  734. pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  735. DefaultLogger::get()->info("IRR: No Meshes loaded, setting AI_SCENE_FLAGS_INCOMPLETE flag");
  736. }
  737. else
  738. {
  739. // copy all meshes to the temporary scene
  740. tempScene->mNumMeshes = (unsigned int)meshes.size();
  741. tempScene->mMeshes = new aiMesh*[tempScene->mNumMeshes];
  742. ::memcpy(tempScene->mMeshes,&meshes[0],tempScene->mNumMeshes);
  743. }
  744. /* Now merge all sub scenes and attach them to the correct
  745. * attachment points in the scenegraph.
  746. */
  747. SceneCombiner::MergeScenes(pScene,tempScene,attach);
  748. /* Finished ... everything destructs automatically and all
  749. * temporary scenes have already been deleted by MergeScenes()
  750. */
  751. }