MDLMaterialLoader.cpp 25 KB

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