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.
 
 
 
 
 
 

2290 lines
67 KiB

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