OgreImporter.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  35. #include <vector>
  36. #include <sstream>
  37. #include "OgreImporter.h"
  38. #include "TinyFormatter.h"
  39. #include "irrXMLWrapper.h"
  40. static const aiImporterDesc desc = {
  41. "Ogre XML Mesh Importer",
  42. "",
  43. "",
  44. "",
  45. aiImporterFlags_SupportTextFlavour,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. "mesh.xml"
  51. };
  52. using namespace std;
  53. namespace Assimp
  54. {
  55. namespace Ogre
  56. {
  57. const aiImporterDesc* OgreImporter::GetInfo() const
  58. {
  59. return &desc;
  60. }
  61. void OgreImporter::SetupProperties(const Importer* pImp)
  62. {
  63. m_userDefinedMaterialLibFile = pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
  64. m_detectTextureTypeFromFilename = pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
  65. }
  66. bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
  67. {
  68. if (!checkSig)
  69. return EndsWith(pFile, ".mesh.xml", false);
  70. const char* tokens[] = { "<mesh>" };
  71. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
  72. }
  73. void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
  74. {
  75. // -------------------- Initial file and XML operations --------------------
  76. // Open
  77. boost::scoped_ptr<IOStream> file(pIOHandler->Open(pFile));
  78. if (file.get() == NULL)
  79. throw DeadlyImportError("Failed to open file " + pFile);
  80. // Read
  81. boost::scoped_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(file.get()));
  82. boost::scoped_ptr<XmlReader> reader(irr::io::createIrrXMLReader(xmlStream.get()));
  83. if (!reader)
  84. throw DeadlyImportError("Failed to create XML Reader for " + pFile);
  85. DefaultLogger::get()->debug("Opened a XML reader for " + pFile);
  86. // Read root node
  87. NextNode(reader.get());
  88. if (!CurrentNodeNameEquals(reader, "mesh"))
  89. throw DeadlyImportError("Root node is not <mesh> but <" + string(reader->getNodeName()) + "> in " + pFile);
  90. // Node names
  91. const string nnSharedGeometry = "sharedgeometry";
  92. const string nnVertexBuffer = "vertexbuffer";
  93. const string nnSubMeshes = "submeshes";
  94. const string nnSubMesh = "submesh";
  95. const string nnSubMeshNames = "submeshnames";
  96. const string nnSkeletonLink = "skeletonlink";
  97. // -------------------- Shared Geometry --------------------
  98. // This can be used to share geometry between submeshes
  99. NextNode(reader.get());
  100. if (CurrentNodeNameEquals(reader, nnSharedGeometry))
  101. {
  102. DefaultLogger::get()->debug("Reading shared geometry");
  103. unsigned int NumVertices = GetAttribute<unsigned int>(reader.get(), "vertexcount");
  104. NextNode(reader.get());
  105. while(CurrentNodeNameEquals(reader, nnVertexBuffer))
  106. ReadVertexBuffer(m_SharedGeometry, reader.get(), NumVertices);
  107. }
  108. // -------------------- Sub Meshes --------------------
  109. if (!CurrentNodeNameEquals(reader, nnSubMeshes))
  110. throw DeadlyImportError("Could not find <submeshes> node inside root <mesh> node");
  111. vector<boost::shared_ptr<SubMesh> > subMeshes;
  112. vector<aiMaterial*> materials;
  113. NextNode(reader.get());
  114. while(CurrentNodeNameEquals(reader, nnSubMesh))
  115. {
  116. SubMesh* submesh = new SubMesh();
  117. ReadSubMesh(subMeshes.size(), *submesh, reader.get());
  118. // Just a index in a array, we add a mesh in each loop cycle, so we get indicies like 0, 1, 2 ... n;
  119. // so it is important to do this before pushing the mesh in the vector!
  120. /// @todo Not sure if this really is needed, refactor out if possible.
  121. submesh->MaterialIndex = subMeshes.size();
  122. subMeshes.push_back(boost::shared_ptr<SubMesh>(submesh));
  123. /** @todo What is the correct way of handling empty ref here.
  124. Does Assimp require there to be a valid material index for each mesh,
  125. even if its a dummy material. */
  126. aiMaterial* material = ReadMaterial(pFile, pIOHandler, submesh->MaterialName);
  127. materials.push_back(material);
  128. }
  129. if (subMeshes.empty())
  130. throw DeadlyImportError("Could not find <submeshes> node inside root <mesh> node");
  131. // This is really a internal error if we failed to create dummy materials.
  132. if (subMeshes.size() != materials.size())
  133. throw DeadlyImportError("Internal Error: Material count does not match the submesh count");
  134. // Skip submesh names.
  135. /// @todo Should these be read to scene etc. metadata?
  136. if (CurrentNodeNameEquals(reader, nnSubMeshNames))
  137. {
  138. NextNode(reader.get());
  139. while(CurrentNodeNameEquals(reader, nnSubMesh))
  140. NextNode(reader.get());
  141. }
  142. // -------------------- Skeleton --------------------
  143. vector<Bone> Bones;
  144. vector<Animation> Animations;
  145. if (CurrentNodeNameEquals(reader, nnSkeletonLink))
  146. {
  147. string skeletonFile = GetAttribute<string>(reader.get(), "name");
  148. if (!skeletonFile.empty())
  149. ReadSkeleton(pFile, pIOHandler, pScene, skeletonFile, Bones, Animations);
  150. else
  151. DefaultLogger::get()->debug("Found a unusual <" + nnSkeletonLink + "> with a empty file reference");
  152. NextNode(reader.get());
  153. }
  154. else
  155. DefaultLogger::get()->debug("Mesh has no assigned skeleton with <" + nnSkeletonLink + ">");
  156. // Now there might be <boneassignments> for the shared geometry
  157. if (CurrentNodeNameEquals(reader, "boneassignments"))
  158. ReadBoneWeights(m_SharedGeometry, reader.get());
  159. // -------------------- Process Results --------------------
  160. BOOST_FOREACH(boost::shared_ptr<SubMesh> submesh, subMeshes)
  161. {
  162. ProcessSubMesh(*submesh.get(), m_SharedGeometry);
  163. }
  164. // -------------------- Apply to aiScene --------------------
  165. // Materials
  166. pScene->mMaterials = new aiMaterial*[materials.size()];
  167. pScene->mNumMaterials = materials.size();
  168. for(size_t i=0, len=materials.size(); i<len; ++i)
  169. pScene->mMaterials[i] = materials[i];
  170. // Meshes
  171. pScene->mMeshes = new aiMesh*[subMeshes.size()];
  172. pScene->mNumMeshes = subMeshes.size();
  173. for(size_t i=0, len=subMeshes.size(); i<len; ++i)
  174. {
  175. boost::shared_ptr<SubMesh> submesh = subMeshes[i];
  176. pScene->mMeshes[i] = CreateAssimpSubMesh(pScene, *(submesh.get()), Bones);
  177. }
  178. // Create the root node
  179. pScene->mRootNode = new aiNode();
  180. pScene->mRootNode->mMeshes = new unsigned int[subMeshes.size()];
  181. pScene->mRootNode->mNumMeshes = subMeshes.size();
  182. for(size_t i=0, len=subMeshes.size(); i<len; ++i)
  183. pScene->mRootNode->mMeshes[i] = static_cast<unsigned int>(i);
  184. // Skeleton and animations
  185. CreateAssimpSkeleton(pScene, Bones, Animations);
  186. }
  187. } // Ogre
  188. } // Assimp
  189. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER