Display.cpp 78 KB

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