Display.cpp 69 KB

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