MD3Loader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 MD3 importer class */
  35. #include "MD3Loader.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. MD3Importer::MD3Importer()
  47. {
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // Destructor, private as well
  51. MD3Importer::~MD3Importer()
  52. {
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Returns whether the class can handle the format of the given file.
  56. bool MD3Importer::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. // not brilliant but working ;-)
  65. if( extension == ".md3" || extension == ".MD3" ||
  66. extension == ".mD3" || extension == ".Md3")
  67. return true;
  68. return false;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Imports the given file into the given scene structure.
  72. void MD3Importer::InternReadFile(
  73. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  74. {
  75. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  76. // Check whether we can read from the file
  77. if( file.get() == NULL)
  78. {
  79. throw new ImportErrorException( "Failed to open md3 file " + pFile + ".");
  80. }
  81. // check whether the md3 file is large enough to contain
  82. // at least the file header
  83. size_t fileSize = file->FileSize();
  84. if( fileSize < sizeof(MD3::Header))
  85. {
  86. throw new ImportErrorException( ".md3 File is too small.");
  87. }
  88. // allocate storage and copy the contents of the file to a memory buffer
  89. this->mBuffer = new unsigned char[fileSize];
  90. file->Read( (void*)mBuffer, 1, fileSize);
  91. this->m_pcHeader = (const MD3::Header*)this->mBuffer;
  92. // check magic number
  93. if (this->m_pcHeader->IDENT != AI_MD3_MAGIC_NUMBER_BE &&
  94. this->m_pcHeader->IDENT != AI_MD3_MAGIC_NUMBER_LE)
  95. {
  96. throw new ImportErrorException( "Invalid md3 file: Magic bytes not found");
  97. }
  98. // check file format version
  99. if (this->m_pcHeader->VERSION > 15)
  100. {
  101. throw new ImportErrorException( "Unsupported md3 file version");
  102. }
  103. // check some values whether they are valid
  104. if (0 == this->m_pcHeader->NUM_FRAMES)
  105. {
  106. throw new ImportErrorException( "Invalid md3 file: NUM_FRAMES is 0");
  107. }
  108. if (0 == this->m_pcHeader->NUM_SURFACES)
  109. {
  110. throw new ImportErrorException( "Invalid md3 file: NUM_SURFACES is 0");
  111. }
  112. if (this->m_pcHeader->OFS_EOF > (int32_t)fileSize)
  113. {
  114. throw new ImportErrorException( "Invalid md3 file: File is too small");
  115. }
  116. // now navigate to the list of surfaces
  117. const MD3::Surface* pcSurfaces = (const MD3::Surface*)
  118. (this->mBuffer + this->m_pcHeader->OFS_SURFACES);
  119. // allocate output storage
  120. pScene->mNumMeshes = this->m_pcHeader->NUM_SURFACES;
  121. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  122. pScene->mNumMaterials = this->m_pcHeader->NUM_SURFACES;
  123. pScene->mMaterials = new aiMaterial*[pScene->mNumMeshes];
  124. unsigned int iNum = this->m_pcHeader->NUM_SURFACES;
  125. unsigned int iNumMaterials = 0;
  126. unsigned int iDefaultMatIndex = 0xFFFFFFFF;
  127. while (iNum-- > 0)
  128. {
  129. // navigate to the vertex list of the surface
  130. const MD3::Vertex* pcVertices = (const MD3::Vertex*)
  131. (((unsigned char*)pcSurfaces) + pcSurfaces->OFS_XYZNORMAL);
  132. // navigate to the triangle list of the surface
  133. const MD3::Triangle* pcTriangles = (const MD3::Triangle*)
  134. (((unsigned char*)pcSurfaces) + pcSurfaces->OFS_TRIANGLES);
  135. // navigate to the texture coordinate list of the surface
  136. const MD3::TexCoord* pcUVs = (const MD3::TexCoord*)
  137. (((unsigned char*)pcSurfaces) + pcSurfaces->OFS_ST);
  138. // navigate to the shader list of the surface
  139. const MD3::Shader* pcShaders = (const MD3::Shader*)
  140. (((unsigned char*)pcSurfaces) + pcSurfaces->OFS_SHADERS);
  141. // if the submesh is empty ignore it
  142. if (0 == pcSurfaces->NUM_VERTICES || 0 == pcSurfaces->NUM_TRIANGLES)
  143. {
  144. pcSurfaces = (const MD3::Surface*)(((unsigned char*)pcSurfaces) + pcSurfaces->OFS_END);
  145. pScene->mNumMeshes--;
  146. continue;
  147. }
  148. // allocate the output mesh
  149. pScene->mMeshes[iNum] = new aiMesh();
  150. aiMesh* pcMesh = pScene->mMeshes[iNum];
  151. pcMesh->mNumVertices = pcSurfaces->NUM_TRIANGLES*3;
  152. pcMesh->mNumBones = 0;
  153. pcMesh->mColors[0] = pcMesh->mColors[1] = pcMesh->mColors[2] = pcMesh->mColors[3] = NULL;
  154. pcMesh->mNumFaces = pcSurfaces->NUM_TRIANGLES;
  155. pcMesh->mFaces = new aiFace[pcSurfaces->NUM_TRIANGLES];
  156. pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
  157. pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
  158. pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
  159. pcMesh->mTextureCoords[1] = pcMesh->mTextureCoords[2] = pcMesh->mTextureCoords[3] = NULL;
  160. pcMesh->mNumUVComponents[0] = 2;
  161. // fill in all triangles
  162. unsigned int iCurrent = 0;
  163. for (unsigned int i = 0; i < (unsigned int)pcSurfaces->NUM_TRIANGLES;++i)
  164. {
  165. pcMesh->mFaces[i].mIndices = new unsigned int[3];
  166. pcMesh->mFaces[i].mNumIndices = 3;
  167. unsigned int iTemp = iCurrent;
  168. for (unsigned int c = 0; c < 3;++c,++iCurrent)
  169. {
  170. // read vertices
  171. pcMesh->mVertices[iCurrent].x = pcVertices[ pcTriangles->INDEXES[c]].X;
  172. pcMesh->mVertices[iCurrent].y = pcVertices[ pcTriangles->INDEXES[c]].Y;
  173. pcMesh->mVertices[iCurrent].z = pcVertices[ pcTriangles->INDEXES[c]].Z*-1.0f;
  174. // convert the normal vector to uncompressed float3 format
  175. LatLngNormalToVec3(pcVertices[pcTriangles->INDEXES[c]].NORMAL,
  176. (float*)&pcMesh->mNormals[iCurrent]);
  177. //std::swap(pcMesh->mNormals[iCurrent].z,pcMesh->mNormals[iCurrent].y);
  178. pcMesh->mNormals[iCurrent].z *= -1.0f;
  179. // read texture coordinates
  180. pcMesh->mTextureCoords[0][iCurrent].x = pcUVs[ pcTriangles->INDEXES[c]].U;
  181. pcMesh->mTextureCoords[0][iCurrent].y = 1.0f - pcUVs[ pcTriangles->INDEXES[c]].V;
  182. }
  183. pcMesh->mFaces[i].mIndices[0] = iTemp+2;
  184. pcMesh->mFaces[i].mIndices[1] = iTemp+1;
  185. pcMesh->mFaces[i].mIndices[2] = iTemp+0;
  186. pcTriangles++;
  187. }
  188. // get the first shader (= texture?) assigned to the surface
  189. if (0 != pcSurfaces->NUM_SHADER)
  190. {
  191. // make a relative path.
  192. // if the MD3's internal path itself and the given path are using
  193. // the same directory remove it
  194. const char* szEndDir1 = strrchr((const char*)this->m_pcHeader->NAME,'\\');
  195. if (!szEndDir1)szEndDir1 = strrchr((const char*)this->m_pcHeader->NAME,'/');
  196. const char* szEndDir2 = strrchr((const char*)pcShaders->NAME,'\\');
  197. if (!szEndDir2)szEndDir2 = strrchr((const char*)pcShaders->NAME,'/');
  198. if (szEndDir1 && szEndDir2)
  199. {
  200. // both of them are valid
  201. const unsigned int iLen1 = (unsigned int)(szEndDir1 - (const char*)this->m_pcHeader->NAME);
  202. const unsigned int iLen2 = std::min (iLen1, (unsigned int)(szEndDir2 - (const char*)pcShaders->NAME) );
  203. bool bSuccess = true;
  204. for (unsigned int a = 0; a < iLen2;++a)
  205. {
  206. char sz = tolower ( pcShaders->NAME[a] );
  207. char sz2 = tolower ( this->m_pcHeader->NAME[a] );
  208. if (sz != sz2)
  209. {
  210. bSuccess = false;
  211. break;
  212. }
  213. }
  214. if (bSuccess)
  215. {
  216. // use the file name only
  217. szEndDir2++;
  218. }
  219. else
  220. {
  221. // use the full path
  222. szEndDir2 = (const char*)pcShaders->NAME;
  223. }
  224. }
  225. // now try to find out whether we have this shader already
  226. bool bHave = false;
  227. for (unsigned int p = 0; p < iNumMaterials;++p)
  228. {
  229. if (iDefaultMatIndex == p)continue;
  230. aiString szOut;
  231. if(AI_SUCCESS == aiGetMaterialString ( (aiMaterial*)pScene->mMaterials[p],
  232. AI_MATKEY_TEXBLEND_DIFFUSE(0),&szOut))
  233. {
  234. if (0 == ASSIMP_stricmp(szOut.data,szEndDir2))
  235. {
  236. // equal. reuse this material (texture)
  237. bHave = true;
  238. pcMesh->mMaterialIndex = p;
  239. break;
  240. }
  241. }
  242. }
  243. if (!bHave)
  244. {
  245. MaterialHelper* pcHelper = new MaterialHelper();
  246. if (szEndDir2)
  247. {
  248. aiString szString;
  249. const size_t iLen = strlen(szEndDir2);
  250. memcpy(szString.data,szEndDir2,iLen+1);
  251. szString.length = iLen-1;
  252. pcHelper->AddProperty(&szString,AI_MATKEY_TEXTURE_DIFFUSE(0));
  253. }
  254. int iMode = (int)aiShadingMode_Gouraud;
  255. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  256. aiColor3D clr;
  257. clr.b = clr.g = clr.r = 1.0f;
  258. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  259. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  260. clr.b = clr.g = clr.r = 0.05f;
  261. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  262. pScene->mMaterials[iNumMaterials] = (aiMaterial*)pcHelper;
  263. pcMesh->mMaterialIndex = iNumMaterials++;
  264. }
  265. }
  266. else
  267. {
  268. if (0xFFFFFFFF != iDefaultMatIndex)
  269. {
  270. pcMesh->mMaterialIndex = iDefaultMatIndex;
  271. }
  272. else
  273. {
  274. MaterialHelper* pcHelper = new MaterialHelper();
  275. // fill in a default material
  276. int iMode = (int)aiShadingMode_Gouraud;
  277. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  278. aiColor3D clr;
  279. clr.b = clr.g = clr.r = 0.6f;
  280. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  281. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  282. clr.b = clr.g = clr.r = 0.05f;
  283. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  284. pScene->mMaterials[iNumMaterials] = (aiMaterial*)pcHelper;
  285. pcMesh->mMaterialIndex = iNumMaterials++;
  286. }
  287. }
  288. pcSurfaces = (const MD3::Surface*)(((unsigned char*)pcSurfaces) + pcSurfaces->OFS_END);
  289. }
  290. if (0 == pScene->mNumMeshes)
  291. {
  292. // cleanup before returning
  293. delete pScene;
  294. throw new ImportErrorException( "Invalid md3 file: File contains no valid mesh");
  295. }
  296. pScene->mNumMaterials = iNumMaterials;
  297. // now we need to generate an empty node graph
  298. pScene->mRootNode = new aiNode();
  299. pScene->mRootNode->mNumChildren = pScene->mNumMeshes;
  300. pScene->mRootNode->mChildren = new aiNode*[pScene->mNumMeshes];
  301. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  302. {
  303. pScene->mRootNode->mChildren[i] = new aiNode();
  304. pScene->mRootNode->mChildren[i]->mParent = pScene->mRootNode;
  305. pScene->mRootNode->mChildren[i]->mNumMeshes = 1;
  306. pScene->mRootNode->mChildren[i]->mMeshes = new unsigned int[1];
  307. pScene->mRootNode->mChildren[i]->mMeshes[0] = i;
  308. }
  309. delete[] this->mBuffer;
  310. return;
  311. }