HMPLoader.cpp 17 KB

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