Material.cpp 37 KB

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