Display.cpp 68 KB

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