HMPLoader.cpp 19 KB

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