Display.cpp 77 KB

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