OgreImporter.cpp 4.7 KB

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