IRRLoader.cpp 24 KB

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