MDLMaterialLoader.cpp 23 KB

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