Display.cpp 77 KB

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