HMPLoader.cpp 18 KB

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