Display.cpp 77 KB

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