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.
 
 
 
 
 
 

1404 lines
39 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. #include "stdafx.h"
  35. #include "assimp_view.h"
  36. namespace AssimpView {
  37. /*static */ CMaterialManager CMaterialManager::s_cInstance;
  38. //-------------------------------------------------------------------------------
  39. // D3DX callback function to fill a texture with a checkers pattern
  40. //
  41. // This pattern is used to mark textures which could not be loaded
  42. //-------------------------------------------------------------------------------
  43. VOID WINAPI FillFunc(D3DXVECTOR4* pOut,
  44. CONST D3DXVECTOR2* pTexCoord,
  45. CONST D3DXVECTOR2* pTexelSize,
  46. LPVOID pData)
  47. {
  48. UNREFERENCED_PARAMETER(pData);
  49. UNREFERENCED_PARAMETER(pTexelSize);
  50. // generate a nice checker pattern (yellow/black)
  51. // size of a square: 32 * 32 px
  52. unsigned int iX = (unsigned int)(pTexCoord->x * 256.0f);
  53. unsigned int iY = (unsigned int)(pTexCoord->y * 256.0f);
  54. bool bBlack = false;
  55. if ((iX / 32) % 2 == 0)
  56. {
  57. if ((iY / 32) % 2 == 0)bBlack = true;
  58. }
  59. else
  60. {
  61. if ((iY / 32) % 2 != 0)bBlack = true;
  62. }
  63. pOut->w = 1.0f;
  64. if (bBlack)
  65. {
  66. pOut->x = pOut->y = pOut->z = 0.0f;
  67. }
  68. else
  69. {
  70. pOut->x = pOut->y = 1.0f;
  71. pOut->z = 0.0f;
  72. }
  73. return;
  74. }
  75. //-------------------------------------------------------------------------------
  76. int CMaterialManager::UpdateSpecularMaterials()
  77. {
  78. if (g_pcAsset && g_pcAsset->pcScene)
  79. {
  80. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  81. {
  82. if (aiShadingMode_Phong == g_pcAsset->apcMeshes[i]->eShadingMode)
  83. {
  84. this->DeleteMaterial(g_pcAsset->apcMeshes[i]);
  85. this->CreateMaterial(g_pcAsset->apcMeshes[i],g_pcAsset->pcScene->mMeshes[i]);
  86. }
  87. }
  88. }
  89. return 1;
  90. }
  91. //-------------------------------------------------------------------------------
  92. int CMaterialManager::SetDefaultTexture(IDirect3DTexture9** p_ppiOut)
  93. {
  94. if (sDefaultTexture) {
  95. sDefaultTexture->AddRef();
  96. *p_ppiOut = sDefaultTexture;
  97. return 1;
  98. }
  99. if(FAILED(g_piDevice->CreateTexture(
  100. 256,
  101. 256,
  102. 0,
  103. 0,
  104. D3DFMT_A8R8G8B8,
  105. D3DPOOL_MANAGED,
  106. p_ppiOut,
  107. NULL)))
  108. {
  109. CLogDisplay::Instance().AddEntry("[ERROR] Unable to create default texture",
  110. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  111. *p_ppiOut = NULL;
  112. return 0;
  113. }
  114. D3DXFillTexture(*p_ppiOut,&FillFunc,NULL);
  115. sDefaultTexture = *p_ppiOut;
  116. sDefaultTexture->AddRef();
  117. // {9785DA94-1D96-426b-B3CB-BADC36347F5E}
  118. static const GUID guidPrivateData =
  119. { 0x9785da94, 0x1d96, 0x426b,
  120. { 0xb3, 0xcb, 0xba, 0xdc, 0x36, 0x34, 0x7f, 0x5e } };
  121. uint32_t iData = 0xFFFFFFFF;
  122. (*p_ppiOut)->SetPrivateData(guidPrivateData,&iData,4,0);
  123. return 1;
  124. }
  125. //-------------------------------------------------------------------------------
  126. bool CMaterialManager::TryLongerPath(char* szTemp,aiString* p_szString)
  127. {
  128. char szTempB[MAX_PATH];
  129. strcpy(szTempB,szTemp);
  130. // go to the beginning of the file name
  131. char* szFile = strrchr(szTempB,'\\');
  132. if (!szFile)szFile = strrchr(szTempB,'/');
  133. char* szFile2 = szTemp + (szFile - szTempB)+1;
  134. szFile++;
  135. char* szExt = strrchr(szFile,'.');
  136. if (!szExt)return false;
  137. szExt++;
  138. *szFile = 0;
  139. strcat(szTempB,"*.*");
  140. const unsigned int iSize = (const unsigned int) ( szExt - 1 - szFile );
  141. HANDLE h;
  142. WIN32_FIND_DATA info;
  143. // build a list of files
  144. h = FindFirstFile(szTempB, &info);
  145. if (h != INVALID_HANDLE_VALUE)
  146. {
  147. do
  148. {
  149. if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
  150. {
  151. char* szExtFound = strrchr(info.cFileName, '.');
  152. if (szExtFound)
  153. {
  154. ++szExtFound;
  155. if (0 == ASSIMP_stricmp(szExtFound,szExt))
  156. {
  157. const unsigned int iSizeFound = (const unsigned int) (
  158. szExtFound - 1 - info.cFileName);
  159. for (unsigned int i = 0; i < iSizeFound;++i)
  160. info.cFileName[i] = (CHAR)tolower(info.cFileName[i]);
  161. if (0 == memcmp(info.cFileName,szFile2, std::min(iSizeFound,iSize)))
  162. {
  163. // we have it. Build the full path ...
  164. char* sz = strrchr(szTempB,'*');
  165. *(sz-2) = 0x0;
  166. strcat(szTempB,info.cFileName);
  167. // copy the result string back to the aiString
  168. const size_t iLen = strlen(szTempB);
  169. size_t iLen2 = iLen+1;
  170. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  171. memcpy(p_szString->data,szTempB,iLen2);
  172. p_szString->length = iLen;
  173. return true;
  174. }
  175. }
  176. // check whether the 8.3 DOS name is matching
  177. if (0 == ASSIMP_stricmp(info.cAlternateFileName,p_szString->data))
  178. {
  179. strcat(szTempB,info.cAlternateFileName);
  180. // copy the result string back to the aiString
  181. const size_t iLen = strlen(szTempB);
  182. size_t iLen2 = iLen+1;
  183. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  184. memcpy(p_szString->data,szTempB,iLen2);
  185. p_szString->length = iLen;
  186. return true;
  187. }
  188. }
  189. }
  190. }
  191. while (FindNextFile(h, &info));
  192. FindClose(h);
  193. }
  194. return false;
  195. }
  196. //-------------------------------------------------------------------------------
  197. int CMaterialManager::FindValidPath(aiString* p_szString)
  198. {
  199. ai_assert(NULL != p_szString);
  200. aiString pcpy = *p_szString;
  201. if ('*' == p_szString->data[0]) {
  202. // '*' as first character indicates an embedded file
  203. return 5;
  204. }
  205. // first check whether we can directly load the file
  206. FILE* pFile = fopen(p_szString->data,"rb");
  207. if (pFile)fclose(pFile);
  208. else
  209. {
  210. // check whether we can use the directory of the asset as relative base
  211. char szTemp[MAX_PATH*2], tmp2[MAX_PATH*2];
  212. strcpy(szTemp, g_szFileName);
  213. strcpy(tmp2,szTemp);
  214. char* szData = p_szString->data;
  215. if (*szData == '\\' || *szData == '/')++szData;
  216. char* szEnd = strrchr(szTemp,'\\');
  217. if (!szEnd)
  218. {
  219. szEnd = strrchr(szTemp,'/');
  220. if (!szEnd)szEnd = szTemp;
  221. }
  222. szEnd++;
  223. *szEnd = 0;
  224. strcat(szEnd,szData);
  225. pFile = fopen(szTemp,"rb");
  226. if (!pFile)
  227. {
  228. // convert the string to lower case
  229. for (unsigned int i = 0;;++i)
  230. {
  231. if ('\0' == szTemp[i])break;
  232. szTemp[i] = (char)tolower(szTemp[i]);
  233. }
  234. if(TryLongerPath(szTemp,p_szString))return 1;
  235. *szEnd = 0;
  236. // search common sub directories
  237. strcat(szEnd,"tex\\");
  238. strcat(szEnd,szData);
  239. pFile = fopen(szTemp,"rb");
  240. if (!pFile)
  241. {
  242. if(TryLongerPath(szTemp,p_szString))return 1;
  243. *szEnd = 0;
  244. strcat(szEnd,"textures\\");
  245. strcat(szEnd,szData);
  246. pFile = fopen(szTemp,"rb");
  247. if (!pFile)
  248. {
  249. if(TryLongerPath(szTemp, p_szString))return 1;
  250. }
  251. // patch by mark sibly to look for textures files in the asset's base directory.
  252. const char *path=pcpy.data;
  253. const char *p=strrchr( path,'/' );
  254. if( !p ) p=strrchr( path,'\\' );
  255. if( p ){
  256. char *q=strrchr( tmp2,'/' );
  257. if( !q ) q=strrchr( tmp2,'\\' );
  258. if( q ){
  259. strcpy( q+1,p+1 );
  260. if((pFile=fopen( tmp2,"r" ))){
  261. fclose( pFile );
  262. strcpy(p_szString->data,tmp2);
  263. p_szString->length = strlen(tmp2);
  264. return 1;
  265. }
  266. }
  267. }
  268. return 0;
  269. }
  270. }
  271. fclose(pFile);
  272. // copy the result string back to the aiString
  273. const size_t iLen = strlen(szTemp);
  274. size_t iLen2 = iLen+1;
  275. iLen2 = iLen2 > MAXLEN ? MAXLEN : iLen2;
  276. memcpy(p_szString->data,szTemp,iLen2);
  277. p_szString->length = iLen;
  278. }
  279. return 1;
  280. }
  281. //-------------------------------------------------------------------------------
  282. int CMaterialManager::LoadTexture(IDirect3DTexture9** p_ppiOut,aiString* szPath)
  283. {
  284. ai_assert(NULL != p_ppiOut);
  285. ai_assert(NULL != szPath);
  286. *p_ppiOut = NULL;
  287. const std::string s = szPath->data;
  288. TextureCache::iterator ff;
  289. if ((ff = sCachedTextures.find(s)) != sCachedTextures.end()) {
  290. *p_ppiOut = (*ff).second;
  291. (*p_ppiOut)->AddRef();
  292. return 1;
  293. }
  294. // first get a valid path to the texture
  295. if( 5 == FindValidPath(szPath))
  296. {
  297. // embedded file. Find its index
  298. unsigned int iIndex = atoi(szPath->data+1);
  299. if (iIndex < g_pcAsset->pcScene->mNumTextures)
  300. {
  301. if (0 == g_pcAsset->pcScene->mTextures[iIndex]->mHeight)
  302. {
  303. // it is an embedded file ... don't need the file format hint,
  304. // simply let D3DX load the file
  305. D3DXIMAGE_INFO info;
  306. if (FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
  307. g_pcAsset->pcScene->mTextures[iIndex]->pcData,
  308. g_pcAsset->pcScene->mTextures[iIndex]->mWidth,
  309. D3DX_DEFAULT,
  310. D3DX_DEFAULT,
  311. 1,
  312. D3DUSAGE_AUTOGENMIPMAP,
  313. D3DFMT_UNKNOWN,
  314. D3DPOOL_MANAGED,
  315. D3DX_DEFAULT,
  316. D3DX_DEFAULT,
  317. 0,
  318. &info,
  319. NULL,
  320. p_ppiOut)))
  321. {
  322. std::string sz = "[ERROR] Unable to load embedded texture (#1): ";
  323. sz.append(szPath->data);
  324. CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  325. this->SetDefaultTexture(p_ppiOut);
  326. return 1;
  327. }
  328. }
  329. else
  330. {
  331. // fill a new texture ...
  332. if(FAILED(g_piDevice->CreateTexture(
  333. g_pcAsset->pcScene->mTextures[iIndex]->mWidth,
  334. g_pcAsset->pcScene->mTextures[iIndex]->mHeight,
  335. 0,D3DUSAGE_AUTOGENMIPMAP,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,p_ppiOut,NULL)))
  336. {
  337. std::string sz = "[ERROR] Unable to load embedded texture (#2): ";
  338. sz.append(szPath->data);
  339. CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  340. this->SetDefaultTexture(p_ppiOut);
  341. return 1;
  342. }
  343. // now copy the data to it ... (assume non pow2 to be supported)
  344. D3DLOCKED_RECT sLock;
  345. (*p_ppiOut)->LockRect(0,&sLock,NULL,0);
  346. const aiTexel* pcData = g_pcAsset->pcScene->mTextures[iIndex]->pcData;
  347. for (unsigned int y = 0; y < g_pcAsset->pcScene->mTextures[iIndex]->mHeight;++y)
  348. {
  349. memcpy(sLock.pBits,pcData,g_pcAsset->pcScene->mTextures[iIndex]->
  350. mWidth *sizeof(aiTexel));
  351. sLock.pBits = (char*)sLock.pBits + sLock.Pitch;
  352. pcData += g_pcAsset->pcScene->mTextures[iIndex]->mWidth;
  353. }
  354. (*p_ppiOut)->UnlockRect(0);
  355. (*p_ppiOut)->GenerateMipSubLevels();
  356. }
  357. sCachedTextures[s] = *p_ppiOut;
  358. (*p_ppiOut)->AddRef();
  359. return 1;
  360. }
  361. else
  362. {
  363. std::string sz = "[ERROR] Invalid index for embedded texture: ";
  364. sz.append(szPath->data);
  365. CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  366. SetDefaultTexture(p_ppiOut);
  367. return 1;
  368. }
  369. }
  370. // then call D3DX to load the texture
  371. if (FAILED(D3DXCreateTextureFromFileEx(
  372. g_piDevice,
  373. szPath->data,
  374. D3DX_DEFAULT,
  375. D3DX_DEFAULT,
  376. 0,
  377. 0,
  378. D3DFMT_A8R8G8B8,
  379. D3DPOOL_MANAGED,
  380. D3DX_DEFAULT,
  381. D3DX_DEFAULT,
  382. 0,
  383. NULL,
  384. NULL,
  385. p_ppiOut)))
  386. {
  387. // error ... use the default texture instead
  388. std::string sz = "[ERROR] Unable to load texture: ";
  389. sz.append(szPath->data);
  390. CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  391. this->SetDefaultTexture(p_ppiOut);
  392. }
  393. sCachedTextures[s] = *p_ppiOut;
  394. (*p_ppiOut)->AddRef();
  395. return 1;
  396. }
  397. //-------------------------------------------------------------------------------
  398. void CMaterialManager::DeleteMaterial(AssetHelper::MeshHelper* pcIn)
  399. {
  400. if (!pcIn || !pcIn->piEffect)return;
  401. pcIn->piEffect->Release();
  402. // release all textures associated with the material
  403. if (pcIn->piDiffuseTexture)
  404. {
  405. pcIn->piDiffuseTexture->Release();
  406. pcIn->piDiffuseTexture = NULL;
  407. }
  408. if (pcIn->piSpecularTexture)
  409. {
  410. pcIn->piSpecularTexture->Release();
  411. pcIn->piSpecularTexture = NULL;
  412. }
  413. if (pcIn->piEmissiveTexture)
  414. {
  415. pcIn->piEmissiveTexture->Release();
  416. pcIn->piEmissiveTexture = NULL;
  417. }
  418. if (pcIn->piAmbientTexture)
  419. {
  420. pcIn->piAmbientTexture->Release();
  421. pcIn->piAmbientTexture = NULL;
  422. }
  423. if (pcIn->piOpacityTexture)
  424. {
  425. pcIn->piOpacityTexture->Release();
  426. pcIn->piOpacityTexture = NULL;
  427. }
  428. if (pcIn->piNormalTexture)
  429. {
  430. pcIn->piNormalTexture->Release();
  431. pcIn->piNormalTexture = NULL;
  432. }
  433. if (pcIn->piShininessTexture)
  434. {
  435. pcIn->piShininessTexture->Release();
  436. pcIn->piShininessTexture = NULL;
  437. }
  438. if (pcIn->piLightmapTexture)
  439. {
  440. pcIn->piLightmapTexture->Release();
  441. pcIn->piLightmapTexture = NULL;
  442. }
  443. pcIn->piEffect = NULL;
  444. }
  445. //-------------------------------------------------------------------------------
  446. void CMaterialManager::HMtoNMIfNecessary(
  447. IDirect3DTexture9* piTexture,
  448. IDirect3DTexture9** piTextureOut,
  449. bool bWasOriginallyHM)
  450. {
  451. ai_assert(NULL != piTexture);
  452. ai_assert(NULL != piTextureOut);
  453. bool bMustConvert = false;
  454. uintptr_t iElement = 3;
  455. *piTextureOut = piTexture;
  456. // Lock the input texture and try to determine its type.
  457. // Criterias:
  458. // - If r,g,b channel are identical it MUST be a height map
  459. // - If one of the rgb channels is used and the others are empty it
  460. // must be a height map, too.
  461. // - If the average color of the whole image is something inside the
  462. // purple range we can be sure it is a normal map
  463. //
  464. // - Otherwise we assume it is a normal map
  465. // To increase performance we take not every pixel
  466. D3DLOCKED_RECT sRect;
  467. D3DSURFACE_DESC sDesc;
  468. piTexture->GetLevelDesc(0,&sDesc);
  469. if (FAILED(piTexture->LockRect(0,&sRect,NULL,D3DLOCK_READONLY)))
  470. {
  471. return;
  472. }
  473. const int iPitchDiff = (int)sRect.Pitch - (int)(sDesc.Width * 4);
  474. struct SColor
  475. {
  476. union
  477. {
  478. struct {unsigned char b,g,r,a;};
  479. char _array[4];
  480. };
  481. };
  482. const SColor* pcData = (const SColor*)sRect.pBits;
  483. union
  484. {
  485. const SColor* pcPointer;
  486. const unsigned char* pcCharPointer;
  487. };
  488. pcPointer = pcData;
  489. // 1. If r,g,b channel are identical it MUST be a height map
  490. bool bIsEqual = true;
  491. for (unsigned int y = 0; y < sDesc.Height;++y)
  492. {
  493. for (unsigned int x = 0; x < sDesc.Width;++x)
  494. {
  495. if (pcPointer->b != pcPointer->r || pcPointer->b != pcPointer->g)
  496. {
  497. bIsEqual = false;
  498. break;
  499. }
  500. pcPointer++;
  501. }
  502. pcCharPointer += iPitchDiff;
  503. }
  504. if (bIsEqual)bMustConvert = true;
  505. else
  506. {
  507. // 2. If one of the rgb channels is used and the others are empty it
  508. // must be a height map, too.
  509. pcPointer = pcData;
  510. while (*pcCharPointer == 0)pcCharPointer++;
  511. iElement = (uintptr_t)(pcCharPointer - (unsigned char*)pcData) % 4;
  512. unsigned int aiIndex[3] = {0,1,2};
  513. if (3 != iElement)aiIndex[iElement] = 3;
  514. pcPointer = pcData;
  515. bIsEqual = true;
  516. if (3 != iElement)
  517. {
  518. for (unsigned int y = 0; y < sDesc.Height;++y)
  519. {
  520. for (unsigned int x = 0; x < sDesc.Width;++x)
  521. {
  522. for (unsigned int ii = 0; ii < 3;++ii)
  523. {
  524. // don't take the alpha channel into account.
  525. // if the texture was stored n RGB888 format D3DX has
  526. // converted it to ARGB8888 format with a fixed alpha channel
  527. if (aiIndex[ii] != 3 && pcPointer->_array[aiIndex[ii]] != 0)
  528. {
  529. bIsEqual = false;
  530. break;
  531. }
  532. }
  533. pcPointer++;
  534. }
  535. pcCharPointer += iPitchDiff;
  536. }
  537. if (bIsEqual)bMustConvert = true;
  538. else
  539. {
  540. // If the average color of the whole image is something inside the
  541. // purple range we can be sure it is a normal map
  542. // (calculate the average color line per line to prevent overflows!)
  543. pcPointer = pcData;
  544. aiColor3D clrColor;
  545. for (unsigned int y = 0; y < sDesc.Height;++y)
  546. {
  547. aiColor3D clrColorLine;
  548. for (unsigned int x = 0; x < sDesc.Width;++x)
  549. {
  550. clrColorLine.r += pcPointer->r;
  551. clrColorLine.g += pcPointer->g;
  552. clrColorLine.b += pcPointer->b;
  553. pcPointer++;
  554. }
  555. clrColor.r += clrColorLine.r /= (float)sDesc.Width;
  556. clrColor.g += clrColorLine.g /= (float)sDesc.Width;
  557. clrColor.b += clrColorLine.b /= (float)sDesc.Width;
  558. pcCharPointer += iPitchDiff;
  559. }
  560. clrColor.r /= (float)sDesc.Height;
  561. clrColor.g /= (float)sDesc.Height;
  562. clrColor.b /= (float)sDesc.Height;
  563. if (!(clrColor.b > 215 &&
  564. clrColor.r > 100 && clrColor.r < 140 &&
  565. clrColor.g > 100 && clrColor.g < 140))
  566. {
  567. // Unable to detect. Believe the original value obtained from the loader
  568. if (bWasOriginallyHM)
  569. {
  570. bMustConvert = true;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. piTexture->UnlockRect(0);
  577. // if the input data is assumed to be a height map we'll
  578. // need to convert it NOW
  579. if (bMustConvert)
  580. {
  581. D3DSURFACE_DESC sDesc;
  582. piTexture->GetLevelDesc(0, &sDesc);
  583. IDirect3DTexture9* piTempTexture;
  584. if(FAILED(g_piDevice->CreateTexture(
  585. sDesc.Width,
  586. sDesc.Height,
  587. piTexture->GetLevelCount(),
  588. sDesc.Usage,
  589. sDesc.Format,
  590. sDesc.Pool, &piTempTexture, NULL)))
  591. {
  592. CLogDisplay::Instance().AddEntry(
  593. "[ERROR] Unable to create normal map texture",
  594. D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  595. return;
  596. }
  597. DWORD dwFlags;
  598. if (3 == iElement)dwFlags = D3DX_CHANNEL_LUMINANCE;
  599. else if (2 == iElement)dwFlags = D3DX_CHANNEL_RED;
  600. else if (1 == iElement)dwFlags = D3DX_CHANNEL_GREEN;
  601. else /*if (0 == iElement)*/dwFlags = D3DX_CHANNEL_BLUE;
  602. if(FAILED(D3DXComputeNormalMap(piTempTexture,
  603. piTexture,NULL,0,dwFlags,1.0f)))
  604. {
  605. CLogDisplay::Instance().AddEntry(
  606. "[ERROR] Unable to compute normal map from height map",
  607. D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  608. piTempTexture->Release();
  609. return;
  610. }
  611. *piTextureOut = piTempTexture;
  612. piTexture->Release();
  613. }
  614. }
  615. //-------------------------------------------------------------------------------
  616. bool CMaterialManager::HasAlphaPixels(IDirect3DTexture9* piTexture)
  617. {
  618. ai_assert(NULL != piTexture);
  619. D3DLOCKED_RECT sRect;
  620. D3DSURFACE_DESC sDesc;
  621. piTexture->GetLevelDesc(0,&sDesc);
  622. if (FAILED(piTexture->LockRect(0,&sRect,NULL,D3DLOCK_READONLY)))
  623. {
  624. return false;
  625. }
  626. const int iPitchDiff = (int)sRect.Pitch - (int)(sDesc.Width * 4);
  627. struct SColor
  628. {
  629. unsigned char b,g,r,a;;
  630. };
  631. const SColor* pcData = (const SColor*)sRect.pBits;
  632. union
  633. {
  634. const SColor* pcPointer;
  635. const unsigned char* pcCharPointer;
  636. };
  637. pcPointer = pcData;
  638. for (unsigned int y = 0; y < sDesc.Height;++y)
  639. {
  640. for (unsigned int x = 0; x < sDesc.Width;++x)
  641. {
  642. if (pcPointer->a != 0xFF)
  643. {
  644. piTexture->UnlockRect(0);
  645. return true;
  646. }
  647. pcPointer++;
  648. }
  649. pcCharPointer += iPitchDiff;
  650. }
  651. piTexture->UnlockRect(0);
  652. return false;
  653. }
  654. //-------------------------------------------------------------------------------
  655. int CMaterialManager::CreateMaterial(
  656. AssetHelper::MeshHelper* pcMesh,const aiMesh* pcSource)
  657. {
  658. ai_assert(NULL != pcMesh);
  659. ai_assert(NULL != pcSource);
  660. ID3DXBuffer* piBuffer;
  661. D3DXMACRO sMacro[64];
  662. // extract all properties from the ASSIMP material structure
  663. const aiMaterial* pcMat = g_pcAsset->pcScene->mMaterials[pcSource->mMaterialIndex];
  664. //
  665. // DIFFUSE COLOR --------------------------------------------------
  666. //
  667. if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_DIFFUSE,
  668. (aiColor4D*)&pcMesh->vDiffuseColor))
  669. {
  670. pcMesh->vDiffuseColor.x = 1.0f;
  671. pcMesh->vDiffuseColor.y = 1.0f;
  672. pcMesh->vDiffuseColor.z = 1.0f;
  673. pcMesh->vDiffuseColor.w = 1.0f;
  674. }
  675. //
  676. // SPECULAR COLOR --------------------------------------------------
  677. //
  678. if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_SPECULAR,
  679. (aiColor4D*)&pcMesh->vSpecularColor))
  680. {
  681. pcMesh->vSpecularColor.x = 1.0f;
  682. pcMesh->vSpecularColor.y = 1.0f;
  683. pcMesh->vSpecularColor.z = 1.0f;
  684. pcMesh->vSpecularColor.w = 1.0f;
  685. }
  686. //
  687. // AMBIENT COLOR --------------------------------------------------
  688. //
  689. if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_AMBIENT,
  690. (aiColor4D*)&pcMesh->vAmbientColor))
  691. {
  692. pcMesh->vAmbientColor.x = 0.0f;
  693. pcMesh->vAmbientColor.y = 0.0f;
  694. pcMesh->vAmbientColor.z = 0.0f;
  695. pcMesh->vAmbientColor.w = 1.0f;
  696. }
  697. //
  698. // EMISSIVE COLOR -------------------------------------------------
  699. //
  700. if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_EMISSIVE,
  701. (aiColor4D*)&pcMesh->vEmissiveColor))
  702. {
  703. pcMesh->vEmissiveColor.x = 0.0f;
  704. pcMesh->vEmissiveColor.y = 0.0f;
  705. pcMesh->vEmissiveColor.z = 0.0f;
  706. pcMesh->vEmissiveColor.w = 1.0f;
  707. }
  708. //
  709. // Opacity --------------------------------------------------------
  710. //
  711. if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_OPACITY,&pcMesh->fOpacity))
  712. {
  713. pcMesh->fOpacity = 1.0f;
  714. }
  715. //
  716. // Shading Model --------------------------------------------------
  717. //
  718. bool bDefault = false;
  719. if(AI_SUCCESS != aiGetMaterialInteger(pcMat,AI_MATKEY_SHADING_MODEL,(int*)&pcMesh->eShadingMode ))
  720. {
  721. bDefault = true;
  722. pcMesh->eShadingMode = aiShadingMode_Gouraud;
  723. }
  724. //
  725. // Shininess ------------------------------------------------------
  726. //
  727. if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS,&pcMesh->fShininess))
  728. {
  729. // assume 15 as default shininess
  730. pcMesh->fShininess = 15.0f;
  731. }
  732. else if (bDefault)pcMesh->eShadingMode = aiShadingMode_Phong;
  733. //
  734. // Shininess strength ------------------------------------------------------
  735. //
  736. if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS_STRENGTH,&pcMesh->fSpecularStrength))
  737. {
  738. // assume 1.0 as default shininess strength
  739. pcMesh->fSpecularStrength = 1.0f;
  740. }
  741. aiString szPath;
  742. aiTextureMapMode mapU(aiTextureMapMode_Wrap),mapV(aiTextureMapMode_Wrap);
  743. bool bib =false;
  744. if (pcSource->mTextureCoords[0])
  745. {
  746. //
  747. // DIFFUSE TEXTURE ------------------------------------------------
  748. //
  749. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_DIFFUSE(0),&szPath))
  750. {
  751. LoadTexture(&pcMesh->piDiffuseTexture,&szPath);
  752. aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0),(int*)&mapU);
  753. aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0),(int*)&mapV);
  754. }
  755. //
  756. // SPECULAR TEXTURE ------------------------------------------------
  757. //
  758. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SPECULAR(0),&szPath))
  759. {
  760. LoadTexture(&pcMesh->piSpecularTexture,&szPath);
  761. }
  762. //
  763. // OPACITY TEXTURE ------------------------------------------------
  764. //
  765. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_OPACITY(0),&szPath))
  766. {
  767. LoadTexture(&pcMesh->piOpacityTexture,&szPath);
  768. }
  769. else
  770. {
  771. int flags = 0;
  772. aiGetMaterialInteger(pcMat,AI_MATKEY_TEXFLAGS_DIFFUSE(0),&flags);
  773. // try to find out whether the diffuse texture has any
  774. // non-opaque pixels. If we find a few, use it as opacity texture
  775. if (pcMesh->piDiffuseTexture && !(flags & aiTextureFlags_IgnoreAlpha) && HasAlphaPixels(pcMesh->piDiffuseTexture))
  776. {
  777. int iVal;
  778. // NOTE: This special value is set by the tree view if the user
  779. // manually removes the alpha texture from the view ...
  780. if (AI_SUCCESS != aiGetMaterialInteger(pcMat,"no_a_from_d",0,0,&iVal))
  781. {
  782. pcMesh->piOpacityTexture = pcMesh->piDiffuseTexture;
  783. pcMesh->piOpacityTexture->AddRef();
  784. }
  785. }
  786. }
  787. //
  788. // AMBIENT TEXTURE ------------------------------------------------
  789. //
  790. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_AMBIENT(0),&szPath))
  791. {
  792. LoadTexture(&pcMesh->piAmbientTexture,&szPath);
  793. }
  794. //
  795. // EMISSIVE TEXTURE ------------------------------------------------
  796. //
  797. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_EMISSIVE(0),&szPath))
  798. {
  799. LoadTexture(&pcMesh->piEmissiveTexture,&szPath);
  800. }
  801. //
  802. // Shininess TEXTURE ------------------------------------------------
  803. //
  804. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SHININESS(0),&szPath))
  805. {
  806. LoadTexture(&pcMesh->piShininessTexture,&szPath);
  807. }
  808. //
  809. // Lightmap TEXTURE ------------------------------------------------
  810. //
  811. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_LIGHTMAP(0),&szPath))
  812. {
  813. LoadTexture(&pcMesh->piLightmapTexture,&szPath);
  814. }
  815. //
  816. // NORMAL/HEIGHT MAP ------------------------------------------------
  817. //
  818. bool bHM = false;
  819. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_NORMALS(0),&szPath))
  820. {
  821. LoadTexture(&pcMesh->piNormalTexture,&szPath);
  822. }
  823. else
  824. {
  825. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_HEIGHT(0),&szPath))
  826. {
  827. LoadTexture(&pcMesh->piNormalTexture,&szPath);
  828. }
  829. else bib = true;
  830. bHM = true;
  831. }
  832. // normal/height maps are sometimes mixed up. Try to detect the type
  833. // of the texture automatically
  834. if (pcMesh->piNormalTexture)
  835. {
  836. HMtoNMIfNecessary(pcMesh->piNormalTexture, &pcMesh->piNormalTexture,bHM);
  837. }
  838. }
  839. // check whether a global background texture is contained
  840. // in this material. Some loaders set this value ...
  841. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_GLOBAL_BACKGROUND_IMAGE,&szPath))
  842. {
  843. CBackgroundPainter::Instance().SetTextureBG(szPath.data);
  844. }
  845. // BUGFIX: If the shininess is 0.0f disable phong lighting
  846. // This is a workaround for some meshes in the DX SDK (e.g. tiny.x)
  847. // FIX: Added this check to the x-loader, but the line remains to
  848. // catch other loader doing the same ...
  849. if (0.0f == pcMesh->fShininess){
  850. pcMesh->eShadingMode = aiShadingMode_Gouraud;
  851. }
  852. int two_sided = 0;
  853. aiGetMaterialInteger(pcMat,AI_MATKEY_TWOSIDED,&two_sided);
  854. pcMesh->twosided = (two_sided != 0);
  855. // check whether we have already a material using the same
  856. // shader. This will decrease loading time rapidly ...
  857. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  858. {
  859. if (g_pcAsset->pcScene->mMeshes[i] == pcSource)
  860. {
  861. break;
  862. }
  863. AssetHelper::MeshHelper* pc = g_pcAsset->apcMeshes[i];
  864. if ((pcMesh->piDiffuseTexture != NULL ? true : false) !=
  865. (pc->piDiffuseTexture != NULL ? true : false))
  866. continue;
  867. if ((pcMesh->piSpecularTexture != NULL ? true : false) !=
  868. (pc->piSpecularTexture != NULL ? true : false))
  869. continue;
  870. if ((pcMesh->piAmbientTexture != NULL ? true : false) !=
  871. (pc->piAmbientTexture != NULL ? true : false))
  872. continue;
  873. if ((pcMesh->piEmissiveTexture != NULL ? true : false) !=
  874. (pc->piEmissiveTexture != NULL ? true : false))
  875. continue;
  876. if ((pcMesh->piNormalTexture != NULL ? true : false) !=
  877. (pc->piNormalTexture != NULL ? true : false))
  878. continue;
  879. if ((pcMesh->piOpacityTexture != NULL ? true : false) !=
  880. (pc->piOpacityTexture != NULL ? true : false))
  881. continue;
  882. if ((pcMesh->piShininessTexture != NULL ? true : false) !=
  883. (pc->piShininessTexture != NULL ? true : false))
  884. continue;
  885. if ((pcMesh->piLightmapTexture != NULL ? true : false) !=
  886. (pc->piLightmapTexture != NULL ? true : false))
  887. continue;
  888. if ((pcMesh->eShadingMode != aiShadingMode_Gouraud ? true : false) !=
  889. (pc->eShadingMode != aiShadingMode_Gouraud ? true : false))
  890. continue;
  891. if ((pcMesh->fOpacity != 1.0f ? true : false) != (pc->fOpacity != 1.0f ? true : false))
  892. continue;
  893. if (pcSource->HasBones() != g_pcAsset->pcScene->mMeshes[i]->HasBones())
  894. continue;
  895. // we can reuse this material
  896. if (pc->piEffect)
  897. {
  898. pcMesh->piEffect = pc->piEffect;
  899. pc->bSharedFX = pcMesh->bSharedFX = true;
  900. pcMesh->piEffect->AddRef();
  901. return 2;
  902. }
  903. }
  904. m_iShaderCount++;
  905. // build macros for the HLSL compiler
  906. unsigned int iCurrent = 0;
  907. if (pcMesh->piDiffuseTexture)
  908. {
  909. sMacro[iCurrent].Name = "AV_DIFFUSE_TEXTURE";
  910. sMacro[iCurrent].Definition = "1";
  911. ++iCurrent;
  912. if (mapU == aiTextureMapMode_Wrap)
  913. sMacro[iCurrent].Name = "AV_WRAPU";
  914. else if (mapU == aiTextureMapMode_Mirror)
  915. sMacro[iCurrent].Name = "AV_MIRRORU";
  916. else // if (mapU == aiTextureMapMode_Clamp)
  917. sMacro[iCurrent].Name = "AV_CLAMPU";
  918. sMacro[iCurrent].Definition = "1";
  919. ++iCurrent;
  920. if (mapV == aiTextureMapMode_Wrap)
  921. sMacro[iCurrent].Name = "AV_WRAPV";
  922. else if (mapV == aiTextureMapMode_Mirror)
  923. sMacro[iCurrent].Name = "AV_MIRRORV";
  924. else // if (mapV == aiTextureMapMode_Clamp)
  925. sMacro[iCurrent].Name = "AV_CLAMPV";
  926. sMacro[iCurrent].Definition = "1";
  927. ++iCurrent;
  928. }
  929. if (pcMesh->piSpecularTexture)
  930. {
  931. sMacro[iCurrent].Name = "AV_SPECULAR_TEXTURE";
  932. sMacro[iCurrent].Definition = "1";
  933. ++iCurrent;
  934. }
  935. if (pcMesh->piAmbientTexture)
  936. {
  937. sMacro[iCurrent].Name = "AV_AMBIENT_TEXTURE";
  938. sMacro[iCurrent].Definition = "1";
  939. ++iCurrent;
  940. }
  941. if (pcMesh->piEmissiveTexture)
  942. {
  943. sMacro[iCurrent].Name = "AV_EMISSIVE_TEXTURE";
  944. sMacro[iCurrent].Definition = "1";
  945. ++iCurrent;
  946. }
  947. char buff[32];
  948. if (pcMesh->piLightmapTexture)
  949. {
  950. sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE";
  951. sMacro[iCurrent].Definition = "1";
  952. ++iCurrent;
  953. int idx;
  954. if(AI_SUCCESS == aiGetMaterialInteger(pcMat,AI_MATKEY_UVWSRC_LIGHTMAP(0),&idx) && idx >= 1 && pcSource->mTextureCoords[idx]) {
  955. sMacro[iCurrent].Name = "AV_TWO_UV";
  956. sMacro[iCurrent].Definition = "1";
  957. ++iCurrent;
  958. sMacro[iCurrent].Definition = "IN.TexCoord1";
  959. }
  960. else sMacro[iCurrent].Definition = "IN.TexCoord0";
  961. sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE_UV_COORD";
  962. ++iCurrent;float f= 1.f;
  963. aiGetMaterialFloat(pcMat,AI_MATKEY_TEXBLEND_LIGHTMAP(0),&f);
  964. sprintf(buff,"%f",f);
  965. sMacro[iCurrent].Name = "LM_STRENGTH";
  966. sMacro[iCurrent].Definition = buff;
  967. ++iCurrent;
  968. }
  969. if (pcMesh->piNormalTexture && !bib)
  970. {
  971. sMacro[iCurrent].Name = "AV_NORMAL_TEXTURE";
  972. sMacro[iCurrent].Definition = "1";
  973. ++iCurrent;
  974. }
  975. if (pcMesh->piOpacityTexture)
  976. {
  977. sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE";
  978. sMacro[iCurrent].Definition = "1";
  979. ++iCurrent;
  980. if (pcMesh->piOpacityTexture == pcMesh->piDiffuseTexture)
  981. {
  982. sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK";
  983. sMacro[iCurrent].Definition = "a";
  984. ++iCurrent;
  985. }
  986. else
  987. {
  988. sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK";
  989. sMacro[iCurrent].Definition = "r";
  990. ++iCurrent;
  991. }
  992. }
  993. if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !g_sOptions.bNoSpecular)
  994. {
  995. sMacro[iCurrent].Name = "AV_SPECULAR_COMPONENT";
  996. sMacro[iCurrent].Definition = "1";
  997. ++iCurrent;
  998. if (pcMesh->piShininessTexture)
  999. {
  1000. sMacro[iCurrent].Name = "AV_SHININESS_TEXTURE";
  1001. sMacro[iCurrent].Definition = "1";
  1002. ++iCurrent;
  1003. }
  1004. }
  1005. if (1.0f != pcMesh->fOpacity)
  1006. {
  1007. sMacro[iCurrent].Name = "AV_OPACITY";
  1008. sMacro[iCurrent].Definition = "1";
  1009. ++iCurrent;
  1010. }
  1011. if( pcSource->HasBones())
  1012. {
  1013. sMacro[iCurrent].Name = "AV_SKINNING";
  1014. sMacro[iCurrent].Definition = "1";
  1015. ++iCurrent;
  1016. }
  1017. // If a cubemap is active, we'll need to lookup it for calculating
  1018. // a physically correct reflection
  1019. if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode())
  1020. {
  1021. sMacro[iCurrent].Name = "AV_SKYBOX_LOOKUP";
  1022. sMacro[iCurrent].Definition = "1";
  1023. ++iCurrent;
  1024. }
  1025. sMacro[iCurrent].Name = NULL;
  1026. sMacro[iCurrent].Definition = NULL;
  1027. // compile the shader
  1028. if(FAILED( D3DXCreateEffect(g_piDevice,
  1029. g_szMaterialShader.c_str(),(UINT)g_szMaterialShader.length(),
  1030. (const D3DXMACRO*)sMacro,NULL,0,NULL,&pcMesh->piEffect,&piBuffer)))
  1031. {
  1032. // failed to compile the shader
  1033. if( piBuffer)
  1034. {
  1035. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  1036. piBuffer->Release();
  1037. }
  1038. // use the default material instead
  1039. if (g_piDefaultEffect)
  1040. {
  1041. pcMesh->piEffect = g_piDefaultEffect;
  1042. g_piDefaultEffect->AddRef();
  1043. }
  1044. // get the name of the material and use it in the log message
  1045. if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szPath) &&
  1046. '\0' != szPath.data[0])
  1047. {
  1048. std::string sz = "[ERROR] Unable to load material: ";
  1049. sz.append(szPath.data);
  1050. CLogDisplay::Instance().AddEntry(sz);
  1051. }
  1052. else
  1053. {
  1054. CLogDisplay::Instance().AddEntry("Unable to load material: UNNAMED");
  1055. }
  1056. return 0;
  1057. } else
  1058. {
  1059. // use Fixed Function effect when working with shaderless cards
  1060. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1061. pcMesh->piEffect->SetTechnique( "MaterialFX_FF");
  1062. }
  1063. if( piBuffer) piBuffer->Release();
  1064. // now commit all constants to the shader
  1065. //
  1066. // This is not necessary for shared shader. Shader constants for
  1067. // shared shaders are automatically recommited before the shader
  1068. // is being used for a particular mesh
  1069. if (1.0f != pcMesh->fOpacity)
  1070. pcMesh->piEffect->SetFloat("TRANSPARENCY",pcMesh->fOpacity);
  1071. if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !g_sOptions.bNoSpecular)
  1072. {
  1073. pcMesh->piEffect->SetFloat("SPECULARITY",pcMesh->fShininess);
  1074. pcMesh->piEffect->SetFloat("SPECULAR_STRENGTH",pcMesh->fSpecularStrength);
  1075. }
  1076. pcMesh->piEffect->SetVector("DIFFUSE_COLOR",&pcMesh->vDiffuseColor);
  1077. pcMesh->piEffect->SetVector("SPECULAR_COLOR",&pcMesh->vSpecularColor);
  1078. pcMesh->piEffect->SetVector("AMBIENT_COLOR",&pcMesh->vAmbientColor);
  1079. pcMesh->piEffect->SetVector("EMISSIVE_COLOR",&pcMesh->vEmissiveColor);
  1080. if (pcMesh->piDiffuseTexture)
  1081. pcMesh->piEffect->SetTexture("DIFFUSE_TEXTURE",pcMesh->piDiffuseTexture);
  1082. if (pcMesh->piOpacityTexture)
  1083. pcMesh->piEffect->SetTexture("OPACITY_TEXTURE",pcMesh->piOpacityTexture);
  1084. if (pcMesh->piSpecularTexture)
  1085. pcMesh->piEffect->SetTexture("SPECULAR_TEXTURE",pcMesh->piSpecularTexture);
  1086. if (pcMesh->piAmbientTexture)
  1087. pcMesh->piEffect->SetTexture("AMBIENT_TEXTURE",pcMesh->piAmbientTexture);
  1088. if (pcMesh->piEmissiveTexture)
  1089. pcMesh->piEffect->SetTexture("EMISSIVE_TEXTURE",pcMesh->piEmissiveTexture);
  1090. if (pcMesh->piNormalTexture)
  1091. pcMesh->piEffect->SetTexture("NORMAL_TEXTURE",pcMesh->piNormalTexture);
  1092. if (pcMesh->piShininessTexture)
  1093. pcMesh->piEffect->SetTexture("SHININESS_TEXTURE",pcMesh->piShininessTexture);
  1094. if (pcMesh->piLightmapTexture)
  1095. pcMesh->piEffect->SetTexture("LIGHTMAP_TEXTURE",pcMesh->piLightmapTexture);
  1096. if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode()){
  1097. pcMesh->piEffect->SetTexture("lw_tex_envmap",CBackgroundPainter::Instance().GetTexture());
  1098. }
  1099. return 1;
  1100. }
  1101. //-------------------------------------------------------------------------------
  1102. int CMaterialManager::SetupMaterial (
  1103. AssetHelper::MeshHelper* pcMesh,
  1104. const aiMatrix4x4& pcProj,
  1105. const aiMatrix4x4& aiMe,
  1106. const aiMatrix4x4& pcCam,
  1107. const aiVector3D& vPos)
  1108. {
  1109. ai_assert(NULL != pcMesh);
  1110. if (!pcMesh->piEffect)return 0;
  1111. ID3DXEffect* piEnd = pcMesh->piEffect;
  1112. piEnd->SetMatrix("WorldViewProjection",
  1113. (const D3DXMATRIX*)&pcProj);
  1114. piEnd->SetMatrix("World",(const D3DXMATRIX*)&aiMe);
  1115. piEnd->SetMatrix("WorldInverseTranspose",
  1116. (const D3DXMATRIX*)&pcCam);
  1117. D3DXVECTOR4 apcVec[5];
  1118. memset(apcVec,0,sizeof(apcVec));
  1119. apcVec[0].x = g_avLightDirs[0].x;
  1120. apcVec[0].y = g_avLightDirs[0].y;
  1121. apcVec[0].z = g_avLightDirs[0].z;
  1122. apcVec[0].w = 0.0f;
  1123. apcVec[1].x = g_avLightDirs[0].x * -1.0f;
  1124. apcVec[1].y = g_avLightDirs[0].y * -1.0f;
  1125. apcVec[1].z = g_avLightDirs[0].z * -1.0f;
  1126. apcVec[1].w = 0.0f;
  1127. D3DXVec4Normalize(&apcVec[0],&apcVec[0]);
  1128. D3DXVec4Normalize(&apcVec[1],&apcVec[1]);
  1129. piEnd->SetVectorArray("afLightDir",apcVec,5);
  1130. apcVec[0].x = ((g_avLightColors[0] >> 16) & 0xFF) / 255.0f;
  1131. apcVec[0].y = ((g_avLightColors[0] >> 8) & 0xFF) / 255.0f;
  1132. apcVec[0].z = ((g_avLightColors[0]) & 0xFF) / 255.0f;
  1133. apcVec[0].w = 1.0f;
  1134. if( g_sOptions.b3Lights)
  1135. {
  1136. apcVec[1].x = ((g_avLightColors[1] >> 16) & 0xFF) / 255.0f;
  1137. apcVec[1].y = ((g_avLightColors[1] >> 8) & 0xFF) / 255.0f;
  1138. apcVec[1].z = ((g_avLightColors[1]) & 0xFF) / 255.0f;
  1139. apcVec[1].w = 0.0f;
  1140. } else
  1141. {
  1142. apcVec[1].x = 0.0f;
  1143. apcVec[1].y = 0.0f;
  1144. apcVec[1].z = 0.0f;
  1145. apcVec[1].w = 0.0f;
  1146. }
  1147. apcVec[0] *= g_fLightIntensity;
  1148. apcVec[1] *= g_fLightIntensity;
  1149. piEnd->SetVectorArray("afLightColor",apcVec,5);
  1150. apcVec[0].x = ((g_avLightColors[2] >> 16) & 0xFF) / 255.0f;
  1151. apcVec[0].y = ((g_avLightColors[2] >> 8) & 0xFF) / 255.0f;
  1152. apcVec[0].z = ((g_avLightColors[2]) & 0xFF) / 255.0f;
  1153. apcVec[0].w = 1.0f;
  1154. apcVec[1].x = ((g_avLightColors[2] >> 16) & 0xFF) / 255.0f;
  1155. apcVec[1].y = ((g_avLightColors[2] >> 8) & 0xFF) / 255.0f;
  1156. apcVec[1].z = ((g_avLightColors[2]) & 0xFF) / 255.0f;
  1157. apcVec[1].w = 0.0f;
  1158. // FIX: light intensity doesn't apply to ambient color
  1159. //apcVec[0] *= g_fLightIntensity;
  1160. //apcVec[1] *= g_fLightIntensity;
  1161. piEnd->SetVectorArray("afLightColorAmbient",apcVec,5);
  1162. apcVec[0].x = vPos.x;
  1163. apcVec[0].y = vPos.y;
  1164. apcVec[0].z = vPos.z;
  1165. piEnd->SetVector( "vCameraPos",&apcVec[0]);
  1166. // if the effect instance is shared by multiple materials we need to
  1167. // recommit its whole state once per frame ...
  1168. if (pcMesh->bSharedFX)
  1169. {
  1170. // now commit all constants to the shader
  1171. if (1.0f != pcMesh->fOpacity)
  1172. pcMesh->piEffect->SetFloat("TRANSPARENCY",pcMesh->fOpacity);
  1173. if (pcMesh->eShadingMode != aiShadingMode_Gouraud)
  1174. {
  1175. pcMesh->piEffect->SetFloat("SPECULARITY",pcMesh->fShininess);
  1176. pcMesh->piEffect->SetFloat("SPECULAR_STRENGTH",pcMesh->fSpecularStrength);
  1177. }
  1178. pcMesh->piEffect->SetVector("DIFFUSE_COLOR",&pcMesh->vDiffuseColor);
  1179. pcMesh->piEffect->SetVector("SPECULAR_COLOR",&pcMesh->vSpecularColor);
  1180. pcMesh->piEffect->SetVector("AMBIENT_COLOR",&pcMesh->vAmbientColor);
  1181. pcMesh->piEffect->SetVector("EMISSIVE_COLOR",&pcMesh->vEmissiveColor);
  1182. if (pcMesh->piOpacityTexture)
  1183. pcMesh->piEffect->SetTexture("OPACITY_TEXTURE",pcMesh->piOpacityTexture);
  1184. if (pcMesh->piDiffuseTexture)
  1185. pcMesh->piEffect->SetTexture("DIFFUSE_TEXTURE",pcMesh->piDiffuseTexture);
  1186. if (pcMesh->piSpecularTexture)
  1187. pcMesh->piEffect->SetTexture("SPECULAR_TEXTURE",pcMesh->piSpecularTexture);
  1188. if (pcMesh->piAmbientTexture)
  1189. pcMesh->piEffect->SetTexture("AMBIENT_TEXTURE",pcMesh->piAmbientTexture);
  1190. if (pcMesh->piEmissiveTexture)
  1191. pcMesh->piEffect->SetTexture("EMISSIVE_TEXTURE",pcMesh->piEmissiveTexture);
  1192. if (pcMesh->piNormalTexture)
  1193. pcMesh->piEffect->SetTexture("NORMAL_TEXTURE",pcMesh->piNormalTexture);
  1194. if (pcMesh->piShininessTexture)
  1195. pcMesh->piEffect->SetTexture("SHININESS_TEXTURE",pcMesh->piShininessTexture);
  1196. if (pcMesh->piLightmapTexture)
  1197. pcMesh->piEffect->SetTexture("LIGHTMAP_TEXTURE",pcMesh->piLightmapTexture);
  1198. if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode())
  1199. {
  1200. piEnd->SetTexture("lw_tex_envmap",CBackgroundPainter::Instance().GetTexture());
  1201. }
  1202. }
  1203. // disable culling, if necessary
  1204. if (pcMesh->twosided && g_sOptions.bCulling) {
  1205. g_piDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
  1206. }
  1207. // setup the correct shader technique to be used for drawing
  1208. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1209. {
  1210. g_piDefaultEffect->SetTechnique( "MaterialFXSpecular_FF");
  1211. } else
  1212. if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(3,0) || g_sOptions.bLowQuality)
  1213. {
  1214. if (g_sOptions.b3Lights)
  1215. piEnd->SetTechnique("MaterialFXSpecular_PS20_D2");
  1216. else piEnd->SetTechnique("MaterialFXSpecular_PS20_D1");
  1217. }
  1218. else
  1219. {
  1220. if (g_sOptions.b3Lights)
  1221. piEnd->SetTechnique("MaterialFXSpecular_D2");
  1222. else piEnd->SetTechnique("MaterialFXSpecular_D1");
  1223. }
  1224. // activate the effect
  1225. UINT dwPasses = 0;
  1226. piEnd->Begin(&dwPasses,0);
  1227. piEnd->BeginPass(0);
  1228. return 1;
  1229. }
  1230. //-------------------------------------------------------------------------------
  1231. int CMaterialManager::EndMaterial (AssetHelper::MeshHelper* pcMesh)
  1232. {
  1233. ai_assert(NULL != pcMesh);
  1234. if (!pcMesh->piEffect)return 0;
  1235. // end the effect
  1236. pcMesh->piEffect->EndPass();
  1237. pcMesh->piEffect->End();
  1238. // reenable culling if necessary
  1239. if (pcMesh->twosided && g_sOptions.bCulling) {
  1240. g_piDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
  1241. }
  1242. return 1;
  1243. }
  1244. }; // end namespace AssimpView