MDRLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 MDR importer class */
  35. #include "AssimpPCH.h"
  36. #include "MDRLoader.h"
  37. using namespace Assimp;
  38. using namespace Assimp::MDR;
  39. // ------------------------------------------------------------------------------------------------
  40. // Constructor to be privately used by Importer
  41. MDRImporter::MDRImporter()
  42. {
  43. }
  44. // ------------------------------------------------------------------------------------------------
  45. // Destructor, private as well
  46. MDRImporter::~MDRImporter()
  47. {
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Returns whether the class can handle the format of the given file.
  51. bool MDRImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  52. {
  53. // simple check of file extension is enough for the moment
  54. std::string::size_type pos = pFile.find_last_of('.');
  55. // no file extension - can't read
  56. if( pos == std::string::npos)
  57. return false;
  58. std::string extension = pFile.substr( pos);
  59. return !(extension.length() != 4 || extension[0] != '.' ||
  60. extension[1] != 'm' && extension[1] != 'M' ||
  61. extension[2] != 'd' && extension[2] != 'D' ||
  62. extension[3] != 'r' && extension[3] != 'R');
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Validate the header of the given MDR file
  66. void MDRImporter::ValidateHeader()
  67. {
  68. AI_SWAP4(pcLOD->version);
  69. AI_SWAP4(pcLOD->numBones);
  70. AI_SWAP4(pcLOD->numTags);
  71. AI_SWAP4(pcLOD->numFrames);
  72. AI_SWAP4(pcLOD->ofsFrames);
  73. AI_SWAP4(pcLOD->ofsTags);
  74. AI_SWAP4(pcLOD->numLODs);
  75. AI_SWAP4(pcLOD->ofsLODs);
  76. if (pcHeader->ident != AI_MDR_MAGIC_NUMBER_BE &&
  77. pcHeader->ident != AI_MDR_MAGIC_NUMBER_LE)
  78. {
  79. char szBuffer[5];
  80. szBuffer[0] = ((char*)&pcHeader->ident)[0];
  81. szBuffer[1] = ((char*)&pcHeader->ident)[1];
  82. szBuffer[2] = ((char*)&pcHeader->ident)[2];
  83. szBuffer[3] = ((char*)&pcHeader->ident)[3];
  84. szBuffer[4] = '\0';
  85. throw new ImportErrorException("Invalid MDR magic word: should be 5MDR, the "
  86. "magic word found is " + std::string( szBuffer ));
  87. }
  88. if (pcHeader->version != AI_MDR_VERSION)
  89. DefaultLogger::get()->warn("Unsupported MDR file version (2 (AI_MDR_VERSION) was expected)");
  90. if (!pcHeader->numBones)
  91. DefaultLogger::get()->warn("MDR: At least one bone must be there");
  92. // validate all LODs
  93. if (pcHeader->ofsLODs > fileSize)
  94. // validate all frames
  95. if (pcHeader->ofsFrames + sizeof(MDR::Frame) * (pcHeader->numBones-1) *
  96. sizeof(MDR::Bone) * pcHeader->numFrames > fileSize)
  97. {
  98. throw new ImportErrorException("MDR: header is invalid - frame out of range");
  99. }
  100. // check whether the requested frame is existing
  101. if (this->configFrameID >= pcHeader->numFrames)
  102. throw new ImportErrorException("The requested frame is not available");
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Validate the header of a given MDR file LOD
  106. void MDRImporter::ValidateLODHeader(BE_NCONST MDR::LOD* pcLOD)
  107. {
  108. AI_SWAP4(pcLOD->ofsSurfaces);
  109. AI_SWAP4(pcLOD->numSurfaces);
  110. AI_SWAP4(pcLOD->ofsEnd);
  111. const unsigned int iMax = this->fileSize - (unsigned int)((int8_t*)pcLOD-(int8_t*)pcHeader);
  112. if (!pcLOD->numSurfaces)
  113. throw new ImportErrorException("MDR: LOD has zero surfaces assigned");
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Validate the header of a given MDR file surface
  117. void MDRImporter::ValidateSurfaceHeader(BE_NCONST MDR::Surface* pcSurf)
  118. {
  119. AI_SWAP4(pcSurf->ident);
  120. AI_SWAP4(pcSurf->numBoneReferences);
  121. AI_SWAP4(pcSurf->numTriangles);
  122. AI_SWAP4(pcSurf->numVerts);
  123. AI_SWAP4(pcSurf->ofsBoneReferences);
  124. AI_SWAP4(pcSurf->ofsEnd);
  125. AI_SWAP4(pcSurf->ofsTriangles);
  126. AI_SWAP4(pcSurf->ofsVerts);
  127. AI_SWAP4(pcSurf->shaderIndex);
  128. const unsigned int iMax = this->fileSize - (unsigned int)((int8_t*)pcSurf-(int8_t*)pcHeader);
  129. // not exact - there could be extra data in the vertices.
  130. if (pcSurf->ofsTriangles + pcSurf->numTriangles*sizeof(MDR::Triangle) > iMax ||
  131. pcSurf->ofsVerts + pcSurf->numVerts*sizeof(MDR::Vertex) > iMax)
  132. {
  133. throw new ImportErrorException("MDR: Surface header is invalid");
  134. }
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. // Setup configuration properties
  138. void MDRImporter::SetupProperties(const Importer* pImp)
  139. {
  140. // The AI_CONFIG_IMPORT_MDR_KEYFRAME option overrides the
  141. // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
  142. if(0xffffffff == (this->configFrameID = pImp->GetPropertyInteger(
  143. AI_CONFIG_IMPORT_MDR_KEYFRAME,0xffffffff)))
  144. {
  145. this->configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
  146. }
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. // Imports the given file into the given scene structure.
  150. void MDRImporter::InternReadFile( const std::string& pFile,
  151. aiScene* pScene, IOSystem* pIOHandler)
  152. {
  153. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  154. // Check whether we can read from the file
  155. if( file.get() == NULL)
  156. throw new ImportErrorException( "Failed to open MDR file " + pFile + ".");
  157. // check whether the mdr file is large enough to contain the file header
  158. fileSize = (unsigned int)file->FileSize();
  159. if( fileSize < sizeof(MDR::Header))
  160. throw new ImportErrorException( "MDR File is too small.");
  161. std::vector<unsigned char> mBuffer2(fileSize);
  162. file->Read( &mBuffer2[0], 1, fileSize);
  163. mBuffer = &mBuffer2[0];
  164. // validate the file header and do BigEndian byte swapping for all sub headers
  165. this->pcHeader = (BE_NCONST MDR::Header*)this->mBuffer;
  166. this->ValidateHeader();
  167. // go to the first LOD
  168. LE_NCONST MDR::LOD* lod = (LE_NCONST MDR::LOD*)((uint8_t*)this->pcHeader+pcHeader->ofsLODs);
  169. std::vector<aiMesh*> outMeshes;
  170. outMeshes.reserve(lod->numSurfaces);
  171. // get a pointer to the first surface
  172. LE_NCONST MDR::Surface* surf = (LE_NCONST MDR::Surface*)((uint8_t*)lod+lod->ofsSurfaces);
  173. for (uint32_t i = 0; i < lod->numSurfaces; ++i)
  174. {
  175. if (surf->numTriangles && surf->numVerts && surf->numBoneReferences)
  176. {
  177. outMeshes.push_back(new aiMesh());
  178. aiMesh* mesh = outMeshes.back();
  179. mesh->mNumFaces = surf->numTriangles;
  180. mesh->mNumVertices = mesh->mNumFaces*3;
  181. mesh->mNumBones = surf->numBoneReferences;
  182. mesh->mFaces = new aiFace[mesh->mNumFaces];
  183. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  184. mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  185. mesh->mBones = new aiBone*[mesh->mNumBones];
  186. // allocate output bones
  187. for (unsigned int p = 0; p < mesh->mNumBones;++p)
  188. {
  189. aiBone* bone = mesh->mBones[p] = new aiBone();
  190. bone->mName.length = ::sprintf( bone->mName.data, "B_%i",p);
  191. }
  192. std::vector<BoneWeightInfo> mWeights;
  193. mWeights.reserve(surf->numVerts << 1);
  194. std::vector<VertexInfo> mVertices(surf->numVerts);
  195. // get a pointer to the first vertex
  196. LE_NCONST MDR::Vertex* v = (LE_NCONST MDR::Vertex*)((uint8_t*)surf+surf->ofsVerts);
  197. for (unsigned int m = 0; m < surf->numVerts; ++m)
  198. {
  199. // get a pointer to the next vertex
  200. v = (LE_NCONST MDR::Vertex*)((uint8_t*)(v+1) + v->numWeights*sizeof(MDR::Weight));
  201. #ifndef AI_BUILD_BIG_ENDIAN
  202. AI_SWAP4(v->numWeights);
  203. AI_SWAP4(v->normal.x);AI_SWAP4(v->normal.y);AI_SWAP4(v->normal.z);
  204. AI_SWAP4(v->texCoords.x);AI_SWAP4(v->texCoords.y);
  205. #endif
  206. VertexInfo& vert = mVertices[m];
  207. vert.uv.x = v->texCoords.x; vert.uv.y = v->texCoords.y;
  208. vert.normal = v->normal;
  209. vert.start = (unsigned int)mWeights.size();
  210. vert.num = v->numWeights;
  211. for (unsigned int l = 0; l < vert.num; ++l)
  212. {
  213. }
  214. }
  215. // find out how large the output weight buffers must be
  216. LE_NCONST MDR::Triangle* tri = (LE_NCONST MDR::Triangle*)((uint8_t*)surf+surf->ofsTriangles);
  217. LE_NCONST MDR::Triangle* const triEnd = tri + surf->numTriangles;
  218. for (; tri != triEnd; ++tri)
  219. {
  220. for (unsigned int o = 0; o < 3;++o)
  221. {
  222. #ifndef AI_BUILD_BIG_ENDIAN
  223. AI_SWAP4(tri->indexes[o]);
  224. #endif
  225. register unsigned int temp = tri->indexes[o];
  226. if (temp >= surf->numVerts)
  227. throw new ImportErrorException("MDR: Vertex index is out of range");
  228. VertexInfo& vert = mVertices[temp];
  229. for (unsigned int l = vert.start; l < vert.start + vert.num; ++l)
  230. {
  231. if (mWeights[l].first >= surf->numBoneReferences)
  232. throw new ImportErrorException("MDR: Bone index is out of range");
  233. ++mesh->mBones[mWeights[l].first]->mNumWeights;
  234. }
  235. }
  236. }
  237. // allocate storage for output bone weights
  238. for (unsigned int p = 0; p < mesh->mNumBones;++p)
  239. {
  240. aiBone* bone = mesh->mBones[p];
  241. ai_assert(0 != bone->mNumWeights);
  242. bone->mWeights = new aiVertexWeight[bone->mNumWeights];
  243. }
  244. // and build the final output buffers
  245. }
  246. // get a pointer to the next surface
  247. surf = (LE_NCONST MDR::Surface*)((uint8_t*)surf + surf->ofsEnd);
  248. }
  249. pScene->mNumMeshes = (unsigned int) outMeshes.size();
  250. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  251. ::memcpy(pScene->mMeshes,&outMeshes[0],sizeof(void*)*pScene->mNumMeshes);
  252. }