2
0

HMPLoader.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 MDL importer class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "MaterialSystem.h"
  38. #include "HMPLoader.h"
  39. #include "MD2FileData.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. HMPImporter::HMPImporter()
  44. {
  45. // nothing to do here
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. HMPImporter::~HMPImporter()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the class can handle the format of the given file.
  55. bool HMPImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  56. {
  57. // simple check of file extension is enough for the moment
  58. std::string::size_type pos = pFile.find_last_of('.');
  59. // no file extension - can't read
  60. if( pos == std::string::npos)return false;
  61. std::string extension = pFile.substr( pos);
  62. for( std::string::iterator it = extension.begin(); it != extension.end(); ++it)
  63. *it = tolower( *it);
  64. return extension == ".hmp";
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Imports the given file into the given scene structure.
  68. void HMPImporter::InternReadFile( const std::string& pFile,
  69. aiScene* pScene, IOSystem* pIOHandler)
  70. {
  71. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  72. // Check whether we can read from the file
  73. if( file.get() == NULL)
  74. throw new ImportErrorException( "Failed to open HMP file " + pFile + ".");
  75. // Check whether the HMP file is large enough to contain
  76. // at least the file header
  77. const size_t fileSize = file->FileSize();
  78. if( fileSize < 50)
  79. throw new ImportErrorException( "HMP File is too small.");
  80. // Allocate storage and copy the contents of the file to a memory buffer
  81. this->pScene = pScene;
  82. this->pIOHandler = pIOHandler;
  83. std::vector<uint8_t> buffer(fileSize+1);
  84. mBuffer = &buffer[0];
  85. file->Read( (void*)mBuffer, 1, fileSize);
  86. iFileSize = (unsigned int)fileSize;
  87. // Determine the file subtype and call the appropriate member function
  88. uint32_t iMagic = *((uint32_t*)this->mBuffer);
  89. // HMP4 format
  90. if (AI_HMP_MAGIC_NUMBER_LE_4 == iMagic ||
  91. AI_HMP_MAGIC_NUMBER_BE_4 == iMagic)
  92. {
  93. DefaultLogger::get()->debug("HMP subtype: 3D GameStudio A4, magic word is HMP4");
  94. InternReadFile_HMP4();
  95. }
  96. // HMP5 format
  97. else if (AI_HMP_MAGIC_NUMBER_LE_5 == iMagic ||
  98. AI_HMP_MAGIC_NUMBER_BE_5 == iMagic)
  99. {
  100. DefaultLogger::get()->debug("HMP subtype: 3D GameStudio A5, magic word is HMP5");
  101. InternReadFile_HMP5();
  102. }
  103. // HMP7 format
  104. else if (AI_HMP_MAGIC_NUMBER_LE_7 == iMagic ||
  105. AI_HMP_MAGIC_NUMBER_BE_7 == iMagic)
  106. {
  107. DefaultLogger::get()->debug("HMP subtype: 3D GameStudio A7, magic word is HMP7");
  108. InternReadFile_HMP7();
  109. }
  110. else
  111. {
  112. // Print the magic word to the logger
  113. char szBuffer[5];
  114. szBuffer[0] = ((char*)&iMagic)[0];
  115. szBuffer[1] = ((char*)&iMagic)[1];
  116. szBuffer[2] = ((char*)&iMagic)[2];
  117. szBuffer[3] = ((char*)&iMagic)[3];
  118. szBuffer[4] = '\0';
  119. // We're definitely unable to load this file
  120. throw new ImportErrorException( "Unknown HMP subformat " + pFile +
  121. ". Magic word (" + szBuffer + ") is not known");
  122. }
  123. // Set the AI_SCENE_FLAGS_TERRAIN bit
  124. pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN;
  125. // File buffer destructs automatically now
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. void HMPImporter::ValidateHeader_HMP457( )
  129. {
  130. const HMP::Header_HMP5* const pcHeader = (const HMP::Header_HMP5*)this->mBuffer;
  131. if (120 > this->iFileSize)
  132. {
  133. throw new ImportErrorException("HMP file is too small (header size is "
  134. "120 bytes, this file is smaller)");
  135. }
  136. if (!pcHeader->ftrisize_x || !pcHeader->ftrisize_y)
  137. throw new ImportErrorException("Size of triangles in either x or y direction is zero");
  138. if(pcHeader->fnumverts_x < 1.0f || (pcHeader->numverts/pcHeader->fnumverts_x) < 1.0f)
  139. throw new ImportErrorException("Number of triangles in either x or y direction is zero");
  140. if(!pcHeader->numframes)
  141. throw new ImportErrorException("There are no frames. At least one should be there");
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. void HMPImporter::InternReadFile_HMP4( )
  145. {
  146. throw new ImportErrorException("HMP4 is currently not supported");
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. void HMPImporter::InternReadFile_HMP5( )
  150. {
  151. // read the file header and skip everything to byte 84
  152. const HMP::Header_HMP5* pcHeader = (const HMP::Header_HMP5*)mBuffer;
  153. const unsigned char* szCurrent = (const unsigned char*)(mBuffer+84);
  154. ValidateHeader_HMP457();
  155. // generate an output mesh
  156. pScene->mNumMeshes = 1;
  157. pScene->mMeshes = new aiMesh*[1];
  158. aiMesh* pcMesh = pScene->mMeshes[0] = new aiMesh();
  159. pcMesh->mMaterialIndex = 0;
  160. pcMesh->mVertices = new aiVector3D[pcHeader->numverts];
  161. pcMesh->mNormals = new aiVector3D[pcHeader->numverts];
  162. const unsigned int height = (unsigned int)(pcHeader->numverts / pcHeader->fnumverts_x);
  163. const unsigned int width = (unsigned int)pcHeader->fnumverts_x;
  164. // generate/load a material for the terrain
  165. CreateMaterial(szCurrent,&szCurrent);
  166. // goto offset 120, I don't know why ...
  167. // (fixme) is this the frame header? I assume yes since it starts with 2.
  168. szCurrent += 36;
  169. SizeCheck(szCurrent + sizeof(const HMP::Vertex_HMP7)*height*width);
  170. // now load all vertices from the file
  171. aiVector3D* pcVertOut = pcMesh->mVertices;
  172. aiVector3D* pcNorOut = pcMesh->mNormals;
  173. const HMP::Vertex_HMP5* src = (const HMP::Vertex_HMP5*) szCurrent;
  174. for (unsigned int y = 0; y < height;++y)
  175. {
  176. for (unsigned int x = 0; x < width;++x)
  177. {
  178. pcVertOut->x = x * pcHeader->ftrisize_x;
  179. pcVertOut->y = y * pcHeader->ftrisize_y;
  180. pcVertOut->z = (((float)src->z / 0xffff)-0.5f) * pcHeader->ftrisize_x * 8.0f;
  181. MD2::LookupNormalIndex(src->normals162index, *pcNorOut );
  182. ++pcVertOut;++pcNorOut;++src;
  183. }
  184. }
  185. // generate texture coordinates if necessary
  186. if (pcHeader->numskins)GenerateTextureCoords(width,height);
  187. // now build a list of faces
  188. CreateOutputFaceList(width,height);
  189. // there is no nodegraph in HMP files. Simply assign the one mesh
  190. // (no, not the one ring) to the root node
  191. pScene->mRootNode = new aiNode();
  192. pScene->mRootNode->mName.Set("terrain_root");
  193. pScene->mRootNode->mNumMeshes = 1;
  194. pScene->mRootNode->mMeshes = new unsigned int[1];
  195. pScene->mRootNode->mMeshes[0] = 0;
  196. }
  197. // ------------------------------------------------------------------------------------------------
  198. void HMPImporter::InternReadFile_HMP7( )
  199. {
  200. // read the file header and skip everything to byte 84
  201. const HMP::Header_HMP5* const pcHeader = (const HMP::Header_HMP5*)mBuffer;
  202. const unsigned char* szCurrent = (const unsigned char*)(mBuffer+84);
  203. ValidateHeader_HMP457();
  204. // generate an output mesh
  205. pScene->mNumMeshes = 1;
  206. pScene->mMeshes = new aiMesh*[1];
  207. aiMesh* pcMesh = pScene->mMeshes[0] = new aiMesh();
  208. pcMesh->mMaterialIndex = 0;
  209. pcMesh->mVertices = new aiVector3D[pcHeader->numverts];
  210. pcMesh->mNormals = new aiVector3D[pcHeader->numverts];
  211. const unsigned int height = (unsigned int)(pcHeader->numverts / pcHeader->fnumverts_x);
  212. const unsigned int width = (unsigned int)pcHeader->fnumverts_x;
  213. // generate/load a material for the terrain
  214. CreateMaterial(szCurrent,&szCurrent);
  215. // goto offset 120, I don't know why ...
  216. // (fixme) is this the frame header? I assume yes since it starts with 2.
  217. szCurrent += 36;
  218. SizeCheck(szCurrent + sizeof(const HMP::Vertex_HMP7)*height*width);
  219. // now load all vertices from the file
  220. aiVector3D* pcVertOut = pcMesh->mVertices;
  221. aiVector3D* pcNorOut = pcMesh->mNormals;
  222. const HMP::Vertex_HMP7* src = (const HMP::Vertex_HMP7*) szCurrent;
  223. for (unsigned int y = 0; y < height;++y)
  224. {
  225. for (unsigned int x = 0; x < width;++x)
  226. {
  227. pcVertOut->x = x * pcHeader->ftrisize_x;
  228. pcVertOut->y = y * pcHeader->ftrisize_y;
  229. // FIXME: What exctly is the correct scaling factor to use?
  230. // possibly pcHeader->scale_origin[2] in combination with a
  231. // signed interpretation of src->z?
  232. pcVertOut->z = (((float)src->z / 0xffff)-0.5f) * pcHeader->ftrisize_x * 8.0f;
  233. pcNorOut->x = ((float)src->normal_x / 0x80 ); // * pcHeader->scale_origin[0];
  234. pcNorOut->y = ((float)src->normal_y / 0x80 ); // * pcHeader->scale_origin[1];
  235. pcNorOut->z = 1.0f;
  236. pcNorOut->Normalize();
  237. ++pcVertOut;++pcNorOut;++src;
  238. }
  239. }
  240. // generate texture coordinates if necessary
  241. if (pcHeader->numskins)GenerateTextureCoords(width,height);
  242. // now build a list of faces
  243. CreateOutputFaceList(width,height);
  244. // there is no nodegraph in HMP files. Simply assign the one mesh
  245. // (no, not the One Ring) to the root node
  246. pScene->mRootNode = new aiNode();
  247. pScene->mRootNode->mName.Set("terrain_root");
  248. pScene->mRootNode->mNumMeshes = 1;
  249. pScene->mRootNode->mMeshes = new unsigned int[1];
  250. pScene->mRootNode->mMeshes[0] = 0;
  251. }
  252. // ------------------------------------------------------------------------------------------------
  253. void HMPImporter::CreateMaterial(const unsigned char* szCurrent,
  254. const unsigned char** szCurrentOut)
  255. {
  256. aiMesh* const pcMesh = pScene->mMeshes[0];
  257. const HMP::Header_HMP5* const pcHeader = (const HMP::Header_HMP5*)mBuffer;
  258. // we don't need to generate texture coordinates if
  259. // we have no textures in the file ...
  260. if (pcHeader->numskins)
  261. {
  262. pcMesh->mTextureCoords[0] = new aiVector3D[pcHeader->numverts];
  263. pcMesh->mNumUVComponents[0] = 2;
  264. // now read the first skin and skip all others
  265. ReadFirstSkin(pcHeader->numskins,szCurrent,&szCurrent);
  266. }
  267. else
  268. {
  269. // generate a default material
  270. const int iMode = (int)aiShadingMode_Gouraud;
  271. MaterialHelper* pcHelper = new MaterialHelper();
  272. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  273. aiColor3D clr;
  274. clr.b = clr.g = clr.r = 0.6f;
  275. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  276. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  277. clr.b = clr.g = clr.r = 0.05f;
  278. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  279. aiString szName;
  280. szName.Set(AI_DEFAULT_MATERIAL_NAME);
  281. pcHelper->AddProperty(&szName,AI_MATKEY_NAME);
  282. // add the material to the scene
  283. pScene->mNumMaterials = 1;
  284. pScene->mMaterials = new aiMaterial*[1];
  285. pScene->mMaterials[0] = pcHelper;
  286. }
  287. *szCurrentOut = szCurrent;
  288. }
  289. // ------------------------------------------------------------------------------------------------
  290. void HMPImporter::CreateOutputFaceList(unsigned int width,unsigned int height)
  291. {
  292. aiMesh* const pcMesh = this->pScene->mMeshes[0];
  293. // Allocate enough storage
  294. pcMesh->mNumFaces = (width-1) * (height-1);
  295. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  296. pcMesh->mNumVertices = pcMesh->mNumFaces*4;
  297. aiVector3D* pcVertices = new aiVector3D[pcMesh->mNumVertices];
  298. aiVector3D* pcNormals = new aiVector3D[pcMesh->mNumVertices];
  299. aiFace* pcFaceOut(pcMesh->mFaces);
  300. aiVector3D* pcVertOut = pcVertices;
  301. aiVector3D* pcNorOut = pcNormals;
  302. aiVector3D* pcUVs = pcMesh->mTextureCoords[0] ? new aiVector3D[pcMesh->mNumVertices] : NULL;
  303. aiVector3D* pcUVOut(pcUVs);
  304. // Build the terrain square
  305. unsigned int iCurrent = 0;
  306. for (unsigned int y = 0; y < height-1;++y) {
  307. for (unsigned int x = 0; x < width-1;++x,++pcFaceOut) {
  308. pcFaceOut->mNumIndices = 4;
  309. pcFaceOut->mIndices = new unsigned int[4];
  310. *pcVertOut++ = pcMesh->mVertices[y*width+x];
  311. *pcVertOut++ = pcMesh->mVertices[(y+1)*width+x];
  312. *pcVertOut++ = pcMesh->mVertices[(y+1)*width+x+1];
  313. *pcVertOut++ = pcMesh->mVertices[y*width+x+1];
  314. *pcNorOut++ = pcMesh->mNormals[y*width+x];
  315. *pcNorOut++ = pcMesh->mNormals[(y+1)*width+x];
  316. *pcNorOut++ = pcMesh->mNormals[(y+1)*width+x+1];
  317. *pcNorOut++ = pcMesh->mNormals[y*width+x+1];
  318. if (pcMesh->mTextureCoords[0])
  319. {
  320. *pcUVOut++ = pcMesh->mTextureCoords[0][y*width+x];
  321. *pcUVOut++ = pcMesh->mTextureCoords[0][(y+1)*width+x];
  322. *pcUVOut++ = pcMesh->mTextureCoords[0][(y+1)*width+x+1];
  323. *pcUVOut++ = pcMesh->mTextureCoords[0][y*width+x+1];
  324. }
  325. for (unsigned int i = 0; i < 4;++i)
  326. pcFaceOut->mIndices[i] = iCurrent++;
  327. }
  328. }
  329. delete[] pcMesh->mVertices;
  330. pcMesh->mVertices = pcVertices;
  331. delete[] pcMesh->mNormals;
  332. pcMesh->mNormals = pcNormals;
  333. if (pcMesh->mTextureCoords[0])
  334. {
  335. delete[] pcMesh->mTextureCoords[0];
  336. pcMesh->mTextureCoords[0] = pcUVs;
  337. }
  338. }
  339. // ------------------------------------------------------------------------------------------------
  340. void HMPImporter::ReadFirstSkin(unsigned int iNumSkins, const unsigned char* szCursor,
  341. const unsigned char** szCursorOut)
  342. {
  343. ai_assert(0 != iNumSkins && NULL != szCursor);
  344. // read the type of the skin ...
  345. // sometimes we need to skip 12 bytes here, I don't know why ...
  346. uint32_t iType = *((uint32_t*)szCursor);szCursor += sizeof(uint32_t);
  347. if (0 == iType)
  348. {
  349. szCursor += sizeof(uint32_t) * 2;
  350. iType = *((uint32_t*)szCursor);szCursor += sizeof(uint32_t);
  351. if (!iType)
  352. throw new ImportErrorException("Unable to read HMP7 skin chunk");
  353. }
  354. // read width and height
  355. uint32_t iWidth = *((uint32_t*)szCursor); szCursor += sizeof(uint32_t);
  356. uint32_t iHeight = *((uint32_t*)szCursor); szCursor += sizeof(uint32_t);
  357. // allocate an output material
  358. MaterialHelper* pcMat = new MaterialHelper();
  359. // read the skin, this works exactly as for MDL7
  360. ParseSkinLump_3DGS_MDL7(szCursor,&szCursor,
  361. pcMat,iType,iWidth,iHeight);
  362. // now we need to skip any other skins ...
  363. for (unsigned int i = 1; i< iNumSkins;++i)
  364. {
  365. iType = *((uint32_t*)szCursor); szCursor += sizeof(uint32_t);
  366. iWidth = *((uint32_t*)szCursor); szCursor += sizeof(uint32_t);
  367. iHeight = *((uint32_t*)szCursor); szCursor += sizeof(uint32_t);
  368. SkipSkinLump_3DGS_MDL7(szCursor,&szCursor,iType,iWidth,iHeight);
  369. SizeCheck(szCursor);
  370. }
  371. // setup the material ...
  372. pScene->mNumMaterials = 1;
  373. pScene->mMaterials = new aiMaterial*[1];
  374. pScene->mMaterials[0] = pcMat;
  375. *szCursorOut = szCursor;
  376. }
  377. // ------------------------------------------------------------------------------------------------
  378. void HMPImporter::GenerateTextureCoords(
  379. const unsigned int width, const unsigned int height)
  380. {
  381. ai_assert(NULL != pScene->mMeshes && NULL != pScene->mMeshes[0] &&
  382. NULL != pScene->mMeshes[0]->mTextureCoords[0]);
  383. aiVector3D* uv = pScene->mMeshes[0]->mTextureCoords[0];
  384. const float fY = (1.0f / height) + (1.0f / height) / (height-1);
  385. const float fX = (1.0f / width) + (1.0f / width) / (width-1);
  386. for (unsigned int y = 0; y < height;++y) {
  387. for (unsigned int x = 0; x < width;++x,++uv) {
  388. uv->y = 1.0f-fY*y;
  389. uv->x = fX*x;
  390. uv->z = 0.0f;
  391. }
  392. }
  393. }