IRRLoader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 "StandardShapes.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. IRRImporter::IRRImporter()
  44. {
  45. // nothing to do here
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. IRRImporter::~IRRImporter()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the class can handle the format of the given file.
  55. bool IRRImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  56. {
  57. /* NOTE: A simple check for the file extension is not enough
  58. * here. Irrmesh and irr are easy, but xml is too generic
  59. * and could be collada, too. So we need to open the file and
  60. * search for typical tokens.
  61. */
  62. std::string::size_type pos = pFile.find_last_of('.');
  63. // no file extension - can't read
  64. if( pos == std::string::npos)
  65. return false;
  66. std::string extension = pFile.substr( pos);
  67. for (std::string::iterator i = extension.begin(); i != extension.end();++i)
  68. *i = ::tolower(*i);
  69. if (extension == ".irr")return true;
  70. else if (extension == ".xml")
  71. {
  72. /* If CanRead() is called to check whether the loader
  73. * supports a specific file extension in general we
  74. * must return true here.
  75. */
  76. if (!pIOHandler)return true;
  77. const char* tokens[] = {"irr_scene"};
  78. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  79. }
  80. return false;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Imports the given file into the given scene structure.
  84. void IRRImporter::InternReadFile( const std::string& pFile,
  85. aiScene* pScene, IOSystem* pIOHandler)
  86. {
  87. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  88. // Check whether we can read from the file
  89. if( file.get() == NULL)
  90. throw new ImportErrorException( "Failed to open IRR file " + pFile + "");
  91. // Construct the irrXML parser
  92. CIrrXML_IOStreamReader st(file.get());
  93. reader = createIrrXMLReader((IFileReadCallBack*) &st);
  94. // The root node of the scene
  95. Node* root = new Node(Node::DUMMY);
  96. root->parent = NULL;
  97. // Current node parent
  98. Node* curParent = root;
  99. // Scenegraph node we're currently working on
  100. Node* curNode = NULL;
  101. // List of output cameras
  102. std::vector<aiCamera*> cameras;
  103. // List of output lights
  104. std::vector<aiLight*> lights;
  105. // List of output meshes
  106. std::vector<aiMesh*> meshes;
  107. // List of output animation channels
  108. std::vector<aiNodeAnim*> animations;
  109. BatchLoader batch(pIOHandler);
  110. cameras.reserve(5);
  111. lights.reserve(5);
  112. animations.reserve(5);
  113. meshes.reserve(5);
  114. bool inMaterials = false;
  115. // Parse the XML file
  116. while (reader->read())
  117. {
  118. switch (reader->getNodeType())
  119. {
  120. case EXN_ELEMENT:
  121. if (!ASSIMP_stricmp(reader->getNodeName(),"node"))
  122. {
  123. /* What we're going to do with the node depends
  124. * on its type:
  125. *
  126. * "mesh" - Load a mesh from an external file
  127. * "cube" - Generate a cube
  128. * "skybox" - Generate a skybox
  129. * "light" - A light source
  130. * "sphere" - Generate a sphere mesh
  131. * "animatedMesh" - Load an animated mesh from an external file
  132. * and join its animation channels with ours.
  133. * "empty" - A dummy node
  134. * "camera" - A camera
  135. *
  136. * Each of these nodes can be animated.
  137. */
  138. const char* sz = reader->getAttributeValueSafe("type");
  139. Node* nd;
  140. if (!ASSIMP_stricmp(sz,"mesh"))
  141. {
  142. nd = new Node(Node::MESH);
  143. }
  144. else if (!ASSIMP_stricmp(sz,"cube"))
  145. {
  146. nd = new Node(Node::CUBE);
  147. meshes.push_back(StandardShapes::MakeMesh(&StandardShapes::MakeHexahedron));
  148. }
  149. else if (!ASSIMP_stricmp(sz,"skybox"))
  150. {
  151. nd = new Node(Node::SKYBOX);
  152. }
  153. else if (!ASSIMP_stricmp(sz,"camera"))
  154. {
  155. nd = new Node(Node::CAMERA);
  156. // Setup a temporary name for the camera
  157. aiCamera* cam = new aiCamera();
  158. cam->mName.Set( nd->name );
  159. cameras.push_back(cam);
  160. }
  161. else if (!ASSIMP_stricmp(sz,"light"))
  162. {
  163. nd = new Node(Node::LIGHT);
  164. // Setup a temporary name for the light
  165. aiLight* cam = new aiLight();
  166. cam->mName.Set( nd->name );
  167. lights.push_back(cam);
  168. }
  169. else if (!ASSIMP_stricmp(sz,"sphere"))
  170. {
  171. nd = new Node(Node::SPHERE);
  172. }
  173. else if (!ASSIMP_stricmp(sz,"animatedMesh"))
  174. {
  175. nd = new Node(Node::ANIMMESH);
  176. }
  177. else if (!ASSIMP_stricmp(sz,"empty"))
  178. {
  179. nd = new Node(Node::DUMMY);
  180. }
  181. else
  182. {
  183. DefaultLogger::get()->warn("IRR: Found unknown node: " + std::string(sz));
  184. /* We skip the contents of nodes we don't know.
  185. * We parse the transformation and all animators
  186. * and skip the rest.
  187. */
  188. nd = new Node(Node::DUMMY);
  189. }
  190. /* Attach the newly created node to the scenegraph
  191. */
  192. curNode = nd;
  193. nd->parent = curParent;
  194. curParent->children.push_back(nd);
  195. }
  196. else if (!ASSIMP_stricmp(reader->getNodeName(),"materials"))
  197. {
  198. inMaterials = true;
  199. }
  200. else if (!ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  201. {
  202. /* We should have a valid node here
  203. */
  204. if (!curNode)
  205. {
  206. DefaultLogger::get()->error("IRR: Encountered <attributes> element, but "
  207. "there is no node active");
  208. continue;
  209. }
  210. if (inMaterials && curNode->type == Node::ANIMMESH ||
  211. curNode->type == Node::MESH )
  212. {
  213. /* This is a material description - parse it!
  214. */
  215. curNode->materials.push_back(std::pair< aiMaterial*, unsigned int > () );
  216. std::pair< aiMaterial*, unsigned int >& p = curNode->materials.back();
  217. p.first = ParseMaterial(p.second);
  218. continue;
  219. }
  220. /* Parse all elements in the attributes block
  221. * and process them.
  222. */
  223. while (reader->read())
  224. {
  225. if (reader->getNodeType() == EXN_ELEMENT)
  226. {
  227. if (!ASSIMP_stricmp(reader->getNodeName(),"vector3d"))
  228. {
  229. VectorProperty prop;
  230. ReadVectorProperty(prop);
  231. // Convert to our coordinate system
  232. std::swap( (float&)prop.value.z, (float&)prop.value.y );
  233. prop.value.y *= -1.f;
  234. if (prop.name == "Position")
  235. {
  236. curNode->position = prop.value;
  237. }
  238. else if (prop.name == "Rotation")
  239. {
  240. curNode->rotation = prop.value;
  241. }
  242. else if (prop.name == "Scale")
  243. {
  244. curNode->scaling = prop.value;
  245. }
  246. else if (Node::CAMERA == curNode->type)
  247. {
  248. aiCamera* cam = cameras.back();
  249. if (prop.name == "Target")
  250. {
  251. cam->mLookAt = prop.value;
  252. }
  253. else if (prop.name == "UpVector")
  254. {
  255. cam->mUp = prop.value;
  256. }
  257. }
  258. }
  259. else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
  260. {
  261. FloatProperty prop;
  262. ReadFloatProperty(prop);
  263. if (prop.name == "FramesPerSecond" &&
  264. Node::ANIMMESH == curNode->type)
  265. {
  266. curNode->framesPerSecond = prop.value;
  267. }
  268. else if (Node::CAMERA == curNode->type)
  269. {
  270. /* This is the vertical, not the horizontal FOV.
  271. * We need to compute the right FOV from the
  272. * screen aspect which we don't know yet.
  273. */
  274. if (prop.name == "Fovy")
  275. {
  276. cameras.back()->mHorizontalFOV = prop.value;
  277. }
  278. else if (prop.name == "Aspect")
  279. {
  280. cameras.back()->mAspect = prop.value;
  281. }
  282. else if (prop.name == "ZNear")
  283. {
  284. cameras.back()->mClipPlaneNear = prop.value;
  285. }
  286. else if (prop.name == "ZFar")
  287. {
  288. cameras.back()->mClipPlaneFar = prop.value;
  289. }
  290. }
  291. }
  292. else if (!ASSIMP_stricmp(reader->getNodeName(),"string"))
  293. {
  294. StringProperty prop;
  295. ReadStringProperty(prop);
  296. if (prop.value.length())
  297. {
  298. if (prop.name == "Name")
  299. {
  300. curNode->name = prop.value;
  301. /* If we're either a camera or a light source
  302. * we need to update the name in the aiLight/
  303. * aiCamera structure, too.
  304. */
  305. if (Node::CAMERA == curNode->type)
  306. {
  307. cameras.back()->mName.Set(prop.value);
  308. }
  309. else if (Node::LIGHT == curNode->type)
  310. {
  311. lights.back()->mName.Set(prop.value);
  312. }
  313. }
  314. else if (prop.name == "Mesh" && Node::MESH == curNode->type ||
  315. Node::ANIMMESH == curNode->type)
  316. {
  317. /* This is the file name of the mesh - either
  318. * animated or not. We don't need any postprocessing
  319. * steps here. However, it would be useful it we'd
  320. * be able to use RemoveVC to remove animations
  321. * if this isn't an animated mesh. But that's not
  322. * possible at the moment.
  323. */
  324. batch.AddLoadRequest(prop.value);
  325. }
  326. }
  327. }
  328. }
  329. else if (reader->getNodeType() == EXN_ELEMENT_END &&
  330. !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  331. {
  332. break;
  333. }
  334. }
  335. }
  336. break;
  337. case EXN_ELEMENT_END:
  338. if (!ASSIMP_stricmp(reader->getNodeName(),"node"))
  339. {
  340. if (!curNode)
  341. {
  342. // currently is no node set. We need to go
  343. // back in the node hierarchy
  344. curParent = curParent->parent;
  345. if (!curParent)
  346. {
  347. curParent = root;
  348. DefaultLogger::get()->error("IRR: Too many closing <node> elements");
  349. }
  350. }
  351. else curNode = NULL;
  352. }
  353. else if (!ASSIMP_stricmp(reader->getNodeName(),"materials"))
  354. {
  355. inMaterials = false;
  356. }
  357. break;
  358. default:
  359. // GCC complains that not all enumeration values are handled
  360. break;
  361. }
  362. }
  363. /* Now iterate through all cameras and compute their final (horizontal) FOV
  364. */
  365. for (std::vector<aiCamera*>::iterator it = cameras.begin(), end = cameras.end();
  366. it != end; ++it)
  367. {
  368. aiCamera* cam = *it;
  369. if (cam->mAspect) // screen aspect could be missing
  370. {
  371. cam->mHorizontalFOV *= cam->mAspect;
  372. }
  373. else DefaultLogger::get()->warn("IRR: Camera aspect is not given, can't compute horizontal FOV");
  374. }
  375. }