You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

828 lines
23 KiB

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