HMPLoader.cpp 17 KB

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