HMPLoader.cpp 18 KB

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