Material.cpp 50 KB

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