HMPLoader.cpp 15 KB

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