Display.cpp 77 KB

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