OgreImporter.cpp 4.6 KB

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