MD4Loader.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 MD4 importer class */
  35. #include "MD4Loader.h"
  36. #include "MaterialSystem.h"
  37. #include "../include/IOStream.h"
  38. #include "../include/IOSystem.h"
  39. #include "../include/aiMesh.h"
  40. #include "../include/aiScene.h"
  41. #include "../include/aiAssert.h"
  42. #include <boost/scoped_ptr.hpp>
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. MD4Importer::MD4Importer()
  47. {
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor, private as well
  51. MD4Importer::~MD4Importer()
  52. {
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the class can handle the format of the given file.
  56. bool MD4Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  57. {
  58. // simple check of file extension is enough for the moment
  59. std::string::size_type pos = pFile.find_last_of('.');
  60. // no file extension - can't read
  61. if( pos == std::string::npos)
  62. return false;
  63. std::string extension = pFile.substr( pos);
  64. if (extension.length() < 4)return false;
  65. if (extension[0] != '.')return false;
  66. // not brilliant but working ;-)
  67. if( extension == ".md4" || extension == ".MD4" ||
  68. extension == ".mD4" || extension == ".Md4")
  69. {
  70. return true;
  71. }
  72. #if (!defined AI_MD4_DONT_SUPPORT_RAVENSOFT_MDR)
  73. if (extension[1] != 'm' && extension[1] != 'M')return false;
  74. if (extension[2] != 'd' && extension[2] != 'D')return false;
  75. if (extension[3] != 'r' && extension[3] != 'R')return false;
  76. #endif
  77. return true;
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Imports the given file into the given scene structure.
  81. void MD4Importer::InternReadFile(
  82. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  83. {
  84. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  85. // Check whether we can read from the file
  86. if( file.get() == NULL)
  87. {
  88. throw new ImportErrorException( "Failed to open md4/mdr file " + pFile + ".");
  89. }
  90. // check whether the md4 file is large enough to contain
  91. // at least the file header
  92. size_t fileSize = file->FileSize();
  93. if( fileSize < sizeof(MD4::Header))
  94. {
  95. throw new ImportErrorException( ".mdd File is too small.");
  96. }
  97. return;
  98. }