OgreImporter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  34. #include "OgreImporter.h"
  35. #include "OgreBinarySerializer.h"
  36. #include "OgreXmlSerializer.h"
  37. #include <assimp/Importer.hpp>
  38. #include <memory>
  39. static const aiImporterDesc desc = {
  40. "Ogre3D Mesh Importer",
  41. "",
  42. "",
  43. "",
  44. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. "mesh mesh.xml"
  50. };
  51. namespace Assimp
  52. {
  53. namespace Ogre
  54. {
  55. const aiImporterDesc* OgreImporter::GetInfo() const
  56. {
  57. return &desc;
  58. }
  59. void OgreImporter::SetupProperties(const Importer* pImp)
  60. {
  61. m_userDefinedMaterialLibFile = pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
  62. m_detectTextureTypeFromFilename = pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
  63. }
  64. bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
  65. {
  66. if (!checkSig) {
  67. return EndsWith(pFile, ".mesh.xml", false) || EndsWith(pFile, ".mesh", false);
  68. }
  69. if (EndsWith(pFile, ".mesh.xml", false))
  70. {
  71. const char* tokens[] = { "<mesh>" };
  72. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
  73. }
  74. else
  75. {
  76. /// @todo Read and validate first header chunk?
  77. return EndsWith(pFile, ".mesh", false);
  78. }
  79. }
  80. void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
  81. {
  82. // Open source file
  83. IOStream *f = pIOHandler->Open(pFile, "rb");
  84. if (!f) {
  85. throw DeadlyImportError("Failed to open file " + pFile);
  86. }
  87. // Binary .mesh import
  88. if (EndsWith(pFile, ".mesh", false))
  89. {
  90. /// @note MemoryStreamReader takes ownership of f.
  91. MemoryStreamReader reader(f);
  92. // Import mesh
  93. std::unique_ptr<Mesh> mesh(OgreBinarySerializer::ImportMesh(&reader));
  94. // Import skeleton
  95. OgreBinarySerializer::ImportSkeleton(pIOHandler, mesh.get());
  96. // Import mesh referenced materials
  97. ReadMaterials(pFile, pIOHandler, pScene, mesh.get());
  98. // Convert to Assimp
  99. mesh->ConvertToAssimpScene(pScene);
  100. }
  101. // XML .mesh.xml import
  102. else
  103. {
  104. /// @note XmlReader does not take ownership of f, hence the scoped ptr.
  105. std::unique_ptr<IOStream> scopedFile(f);
  106. std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(scopedFile.get()));
  107. std::unique_ptr<XmlReader> reader(irr::io::createIrrXMLReader(xmlStream.get()));
  108. // Import mesh
  109. std::unique_ptr<MeshXml> mesh(OgreXmlSerializer::ImportMesh(reader.get()));
  110. // Import skeleton
  111. OgreXmlSerializer::ImportSkeleton(pIOHandler, mesh.get());
  112. // Import mesh referenced materials
  113. ReadMaterials(pFile, pIOHandler, pScene, mesh.get());
  114. // Convert to Assimp
  115. mesh->ConvertToAssimpScene(pScene);
  116. }
  117. }
  118. } // Ogre
  119. } // Assimp
  120. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER