MD2Loader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /** @file Implementation of the MD2 importer class */
  2. #include "MD2Loader.h"
  3. #include "MaterialSystem.h"
  4. #include "MD2NormalTable.h"
  5. #include "../include/IOStream.h"
  6. #include "../include/IOSystem.h"
  7. #include "../include/aiMesh.h"
  8. #include "../include/aiScene.h"
  9. #include "../include/aiAssert.h"
  10. #include <boost/scoped_ptr.hpp>
  11. using namespace Assimp;
  12. // ------------------------------------------------------------------------------------------------
  13. inline bool is_qnan(float p_fIn)
  14. {
  15. // NOTE: Comparison against qnan is generally problematic
  16. // because qnan == qnan is false AFAIK
  17. union FTOINT
  18. {
  19. float fFloat;
  20. int32_t iInt;
  21. } one, two;
  22. one.fFloat = std::numeric_limits<float>::quiet_NaN();
  23. two.fFloat = p_fIn;
  24. return (one.iInt == two.iInt);
  25. }
  26. // ------------------------------------------------------------------------------------------------
  27. inline bool is_not_qnan(float p_fIn)
  28. {
  29. return !is_qnan(p_fIn);
  30. }
  31. // ------------------------------------------------------------------------------------------------
  32. // Constructor to be privately used by Importer
  33. MD2Importer::MD2Importer()
  34. {
  35. }
  36. // ------------------------------------------------------------------------------------------------
  37. // Destructor, private as well
  38. MD2Importer::~MD2Importer()
  39. {
  40. }
  41. // ------------------------------------------------------------------------------------------------
  42. // Returns whether the class can handle the format of the given file.
  43. bool MD2Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  44. {
  45. // simple check of file extension is enough for the moment
  46. std::string::size_type pos = pFile.find_last_of('.');
  47. // no file extension - can't read
  48. if( pos == std::string::npos)
  49. return false;
  50. std::string extension = pFile.substr( pos);
  51. // not brilliant but working ;-)
  52. if( extension == ".md2" || extension == ".MD2" ||
  53. extension == ".mD2" || extension == ".Md2")
  54. return true;
  55. return false;
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Imports the given file into the given scene structure.
  59. void MD2Importer::InternReadFile(
  60. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  61. {
  62. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  63. // Check whether we can read from the file
  64. if( file.get() == NULL)
  65. {
  66. throw new ImportErrorException( "Failed to open md2 file " + pFile + ".");
  67. }
  68. // check whether the md3 file is large enough to contain
  69. // at least the file header
  70. size_t fileSize = file->FileSize();
  71. if( fileSize < sizeof(MD2::Header))
  72. {
  73. throw new ImportErrorException( ".md2 File is too small.");
  74. }
  75. // allocate storage and copy the contents of the file to a memory buffer
  76. this->mBuffer = new unsigned char[fileSize];
  77. file->Read( (void*)mBuffer, 1, fileSize);
  78. this->m_pcHeader = (const MD2::Header*)this->mBuffer;
  79. // check magic number
  80. if (this->m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_BE &&
  81. this->m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_LE)
  82. {
  83. throw new ImportErrorException( "Invalid md2 file: Magic bytes not found");
  84. }
  85. // check file format version
  86. if (this->m_pcHeader->version != 8)
  87. {
  88. throw new ImportErrorException( "Unsupported md3 file version");
  89. }
  90. // check some values whether they are valid
  91. if (0 == this->m_pcHeader->numFrames)
  92. {
  93. throw new ImportErrorException( "Invalid md2 file: NUM_FRAMES is 0");
  94. }
  95. if (this->m_pcHeader->offsetEnd > (int32_t)fileSize)
  96. {
  97. throw new ImportErrorException( "Invalid md2 file: File is too small");
  98. }
  99. // there won't be more than one mesh inside the file
  100. pScene->mNumMaterials = 1;
  101. pScene->mRootNode = new aiNode();
  102. pScene->mRootNode->mNumMeshes = 1;
  103. pScene->mRootNode->mMeshes = new unsigned int[1];
  104. pScene->mRootNode->mMeshes[0] = 0;
  105. pScene->mMaterials = new aiMaterial*[1];
  106. pScene->mMaterials[0] = new MaterialHelper();
  107. pScene->mNumMeshes = 1;
  108. pScene->mMeshes = new aiMesh*[1];
  109. pScene->mMeshes[0] = new aiMesh();
  110. // navigate to the begin of the frame data
  111. const MD2::Frame* pcFrame = (const MD2::Frame*) ((unsigned char*)this->m_pcHeader +
  112. this->m_pcHeader->offsetFrames);
  113. // navigate to the begin of the triangle data
  114. MD2::Triangle* pcTriangles = (MD2::Triangle*) ((unsigned char*)this->m_pcHeader +
  115. this->m_pcHeader->offsetTriangles);
  116. // navigate to the begin of the tex coords data
  117. const MD2::TexCoord* pcTexCoords = (const MD2::TexCoord*) ((unsigned char*)this->m_pcHeader +
  118. this->m_pcHeader->offsetTexCoords);
  119. // navigate to the begin of the vertex data
  120. const MD2::Vertex* pcVerts = (const MD2::Vertex*) (pcFrame->vertices);
  121. pScene->mMeshes[0]->mNumFaces = this->m_pcHeader->numTriangles;
  122. pScene->mMeshes[0]->mFaces = new aiFace[this->m_pcHeader->numTriangles];
  123. // temporary vectors for position/texture coordinates/normals
  124. std::vector<aiVector3D> vPositions;
  125. std::vector<aiVector3D> vTexCoords;
  126. std::vector<aiVector3D> vNormals;
  127. vPositions.resize(this->m_pcHeader->numVertices,aiVector3D());
  128. vTexCoords.resize(this->m_pcHeader->numVertices,aiVector3D(
  129. std::numeric_limits<float>::quiet_NaN(),
  130. std::numeric_limits<float>::quiet_NaN(),0.0f));
  131. vNormals.resize(this->m_pcHeader->numVertices,aiVector3D());
  132. // not sure whether there are MD2 files without texture coordinates
  133. if (0 != this->m_pcHeader->numTexCoords && 0 != this->m_pcHeader->numSkins)
  134. {
  135. // navigate to the first texture associated with the mesh
  136. const MD2::Skin* pcSkins = (const MD2::Skin*) ((unsigned char*)this->m_pcHeader +
  137. this->m_pcHeader->offsetSkins);
  138. const int iMode = (int)aiShadingMode_Gouraud;
  139. MaterialHelper* pcHelper = (MaterialHelper*)pScene->mMaterials[0];
  140. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  141. aiColor3D clr;
  142. clr.b = clr.g = clr.r = 1.0f;
  143. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  144. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  145. clr.b = clr.g = clr.r = 0.05f;
  146. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  147. aiString szString;
  148. const size_t iLen = strlen(pcSkins->name);
  149. memcpy(szString.data,pcSkins->name,iLen+1);
  150. szString.length = iLen-1;
  151. pcHelper->AddProperty(&szString,AI_MATKEY_TEXTURE_DIFFUSE(0));
  152. }
  153. else
  154. {
  155. // apply a default material
  156. const int iMode = (int)aiShadingMode_Gouraud;
  157. MaterialHelper* pcHelper = (MaterialHelper*)pScene->mMaterials[0];
  158. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  159. aiColor3D clr;
  160. clr.b = clr.g = clr.r = 0.6f;
  161. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  162. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  163. clr.b = clr.g = clr.r = 0.05f;
  164. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  165. }
  166. // now read all vertices of the frame
  167. for (unsigned int i = 0; i < (unsigned int)this->m_pcHeader->numVertices;++i)
  168. {
  169. // read x,y, and z component of the vertex
  170. aiVector3D& vec = vPositions[i];
  171. vec.x = (float)pcVerts[i].vertex[0] * pcFrame->scale[0];
  172. vec.x += pcFrame->translate[0];
  173. // (flip z and y component)
  174. vec.z = (float)pcVerts[i].vertex[1] * pcFrame->scale[1];
  175. vec.z += pcFrame->translate[1];
  176. vec.y = (float)pcVerts[i].vertex[2] * pcFrame->scale[2];
  177. vec.y += pcFrame->translate[2];
  178. // read the normal vector from the precalculated normal table
  179. vNormals[i] = *((const aiVector3D*)(&g_avNormals[std::min(
  180. int(pcVerts[i].lightNormalIndex),
  181. int(sizeof(g_avNormals) / sizeof(g_avNormals[0]))-1)]));
  182. std::swap ( vNormals[i].y,vNormals[i].z );
  183. }
  184. // now read all triangles of the first frame, apply scaling and translation
  185. if (0 != this->m_pcHeader->numTexCoords)
  186. {
  187. for (unsigned int i = 0; i < (unsigned int)this->m_pcHeader->numTriangles;++i)
  188. {
  189. // allocate the face
  190. pScene->mMeshes[0]->mFaces[i].mIndices = new unsigned int[3];
  191. pScene->mMeshes[0]->mFaces[i].mNumIndices = 3;
  192. // copy texture coordinates
  193. // check whether they are different from the previous value at this index.
  194. // In this case, create a full separate set of vertices/normals/texcoords
  195. for (unsigned int c = 0; c < 3;++c)
  196. {
  197. // validate vertex indices
  198. if (pcTriangles[i].vertexIndices[c] >= this->m_pcHeader->numVertices)
  199. pcTriangles[i].vertexIndices[c] = this->m_pcHeader->numVertices-1;
  200. // copy face indices
  201. pScene->mMeshes[0]->mFaces[i].mIndices[c] = (unsigned int)pcTriangles[i].vertexIndices[c];
  202. // validate texture coordinates
  203. if (pcTriangles[i].textureIndices[c] >= this->m_pcHeader->numTexCoords)
  204. pcTriangles[i].textureIndices[c] = this->m_pcHeader->numTexCoords-1;
  205. aiVector3D* pcOut = &vTexCoords[pScene->mMeshes[0]->mFaces[i].mIndices[c]];
  206. float u,v;
  207. u = (float)pcTexCoords[pcTriangles[i].textureIndices[c]].s / this->m_pcHeader->skinWidth;
  208. v = (float)pcTexCoords[pcTriangles[i].textureIndices[c]].t / this->m_pcHeader->skinHeight;
  209. if ( is_not_qnan ( pcOut->x ) && (pcOut->x != u || pcOut->y != v))
  210. {
  211. // generate a separate vertex/index set
  212. vTexCoords.push_back(aiVector3D(u,v,0.0f));
  213. vPositions.push_back(vPositions[pcTriangles[i].vertexIndices[c]]);
  214. vNormals.push_back(vPositions[pcTriangles[i].vertexIndices[c]]);
  215. unsigned int iPos = vTexCoords.size()-1;
  216. pScene->mMeshes[0]->mFaces[i].mIndices[c] = iPos;
  217. }
  218. else
  219. {
  220. pcOut->x = u;
  221. pcOut->y = v;
  222. }
  223. }
  224. }
  225. }
  226. else
  227. {
  228. for (unsigned int i = 0; i < (unsigned int)this->m_pcHeader->numTriangles;++i)
  229. {
  230. // allocate the face
  231. pScene->mMeshes[0]->mFaces[i].mIndices = new unsigned int[3];
  232. pScene->mMeshes[0]->mFaces[i].mNumIndices = 3;
  233. // validate vertex indices
  234. if (pcTriangles[i].vertexIndices[0] >= this->m_pcHeader->numVertices)
  235. pcTriangles[i].vertexIndices[0] = this->m_pcHeader->numVertices-1;
  236. if (pcTriangles[i].vertexIndices[1] >= this->m_pcHeader->numVertices)
  237. pcTriangles[i].vertexIndices[1] = this->m_pcHeader->numVertices-1;
  238. if (pcTriangles[i].vertexIndices[2] >= this->m_pcHeader->numVertices)
  239. pcTriangles[i].vertexIndices[2] = this->m_pcHeader->numVertices-1;
  240. // copy face indices
  241. pScene->mMeshes[0]->mFaces[i].mIndices[0] = (unsigned int)pcTriangles[i].vertexIndices[0];
  242. pScene->mMeshes[0]->mFaces[i].mIndices[1] = (unsigned int)pcTriangles[i].vertexIndices[1];
  243. pScene->mMeshes[0]->mFaces[i].mIndices[2] = (unsigned int)pcTriangles[i].vertexIndices[2];
  244. }
  245. }
  246. // allocate output storage
  247. pScene->mMeshes[0]->mNumVertices = vPositions.size();
  248. pScene->mMeshes[0]->mVertices = new aiVector3D[vPositions.size()];
  249. pScene->mMeshes[0]->mNormals = new aiVector3D[vPositions.size()];
  250. pScene->mMeshes[0]->mTextureCoords[0] = new aiVector3D[vPositions.size()];
  251. // memcpy() the data to the c-syle arrays
  252. memcpy(pScene->mMeshes[0]->mVertices, &vPositions[0], vPositions.size() * sizeof(aiVector3D));
  253. memcpy(pScene->mMeshes[0]->mNormals, &vNormals[0], vPositions.size() * sizeof(aiVector3D));
  254. memcpy(pScene->mMeshes[0]->mTextureCoords[0], &vTexCoords[0], vPositions.size() * sizeof(aiVector3D));
  255. return;
  256. }