MDLMaterialLoader.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 material part of the MDL importer class */
  35. // internal headers
  36. #include "MaterialSystem.h"
  37. #include "MDLLoader.h"
  38. #include "MDLDefaultColorMap.h"
  39. #include "qnan.h"
  40. // public ASSIMP headers
  41. #include "../include/DefaultLogger.h"
  42. #include "../include/IOStream.h"
  43. #include "../include/IOSystem.h"
  44. #include "../include/aiMesh.h"
  45. #include "../include/aiScene.h"
  46. #include "../include/aiAssert.h"
  47. using namespace Assimp;
  48. // ------------------------------------------------------------------------------------------------
  49. void MDLImporter::SearchPalette(const unsigned char** pszColorMap)
  50. {
  51. // now try to find the color map in the current directory
  52. IOStream* pcStream = this->pIOHandler->Open("colormap.lmp","rb");
  53. const unsigned char* szColorMap = (const unsigned char*)::g_aclrDefaultColorMap;
  54. if(pcStream)
  55. {
  56. if (pcStream->FileSize() >= 768)
  57. {
  58. szColorMap = new unsigned char[256*3];
  59. pcStream->Read(const_cast<unsigned char*>(szColorMap),256*3,1);
  60. DefaultLogger::get()->info("Found valid colormap.lmp in directory. "
  61. "It will be used to decode embedded textures in palletized formats.");
  62. }
  63. delete pcStream;
  64. pcStream = NULL;
  65. }
  66. *pszColorMap = szColorMap;
  67. return;
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. void MDLImporter::FreePalette(const unsigned char* szColorMap)
  71. {
  72. if (szColorMap != (const unsigned char*)::g_aclrDefaultColorMap)
  73. {
  74. delete[] szColorMap;
  75. }
  76. return;
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. aiColor4D MDLImporter::ReplaceTextureWithColor(const aiTexture* pcTexture)
  80. {
  81. ai_assert(NULL != pcTexture);
  82. aiColor4D clrOut;
  83. clrOut.r = std::numeric_limits<float>::quiet_NaN();
  84. if (!pcTexture->mHeight || !pcTexture->mWidth)return clrOut;
  85. const unsigned int iNumPixels = pcTexture->mHeight*pcTexture->mWidth;
  86. const aiTexel* pcTexel = pcTexture->pcData+1;
  87. const aiTexel* const pcTexelEnd = &pcTexture->pcData[iNumPixels];
  88. while (pcTexel != pcTexelEnd)
  89. {
  90. if (*pcTexel != *(pcTexel-1))
  91. {
  92. pcTexel = NULL;break;
  93. }
  94. ++pcTexel;
  95. }
  96. if (pcTexel)
  97. {
  98. clrOut.r = pcTexture->pcData->r / 255.0f;
  99. clrOut.g = pcTexture->pcData->g / 255.0f;
  100. clrOut.b = pcTexture->pcData->b / 255.0f;
  101. clrOut.a = pcTexture->pcData->a / 255.0f;
  102. }
  103. return clrOut;
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. void MDLImporter::CreateTextureARGB8_3DGS_MDL3(const unsigned char* szData)
  107. {
  108. const MDL::Header* pcHeader = (const MDL::Header*)this->mBuffer;
  109. VALIDATE_FILE_SIZE(szData + pcHeader->skinwidth *
  110. pcHeader->skinheight);
  111. // allocate a new texture object
  112. aiTexture* pcNew = new aiTexture();
  113. pcNew->mWidth = pcHeader->skinwidth;
  114. pcNew->mHeight = pcHeader->skinheight;
  115. pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
  116. const unsigned char* szColorMap;
  117. this->SearchPalette(&szColorMap);
  118. // copy texture data
  119. for (unsigned int i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  120. {
  121. const unsigned char val = szData[i];
  122. const unsigned char* sz = &szColorMap[val*3];
  123. pcNew->pcData[i].a = 0xFF;
  124. pcNew->pcData[i].r = *sz++;
  125. pcNew->pcData[i].g = *sz++;
  126. pcNew->pcData[i].b = *sz;
  127. }
  128. this->FreePalette(szColorMap);
  129. // store the texture
  130. aiTexture** pc = this->pScene->mTextures;
  131. this->pScene->mTextures = new aiTexture*[this->pScene->mNumTextures+1];
  132. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  133. this->pScene->mTextures[i] = pc[i];
  134. this->pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  135. this->pScene->mNumTextures++;
  136. delete[] pc;
  137. return;
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. void MDLImporter::CreateTexture_3DGS_MDL4(const unsigned char* szData,
  141. unsigned int iType,
  142. unsigned int* piSkip)
  143. {
  144. const MDL::Header* pcHeader = (const MDL::Header*)this->mBuffer;
  145. ai_assert(NULL != piSkip);
  146. if (iType == 1 || iType > 3)
  147. {
  148. DefaultLogger::get()->error("Unsupported texture file format");
  149. return;
  150. }
  151. bool bNoRead = *piSkip == 0xffffffff;
  152. // allocate a new texture object
  153. aiTexture* pcNew = new aiTexture();
  154. pcNew->mWidth = pcHeader->skinwidth;
  155. pcNew->mHeight = pcHeader->skinheight;
  156. if (bNoRead)pcNew->pcData = (aiTexel*)0xffffffff;
  157. this->ParseTextureColorData(szData,iType,piSkip,pcNew);
  158. // store the texture
  159. if (!bNoRead)
  160. {
  161. if (!this->pScene->mNumTextures)
  162. {
  163. this->pScene->mNumTextures = 1;
  164. this->pScene->mTextures = new aiTexture*[1];
  165. this->pScene->mTextures[0] = pcNew;
  166. }
  167. else
  168. {
  169. aiTexture** pc = this->pScene->mTextures;
  170. this->pScene->mTextures = new aiTexture*[this->pScene->mNumTextures+1];
  171. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  172. this->pScene->mTextures[i] = pc[i];
  173. this->pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  174. this->pScene->mNumTextures++;
  175. delete[] pc;
  176. }
  177. }
  178. else delete pcNew;
  179. return;
  180. }
  181. // ------------------------------------------------------------------------------------------------
  182. void MDLImporter::ParseTextureColorData(const unsigned char* szData,
  183. unsigned int iType,
  184. unsigned int* piSkip,
  185. aiTexture* pcNew)
  186. {
  187. // allocate storage for the texture image
  188. if ((aiTexel*)0xffffffff != pcNew->pcData)
  189. pcNew->pcData = new aiTexel[pcNew->mWidth * pcNew->mHeight];
  190. // R5G6B5 format (with or without MIPs)
  191. // ****************************************************************
  192. if (2 == iType || 10 == iType)
  193. {
  194. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*2);
  195. // copy texture data
  196. unsigned int i;
  197. if ((aiTexel*)0xffffffff != pcNew->pcData)
  198. {
  199. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  200. {
  201. MDL::RGB565 val = ((MDL::RGB565*)szData)[i];
  202. pcNew->pcData[i].a = 0xFF;
  203. pcNew->pcData[i].r = (unsigned char)val.b << 3;
  204. pcNew->pcData[i].g = (unsigned char)val.g << 2;
  205. pcNew->pcData[i].b = (unsigned char)val.r << 3;
  206. }
  207. }
  208. else i = pcNew->mWidth*pcNew->mHeight;
  209. *piSkip = i * 2;
  210. // apply MIP maps
  211. if (10 == iType)
  212. {
  213. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  214. VALIDATE_FILE_SIZE(szData + *piSkip);
  215. }
  216. }
  217. // ARGB4 format (with or without MIPs)
  218. // ****************************************************************
  219. else if (3 == iType || 11 == iType)
  220. {
  221. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  222. // copy texture data
  223. unsigned int i;
  224. if ((aiTexel*)0xffffffff != pcNew->pcData)
  225. {
  226. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  227. {
  228. MDL::ARGB4 val = ((MDL::ARGB4*)szData)[i];
  229. pcNew->pcData[i].a = (unsigned char)val.a << 4;
  230. pcNew->pcData[i].r = (unsigned char)val.r << 4;
  231. pcNew->pcData[i].g = (unsigned char)val.g << 4;
  232. pcNew->pcData[i].b = (unsigned char)val.b << 4;
  233. }
  234. }
  235. else i = pcNew->mWidth*pcNew->mHeight;
  236. *piSkip = i * 2;
  237. // apply MIP maps
  238. if (11 == iType)
  239. {
  240. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 1;
  241. VALIDATE_FILE_SIZE(szData + *piSkip);
  242. }
  243. }
  244. // RGB8 format (with or without MIPs)
  245. // ****************************************************************
  246. else if (4 == iType || 12 == iType)
  247. {
  248. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*3);
  249. // copy texture data
  250. unsigned int i;
  251. if ((aiTexel*)0xffffffff != pcNew->pcData)
  252. {
  253. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  254. {
  255. const unsigned char* _szData = &szData[i*3];
  256. pcNew->pcData[i].a = 0xFF;
  257. pcNew->pcData[i].b = *_szData++;
  258. pcNew->pcData[i].g = *_szData++;
  259. pcNew->pcData[i].r = *_szData;
  260. }
  261. }
  262. else i = pcNew->mWidth*pcNew->mHeight;
  263. // apply MIP maps
  264. *piSkip = i * 3;
  265. if (12 == iType)
  266. {
  267. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) *3;
  268. VALIDATE_FILE_SIZE(szData + *piSkip);
  269. }
  270. }
  271. // ARGB8 format (with ir without MIPs)
  272. // ****************************************************************
  273. else if (5 == iType || 13 == iType)
  274. {
  275. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight*4);
  276. // copy texture data
  277. unsigned int i;
  278. if ((aiTexel*)0xffffffff != pcNew->pcData)
  279. {
  280. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  281. {
  282. const unsigned char* _szData = &szData[i*4];
  283. pcNew->pcData[i].b = *_szData++;
  284. pcNew->pcData[i].g = *_szData++;
  285. pcNew->pcData[i].r = *_szData++;
  286. pcNew->pcData[i].a = *_szData;
  287. }
  288. }
  289. else i = pcNew->mWidth*pcNew->mHeight;
  290. // apply MIP maps
  291. *piSkip = i << 2;
  292. if (13 == iType)
  293. {
  294. *piSkip += ((i >> 2) + (i >> 4) + (i >> 6)) << 2;
  295. }
  296. }
  297. // palletized 8 bit texture. As for Quake 1
  298. // ****************************************************************
  299. else if (0 == iType)
  300. {
  301. VALIDATE_FILE_SIZE(szData + pcNew->mWidth*pcNew->mHeight);
  302. // copy texture data
  303. unsigned int i;
  304. if ((aiTexel*)0xffffffff != pcNew->pcData)
  305. {
  306. const unsigned char* szColorMap;
  307. this->SearchPalette(&szColorMap);
  308. for (i = 0; i < pcNew->mWidth*pcNew->mHeight;++i)
  309. {
  310. const unsigned char val = szData[i];
  311. const unsigned char* sz = &szColorMap[val*3];
  312. pcNew->pcData[i].a = 0xFF;
  313. pcNew->pcData[i].r = *sz++;
  314. pcNew->pcData[i].g = *sz++;
  315. pcNew->pcData[i].b = *sz;
  316. }
  317. this->FreePalette(szColorMap);
  318. }
  319. else i = pcNew->mWidth*pcNew->mHeight;
  320. *piSkip = i;
  321. // FIXME: Also support for MIP maps?
  322. }
  323. return;
  324. }
  325. // ------------------------------------------------------------------------------------------------
  326. void MDLImporter::CreateTexture_3DGS_MDL5(const unsigned char* szData,
  327. unsigned int iType,
  328. unsigned int* piSkip)
  329. {
  330. ai_assert(NULL != piSkip);
  331. bool bNoRead = *piSkip == 0xffffffff;
  332. // allocate a new texture object
  333. aiTexture* pcNew = new aiTexture();
  334. VALIDATE_FILE_SIZE(szData+8);
  335. // first read the size of the texture
  336. pcNew->mWidth = *((uint32_t*)szData);
  337. szData += sizeof(uint32_t);
  338. pcNew->mHeight = *((uint32_t*)szData);
  339. szData += sizeof(uint32_t);
  340. if (bNoRead)pcNew->pcData = (aiTexel*)0xffffffff;
  341. // this should not occur - at least the docs say it shouldn't
  342. // however, you can easily try out what MED does if you have
  343. // a model with a DDS texture and export it to MDL5 ...
  344. // yes, you're right. It embedds the DDS texture ... :cry:
  345. if (6 == iType)
  346. {
  347. // this is a compressed texture in DDS format
  348. *piSkip = pcNew->mWidth;
  349. VALIDATE_FILE_SIZE(szData + *piSkip);
  350. if (!bNoRead)
  351. {
  352. // place a hint and let the application know that it's
  353. // a DDS file
  354. pcNew->mHeight = 0;
  355. pcNew->achFormatHint[0] = 'd';
  356. pcNew->achFormatHint[1] = 'd';
  357. pcNew->achFormatHint[2] = 's';
  358. pcNew->achFormatHint[3] = '\0';
  359. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  360. ::memcpy(pcNew->pcData,szData,pcNew->mWidth);
  361. }
  362. }
  363. else
  364. {
  365. // parse the color data of the texture
  366. this->ParseTextureColorData(szData,iType,
  367. piSkip,pcNew);
  368. }
  369. *piSkip += sizeof(uint32_t) * 2;
  370. if (!bNoRead)
  371. {
  372. // store the texture
  373. if (!this->pScene->mNumTextures)
  374. {
  375. this->pScene->mNumTextures = 1;
  376. this->pScene->mTextures = new aiTexture*[1];
  377. this->pScene->mTextures[0] = pcNew;
  378. }
  379. else
  380. {
  381. aiTexture** pc = this->pScene->mTextures;
  382. this->pScene->mTextures = new aiTexture*[this->pScene->mNumTextures+1];
  383. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  384. this->pScene->mTextures[i] = pc[i];
  385. this->pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  386. this->pScene->mNumTextures++;
  387. delete[] pc;
  388. }
  389. }
  390. else delete pcNew;
  391. return;
  392. }
  393. // ------------------------------------------------------------------------------------------------
  394. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  395. const unsigned char* szCurrent,
  396. const unsigned char** szCurrentOut,
  397. MaterialHelper* pcMatOut,
  398. unsigned int iType,
  399. unsigned int iWidth,
  400. unsigned int iHeight)
  401. {
  402. aiTexture* pcNew = NULL;
  403. // get the type of the skin
  404. unsigned int iMasked = (unsigned int)(iType & 0xF);
  405. if (0x1 == iMasked)
  406. {
  407. // ***** REFERENCE TO ANOTHER SKIN INDEX *****
  408. // NOTE: Documentation - if you can call it a documentation, I prefer
  409. // the expression "rubbish" - states it is currently unused. However,
  410. // I don't know what ideas the terrible developers of Conitec will
  411. // have tomorrow, so Im going to implement it.
  412. int referrer = (int)iWidth;
  413. pcMatOut->AddProperty<int>(&referrer,1,AI_MDL7_REFERRER_MATERIAL);
  414. }
  415. else if (0x6 == iMasked)
  416. {
  417. // ***** EMBEDDED DDS FILE *****
  418. if (1 != iHeight)
  419. {
  420. DefaultLogger::get()->warn("Found a reference to an embedded DDS texture, "
  421. "but texture height is not equal to 1, which is not supported by MED");
  422. }
  423. pcNew = new aiTexture();
  424. pcNew->mHeight = 0;
  425. pcNew->mWidth = iWidth;
  426. pcNew->achFormatHint[0] = 'd';
  427. pcNew->achFormatHint[1] = 'd';
  428. pcNew->achFormatHint[2] = 's';
  429. pcNew->achFormatHint[3] = '\0';
  430. pcNew->pcData = (aiTexel*) new unsigned char[pcNew->mWidth];
  431. memcpy(pcNew->pcData,szCurrent,pcNew->mWidth);
  432. szCurrent += iWidth;
  433. }
  434. if (0x7 == iMasked)
  435. {
  436. // ***** REFERENCE TO EXTERNAL FILE *****
  437. if (1 != iHeight)
  438. {
  439. DefaultLogger::get()->warn("Found a reference to an external texture, "
  440. "but texture height is not equal to 1, which is not supported by MED");
  441. }
  442. aiString szFile;
  443. const size_t iLen = strlen((const char*)szCurrent);
  444. size_t iLen2 = iLen+1;
  445. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  446. memcpy(szFile.data,(const char*)szCurrent,iLen2);
  447. szFile.length = iLen;
  448. szCurrent += iLen2;
  449. // place this as diffuse texture
  450. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  451. }
  452. else if (iMasked || !iType || (iType && iWidth && iHeight))
  453. {
  454. // ***** STANDARD COLOR TEXTURE *****
  455. pcNew = new aiTexture();
  456. if (!iHeight || !iWidth)
  457. {
  458. DefaultLogger::get()->warn("Found embedded texture, but its width "
  459. "an height are both 0. Is this a joke?");
  460. // generate an empty chess pattern
  461. pcNew->mWidth = pcNew->mHeight = 8;
  462. pcNew->pcData = new aiTexel[64];
  463. for (unsigned int x = 0; x < 8;++x)
  464. {
  465. for (unsigned int y = 0; y < 8;++y)
  466. {
  467. bool bSet = false;
  468. if (0 == x % 2 && 0 != y % 2 ||
  469. 0 != x % 2 && 0 == y % 2)bSet = true;
  470. aiTexel* pc = &pcNew->pcData[y * 8 + x];
  471. if (bSet)pc->r = pc->b = pc->g = 0xFF;
  472. else pc->r = pc->b = pc->g = 0;
  473. pc->a = 0xFF;
  474. }
  475. }
  476. }
  477. else
  478. {
  479. // it is a standard color texture. Fill in width and height
  480. // and call the same function we used for loading MDL5 files
  481. pcNew->mWidth = iWidth;
  482. pcNew->mHeight = iHeight;
  483. unsigned int iSkip = 0;
  484. this->ParseTextureColorData(szCurrent,iMasked,&iSkip,pcNew);
  485. // skip length of texture data
  486. szCurrent += iSkip;
  487. }
  488. }
  489. // sometimes there are MDL7 files which have a monochrome
  490. // texture instead of material colors ... posssible they have
  491. // been converted to MDL7 from other formats, such as MDL5
  492. aiColor4D clrTexture;
  493. if (pcNew)clrTexture = this->ReplaceTextureWithColor(pcNew);
  494. else clrTexture.r = std::numeric_limits<float>::quiet_NaN();
  495. // check whether a material definition is contained in the skin
  496. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  497. {
  498. const MDL::Material_MDL7* pcMatIn = (const MDL::Material_MDL7*)szCurrent;
  499. szCurrent = (unsigned char*)(pcMatIn+1);
  500. VALIDATE_FILE_SIZE(szCurrent);
  501. aiColor3D clrTemp;
  502. #define COLOR_MULTIPLY_RGB() \
  503. if (is_not_qnan(clrTexture.r)) \
  504. { \
  505. clrTemp.r *= clrTexture.r; \
  506. clrTemp.g *= clrTexture.g; \
  507. clrTemp.b *= clrTexture.b; \
  508. }
  509. // read diffuse color
  510. clrTemp.r = pcMatIn->Diffuse.r;
  511. clrTemp.g = pcMatIn->Diffuse.g;
  512. clrTemp.b = pcMatIn->Diffuse.b;
  513. COLOR_MULTIPLY_RGB();
  514. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_DIFFUSE);
  515. // read specular color
  516. clrTemp.r = pcMatIn->Specular.r;
  517. clrTemp.g = pcMatIn->Specular.g;
  518. clrTemp.b = pcMatIn->Specular.b;
  519. COLOR_MULTIPLY_RGB();
  520. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_SPECULAR);
  521. // read ambient color
  522. clrTemp.r = pcMatIn->Ambient.r;
  523. clrTemp.g = pcMatIn->Ambient.g;
  524. clrTemp.b = pcMatIn->Ambient.b;
  525. COLOR_MULTIPLY_RGB();
  526. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_AMBIENT);
  527. // read emissive color
  528. clrTemp.r = pcMatIn->Emissive.r;
  529. clrTemp.g = pcMatIn->Emissive.g;
  530. clrTemp.b = pcMatIn->Emissive.b;
  531. pcMatOut->AddProperty<aiColor3D>(&clrTemp,1,AI_MATKEY_COLOR_EMISSIVE);
  532. // FIX: Take the opacity from the ambient color
  533. // the doc says something else, but it is fact that MED exports the
  534. // opacity like this .... ARRRGGHH!
  535. clrTemp.r = pcMatIn->Ambient.a;
  536. if (is_not_qnan(clrTexture.r))clrTemp.r *= clrTexture.a;
  537. pcMatOut->AddProperty<float>(&clrTemp.r,1,AI_MATKEY_OPACITY);
  538. // read phong power
  539. int iShadingMode = (int)aiShadingMode_Gouraud;
  540. if (0.0f != pcMatIn->Power)
  541. {
  542. iShadingMode = (int)aiShadingMode_Phong;
  543. pcMatOut->AddProperty<float>(&pcMatIn->Power,1,AI_MATKEY_SHININESS);
  544. }
  545. pcMatOut->AddProperty<int>(&iShadingMode,1,AI_MATKEY_SHADING_MODEL);
  546. }
  547. else if (is_not_qnan(clrTexture.r))
  548. {
  549. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_DIFFUSE);
  550. pcMatOut->AddProperty<aiColor4D>(&clrTexture,1,AI_MATKEY_COLOR_SPECULAR);
  551. }
  552. // if the texture could be replaced by a single material color
  553. // we don't need the texture anymore
  554. if (is_not_qnan(clrTexture.r))
  555. {
  556. delete pcNew;
  557. pcNew = NULL;
  558. }
  559. // if an ASCII effect description (HLSL?) is contained in the file,
  560. // we can simply ignore it ...
  561. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  562. {
  563. VALIDATE_FILE_SIZE(szCurrent);
  564. int32_t iMe = *((int32_t*)szCurrent);
  565. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  566. VALIDATE_FILE_SIZE(szCurrent);
  567. }
  568. // if an embedded texture has been loaded setup the corresponding
  569. // data structures in the aiScene instance
  570. if (pcNew && this->pScene->mNumTextures <= 999)
  571. {
  572. // place this as diffuse texture
  573. char szCurrent[5];
  574. ::sprintf(szCurrent,"*%i",this->pScene->mNumTextures);
  575. aiString szFile;
  576. const size_t iLen = strlen((const char*)szCurrent);
  577. ::memcpy(szFile.data,(const char*)szCurrent,iLen+1);
  578. szFile.length = iLen;
  579. pcMatOut->AddProperty(&szFile,AI_MATKEY_TEXTURE_DIFFUSE(0));
  580. // store the texture
  581. if (!this->pScene->mNumTextures)
  582. {
  583. this->pScene->mNumTextures = 1;
  584. this->pScene->mTextures = new aiTexture*[1];
  585. this->pScene->mTextures[0] = pcNew;
  586. }
  587. else
  588. {
  589. aiTexture** pc = this->pScene->mTextures;
  590. this->pScene->mTextures = new aiTexture*[this->pScene->mNumTextures+1];
  591. for (unsigned int i = 0; i < this->pScene->mNumTextures;++i)
  592. this->pScene->mTextures[i] = pc[i];
  593. this->pScene->mTextures[this->pScene->mNumTextures] = pcNew;
  594. this->pScene->mNumTextures++;
  595. delete[] pc;
  596. }
  597. }
  598. VALIDATE_FILE_SIZE(szCurrent);
  599. *szCurrentOut = szCurrent;
  600. }
  601. // ------------------------------------------------------------------------------------------------
  602. void MDLImporter::SkipSkinLump_3DGS_MDL7(
  603. const unsigned char* szCurrent,
  604. const unsigned char** szCurrentOut,
  605. unsigned int iType,
  606. unsigned int iWidth,
  607. unsigned int iHeight)
  608. {
  609. // get the type of the skin
  610. unsigned int iMasked = (unsigned int)(iType & 0xF);
  611. if (0x6 == iMasked)
  612. {
  613. szCurrent += iWidth;
  614. }
  615. if (0x7 == iMasked)
  616. {
  617. const size_t iLen = ::strlen((const char*)szCurrent);
  618. szCurrent += iLen+1;
  619. }
  620. else if (iMasked || !iType)
  621. {
  622. if (iMasked || !iType || (iType && iWidth && iHeight))
  623. {
  624. // ParseTextureColorData(..., aiTexture::pcData == 0xffffffff) will simply
  625. // return the size of the color data in bytes in iSkip
  626. unsigned int iSkip = 0;
  627. aiTexture tex;
  628. tex.pcData = reinterpret_cast<aiTexel*>(0xffffffff);
  629. tex.mHeight = iHeight;
  630. tex.mWidth = iWidth;
  631. this->ParseTextureColorData(szCurrent,iMasked,&iSkip,&tex);
  632. // FIX: Important, otherwise the destructor will crash
  633. tex.pcData = NULL;
  634. // skip length of texture data
  635. szCurrent += iSkip;
  636. }
  637. }
  638. // check whether a material definition is contained in the skin
  639. if (iType & AI_MDL7_SKINTYPE_MATERIAL)
  640. {
  641. const MDL::Material_MDL7* pcMatIn = (const MDL::Material_MDL7*)szCurrent;
  642. szCurrent = (unsigned char*)(pcMatIn+1);
  643. }
  644. // if an ASCII effect description (HLSL?) is contained in the file,
  645. // we can simply ignore it ...
  646. if (iType & AI_MDL7_SKINTYPE_MATERIAL_ASCDEF)
  647. {
  648. int32_t iMe = *((int32_t*)szCurrent);
  649. szCurrent += sizeof(char) * iMe + sizeof(int32_t);
  650. }
  651. *szCurrentOut = szCurrent;
  652. }
  653. // ------------------------------------------------------------------------------------------------
  654. void MDLImporter::ParseSkinLump_3DGS_MDL7(
  655. const unsigned char* szCurrent,
  656. const unsigned char** szCurrentOut,
  657. std::vector<MaterialHelper*>& pcMats)
  658. {
  659. ai_assert(NULL != szCurrent);
  660. ai_assert(NULL != szCurrentOut);
  661. *szCurrentOut = szCurrent;
  662. const MDL::Skin_MDL7* pcSkin = (const MDL::Skin_MDL7*)szCurrent;
  663. szCurrent += 12;
  664. // allocate an output material
  665. MaterialHelper* pcMatOut = new MaterialHelper();
  666. pcMats.push_back(pcMatOut);
  667. // skip length of file name
  668. szCurrent += AI_MDL7_MAX_TEXNAMESIZE;
  669. this->ParseSkinLump_3DGS_MDL7(szCurrent,szCurrentOut,pcMatOut,
  670. pcSkin->typ,pcSkin->width,pcSkin->height);
  671. // place the name of the skin in the material
  672. if (pcSkin->texture_name[0])
  673. {
  674. // the 0 termination could be there or not - we can't know
  675. aiString szFile;
  676. ::memcpy(szFile.data,pcSkin->texture_name,sizeof(pcSkin->texture_name));
  677. szFile.data[sizeof(pcSkin->texture_name)] = '\0';
  678. szFile.length = ::strlen(szFile.data);
  679. pcMatOut->AddProperty(&szFile,AI_MATKEY_NAME);
  680. }
  681. return;
  682. }