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

2315 lines
68 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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. namespace Assimp.Viewer {
  35. public struct SVertex
  36. {
  37. float x,y,z,w,u,v;
  38. };
  39. partial class AssimpView {
  40. #if false
  41. extern COLORREF g_aclCustomColors[16] /*= {0}*/;
  42. extern HKEY g_hRegistry;
  43. extern float g_fLoadTime;
  44. //-------------------------------------------------------------------------------
  45. // Table of colors used for normal vectors.
  46. //-------------------------------------------------------------------------------
  47. D3DXVECTOR4 g_aclNormalColors[14] =
  48. {
  49. D3DXVECTOR4(0xFF / 255.0f,0xFF / 255.0f,0xFF / 255.0f, 1.0f), // white
  50. D3DXVECTOR4(0xFF / 255.0f,0x00 / 255.0f,0x00 / 255.0f,1.0f), // red
  51. D3DXVECTOR4(0x00 / 255.0f,0xFF / 255.0f,0x00 / 255.0f,1.0f), // green
  52. D3DXVECTOR4(0x00 / 255.0f,0x00 / 255.0f,0xFF / 255.0f,1.0f), // blue
  53. D3DXVECTOR4(0xFF / 255.0f,0xFF / 255.0f,0x00 / 255.0f,1.0f), // yellow
  54. D3DXVECTOR4(0xFF / 255.0f,0x00 / 255.0f,0xFF / 255.0f,1.0f), // magenta
  55. D3DXVECTOR4(0x00 / 255.0f,0xFF / 255.0f,0xFF / 255.0f,1.0f), // wtf
  56. D3DXVECTOR4(0xFF / 255.0f,0x60 / 255.0f,0x60 / 255.0f,1.0f), // light red
  57. D3DXVECTOR4(0x60 / 255.0f,0xFF / 255.0f,0x60 / 255.0f,1.0f), // light green
  58. D3DXVECTOR4(0x60 / 255.0f,0x60 / 255.0f,0xFF / 255.0f,1.0f), // light blue
  59. D3DXVECTOR4(0xA0 / 255.0f,0x00 / 255.0f,0x00 / 255.0f,1.0f), // dark red
  60. D3DXVECTOR4(0x00 / 255.0f,0xA0 / 255.0f,0x00 / 255.0f,1.0f), // dark green
  61. D3DXVECTOR4(0x00 / 255.0f,0x00 / 255.0f,0xA0 / 255.0f,1.0f), // dark blue
  62. D3DXVECTOR4(0x88 / 255.0f,0x88 / 255.0f,0x88 / 255.0f, 1.0f) // gray
  63. };
  64. //-------------------------------------------------------------------------------
  65. // Recursivly count the number of nodes in an asset's node graph
  66. // Used by LoadAsset()
  67. //-------------------------------------------------------------------------------
  68. void GetNodeCount(aiNode* pcNode, unsigned int* piCnt)
  69. {
  70. *piCnt = *piCnt+1;
  71. for (unsigned int i = 0; i < pcNode->mNumChildren;++i)
  72. GetNodeCount(pcNode->mChildren[i],piCnt);
  73. }
  74. //-------------------------------------------------------------------------------
  75. int CDisplay::EnableAnimTools(BOOL hm)
  76. {
  77. EnableWindow(GetDlgItem(g_hDlg,IDC_PLAY),hm);
  78. EnableWindow(GetDlgItem(g_hDlg,IDC_SLIDERANIM),hm);
  79. return 1;
  80. }
  81. //-------------------------------------------------------------------------------
  82. // Fill animation combo box
  83. int CDisplay::FillAnimList(void)
  84. {
  85. if (0 != g_pcAsset->pcScene->mNumAnimations)
  86. {
  87. // now fill in all animation names
  88. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumAnimations;++i) {
  89. SendDlgItemMessage(g_hDlg,IDC_COMBO1,CB_ADDSTRING,0,
  90. ( LPARAM ) g_pcAsset->pcScene->mAnimations[i]->mName.data);
  91. }
  92. // also add a dummy - 'none'
  93. SendDlgItemMessage(g_hDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"none");
  94. // select first
  95. SendDlgItemMessage(g_hDlg,IDC_COMBO1,CB_SETCURSEL,0,0);
  96. EnableAnimTools(TRUE);
  97. }
  98. else // tools remain disabled
  99. EnableAnimTools(FALSE);
  100. return 1;
  101. }
  102. //-------------------------------------------------------------------------------
  103. // Clear the list of animations
  104. int CDisplay::ClearAnimList(void)
  105. {
  106. // clear the combo box
  107. SendDlgItemMessage(g_hDlg,IDC_COMBO1,CB_RESETCONTENT,0,0);
  108. return 1;
  109. }
  110. //-------------------------------------------------------------------------------
  111. // Clear the tree view
  112. int CDisplay::ClearDisplayList(void)
  113. {
  114. // clear the combo box
  115. TreeView_DeleteAllItems(GetDlgItem(g_hDlg,IDC_TREE1));
  116. this->Reset();
  117. return 1;
  118. }
  119. //-------------------------------------------------------------------------------
  120. // Add a specific node to the display list
  121. int CDisplay::AddNodeToDisplayList(
  122. unsigned int iIndex,
  123. unsigned int iDepth,
  124. aiNode* pcNode,
  125. HTREEITEM hRoot)
  126. {
  127. ai_assert(NULL != pcNode);
  128. ai_assert(NULL != hRoot);
  129. char chTemp[MAXLEN];
  130. if(0 == pcNode->mName.length) {
  131. if (iIndex >= 100) {
  132. iIndex += iDepth * 1000;
  133. }
  134. else if (iIndex >= 10)
  135. {
  136. iIndex += iDepth * 100;
  137. }
  138. else iIndex += iDepth * 10;
  139. sprintf(chTemp,"Node %i",iIndex);
  140. }
  141. else {
  142. sprintf(chTemp,"%s",pcNode->mName.data);
  143. }
  144. sprintf(chTemp+strlen(chTemp), iIndex ? " (%i)" : " (%i meshes)",pcNode->mNumMeshes);
  145. TVITEMEXW tvi;
  146. TVINSERTSTRUCTW sNew;
  147. wchar_t tmp[512];
  148. int t = MultiByteToWideChar(CP_UTF8,0,chTemp,-1,tmp,512);
  149. tvi.pszText = tmp;
  150. tvi.cchTextMax = (int)t;
  151. tvi.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_HANDLE | TVIF_PARAM;
  152. tvi.iImage = this->m_aiImageList[AI_VIEW_IMGLIST_NODE];
  153. tvi.iSelectedImage = this->m_aiImageList[AI_VIEW_IMGLIST_NODE];
  154. tvi.lParam = (LPARAM)5;
  155. sNew.itemex = tvi;
  156. sNew.hInsertAfter = TVI_LAST;
  157. sNew.hParent = hRoot;
  158. // add the item to the list
  159. HTREEITEM hTexture = (HTREEITEM)SendMessage(GetDlgItem(g_hDlg,IDC_TREE1),
  160. TVM_INSERTITEMW,
  161. 0,
  162. (LPARAM)(LPTVINSERTSTRUCT)&sNew);
  163. // recursively add all child nodes
  164. ++iDepth;
  165. for (unsigned int i = 0; i< pcNode->mNumChildren;++i){
  166. AddNodeToDisplayList(i,iDepth,pcNode->mChildren[i],hTexture);
  167. }
  168. // add the node to the list
  169. NodeInfo info;
  170. info.hTreeItem = hTexture;
  171. info.psNode = pcNode;
  172. this->AddNode(info);
  173. return 1;
  174. }
  175. //-------------------------------------------------------------------------------
  176. int CDisplay::AddMeshToDisplayList(unsigned int iIndex, HTREEITEM hRoot)
  177. {
  178. aiMesh* pcMesh = g_pcAsset->pcScene->mMeshes[iIndex];
  179. char chTemp[MAXLEN];
  180. if(0 == pcMesh->mName.length) {
  181. sprintf(chTemp,"Mesh %i",iIndex);
  182. }
  183. else {
  184. sprintf(chTemp,"%s",pcMesh->mName.data);
  185. }
  186. sprintf(chTemp+strlen(chTemp), iIndex ? " (%i)" : " (%i faces)",pcMesh->mNumFaces);
  187. TVITEMEXW tvi;
  188. TVINSERTSTRUCTW sNew;
  189. wchar_t tmp[512];
  190. int t = MultiByteToWideChar(CP_UTF8,0,chTemp,-1,tmp,512);
  191. tvi.pszText = tmp;
  192. tvi.cchTextMax = (int)t;
  193. tvi.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_HANDLE | TVIF_PARAM;
  194. tvi.iImage = this->m_aiImageList[AI_VIEW_IMGLIST_NODE];
  195. tvi.iSelectedImage = this->m_aiImageList[AI_VIEW_IMGLIST_NODE];
  196. tvi.lParam = (LPARAM)5;
  197. sNew.itemex = tvi;
  198. sNew.hInsertAfter = TVI_LAST;
  199. sNew.hParent = hRoot;
  200. // add the item to the list
  201. HTREEITEM hTexture = (HTREEITEM)SendMessage(GetDlgItem(g_hDlg,IDC_TREE1),
  202. TVM_INSERTITEMW,
  203. 0,
  204. (LPARAM)(LPTVINSERTSTRUCT)&sNew);
  205. // add the mesh to the list of all mesh entries in the scene browser
  206. MeshInfo info;
  207. info.hTreeItem = hTexture;
  208. info.psMesh = pcMesh;
  209. AddMesh(info);
  210. return 1;
  211. }
  212. //-------------------------------------------------------------------------------
  213. // Replace the currently selected texture by another one
  214. int CDisplay::ReplaceCurrentTexture(const char* szPath)
  215. {
  216. ai_assert(NULL != szPath);
  217. // well ... try to load it
  218. IDirect3DTexture9* piTexture = NULL;
  219. aiString szString;
  220. strcpy(szString.data,szPath);
  221. szString.length = strlen(szPath);
  222. CMaterialManager::Instance().LoadTexture(&piTexture,&szString);
  223. if (!piTexture) {
  224. CLogDisplay::Instance().AddEntry("[ERROR] Unable to load this texture",
  225. D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));
  226. return 0;
  227. }
  228. // we must also change the icon of the corresponding tree
  229. // view item if the default texture was previously set
  230. TVITEMEX tvi;
  231. tvi.mask = TVIF_SELECTEDIMAGE | TVIF_IMAGE;
  232. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_MATERIAL];
  233. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_MATERIAL];
  234. TreeView_SetItem(GetDlgItem(g_hDlg,IDC_TREE1),
  235. m_pcCurrentTexture->hTreeItem);
  236. // change this in the old aiMaterial structure, too
  237. Assimp::MaterialHelper* pcMat = (Assimp::MaterialHelper*)
  238. g_pcAsset->pcScene->mMaterials[m_pcCurrentTexture->iMatIndex];
  239. // update all meshes referencing this material
  240. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  241. {
  242. if (this->m_pcCurrentTexture->iMatIndex != g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  243. continue;
  244. AssetHelper::MeshHelper* pcMesh = g_pcAsset->apcMeshes[i];
  245. IDirect3DTexture9** tex = NULL;
  246. const char* tex_string = NULL;
  247. switch (this->m_pcCurrentTexture->iType)
  248. {
  249. case aiTextureType_DIFFUSE:
  250. tex = &pcMesh->piDiffuseTexture;
  251. tex_string = "DIFFUSE_TEXTURE";
  252. break;
  253. case aiTextureType_AMBIENT:
  254. tex = &pcMesh->piAmbientTexture;
  255. tex_string = "AMBIENT_TEXTURE";
  256. break;
  257. case aiTextureType_SPECULAR:
  258. tex = &pcMesh->piSpecularTexture;
  259. tex_string = "SPECULAR_TEXTURE";
  260. break;
  261. case aiTextureType_EMISSIVE:
  262. tex = &pcMesh->piEmissiveTexture;
  263. tex_string = "EMISSIVE_TEXTURE";
  264. break;
  265. case aiTextureType_LIGHTMAP:
  266. tex = &pcMesh->piLightmapTexture;
  267. tex_string = "LIGHTMAP_TEXTURE";
  268. break;
  269. case aiTextureType_DISPLACEMENT:
  270. case aiTextureType_REFLECTION:
  271. case aiTextureType_UNKNOWN:
  272. break;
  273. case aiTextureType_SHININESS:
  274. tex = &pcMesh->piShininessTexture;
  275. tex_string = "SHININESS_TEXTURE";
  276. break;
  277. case aiTextureType_NORMALS:
  278. case aiTextureType_HEIGHT:
  279. // special handling here
  280. if (pcMesh->piNormalTexture && pcMesh->piNormalTexture != piTexture) {
  281. piTexture->AddRef();
  282. pcMesh->piNormalTexture->Release();
  283. pcMesh->piNormalTexture = piTexture;
  284. CMaterialManager::Instance().HMtoNMIfNecessary(pcMesh->piNormalTexture,&pcMesh->piNormalTexture,true);
  285. m_pcCurrentTexture->piTexture = &pcMesh->piNormalTexture;
  286. if (!pcMesh->bSharedFX) {
  287. pcMesh->piEffect->SetTexture("NORMAL_TEXTURE",piTexture);
  288. }
  289. }
  290. break;
  291. default: //case aiTextureType_OPACITY && case aiTextureType_OPACITY | 0x40000000:
  292. tex = &pcMesh->piOpacityTexture;
  293. tex_string = "OPACITY_TEXTURE";
  294. break;
  295. };
  296. if (tex && *tex && *tex != piTexture)
  297. {
  298. (**tex).Release();
  299. *tex = piTexture;
  300. m_pcCurrentTexture->piTexture = tex;
  301. //if (!pcMesh->bSharedFX){
  302. pcMesh->piEffect->SetTexture(tex_string,piTexture);
  303. //}
  304. }
  305. }
  306. // now update the material itself
  307. aiString szOld;
  308. aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE(m_pcCurrentTexture->iType,0),&szOld);
  309. pcMat->AddProperty(&szString,AI_MATKEY_TEXTURE(m_pcCurrentTexture->iType,0));
  310. #if 0
  311. char szBuffer[512];
  312. sprintf(szBuffer,"%s%s",szKey,"_old");
  313. if (AI_SUCCESS != aiGetMaterialString(pcMat, szBuffer, &szOld))
  314. {
  315. pcMat->AddProperty(&szOld,szBuffer );
  316. }
  317. else if (szString.length == szOld.length &&
  318. 0 == ASSIMP_stricmp(szString.data,szOld.data))
  319. {
  320. pcMat->RemoveProperty(szBuffer);
  321. }
  322. #endif
  323. return 1;
  324. }
  325. //-------------------------------------------------------------------------------
  326. int CDisplay::AddTextureToDisplayList(unsigned int iType,
  327. unsigned int iIndex,
  328. const aiString* szPath,
  329. HTREEITEM hFX,
  330. unsigned int iUVIndex /*= 0*/,
  331. const float fBlendFactor /*= 0.0f*/,
  332. aiTextureOp eTextureOp /*= aiTextureOp_Multiply*/,
  333. unsigned int iMesh /*= 0*/)
  334. {
  335. ai_assert(NULL != szPath);
  336. ai_assert(NULL != pcMat);
  337. char chTemp[512];
  338. char chTempEmb[256];
  339. const char* sz = strrchr(szPath->data,'\\');
  340. if (!sz)sz = strrchr(szPath->data,'/');
  341. if (!sz)
  342. {
  343. if ('*' == *szPath->data)
  344. {
  345. int iIndex = atoi(szPath->data+1);
  346. sprintf(chTempEmb,"Embedded #%i",iIndex);
  347. sz = chTempEmb;
  348. }
  349. else
  350. {
  351. sz = szPath->data;
  352. }
  353. }
  354. bool bIsExtraOpacity = 0 != (iType & 0x40000000);
  355. const char* szType;
  356. IDirect3DTexture9** piTexture;
  357. switch (iType)
  358. {
  359. case aiTextureType_DIFFUSE:
  360. piTexture = &g_pcAsset->apcMeshes[iMesh]->piDiffuseTexture;
  361. szType = "Diffuse";
  362. break;
  363. case aiTextureType_SPECULAR:
  364. piTexture = &g_pcAsset->apcMeshes[iMesh]->piSpecularTexture;
  365. szType = "Specular";
  366. break;
  367. case aiTextureType_AMBIENT:
  368. piTexture = &g_pcAsset->apcMeshes[iMesh]->piAmbientTexture;
  369. szType = "Ambient";
  370. break;
  371. case aiTextureType_EMISSIVE:
  372. piTexture = &g_pcAsset->apcMeshes[iMesh]->piEmissiveTexture;
  373. szType = "Emissive";
  374. break;
  375. case aiTextureType_HEIGHT:
  376. piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture;
  377. szType = "Heightmap";
  378. break;
  379. case aiTextureType_NORMALS:
  380. piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture;
  381. szType = "Normalmap";
  382. break;
  383. case aiTextureType_SHININESS:
  384. piTexture = &g_pcAsset->apcMeshes[iMesh]->piShininessTexture;
  385. szType = "Shininess";
  386. break;
  387. case aiTextureType_LIGHTMAP:
  388. piTexture = &g_pcAsset->apcMeshes[iMesh]->piLightmapTexture;
  389. szType = "Lightmap";
  390. break;
  391. case aiTextureType_DISPLACEMENT:
  392. piTexture = NULL;
  393. szType = "Displacement";
  394. break;
  395. case aiTextureType_REFLECTION:
  396. piTexture = NULL;
  397. szType = "Reflection";
  398. break;
  399. case aiTextureType_UNKNOWN:
  400. piTexture = NULL;
  401. szType = "Unknown";
  402. break;
  403. default: // opacity + opacity | mask
  404. piTexture = &g_pcAsset->apcMeshes[iMesh]->piOpacityTexture;
  405. szType = "Opacity";
  406. break;
  407. };
  408. if (bIsExtraOpacity) {
  409. sprintf(chTemp,"%s %i (<copy of diffuse #1>)",szType,iIndex+1);
  410. }
  411. else
  412. sprintf(chTemp,"%s %i (%s)",szType,iIndex+1,sz);
  413. TVITEMEX tvi;
  414. TVINSERTSTRUCT sNew;
  415. tvi.pszText = chTemp;
  416. tvi.cchTextMax = (int)strlen(chTemp);
  417. tvi.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_HANDLE | TVIF_HANDLE;
  418. tvi.lParam = (LPARAM)20;
  419. // find out whether this is the default texture or not
  420. if (piTexture && *piTexture) {
  421. // {9785DA94-1D96-426b-B3CB-BADC36347F5E}
  422. static const GUID guidPrivateData =
  423. { 0x9785da94, 0x1d96, 0x426b,
  424. { 0xb3, 0xcb, 0xba, 0xdc, 0x36, 0x34, 0x7f, 0x5e } };
  425. uint32_t iData = 0;
  426. DWORD dwSize = 4;
  427. (*piTexture)->GetPrivateData(guidPrivateData,&iData,&dwSize);
  428. if (0xFFFFFFFF == iData)
  429. {
  430. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE_INVALID];
  431. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE_INVALID];
  432. }
  433. else
  434. {
  435. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE];
  436. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE];
  437. }
  438. }
  439. else
  440. {
  441. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE_INVALID];
  442. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_TEXTURE_INVALID];
  443. }
  444. sNew.itemex = tvi;
  445. sNew.hInsertAfter = TVI_LAST;
  446. sNew.hParent = hFX;
  447. // add the item to the list
  448. HTREEITEM hTexture = (HTREEITEM)SendMessage(GetDlgItem(g_hDlg,IDC_TREE1),
  449. TVM_INSERTITEM,
  450. 0,
  451. (LPARAM)(LPTVINSERTSTRUCT)&sNew);
  452. // add it to the list
  453. CDisplay::TextureInfo sInfo;
  454. sInfo.iUV = iUVIndex;
  455. sInfo.fBlend = fBlendFactor;
  456. sInfo.eOp = eTextureOp;
  457. sInfo.szPath = szPath->data;
  458. sInfo.hTreeItem = hTexture;
  459. sInfo.piTexture = piTexture;
  460. sInfo.iType = iType;
  461. sInfo.iMatIndex = g_pcAsset->pcScene->mMeshes[iMesh]->mMaterialIndex;
  462. AddTexture(sInfo);
  463. return 1;
  464. }
  465. //-------------------------------------------------------------------------------
  466. int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,
  467. unsigned int iIndex)
  468. {
  469. ai_assert(NULL != hRoot);
  470. aiMaterial* pcMat = g_pcAsset->pcScene->mMaterials[iIndex];
  471. // find the first mesh using this material index
  472. unsigned int iMesh = 0;
  473. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  474. {
  475. if (iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  476. {
  477. iMesh = i;
  478. break;
  479. }
  480. }
  481. // use the name of the material, if possible
  482. char chTemp[512];
  483. aiString szOut;
  484. if (AI_SUCCESS != aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szOut))
  485. {
  486. sprintf(chTemp,"Material %i",iIndex+1);
  487. }
  488. else
  489. {
  490. sprintf(chTemp,"%s (%i)",szOut.data,iIndex+1);
  491. }
  492. TVITEMEXW tvi;
  493. TVINSERTSTRUCTW sNew;
  494. wchar_t tmp[512];
  495. int t = MultiByteToWideChar(CP_UTF8,0,chTemp,-1,tmp,512);
  496. tvi.pszText = tmp;
  497. tvi.cchTextMax = (int)t;
  498. tvi.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_HANDLE | TVIF_PARAM ;
  499. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_MATERIAL];
  500. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_MATERIAL];
  501. tvi.lParam = (LPARAM)10;
  502. //tvi.state = TVIS_EXPANDED | TVIS_EXPANDEDONCE ;
  503. sNew.itemex = tvi;
  504. sNew.hInsertAfter = TVI_LAST;
  505. sNew.hParent = hRoot;
  506. // add the item to the list
  507. HTREEITEM hTexture = (HTREEITEM)SendMessage(GetDlgItem(g_hDlg,IDC_TREE1),
  508. TVM_INSERTITEMW,
  509. 0,
  510. (LPARAM)(LPTVINSERTSTRUCT)&sNew);
  511. // for each texture in the list ... add it
  512. unsigned int iUV;
  513. float fBlend;
  514. aiTextureOp eOp;
  515. aiString szPath;
  516. bool bNoOpacity = true;
  517. for (unsigned int i = 0; i <= AI_TEXTURE_TYPE_MAX;++i)
  518. {
  519. unsigned int iNum = 0;
  520. while (true)
  521. {
  522. if (AI_SUCCESS != aiGetMaterialTexture(pcMat,(aiTextureType)i,iNum,
  523. &szPath,NULL, &iUV,&fBlend,&eOp))
  524. {
  525. break;
  526. }
  527. if (aiTextureType_OPACITY == i)bNoOpacity = false;
  528. AddTextureToDisplayList(i,iNum,&szPath,hTexture,iUV,fBlend,eOp,iMesh);
  529. ++iNum;
  530. }
  531. }
  532. AssetHelper::MeshHelper* pcMesh = g_pcAsset->apcMeshes[iMesh];
  533. if (pcMesh->piDiffuseTexture && pcMesh->piDiffuseTexture == pcMesh->piOpacityTexture && bNoOpacity)
  534. {
  535. // check whether the diffuse texture is not a default texture
  536. // {9785DA94-1D96-426b-B3CB-BADC36347F5E}
  537. static const GUID guidPrivateData =
  538. { 0x9785da94, 0x1d96, 0x426b,
  539. { 0xb3, 0xcb, 0xba, 0xdc, 0x36, 0x34, 0x7f, 0x5e } };
  540. uint32_t iData = 0;
  541. DWORD dwSize = 4;
  542. if(FAILED( pcMesh->piDiffuseTexture->GetPrivateData(guidPrivateData,&iData,&dwSize) ||
  543. 0xffffffff == iData))
  544. {
  545. // seems the diffuse texture contains alpha, therefore it has been
  546. // added to the opacity channel, too. Add a special value ...
  547. AddTextureToDisplayList(aiTextureType_OPACITY | 0x40000000,
  548. 0,&szPath,hTexture,iUV,fBlend,eOp,iMesh);
  549. }
  550. }
  551. // add the material to the list
  552. MaterialInfo info;
  553. info.hTreeItem = hTexture;
  554. info.psMaterial = pcMat;
  555. info.iIndex = iIndex;
  556. info.piEffect = g_pcAsset->apcMeshes[iMesh]->piEffect;
  557. this->AddMaterial(info);
  558. return 1;
  559. }
  560. //-------------------------------------------------------------------------------
  561. // Expand all elements in the treeview
  562. int CDisplay::ExpandTree()
  563. {
  564. // expand all materials
  565. for (std::vector< MaterialInfo >::iterator
  566. i = m_asMaterials.begin();
  567. i != m_asMaterials.end();++i)
  568. {
  569. TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),(*i).hTreeItem,TVE_EXPAND);
  570. }
  571. // expand all nodes
  572. for (std::vector< NodeInfo >::iterator
  573. i = m_asNodes.begin();
  574. i != m_asNodes.end();++i)
  575. {
  576. TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),(*i).hTreeItem,TVE_EXPAND);
  577. }
  578. TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),m_hRoot,TVE_EXPAND);
  579. return 1;
  580. }
  581. //-------------------------------------------------------------------------------
  582. // Get image list for tree view
  583. int CDisplay::LoadImageList(void)
  584. {
  585. if (!m_hImageList)
  586. {
  587. // First, create the image list we will need.
  588. // FIX: Need RGB888 color space to display all colors correctly
  589. HIMAGELIST hIml = ImageList_Create( 16,16,ILC_COLOR24, 5, 0 );
  590. // Load the bitmaps and add them to the image lists.
  591. HBITMAP hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BFX));
  592. m_aiImageList[AI_VIEW_IMGLIST_MATERIAL] = ImageList_Add(hIml, hBmp, NULL);
  593. DeleteObject(hBmp);
  594. hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BNODE));
  595. m_aiImageList[AI_VIEW_IMGLIST_NODE] = ImageList_Add(hIml, hBmp, NULL);
  596. DeleteObject(hBmp);
  597. hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BTX));
  598. m_aiImageList[AI_VIEW_IMGLIST_TEXTURE] = ImageList_Add(hIml, hBmp, NULL);
  599. DeleteObject(hBmp);
  600. hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BTXI));
  601. m_aiImageList[AI_VIEW_IMGLIST_TEXTURE_INVALID] = ImageList_Add(hIml, hBmp, NULL);
  602. DeleteObject(hBmp);
  603. hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BROOT));
  604. m_aiImageList[AI_VIEW_IMGLIST_MODEL] = ImageList_Add(hIml, hBmp, NULL);
  605. DeleteObject(hBmp);
  606. // Associate the image list with the tree.
  607. TreeView_SetImageList(GetDlgItem(g_hDlg,IDC_TREE1), hIml, TVSIL_NORMAL);
  608. m_hImageList = hIml;
  609. }
  610. return 1;
  611. }
  612. //-------------------------------------------------------------------------------
  613. // Fill tree view
  614. int CDisplay::FillDisplayList(void)
  615. {
  616. LoadImageList();
  617. // Initialize the tree view window.
  618. // fill in the first entry
  619. TVITEMEX tvi;
  620. TVINSERTSTRUCT sNew;
  621. tvi.pszText = "Model";
  622. tvi.cchTextMax = (int)strlen(tvi.pszText);
  623. tvi.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_HANDLE | TVIF_STATE;
  624. tvi.state = TVIS_EXPANDED;
  625. tvi.iImage = m_aiImageList[AI_VIEW_IMGLIST_MODEL];
  626. tvi.iSelectedImage = m_aiImageList[AI_VIEW_IMGLIST_MODEL];
  627. tvi.lParam = (LPARAM)0;
  628. sNew.itemex = tvi;
  629. sNew.hInsertAfter = TVI_ROOT;
  630. sNew.hParent = 0;
  631. // add the root item to the tree
  632. m_hRoot = (HTREEITEM)SendMessage(GetDlgItem(g_hDlg,IDC_TREE1),
  633. TVM_INSERTITEM,
  634. 0,
  635. (LPARAM)(LPTVINSERTSTRUCT)&sNew);
  636. // add each loaded material to the tree
  637. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMaterials;++i)
  638. AddMaterialToDisplayList(m_hRoot,i);
  639. // add each mesh to the tree
  640. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  641. AddMeshToDisplayList(i,m_hRoot);
  642. // now add all loaded nodes recursively
  643. AddNodeToDisplayList(0,0,g_pcAsset->pcScene->mRootNode,m_hRoot);
  644. // now expand all parent nodes in the tree
  645. ExpandTree();
  646. // everything reacts a little bit slowly if D3D is rendering,
  647. // so give GDI a small hint to leave the couch and work ;-)
  648. UpdateWindow(g_hDlg);
  649. return 1;
  650. }
  651. //-------------------------------------------------------------------------------
  652. // Main render loop
  653. int CDisplay::OnRender()
  654. {
  655. // update possible animation
  656. if( g_pcAsset)
  657. {
  658. static double lastPlaying = 0.;
  659. ai_assert( g_pcAsset->mAnimator);
  660. if (g_bPlay) {
  661. g_dCurrent += clock()/ double( CLOCKS_PER_SEC) -lastPlaying;
  662. double time = g_dCurrent;
  663. aiAnimation* mAnim = g_pcAsset->mAnimator->CurrentAnim();
  664. if( mAnim && mAnim->mDuration > 0.0) {
  665. double tps = mAnim->mTicksPerSecond ? mAnim->mTicksPerSecond : 25.f;
  666. time = fmod( time, mAnim->mDuration/tps);
  667. SendDlgItemMessage(g_hDlg,IDC_SLIDERANIM,TBM_SETPOS,TRUE,LPARAM(10000 * (time/(mAnim->mDuration/tps))));
  668. }
  669. g_pcAsset->mAnimator->Calculate( time );
  670. lastPlaying = g_dCurrent;
  671. }
  672. }
  673. // begin the frame
  674. g_piDevice->BeginScene();
  675. switch (m_iViewMode)
  676. {
  677. case VIEWMODE_FULL:
  678. case VIEWMODE_NODE:
  679. RenderFullScene();
  680. break;
  681. case VIEWMODE_MATERIAL:
  682. RenderMaterialView();
  683. break;
  684. case VIEWMODE_TEXTURE:
  685. RenderTextureView();
  686. break;
  687. };
  688. // Now render the log display in the upper right corner of the window
  689. CLogDisplay::Instance().OnRender();
  690. // present the backbuffer
  691. g_piDevice->EndScene();
  692. g_piDevice->Present(NULL,NULL,NULL,NULL);
  693. // don't remove this, problems on some older machines (AMD timing bug)
  694. Sleep(10);
  695. return 1;
  696. }
  697. //-------------------------------------------------------------------------------
  698. // Update UI
  699. void UpdateColorFieldsInUI()
  700. {
  701. InvalidateRect(GetDlgItem(g_hDlg,IDC_LCOLOR1),NULL,TRUE);
  702. InvalidateRect(GetDlgItem(g_hDlg,IDC_LCOLOR2),NULL,TRUE);
  703. InvalidateRect(GetDlgItem(g_hDlg,IDC_LCOLOR3),NULL,TRUE);
  704. UpdateWindow(GetDlgItem(g_hDlg,IDC_LCOLOR1));
  705. UpdateWindow(GetDlgItem(g_hDlg,IDC_LCOLOR2));
  706. UpdateWindow(GetDlgItem(g_hDlg,IDC_LCOLOR3));
  707. }
  708. //-------------------------------------------------------------------------------
  709. // FIll statistics UI
  710. int CDisplay::FillDefaultStatistics(void)
  711. {
  712. if (!g_pcAsset)
  713. {
  714. // clear all stats edit controls
  715. SetDlgItemText(g_hDlg,IDC_EVERT,"0");
  716. SetDlgItemText(g_hDlg,IDC_EFACE,"0");
  717. SetDlgItemText(g_hDlg,IDC_EMAT,"0");
  718. SetDlgItemText(g_hDlg,IDC_ENODE,"0");
  719. SetDlgItemText(g_hDlg,IDC_ESHADER,"0");
  720. SetDlgItemText(g_hDlg,IDC_ETEX,"0");
  721. return 1;
  722. }
  723. // get the number of vertices/faces in the model
  724. unsigned int iNumVert = 0;
  725. unsigned int iNumFaces = 0;
  726. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  727. {
  728. iNumVert += g_pcAsset->pcScene->mMeshes[i]->mNumVertices;
  729. iNumFaces += g_pcAsset->pcScene->mMeshes[i]->mNumFaces;
  730. }
  731. // and fill the statistic edit controls
  732. char szOut[1024];
  733. sprintf(szOut,"%i",(int)iNumVert);
  734. SetDlgItemText(g_hDlg,IDC_EVERT,szOut);
  735. sprintf(szOut,"%i",(int)iNumFaces);
  736. SetDlgItemText(g_hDlg,IDC_EFACE,szOut);
  737. sprintf(szOut,"%i",(int)g_pcAsset->pcScene->mNumMaterials);
  738. SetDlgItemText(g_hDlg,IDC_EMAT,szOut);
  739. sprintf(szOut,"%i",(int)g_pcAsset->pcScene->mNumMeshes);
  740. SetDlgItemText(g_hDlg,IDC_EMESH,szOut);
  741. // need to get the number of nodes
  742. iNumVert = 0;
  743. GetNodeCount(g_pcAsset->pcScene->mRootNode,&iNumVert);
  744. sprintf(szOut,"%i",(int)iNumVert);
  745. SetDlgItemText(g_hDlg,IDC_ENODEWND,szOut);
  746. // now get the number of unique shaders generated for the asset
  747. // (even if the environment changes this number won't change)
  748. sprintf(szOut,"%i", CMaterialManager::Instance().GetShaderCount());
  749. SetDlgItemText(g_hDlg,IDC_ESHADER,szOut);
  750. sprintf(szOut,"%.5f",(float)g_fLoadTime);
  751. SetDlgItemText(g_hDlg,IDC_ELOAD,szOut);
  752. UpdateColorFieldsInUI();
  753. UpdateWindow(g_hDlg);
  754. return 1;
  755. }
  756. //-------------------------------------------------------------------------------
  757. // Reset UI
  758. int CDisplay::Reset(void)
  759. {
  760. // clear all lists
  761. m_asMaterials.clear();
  762. m_asTextures.clear();
  763. m_asNodes.clear();
  764. m_asMeshes.clear();
  765. m_hRoot = NULL;
  766. return OnSetupNormalView();
  767. }
  768. //-------------------------------------------------------------------------------
  769. // reset to standard statistics view
  770. void ShowNormalUIComponents()
  771. {
  772. ShowWindow(GetDlgItem(g_hDlg,IDC_NUMNODES),SW_SHOW);
  773. ShowWindow(GetDlgItem(g_hDlg,IDC_ENODEWND),SW_SHOW);
  774. ShowWindow(GetDlgItem(g_hDlg,IDC_NUMSHADERS),SW_SHOW);
  775. ShowWindow(GetDlgItem(g_hDlg,IDC_LOADTIME),SW_SHOW);
  776. ShowWindow(GetDlgItem(g_hDlg,IDC_ESHADER),SW_SHOW);
  777. ShowWindow(GetDlgItem(g_hDlg,IDC_ELOAD),SW_SHOW);
  778. ShowWindow(GetDlgItem(g_hDlg,IDC_VIEWMATRIX),SW_HIDE);
  779. }
  780. //-------------------------------------------------------------------------------
  781. int CDisplay::OnSetupNormalView()
  782. {
  783. if (VIEWMODE_NODE == m_iViewMode)
  784. {
  785. ShowNormalUIComponents();
  786. }
  787. // now ... change the meaning of the statistics fields back
  788. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMVERTS),"Verts:");
  789. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMNODES),"Nodes:");
  790. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMFACES),"Faces:");
  791. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMSHADERS),"Shd:");
  792. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMATS),"Mats:");
  793. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMESHES),"Mesh:");
  794. SetWindowText(GetDlgItem(g_hDlg,IDC_LOADTIME),"Time:");
  795. FillDefaultStatistics();
  796. SetViewMode(VIEWMODE_FULL);
  797. // for debugging
  798. m_pcCurrentMaterial = NULL;
  799. m_pcCurrentTexture = NULL;
  800. m_pcCurrentNode = NULL;
  801. // redraw the color fields in the UI --- their purpose has possibly changed
  802. UpdateColorFieldsInUI();
  803. UpdateWindow(g_hDlg);
  804. return 1;
  805. }
  806. //-------------------------------------------------------------------------------
  807. int CDisplay::OnSetupNodeView(NodeInfo* pcNew)
  808. {
  809. ai_assert(NULL != pcNew);
  810. if (m_pcCurrentNode == pcNew)return 2;
  811. // now ... change the meaning of the statistics fields back
  812. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMVERTS),"Verts:");
  813. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMFACES),"Faces:");
  814. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMATS),"Mats:");
  815. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMESHES),"Mesh:");
  816. ShowWindow(GetDlgItem(g_hDlg,IDC_NUMNODES),SW_HIDE);
  817. ShowWindow(GetDlgItem(g_hDlg,IDC_ENODEWND),SW_HIDE);
  818. ShowWindow(GetDlgItem(g_hDlg,IDC_NUMSHADERS),SW_HIDE);
  819. ShowWindow(GetDlgItem(g_hDlg,IDC_LOADTIME),SW_HIDE);
  820. ShowWindow(GetDlgItem(g_hDlg,IDC_ESHADER),SW_HIDE);
  821. ShowWindow(GetDlgItem(g_hDlg,IDC_ELOAD),SW_HIDE);
  822. ShowWindow(GetDlgItem(g_hDlg,IDC_VIEWMATRIX),SW_SHOW);
  823. char szTemp[1024];
  824. sprintf(szTemp,
  825. "%.2f %.2f %.2f\r\n"
  826. "%.2f %.2f %.2f\r\n"
  827. "%.2f %.2f %.2f\r\n"
  828. "%.2f %.2f %.2f\r\n",
  829. pcNew->psNode->mTransformation.a1,
  830. pcNew->psNode->mTransformation.b1,
  831. pcNew->psNode->mTransformation.c1,
  832. pcNew->psNode->mTransformation.a2,
  833. pcNew->psNode->mTransformation.b2,
  834. pcNew->psNode->mTransformation.c2,
  835. pcNew->psNode->mTransformation.a3,
  836. pcNew->psNode->mTransformation.b3,
  837. pcNew->psNode->mTransformation.c3,
  838. pcNew->psNode->mTransformation.a4,
  839. pcNew->psNode->mTransformation.b4,
  840. pcNew->psNode->mTransformation.c4);
  841. SetWindowText(GetDlgItem(g_hDlg,IDC_VIEWMATRIX),szTemp);
  842. m_pcCurrentNode = pcNew;
  843. SetViewMode(VIEWMODE_NODE);
  844. return 1;
  845. }
  846. //-------------------------------------------------------------------------------
  847. int CDisplay::OnSetupMaterialView(MaterialInfo* pcNew)
  848. {
  849. ai_assert(NULL != pcNew);
  850. if (m_pcCurrentMaterial == pcNew)return 2;
  851. if (VIEWMODE_NODE == m_iViewMode)
  852. ShowNormalUIComponents();
  853. m_pcCurrentMaterial = pcNew;
  854. SetViewMode(VIEWMODE_MATERIAL);
  855. // redraw the color fields in the UI --- their purpose has possibly changed
  856. UpdateColorFieldsInUI();
  857. UpdateWindow(g_hDlg);
  858. return 1;
  859. }
  860. //-------------------------------------------------------------------------------
  861. int CDisplay::OnSetupTextureView(TextureInfo* pcNew)
  862. {
  863. ai_assert(NULL != pcNew);
  864. if (this->m_pcCurrentTexture == pcNew)return 2;
  865. if (VIEWMODE_NODE == this->m_iViewMode)
  866. {
  867. ShowNormalUIComponents();
  868. }
  869. if ((aiTextureType_OPACITY | 0x40000000) == pcNew->iType)
  870. {
  871. // for opacity textures display a warn message
  872. CLogDisplay::Instance().AddEntry("[INFO] This texture is not existing in the "
  873. "original mesh",D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0));
  874. CLogDisplay::Instance().AddEntry("It is a copy of the alpha channel of the first "
  875. "diffuse texture",D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0));
  876. }
  877. // check whether the pattern background effect is supported
  878. if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(3,0))
  879. {
  880. CLogDisplay::Instance().AddEntry("[WARN] The background shader won't work "
  881. "on your system, it required PS 3.0 hardware. A default color is used ...",
  882. D3DCOLOR_ARGB(0xFF,0xFF,0x00,0));
  883. }
  884. this->m_fTextureZoom = 1000.0f;
  885. this->m_vTextureOffset.x = this->m_vTextureOffset.y = 0.0f;
  886. this->m_pcCurrentTexture = pcNew;
  887. this->SetViewMode(VIEWMODE_TEXTURE);
  888. // now ... change the meaning of the statistics fields
  889. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMVERTS),"Width:");
  890. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMNODES),"Height:");
  891. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMFACES),"Format:");
  892. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMSHADERS),"MIPs:");
  893. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMATS),"UV:");
  894. SetWindowText(GetDlgItem(g_hDlg,IDC_NUMMESHES),"Blend:");
  895. SetWindowText(GetDlgItem(g_hDlg,IDC_LOADTIME),"Op:");
  896. // and fill them with data
  897. D3DSURFACE_DESC sDesc;
  898. if (pcNew->piTexture && *pcNew->piTexture) {
  899. (*pcNew->piTexture)->GetLevelDesc(0,&sDesc);
  900. char szTemp[128];
  901. sprintf(szTemp,"%i",sDesc.Width);
  902. SetWindowText(GetDlgItem(g_hDlg,IDC_EVERT),szTemp);
  903. sprintf(szTemp,"%i",sDesc.Height);
  904. SetWindowText(GetDlgItem(g_hDlg,IDC_ENODEWND),szTemp);
  905. sprintf(szTemp,"%i",(*pcNew->piTexture)->GetLevelCount());
  906. SetWindowText(GetDlgItem(g_hDlg,IDC_ESHADER),szTemp);
  907. sprintf(szTemp,"%i",pcNew->iUV);
  908. SetWindowText(GetDlgItem(g_hDlg,IDC_EMAT),szTemp);
  909. sprintf(szTemp,"%f",pcNew->fBlend);
  910. SetWindowText(GetDlgItem(g_hDlg,IDC_EMESH),szTemp);
  911. const char* szOp;
  912. switch (pcNew->eOp)
  913. {
  914. case aiTextureOp_Add:
  915. szOp = "add";break;
  916. case aiTextureOp_Subtract:
  917. szOp = "sub";break;
  918. case aiTextureOp_Divide:
  919. szOp = "div";break;
  920. case aiTextureOp_SignedAdd:
  921. szOp = "addsign";break;
  922. case aiTextureOp_SmoothAdd:
  923. szOp = "addsmooth";break;
  924. default: szOp = "mul";
  925. };
  926. SetWindowText(GetDlgItem(g_hDlg,IDC_ELOAD),szOp);
  927. // NOTE: Format is always ARGB8888 since other formats are
  928. // converted to this format ...
  929. SetWindowText(GetDlgItem(g_hDlg,IDC_EFACE),"ARGB8");
  930. // check whether this is the default texture
  931. if (pcNew->piTexture)
  932. {
  933. // {9785DA94-1D96-426b-B3CB-BADC36347F5E}
  934. static const GUID guidPrivateData =
  935. { 0x9785da94, 0x1d96, 0x426b,
  936. { 0xb3, 0xcb, 0xba, 0xdc, 0x36, 0x34, 0x7f, 0x5e } };
  937. uint32_t iData = 0;
  938. DWORD dwSize = 4;
  939. (*pcNew->piTexture)->GetPrivateData(guidPrivateData,&iData,&dwSize);
  940. if (0xFFFFFFFF == iData)
  941. {
  942. CLogDisplay::Instance().AddEntry("[ERROR] Texture could not be loaded. "
  943. "The displayed texture is a default texture",
  944. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  945. return 0;
  946. }
  947. }
  948. }
  949. // redraw the color fields in the UI --- their purpose has possibly changed
  950. UpdateColorFieldsInUI();
  951. UpdateWindow(g_hDlg);
  952. return 1;
  953. }
  954. //-------------------------------------------------------------------------------
  955. int CDisplay::OnSetup(HTREEITEM p_hTreeItem)
  956. {
  957. // search in our list for the item
  958. union {
  959. TextureInfo* pcNew;
  960. NodeInfo* pcNew2;
  961. MaterialInfo* pcNew3;
  962. };
  963. pcNew = NULL;
  964. for (std::vector<TextureInfo>::iterator i = m_asTextures.begin();i != m_asTextures.end();++i){
  965. if (p_hTreeItem == (*i).hTreeItem) {
  966. pcNew = &(*i);
  967. break;
  968. }
  969. }
  970. if (pcNew) {
  971. return OnSetupTextureView(pcNew);
  972. }
  973. // seach the node list
  974. for (std::vector<NodeInfo>::iterator i = m_asNodes.begin(); i != m_asNodes.end();++i){
  975. if (p_hTreeItem == (*i).hTreeItem) {
  976. pcNew2 = &(*i);
  977. break;
  978. }
  979. }
  980. if (pcNew2) {
  981. return OnSetupNodeView(pcNew2);
  982. }
  983. // seach the material list
  984. for (std::vector<MaterialInfo>::iterator i = m_asMaterials.begin();i != m_asMaterials.end();++i){
  985. if (p_hTreeItem == (*i).hTreeItem){
  986. pcNew3 = &(*i);
  987. break;
  988. }
  989. }
  990. if (pcNew3) {
  991. return OnSetupMaterialView(pcNew3);
  992. }
  993. return OnSetupNormalView();
  994. }
  995. //-------------------------------------------------------------------------------
  996. int CDisplay::ShowTreeViewContextMenu(HTREEITEM hItem)
  997. {
  998. ai_assert(NULL != hItem);
  999. HMENU hDisplay = NULL;
  1000. // search in our list for the item
  1001. TextureInfo* pcNew = NULL;
  1002. for (std::vector<TextureInfo>::iterator
  1003. i = m_asTextures.begin();
  1004. i != m_asTextures.end();++i)
  1005. {
  1006. if (hItem == (*i).hTreeItem) {
  1007. pcNew = &(*i);
  1008. break;
  1009. }
  1010. }
  1011. if (pcNew)
  1012. {
  1013. HMENU hMenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(IDR_TXPOPUP));
  1014. hDisplay = GetSubMenu(hMenu,0);
  1015. }
  1016. // search in the material list for the item
  1017. MaterialInfo* pcNew2 = NULL;
  1018. for (std::vector<MaterialInfo>::iterator
  1019. i = m_asMaterials.begin();
  1020. i != m_asMaterials.end();++i)
  1021. {
  1022. if (hItem == (*i).hTreeItem) {
  1023. pcNew2 = &(*i);
  1024. break;
  1025. }
  1026. }
  1027. if (pcNew2)
  1028. {
  1029. HMENU hMenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(IDR_MATPOPUP));
  1030. hDisplay = GetSubMenu(hMenu,0);
  1031. }
  1032. if (NULL != hDisplay)
  1033. {
  1034. // select this entry (this should all OnSetup())
  1035. TreeView_Select(GetDlgItem(g_hDlg,IDC_TREE1),hItem,TVGN_CARET);
  1036. // FIX: Render the scene once that the correct texture/material
  1037. // is displayed while the context menu is active
  1038. OnRender();
  1039. POINT sPoint;
  1040. GetCursorPos(&sPoint);
  1041. TrackPopupMenu(hDisplay, TPM_LEFTALIGN, sPoint.x, sPoint.y, 0,
  1042. g_hDlg,NULL);
  1043. }
  1044. return 1;
  1045. }
  1046. //-------------------------------------------------------------------------------
  1047. int CDisplay::HandleTreeViewPopup(WPARAM wParam,LPARAM lParam)
  1048. {
  1049. // get the current selected material
  1050. std::vector<Info> apclrOut;
  1051. const char* szMatKey;
  1052. switch (LOWORD(wParam))
  1053. {
  1054. case ID_SOLONG_CLEARDIFFUSECOLOR:
  1055. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  1056. {
  1057. if (this->m_pcCurrentMaterial->iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  1058. {
  1059. apclrOut.push_back( Info( &g_pcAsset->apcMeshes[i]->vDiffuseColor,
  1060. g_pcAsset->apcMeshes[i],"DIFFUSE_COLOR"));
  1061. }
  1062. }
  1063. szMatKey = "$clr.diffuse";
  1064. break;
  1065. case ID_SOLONG_CLEARSPECULARCOLOR:
  1066. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  1067. {
  1068. if (this->m_pcCurrentMaterial->iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  1069. {
  1070. apclrOut.push_back( Info( &g_pcAsset->apcMeshes[i]->vSpecularColor,
  1071. g_pcAsset->apcMeshes[i],"SPECULAR_COLOR"));
  1072. }
  1073. }
  1074. szMatKey = "$clr.specular";
  1075. break;
  1076. case ID_SOLONG_CLEARAMBIENTCOLOR:
  1077. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  1078. {
  1079. if (this->m_pcCurrentMaterial->iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  1080. {
  1081. apclrOut.push_back( Info( &g_pcAsset->apcMeshes[i]->vAmbientColor,
  1082. g_pcAsset->apcMeshes[i],"AMBIENT_COLOR"));
  1083. }
  1084. }
  1085. szMatKey = "$clr.ambient";
  1086. break;
  1087. case ID_SOLONG_CLEAREMISSIVECOLOR:
  1088. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  1089. {
  1090. if (this->m_pcCurrentMaterial->iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  1091. {
  1092. apclrOut.push_back( Info( &g_pcAsset->apcMeshes[i]->vEmissiveColor,
  1093. g_pcAsset->apcMeshes[i],"EMISSIVE_COLOR"));
  1094. }
  1095. }
  1096. szMatKey = "$clr.emissive";
  1097. break;
  1098. default:
  1099. // let the next function do this ... no spaghetti code ;-)
  1100. HandleTreeViewPopup2(wParam,lParam);
  1101. };
  1102. if (!apclrOut.empty())
  1103. {
  1104. aiColor4D clrOld = *((aiColor4D*)(apclrOut.front().pclrColor));
  1105. CHOOSECOLOR clr;
  1106. clr.lStructSize = sizeof(CHOOSECOLOR);
  1107. clr.hwndOwner = g_hDlg;
  1108. clr.Flags = CC_RGBINIT | CC_FULLOPEN;
  1109. clr.rgbResult = RGB(
  1110. clamp<unsigned char>(clrOld.r * 255.0f),
  1111. clamp<unsigned char>(clrOld.g * 255.0f),
  1112. clamp<unsigned char>(clrOld.b * 255.0f));
  1113. clr.lpCustColors = g_aclCustomColors;
  1114. clr.lpfnHook = NULL;
  1115. clr.lpTemplateName = NULL;
  1116. clr.lCustData = NULL;
  1117. ChooseColor(&clr);
  1118. clrOld.r = (float)(((unsigned int)clr.rgbResult) & 0xFF) / 255.0f;
  1119. clrOld.g = (float)(((unsigned int)clr.rgbResult >> 8) & 0xFF) / 255.0f;
  1120. clrOld.b = (float)(((unsigned int)clr.rgbResult >> 16) & 0xFF) / 255.0f;
  1121. // update the color values in the mesh instances and
  1122. // update all shaders ...
  1123. for (std::vector<Info>::iterator
  1124. i = apclrOut.begin();
  1125. i != apclrOut.end();++i)
  1126. {
  1127. *((*i).pclrColor) = *((D3DXVECTOR4*)&clrOld);
  1128. if (!(*i).pMesh->bSharedFX)
  1129. {
  1130. (*i).pMesh->piEffect->SetVector((*i).szShaderParam,(*i).pclrColor);
  1131. }
  1132. }
  1133. // change the material key ...
  1134. Assimp::MaterialHelper* pcMat = (Assimp::MaterialHelper*)g_pcAsset->pcScene->mMaterials[
  1135. this->m_pcCurrentMaterial->iIndex];
  1136. pcMat->AddProperty<aiColor4D>(&clrOld,1,szMatKey,0,0);
  1137. if (ID_SOLONG_CLEARSPECULARCOLOR == LOWORD(wParam) &&
  1138. aiShadingMode_Gouraud == apclrOut.front().pMesh->eShadingMode)
  1139. {
  1140. CLogDisplay::Instance().AddEntry("[INFO] You have just changed the specular "
  1141. "material color",D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0));
  1142. CLogDisplay::Instance().AddEntry(
  1143. "This is great, especially since there is currently no specular shading",
  1144. D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0));
  1145. }
  1146. }
  1147. return 1;
  1148. }
  1149. //-------------------------------------------------------------------------------
  1150. int CALLBACK TreeViewCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  1151. {
  1152. if (lParamSort == lParam1)return -1;
  1153. if (lParamSort == lParam2)return 1;
  1154. return 0;
  1155. }
  1156. //-------------------------------------------------------------------------------
  1157. int CDisplay::HandleTreeViewPopup2(WPARAM wParam,LPARAM lParam)
  1158. {
  1159. char szFileName[MAX_PATH];
  1160. DWORD dwTemp = MAX_PATH;
  1161. switch (LOWORD(wParam))
  1162. {
  1163. case ID_HEY_REPLACE:
  1164. {
  1165. // get a path to a new texture
  1166. if(ERROR_SUCCESS != RegQueryValueEx(g_hRegistry,"ReplaceTextureSrc",NULL,NULL,
  1167. (BYTE*)szFileName,&dwTemp))
  1168. {
  1169. // Key was not found. Use C:
  1170. strcpy(szFileName,"");
  1171. }
  1172. else
  1173. {
  1174. // need to remove the file name
  1175. char* sz = strrchr(szFileName,'\\');
  1176. if (!sz)sz = strrchr(szFileName,'/');
  1177. if (!sz)*sz = 0;
  1178. }
  1179. OPENFILENAME sFilename1 = {
  1180. sizeof(OPENFILENAME),
  1181. g_hDlg,GetModuleHandle(NULL),
  1182. "Textures\0*.png;*.dds;*.tga;*.bmp;*.tif;*.ppm;*.ppx;*.jpg;*.jpeg;*.exr\0*.*\0",
  1183. NULL, 0, 1,
  1184. szFileName, MAX_PATH, NULL, 0, NULL,
  1185. "Replace this texture",
  1186. OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_NOCHANGEDIR,
  1187. 0, 1, ".jpg", 0, NULL, NULL
  1188. };
  1189. if(GetOpenFileName(&sFilename1) == 0) return 0;
  1190. // Now store the file in the registry
  1191. RegSetValueExA(g_hRegistry,"ReplaceTextureSrc",0,REG_SZ,(const BYTE*)szFileName,MAX_PATH);
  1192. this->ReplaceCurrentTexture(szFileName);
  1193. }
  1194. return 1;
  1195. case ID_HEY_EXPORT:
  1196. {
  1197. if(ERROR_SUCCESS != RegQueryValueEx(g_hRegistry,"TextureExportDest",NULL,NULL,
  1198. (BYTE*)szFileName,&dwTemp))
  1199. {
  1200. // Key was not found. Use C:
  1201. strcpy(szFileName,"");
  1202. }
  1203. else
  1204. {
  1205. // need to remove the file name
  1206. char* sz = strrchr(szFileName,'\\');
  1207. if (!sz)sz = strrchr(szFileName,'/');
  1208. if (!sz)*sz = 0;
  1209. }
  1210. OPENFILENAME sFilename1 = {
  1211. sizeof(OPENFILENAME),
  1212. g_hDlg,GetModuleHandle(NULL),
  1213. "Textures\0*.png;*.dds;*.bmp;*.tif;*.pfm;*.jpg;*.jpeg;*.hdr\0*.*\0", NULL, 0, 1,
  1214. szFileName, MAX_PATH, NULL, 0, NULL,
  1215. "Export texture to file",
  1216. OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_NOCHANGEDIR,
  1217. 0, 1, ".png", 0, NULL, NULL
  1218. };
  1219. if(GetSaveFileName(&sFilename1) == 0) return 0;
  1220. // Now store the file in the registry
  1221. RegSetValueExA(g_hRegistry,"TextureExportDest",0,REG_SZ,(const BYTE*)szFileName,MAX_PATH);
  1222. // determine the file format ...
  1223. D3DXIMAGE_FILEFORMAT eFormat = D3DXIFF_PNG;
  1224. const char* sz = strrchr(szFileName,'.');
  1225. if (sz)
  1226. {
  1227. ++sz;
  1228. if (0 == Assimp::ASSIMP_stricmp(sz,"pfm"))eFormat = D3DXIFF_PFM;
  1229. else if (0 == Assimp::ASSIMP_stricmp(sz,"dds"))eFormat = D3DXIFF_DDS;
  1230. else if (0 == Assimp::ASSIMP_stricmp(sz,"jpg"))eFormat = D3DXIFF_JPG;
  1231. else if (0 == Assimp::ASSIMP_stricmp(sz,"jpeg"))eFormat = D3DXIFF_JPG;
  1232. else if (0 == Assimp::ASSIMP_stricmp(sz,"hdr"))eFormat = D3DXIFF_HDR;
  1233. else if (0 == Assimp::ASSIMP_stricmp(sz,"bmp"))eFormat = D3DXIFF_BMP;
  1234. }
  1235. // get a pointer to the first surface of the current texture
  1236. IDirect3DSurface9* pi = NULL;
  1237. (*this->m_pcCurrentTexture->piTexture)->GetSurfaceLevel(0,&pi);
  1238. if(!pi || FAILED(D3DXSaveSurfaceToFile(szFileName,eFormat,pi,NULL,NULL)))
  1239. {
  1240. CLogDisplay::Instance().AddEntry("[ERROR] Unable to export texture",
  1241. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  1242. }
  1243. else
  1244. {
  1245. CLogDisplay::Instance().AddEntry("[INFO] The texture has been exported",
  1246. D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0));
  1247. }
  1248. if(pi)pi->Release();
  1249. }
  1250. return 1;
  1251. case ID_HEY_REMOVE:
  1252. {
  1253. if(IDYES != MessageBox(g_hDlg,"To recover the texture you need to reload the model. Do you wish to continue?",
  1254. "Remove texture",MB_YESNO)) {
  1255. return 1;
  1256. }
  1257. Assimp::MaterialHelper* pcMat = (Assimp::MaterialHelper*)g_pcAsset->pcScene->mMaterials[
  1258. m_pcCurrentTexture->iMatIndex];
  1259. unsigned int s;
  1260. if (m_pcCurrentTexture->iType == (aiTextureType_OPACITY | 0x40000000))
  1261. {
  1262. // set a special property to indicate that no alpha channel is required
  1263. int iVal = 1;
  1264. pcMat->AddProperty<int>(&iVal,1,"no_a_from_d",0,0);
  1265. s = aiTextureType_OPACITY;
  1266. }
  1267. else s = m_pcCurrentTexture->iType;
  1268. pcMat->RemoveProperty(AI_MATKEY_TEXTURE(m_pcCurrentTexture->iType,0));
  1269. // need to update all meshes associated with this material
  1270. for (unsigned int i = 0;i < g_pcAsset->pcScene->mNumMeshes;++i)
  1271. {
  1272. if (m_pcCurrentTexture->iMatIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
  1273. {
  1274. CMaterialManager::Instance().DeleteMaterial(g_pcAsset->apcMeshes[i]);
  1275. CMaterialManager::Instance().CreateMaterial(g_pcAsset->apcMeshes[i],g_pcAsset->pcScene->mMeshes[i]);
  1276. }
  1277. }
  1278. // find the corresponding MaterialInfo structure
  1279. const unsigned int iMatIndex = m_pcCurrentTexture->iMatIndex;
  1280. for (std::vector<MaterialInfo>::iterator
  1281. a = m_asMaterials.begin();
  1282. a != m_asMaterials.end();++a)
  1283. {
  1284. if (iMatIndex == (*a).iIndex)
  1285. {
  1286. // good news. we will also need to find all other textures
  1287. // associated with this item ...
  1288. for (std::vector<TextureInfo>::iterator
  1289. n = m_asTextures.begin();
  1290. n != m_asTextures.end();++n)
  1291. {
  1292. if ((*n).iMatIndex == iMatIndex)
  1293. {
  1294. n = m_asTextures.erase(n);
  1295. if (m_asTextures.end() == n)break;
  1296. }
  1297. }
  1298. // delete this material from all lists ...
  1299. TreeView_DeleteItem(GetDlgItem(g_hDlg,IDC_TREE1),(*a).hTreeItem);
  1300. this->m_asMaterials.erase(a);
  1301. break;
  1302. }
  1303. }
  1304. // add the new material to the list and make sure it will be fully expanded
  1305. AddMaterialToDisplayList(m_hRoot,iMatIndex);
  1306. HTREEITEM hNewItem = m_asMaterials.back().hTreeItem;
  1307. TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),hNewItem,TVE_EXPAND);
  1308. // we need to sort the list, materials come first, then nodes
  1309. TVSORTCB sSort;
  1310. sSort.hParent = m_hRoot;
  1311. sSort.lParam = 10;
  1312. sSort.lpfnCompare = &TreeViewCompareFunc;
  1313. TreeView_SortChildrenCB(GetDlgItem(g_hDlg,IDC_TREE1),&sSort,0);
  1314. // the texture was selected, but the silly user has just deleted it
  1315. // ... go back to normal viewing mode
  1316. TreeView_Select(GetDlgItem(g_hDlg,IDC_TREE1),m_hRoot,TVGN_CARET);
  1317. return 1;
  1318. }
  1319. }
  1320. return 0;
  1321. }
  1322. //-------------------------------------------------------------------------------
  1323. // Setup stereo view
  1324. int CDisplay::SetupStereoView()
  1325. {
  1326. if (NULL != g_pcAsset && NULL != g_pcAsset->pcScene->mRootNode)
  1327. {
  1328. // enable the RED, GREEN and ALPHA channels
  1329. g_piDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
  1330. D3DCOLORWRITEENABLE_RED |
  1331. D3DCOLORWRITEENABLE_ALPHA |
  1332. D3DCOLORWRITEENABLE_GREEN);
  1333. // move the camera a little bit to the left
  1334. g_sCamera.vPos -= g_sCamera.vRight * 0.03f;
  1335. }
  1336. return 1;
  1337. }
  1338. //-------------------------------------------------------------------------------
  1339. // Do the actual rendering pass for the stereo view
  1340. int CDisplay::RenderStereoView(const aiMatrix4x4& m)
  1341. {
  1342. // and rerender the scene
  1343. if (NULL != g_pcAsset && NULL != g_pcAsset->pcScene->mRootNode)
  1344. {
  1345. // enable the BLUE, GREEN and ALPHA channels
  1346. g_piDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
  1347. D3DCOLORWRITEENABLE_GREEN |
  1348. D3DCOLORWRITEENABLE_ALPHA |
  1349. D3DCOLORWRITEENABLE_BLUE);
  1350. // clear the z-buffer
  1351. g_piDevice->Clear(0,NULL,D3DCLEAR_ZBUFFER,0,1.0f,0);
  1352. // move the camera a little bit to the right
  1353. g_sCamera.vPos += g_sCamera.vRight * 0.06f;
  1354. RenderNode(g_pcAsset->pcScene->mRootNode,m,false);
  1355. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
  1356. RenderNode(g_pcAsset->pcScene->mRootNode,m,true);
  1357. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
  1358. // (move back to the original position)
  1359. g_sCamera.vPos -= g_sCamera.vRight * 0.03f;
  1360. // reenable all channels
  1361. g_piDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
  1362. D3DCOLORWRITEENABLE_RED |
  1363. D3DCOLORWRITEENABLE_GREEN |
  1364. D3DCOLORWRITEENABLE_ALPHA |
  1365. D3DCOLORWRITEENABLE_BLUE);
  1366. }
  1367. return 1;
  1368. }
  1369. //-------------------------------------------------------------------------------
  1370. // Process input for the texture view
  1371. int CDisplay::HandleInputTextureView()
  1372. {
  1373. HandleMouseInputTextureView();
  1374. HandleKeyboardInputTextureView();
  1375. return 1;
  1376. }
  1377. #endif
  1378. //-------------------------------------------------------------------------------
  1379. // Get input for the current state
  1380. private int HandleInput()
  1381. {
  1382. //if(CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode())
  1383. // HandleMouseInputSkyBox();
  1384. // handle input commands
  1385. //HandleMouseInputLightRotate();
  1386. //HandleMouseInputLightIntensityAndColor();
  1387. //if(g_bFPSView)
  1388. //{
  1389. // HandleMouseInputFPS();
  1390. // HandleKeyboardInputFPS();
  1391. //}
  1392. //else
  1393. HandleMouseInputLocal();
  1394. // compute auto rotation depending on the time which has passed
  1395. if (g_sOptions.bRotate)
  1396. {
  1397. aiMatrix4x4 mMat;
  1398. D3DXMatrixRotationYawPitchRoll((D3DXMATRIX*)&mMat,
  1399. g_vRotateSpeed.x * g_fElpasedTime,
  1400. g_vRotateSpeed.y * g_fElpasedTime,
  1401. g_vRotateSpeed.z * g_fElpasedTime);
  1402. g_mWorldRotate = g_mWorldRotate * mMat;
  1403. }
  1404. // Handle rotations of light source(s)
  1405. if (g_sOptions.bLightRotate)
  1406. {
  1407. aiMatrix4x4 mMat;
  1408. D3DXMatrixRotationYawPitchRoll((D3DXMATRIX*)&mMat,
  1409. g_vRotateSpeed.x * g_fElpasedTime * 0.5f,
  1410. g_vRotateSpeed.y * g_fElpasedTime * 0.5f,
  1411. g_vRotateSpeed.z * g_fElpasedTime * 0.5f);
  1412. D3DXVec3TransformNormal((D3DXVECTOR3*)&g_avLightDirs[0],
  1413. (D3DXVECTOR3*)&g_avLightDirs[0],(D3DXMATRIX*)&mMat);
  1414. // 2 lights to rotate?
  1415. if (g_sOptions.b3Lights)
  1416. {
  1417. D3DXVec3TransformNormal((D3DXVECTOR3*)&g_avLightDirs[1],
  1418. (D3DXVECTOR3*)&g_avLightDirs[1],(D3DXMATRIX*)&mMat);
  1419. g_avLightDirs[1].Normalize();
  1420. }
  1421. g_avLightDirs[0].Normalize();
  1422. }
  1423. return 1;
  1424. }
  1425. #if false
  1426. //-------------------------------------------------------------------------------
  1427. // Process input for an empty scen view to allow for skybox rotations
  1428. int CDisplay::HandleInputEmptyScene()
  1429. {
  1430. if(CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode())
  1431. {
  1432. if (g_bFPSView)
  1433. {
  1434. HandleMouseInputFPS();
  1435. HandleKeyboardInputFPS();
  1436. }
  1437. HandleMouseInputSkyBox();
  1438. // need to store the last mouse position in the global variable
  1439. // HandleMouseInputFPS() is doing this internally
  1440. if (!g_bFPSView)
  1441. {
  1442. g_LastmousePos.x = g_mousePos.x;
  1443. g_LastmousePos.y = g_mousePos.y;
  1444. }
  1445. }
  1446. return 1;
  1447. }
  1448. //-------------------------------------------------------------------------------
  1449. // Draw the HUD on top of the scene
  1450. int CDisplay::DrawHUD()
  1451. {
  1452. // HACK: (thom) can't get the effect to work on non-shader cards, therefore deactivated for the moment
  1453. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1454. return 1;
  1455. // get the dimension of the back buffer
  1456. RECT sRect;
  1457. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  1458. sRect.right -= sRect.left;
  1459. sRect.bottom -= sRect.top;
  1460. // commit the texture to the shader
  1461. // FIX: Necessary because the texture view is also using this shader
  1462. g_piPassThroughEffect->SetTexture("TEXTURE_2D",g_pcTexture);
  1463. // NOTE: The shader might be used for other purposes, too.
  1464. // So ensure the right technique is there
  1465. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1466. g_piPassThroughEffect->SetTechnique( "PassThrough_FF");
  1467. else
  1468. g_piPassThroughEffect->SetTechnique("PassThrough");
  1469. // build vertices for drawing from system memory
  1470. UINT dw;
  1471. g_piPassThroughEffect->Begin(&dw,0);
  1472. g_piPassThroughEffect->BeginPass(0);
  1473. D3DSURFACE_DESC sDesc;
  1474. g_pcTexture->GetLevelDesc(0,&sDesc);
  1475. SVertex as[4];
  1476. float fHalfX = ((float)sRect.right-(float)sDesc.Width) / 2.0f;
  1477. float fHalfY = ((float)sRect.bottom-(float)sDesc.Height) / 2.0f;
  1478. as[1].x = fHalfX;
  1479. as[1].y = fHalfY;
  1480. as[1].z = 0.2f;
  1481. as[1].w = 1.0f;
  1482. as[1].u = 0.0f;
  1483. as[1].v = 0.0f;
  1484. as[3].x = (float)sRect.right-fHalfX;
  1485. as[3].y = fHalfY;
  1486. as[3].z = 0.2f;
  1487. as[3].w = 1.0f;
  1488. as[3].u = 1.0f;
  1489. as[3].v = 0.0f;
  1490. as[0].x = fHalfX;
  1491. as[0].y = (float)sRect.bottom-fHalfY;
  1492. as[0].z = 0.2f;
  1493. as[0].w = 1.0f;
  1494. as[0].u = 0.0f;
  1495. as[0].v = 1.0f;
  1496. as[2].x = (float)sRect.right-fHalfX;
  1497. as[2].y = (float)sRect.bottom-fHalfY;
  1498. as[2].z = 0.2f;
  1499. as[2].w = 1.0f;
  1500. as[2].u = 1.0f;
  1501. as[2].v = 1.0f;
  1502. as[0].x -= 0.5f;as[1].x -= 0.5f;as[2].x -= 0.5f;as[3].x -= 0.5f;
  1503. as[0].y -= 0.5f;as[1].y -= 0.5f;as[2].y -= 0.5f;as[3].y -= 0.5f;
  1504. g_piDevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
  1505. g_piDevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
  1506. // draw the screen-filling squad
  1507. DWORD dw2;g_piDevice->GetFVF(&dw2);
  1508. g_piDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
  1509. g_piDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,
  1510. &as,sizeof(SVertex));
  1511. // end the effect and recover the old vertex format
  1512. g_piPassThroughEffect->EndPass();
  1513. g_piPassThroughEffect->End();
  1514. g_piDevice->SetFVF(dw2);
  1515. return 1;
  1516. }
  1517. //-------------------------------------------------------------------------------
  1518. // Render the full scene, all nodes
  1519. int CDisplay::RenderFullScene()
  1520. {
  1521. // reset the color index used for drawing normals
  1522. g_iCurrentColor = 0;
  1523. aiMatrix4x4 pcProj;
  1524. GetProjectionMatrix(pcProj);
  1525. vPos = GetCameraMatrix(mViewProjection);
  1526. mViewProjection = mViewProjection * pcProj;
  1527. // setup wireframe/solid rendering mode
  1528. if (g_sOptions.eDrawMode == RenderOptions::WIREFRAME)
  1529. g_piDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
  1530. else g_piDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
  1531. if (g_sOptions.bCulling)
  1532. g_piDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
  1533. else g_piDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
  1534. // for high-quality mode, enable anisotropic texture filtering
  1535. if (g_sOptions.bLowQuality) {
  1536. for (DWORD d = 0; d < 8;++d) {
  1537. g_piDevice->SetSamplerState(d,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
  1538. g_piDevice->SetSamplerState(d,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
  1539. g_piDevice->SetSamplerState(d,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
  1540. }
  1541. }
  1542. else {
  1543. for (DWORD d = 0; d < 8;++d) {
  1544. g_piDevice->SetSamplerState(d,D3DSAMP_MAGFILTER,D3DTEXF_ANISOTROPIC);
  1545. g_piDevice->SetSamplerState(d,D3DSAMP_MINFILTER,D3DTEXF_ANISOTROPIC);
  1546. g_piDevice->SetSamplerState(d,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
  1547. g_piDevice->SetSamplerState(d,D3DSAMP_MAXANISOTROPY,g_sCaps.MaxAnisotropy);
  1548. }
  1549. }
  1550. // draw the scene background (clear and texture 2d)
  1551. CBackgroundPainter::Instance().OnPreRender();
  1552. // setup the stereo view if necessary
  1553. if (g_sOptions.bStereoView)
  1554. SetupStereoView();
  1555. // draw all opaque objects in the scene
  1556. aiMatrix4x4 m;
  1557. if (NULL != g_pcAsset && NULL != g_pcAsset->pcScene->mRootNode)
  1558. {
  1559. HandleInput();
  1560. m = g_mWorld * g_mWorldRotate;
  1561. RenderNode(g_pcAsset->pcScene->mRootNode,m,false);
  1562. }
  1563. // if a cube texture is loaded as background image, the user
  1564. // should be able to rotate it even if no asset is loaded
  1565. HandleInputEmptyScene();
  1566. // draw the scene background
  1567. CBackgroundPainter::Instance().OnPostRender();
  1568. // draw all non-opaque objects in the scene
  1569. if (NULL != g_pcAsset && NULL != g_pcAsset->pcScene->mRootNode)
  1570. {
  1571. // disable the z-buffer
  1572. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
  1573. RenderNode(g_pcAsset->pcScene->mRootNode,m,true);
  1574. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
  1575. }
  1576. // setup the stereo view if necessary
  1577. if (g_sOptions.bStereoView)
  1578. RenderStereoView(m);
  1579. // render the skeleton if necessary
  1580. if (g_sOptions.bSkeleton && NULL != g_pcAsset && NULL != g_pcAsset->pcScene->mRootNode) {
  1581. // disable the z-buffer
  1582. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
  1583. if (g_sOptions.eDrawMode != RenderOptions::WIREFRAME) {
  1584. g_piDevice->SetRenderState(D3DRS_ZENABLE,FALSE);
  1585. }
  1586. g_piDevice->SetVertexDeclaration( gDefaultVertexDecl);
  1587. // this is very similar to the code in SetupMaterial()
  1588. ID3DXEffect* piEnd = g_piNormalsEffect;
  1589. aiMatrix4x4 pcProj = m * mViewProjection;
  1590. D3DXVECTOR4 vVector(1.f,0.f,0.f,1.f);
  1591. piEnd->SetVector("OUTPUT_COLOR",&vVector);
  1592. piEnd->SetMatrix("WorldViewProjection", (const D3DXMATRIX*)&pcProj);
  1593. UINT dwPasses = 0;
  1594. piEnd->Begin(&dwPasses,0);
  1595. piEnd->BeginPass(0);
  1596. RenderSkeleton(g_pcAsset->pcScene->mRootNode,m,m);
  1597. piEnd->EndPass();piEnd->End();
  1598. g_piDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
  1599. g_piDevice->SetRenderState(D3DRS_ZENABLE,TRUE);
  1600. }
  1601. // draw the HUD texture on top of the rendered scene using
  1602. // pre-projected vertices
  1603. if (!g_bFPSView && g_pcAsset && g_pcTexture)
  1604. DrawHUD();
  1605. return 1;
  1606. }
  1607. //-------------------------------------------------------------------------------
  1608. int CDisplay::RenderMaterialView()
  1609. {
  1610. return 1;
  1611. }
  1612. //-------------------------------------------------------------------------------
  1613. // Render animation skeleton
  1614. int CDisplay::RenderSkeleton (aiNode* piNode,const aiMatrix4x4& piMatrix, const aiMatrix4x4& parent)
  1615. {
  1616. aiMatrix4x4 me = g_pcAsset->mAnimator->GetGlobalTransform( piNode);
  1617. me.Transpose();
  1618. //me *= piMatrix;
  1619. if (piNode->mParent) {
  1620. AssetHelper::LineVertex data[2];
  1621. data[0].dColorDiffuse = data[1].dColorDiffuse = D3DCOLOR_ARGB(0xff,0xff,0,0);
  1622. data[0].vPosition.x = parent.d1;
  1623. data[0].vPosition.y = parent.d2;
  1624. data[0].vPosition.z = parent.d3;
  1625. data[1].vPosition.x = me.d1;
  1626. data[1].vPosition.y = me.d2;
  1627. data[1].vPosition.z = me.d3;
  1628. g_piDevice->DrawPrimitiveUP(D3DPT_LINELIST,1,&data,sizeof(AssetHelper::LineVertex));
  1629. }
  1630. // render all child nodes
  1631. for (unsigned int i = 0; i < piNode->mNumChildren;++i)
  1632. RenderSkeleton(piNode->mChildren[i],piMatrix, me );
  1633. return 1;
  1634. }
  1635. //-------------------------------------------------------------------------------
  1636. // Render a single node
  1637. int CDisplay::RenderNode (aiNode* piNode,const aiMatrix4x4& piMatrix,
  1638. bool bAlpha /*= false*/)
  1639. {
  1640. aiMatrix4x4 aiMe = g_pcAsset->mAnimator->GetGlobalTransform( piNode);
  1641. aiMe.Transpose();
  1642. aiMe *= piMatrix;
  1643. bool bChangedVM = false;
  1644. if (VIEWMODE_NODE == m_iViewMode && m_pcCurrentNode)
  1645. {
  1646. if (piNode != m_pcCurrentNode->psNode)
  1647. {
  1648. // directly call our children
  1649. for (unsigned int i = 0; i < piNode->mNumChildren;++i)
  1650. RenderNode(piNode->mChildren[i],piMatrix,bAlpha );
  1651. return 1;
  1652. }
  1653. m_iViewMode = VIEWMODE_FULL;
  1654. bChangedVM = true;
  1655. }
  1656. aiMatrix4x4 pcProj = aiMe * mViewProjection;
  1657. aiMatrix4x4 pcCam = aiMe;
  1658. pcCam.Inverse().Transpose();
  1659. // VERY UNOPTIMIZED, much stuff is redundant. Who cares?
  1660. if (!g_sOptions.bRenderMats && !bAlpha)
  1661. {
  1662. // this is very similar to the code in SetupMaterial()
  1663. ID3DXEffect* piEnd = g_piDefaultEffect;
  1664. // commit transformation matrices to the shader
  1665. piEnd->SetMatrix("WorldViewProjection",
  1666. (const D3DXMATRIX*)&pcProj);
  1667. piEnd->SetMatrix("World",(const D3DXMATRIX*)&aiMe);
  1668. piEnd->SetMatrix("WorldInverseTranspose",
  1669. (const D3DXMATRIX*)&pcCam);
  1670. if ( CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode())
  1671. {
  1672. pcCam = pcCam * pcProj;
  1673. piEnd->SetMatrix("ViewProj",(const D3DXMATRIX*)&pcCam);
  1674. pcCam.Inverse();
  1675. piEnd->SetMatrix("InvViewProj",(const D3DXMATRIX*)&pcCam);
  1676. }
  1677. // commit light colors and direction to the shader
  1678. D3DXVECTOR4 apcVec[5];
  1679. apcVec[0].x = g_avLightDirs[0].x;
  1680. apcVec[0].y = g_avLightDirs[0].y;
  1681. apcVec[0].z = g_avLightDirs[0].z;
  1682. apcVec[0].w = 0.0f;
  1683. apcVec[1].x = g_avLightDirs[0].x * -1.0f;
  1684. apcVec[1].y = g_avLightDirs[0].y * -1.0f;
  1685. apcVec[1].z = g_avLightDirs[0].z * -1.0f;
  1686. apcVec[1].w = 0.0f;
  1687. D3DXVec4Normalize(&apcVec[0],&apcVec[0]);
  1688. D3DXVec4Normalize(&apcVec[1],&apcVec[1]);
  1689. piEnd->SetVectorArray("afLightDir",apcVec,5);
  1690. apcVec[0].x = ((g_avLightColors[0] >> 16) & 0xFF) / 255.0f;
  1691. apcVec[0].y = ((g_avLightColors[0] >> 8) & 0xFF) / 255.0f;
  1692. apcVec[0].z = ((g_avLightColors[0]) & 0xFF) / 255.0f;
  1693. apcVec[0].w = 1.0f;
  1694. if( g_sOptions.b3Lights)
  1695. {
  1696. apcVec[1].x = ((g_avLightColors[1] >> 16) & 0xFF) / 255.0f;
  1697. apcVec[1].y = ((g_avLightColors[1] >> 8) & 0xFF) / 255.0f;
  1698. apcVec[1].z = ((g_avLightColors[1]) & 0xFF) / 255.0f;
  1699. apcVec[1].w = 0.0f;
  1700. } else
  1701. {
  1702. apcVec[1].x = 0.0f;
  1703. apcVec[1].y = 0.0f;
  1704. apcVec[1].z = 0.0f;
  1705. apcVec[1].w = 0.0f;
  1706. }
  1707. apcVec[0] *= g_fLightIntensity;
  1708. apcVec[1] *= g_fLightIntensity;
  1709. piEnd->SetVectorArray("afLightColor",apcVec,5);
  1710. apcVec[0].x = vPos.x;
  1711. apcVec[0].y = vPos.y;
  1712. apcVec[0].z = vPos.z;
  1713. piEnd->SetVector( "vCameraPos",&apcVec[0]);
  1714. // setup the best technique
  1715. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1716. {
  1717. g_piDefaultEffect->SetTechnique( "DefaultFXSpecular_FF");
  1718. } else
  1719. if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(3,0) || g_sOptions.bLowQuality)
  1720. {
  1721. if (g_sOptions.b3Lights)
  1722. piEnd->SetTechnique("DefaultFXSpecular_PS20_D2");
  1723. else piEnd->SetTechnique("DefaultFXSpecular_PS20_D1");
  1724. }
  1725. else
  1726. {
  1727. if (g_sOptions.b3Lights)
  1728. piEnd->SetTechnique("DefaultFXSpecular_D2");
  1729. else piEnd->SetTechnique("DefaultFXSpecular_D1");
  1730. }
  1731. // setup the default material
  1732. UINT dwPasses = 0;
  1733. piEnd->Begin(&dwPasses,0);
  1734. piEnd->BeginPass(0);
  1735. }
  1736. D3DXVECTOR4 vVector = g_aclNormalColors[g_iCurrentColor];
  1737. if (++g_iCurrentColor == 14)
  1738. {
  1739. g_iCurrentColor = 0;
  1740. }
  1741. if (! (!g_sOptions.bRenderMats && bAlpha ))
  1742. {
  1743. for (unsigned int i = 0; i < piNode->mNumMeshes;++i)
  1744. {
  1745. const aiMesh* mesh = g_pcAsset->pcScene->mMeshes[piNode->mMeshes[i]];
  1746. AssetHelper::MeshHelper* helper = g_pcAsset->apcMeshes[piNode->mMeshes[i]];
  1747. // don't render the mesh if the render pass is incorrect
  1748. if (g_sOptions.bRenderMats && (helper->piOpacityTexture || helper->fOpacity != 1.0f) && !mesh->HasBones())
  1749. {
  1750. if (!bAlpha)continue;
  1751. }
  1752. else if (bAlpha)continue;
  1753. // Upload bone matrices. This maybe is the wrong place to do it, but for the heck of it I don't understand this code flow
  1754. if( mesh->HasBones())
  1755. {
  1756. if( helper->piEffect)
  1757. {
  1758. static float matrices[4*4*60];
  1759. float* tempmat = matrices;
  1760. const std::vector<aiMatrix4x4>& boneMats = g_pcAsset->mAnimator->GetBoneMatrices( piNode, i);
  1761. ai_assert( boneMats.size() == mesh->mNumBones);
  1762. for( unsigned int a = 0; a < mesh->mNumBones; a++)
  1763. {
  1764. const aiMatrix4x4& mat = boneMats[a];
  1765. *tempmat++ = mat.a1; *tempmat++ = mat.a2; *tempmat++ = mat.a3; *tempmat++ = mat.a4;
  1766. *tempmat++ = mat.b1; *tempmat++ = mat.b2; *tempmat++ = mat.b3; *tempmat++ = mat.b4;
  1767. *tempmat++ = mat.c1; *tempmat++ = mat.c2; *tempmat++ = mat.c3; *tempmat++ = mat.c4;
  1768. *tempmat++ = mat.d1; *tempmat++ = mat.d2; *tempmat++ = mat.d3; *tempmat++ = mat.d4;
  1769. //tempmat += 4;
  1770. }
  1771. if( g_sOptions.bRenderMats)
  1772. {
  1773. helper->piEffect->SetMatrixTransposeArray( "gBoneMatrix", (D3DXMATRIX*)matrices, 60);
  1774. } else
  1775. {
  1776. g_piDefaultEffect->SetMatrixTransposeArray( "gBoneMatrix", (D3DXMATRIX*)matrices, 60);
  1777. g_piDefaultEffect->CommitChanges();
  1778. }
  1779. }
  1780. } else
  1781. {
  1782. // upload identity matrices instead. Only the first is ever going to be used in meshes without bones
  1783. if( !g_sOptions.bRenderMats)
  1784. {
  1785. D3DXMATRIX identity( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  1786. g_piDefaultEffect->SetMatrixTransposeArray( "gBoneMatrix", &identity, 1);
  1787. g_piDefaultEffect->CommitChanges();
  1788. }
  1789. }
  1790. // now setup the material
  1791. if (g_sOptions.bRenderMats)
  1792. {
  1793. CMaterialManager::Instance().SetupMaterial( helper, pcProj, aiMe, pcCam, vPos);
  1794. }
  1795. g_piDevice->SetVertexDeclaration( gDefaultVertexDecl);
  1796. if (bAlpha)CMeshRenderer::Instance().DrawSorted(piNode->mMeshes[i],aiMe);
  1797. else CMeshRenderer::Instance().DrawUnsorted(piNode->mMeshes[i]);
  1798. // now end the material
  1799. if (g_sOptions.bRenderMats)
  1800. {
  1801. CMaterialManager::Instance().EndMaterial( helper);
  1802. }
  1803. // render normal vectors?
  1804. if (g_sOptions.bRenderNormals && helper->piVBNormals)
  1805. {
  1806. // this is very similar to the code in SetupMaterial()
  1807. ID3DXEffect* piEnd = g_piNormalsEffect;
  1808. piEnd->SetVector("OUTPUT_COLOR",&vVector);
  1809. piEnd->SetMatrix("WorldViewProjection", (const D3DXMATRIX*)&pcProj);
  1810. UINT dwPasses = 0;
  1811. piEnd->Begin(&dwPasses,0);
  1812. piEnd->BeginPass(0);
  1813. g_piDevice->SetStreamSource(0, helper->piVBNormals, 0, sizeof(AssetHelper::LineVertex));
  1814. g_piDevice->DrawPrimitive(D3DPT_LINELIST,0, g_pcAsset->pcScene->mMeshes[piNode->mMeshes[i]]->mNumVertices);
  1815. piEnd->EndPass();
  1816. piEnd->End();
  1817. }
  1818. }
  1819. // end the default material
  1820. if (!g_sOptions.bRenderMats)
  1821. {
  1822. g_piDefaultEffect->EndPass();
  1823. g_piDefaultEffect->End();
  1824. }
  1825. }
  1826. // render all child nodes
  1827. for (unsigned int i = 0; i < piNode->mNumChildren;++i)
  1828. RenderNode(piNode->mChildren[i],piMatrix,bAlpha );
  1829. // need to reset the viewmode?
  1830. if (bChangedVM)
  1831. m_iViewMode = VIEWMODE_NODE;
  1832. return 1;
  1833. }
  1834. //-------------------------------------------------------------------------------
  1835. int CDisplay::RenderPatternBG()
  1836. {
  1837. if (!g_piPatternEffect)
  1838. {
  1839. // the pattern effect won't work on ps_2_0 cards
  1840. if (g_sCaps.PixelShaderVersion >= D3DPS_VERSION(3,0))
  1841. {
  1842. // seems we have not yet compiled this shader.
  1843. // and NOW is the best time to do that ...
  1844. ID3DXBuffer* piBuffer = NULL;
  1845. if(FAILED( D3DXCreateEffect(g_piDevice,
  1846. g_szCheckerBackgroundShader.c_str(),
  1847. (UINT)g_szCheckerBackgroundShader.length(),
  1848. NULL,
  1849. NULL,
  1850. D3DXSHADER_USE_LEGACY_D3DX9_31_DLL,
  1851. NULL,
  1852. &g_piPatternEffect,&piBuffer)))
  1853. {
  1854. if( piBuffer)
  1855. {
  1856. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  1857. piBuffer->Release();
  1858. }
  1859. return 0;
  1860. }
  1861. if( piBuffer)
  1862. {
  1863. piBuffer->Release();
  1864. piBuffer = NULL;
  1865. }
  1866. }
  1867. else
  1868. {
  1869. // clear the color buffer in magenta
  1870. // (hopefully this is ugly enough that every ps_2_0 cards owner
  1871. // runs to the next shop to buy himself a new card ...)
  1872. g_piDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  1873. D3DCOLOR_ARGB(0xFF,0xFF,0,0xFF), 1.0f,0 );
  1874. return 1;
  1875. }
  1876. }
  1877. // clear the depth buffer only
  1878. g_piDevice->Clear(0,NULL,D3DCLEAR_ZBUFFER,
  1879. D3DCOLOR_ARGB(0xFF,0xFF,0,0xFF), 1.0f,0 );
  1880. // setup the colors to be used ...
  1881. g_piPatternEffect->SetVector("COLOR_ONE",&m_avCheckerColors[0]);
  1882. g_piPatternEffect->SetVector("COLOR_TWO",&m_avCheckerColors[1]);
  1883. // setup the shader
  1884. UINT dw;
  1885. g_piPatternEffect->Begin(&dw,0);
  1886. g_piPatternEffect->BeginPass(0);
  1887. RECT sRect;
  1888. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  1889. sRect.right -= sRect.left;
  1890. sRect.bottom -= sRect.top;
  1891. struct SVertex
  1892. {
  1893. float x,y,z,w;
  1894. };
  1895. // build the screen-filling rectangle
  1896. SVertex as[4];
  1897. as[1].x = 0.0f;
  1898. as[1].y = 0.0f;
  1899. as[1].z = 0.2f;
  1900. as[3].x = (float)sRect.right;
  1901. as[3].y = 0.0f;
  1902. as[3].z = 0.2f;
  1903. as[0].x = 0.0f;
  1904. as[0].y = (float)sRect.bottom;
  1905. as[0].z = 0.2f;
  1906. as[2].x = (float)sRect.right;
  1907. as[2].y = (float)sRect.bottom;
  1908. as[2].z = 0.2f;
  1909. as[0].w = 1.0f;
  1910. as[1].w = 1.0f;
  1911. as[2].w = 1.0f;
  1912. as[3].w = 1.0f;
  1913. as[0].x -= 0.5f;as[1].x -= 0.5f;as[2].x -= 0.5f;as[3].x -= 0.5f;
  1914. as[0].y -= 0.5f;as[1].y -= 0.5f;as[2].y -= 0.5f;as[3].y -= 0.5f;
  1915. // draw the rectangle
  1916. DWORD dw2;g_piDevice->GetFVF(&dw2);
  1917. g_piDevice->SetFVF(D3DFVF_XYZRHW);
  1918. g_piDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,
  1919. &as,sizeof(SVertex));
  1920. g_piDevice->SetFVF(dw2);
  1921. // cleanup
  1922. g_piPatternEffect->EndPass();
  1923. g_piPatternEffect->End();
  1924. return 1;
  1925. }
  1926. //-------------------------------------------------------------------------------
  1927. int CDisplay::RenderTextureView()
  1928. {
  1929. if (!g_pcAsset || !g_pcAsset->pcScene)return 0;
  1930. // handle input
  1931. this->HandleInputTextureView();
  1932. // render the background
  1933. RenderPatternBG();
  1934. // it might be that there is no texture ...
  1935. if (!m_pcCurrentTexture->piTexture)
  1936. {
  1937. // FIX: no such log message. it would be repeated to often
  1938. //CLogDisplay::Instance().AddEntry("Unable to display texture. Image is unreachable.",
  1939. // D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  1940. return 0;
  1941. }
  1942. RECT sRect;
  1943. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  1944. sRect.right -= sRect.left;
  1945. sRect.bottom -= sRect.top;
  1946. // commit the texture to the shader
  1947. g_piPassThroughEffect->SetTexture("TEXTURE_2D",*m_pcCurrentTexture->piTexture);
  1948. if (aiTextureType_OPACITY == m_pcCurrentTexture->iType)
  1949. {
  1950. g_piPassThroughEffect->SetTechnique("PassThroughAlphaFromR");
  1951. }
  1952. else if ((aiTextureType_OPACITY | 0x40000000) == m_pcCurrentTexture->iType)
  1953. {
  1954. g_piPassThroughEffect->SetTechnique("PassThroughAlphaFromA");
  1955. }
  1956. else if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  1957. g_piPassThroughEffect->SetTechnique( "PassThrough_FF");
  1958. else
  1959. g_piPassThroughEffect->SetTechnique("PassThrough");
  1960. UINT dw;
  1961. g_piPassThroughEffect->Begin(&dw,0);
  1962. g_piPassThroughEffect->BeginPass(0);
  1963. if (aiTextureType_HEIGHT == m_pcCurrentTexture->iType ||
  1964. aiTextureType_NORMALS == m_pcCurrentTexture->iType)
  1965. {
  1966. // manually disable alpha blending
  1967. g_piDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
  1968. }
  1969. // build a rectangle which centers the texture
  1970. // scaling is OK, but no stretching
  1971. D3DSURFACE_DESC sDesc;
  1972. if ( m_pcCurrentTexture->piTexture && *m_pcCurrentTexture->piTexture) { /* just a dirty fix */
  1973. (*m_pcCurrentTexture->piTexture)->GetLevelDesc(0,&sDesc);
  1974. struct SVertex{float x,y,z,w,u,v;};
  1975. SVertex as[4];
  1976. const float nx = (float)sRect.right;
  1977. const float ny = (float)sRect.bottom;
  1978. const float x = (float)sDesc.Width;
  1979. const float y = (float)sDesc.Height;
  1980. float f = std::min((nx-30) / x,(ny-30) / y) * (m_fTextureZoom/1000.0f);
  1981. float fHalfX = (nx - (f * x)) / 2.0f;
  1982. float fHalfY = (ny - (f * y)) / 2.0f;
  1983. as[1].x = fHalfX + m_vTextureOffset.x;
  1984. as[1].y = fHalfY + m_vTextureOffset.y;
  1985. as[1].z = 0.2f;
  1986. as[1].w = 1.0f;
  1987. as[1].u = 0.0f;
  1988. as[1].v = 0.0f;
  1989. as[3].x = nx-fHalfX + m_vTextureOffset.x;
  1990. as[3].y = fHalfY + m_vTextureOffset.y;
  1991. as[3].z = 0.2f;
  1992. as[3].w = 1.0f;
  1993. as[3].u = 1.0f;
  1994. as[3].v = 0.0f;
  1995. as[0].x = fHalfX + m_vTextureOffset.x;
  1996. as[0].y = ny-fHalfY + m_vTextureOffset.y;
  1997. as[0].z = 0.2f;
  1998. as[0].w = 1.0f;
  1999. as[0].u = 0.0f;
  2000. as[0].v = 1.0f;
  2001. as[2].x = nx-fHalfX + m_vTextureOffset.x;
  2002. as[2].y = ny-fHalfY + m_vTextureOffset.y;
  2003. as[2].z = 0.2f;
  2004. as[2].w = 1.0f;
  2005. as[2].u = 1.0f;
  2006. as[2].v = 1.0f;
  2007. as[0].x -= 0.5f;as[1].x -= 0.5f;as[2].x -= 0.5f;as[3].x -= 0.5f;
  2008. as[0].y -= 0.5f;as[1].y -= 0.5f;as[2].y -= 0.5f;as[3].y -= 0.5f;
  2009. // draw the rectangle
  2010. DWORD dw2;g_piDevice->GetFVF(&dw2);
  2011. g_piDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
  2012. g_piDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,
  2013. &as,sizeof(SVertex));
  2014. g_piDevice->SetFVF(dw2);
  2015. }
  2016. g_piPassThroughEffect->EndPass();
  2017. g_piPassThroughEffect->End();
  2018. // do we need to draw UV coordinates?
  2019. return 1;
  2020. }
  2021. };
  2022. #endif
  2023. }
  2024. }