assimp_view.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2018, 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 <assimp/StringUtils.h>
  36. #include <map>
  37. #ifdef __MINGW32__
  38. #include <mmsystem.h>
  39. #else
  40. #include <timeapi.h>
  41. #endif
  42. using namespace std;
  43. namespace AssimpView {
  44. extern std::string g_szNormalsShader;
  45. extern std::string g_szDefaultShader;
  46. extern std::string g_szPassThroughShader;
  47. //-------------------------------------------------------------------------------
  48. HINSTANCE g_hInstance = NULL;
  49. HWND g_hDlg = NULL;
  50. IDirect3D9* g_piD3D = NULL;
  51. IDirect3DDevice9* g_piDevice = NULL;
  52. IDirect3DVertexDeclaration9* gDefaultVertexDecl = NULL;
  53. double g_fFPS = 0.0f;
  54. char g_szFileName[MAX_PATH];
  55. ID3DXEffect* g_piDefaultEffect = NULL;
  56. ID3DXEffect* g_piNormalsEffect = NULL;
  57. ID3DXEffect* g_piPassThroughEffect = NULL;
  58. ID3DXEffect* g_piPatternEffect = NULL;
  59. bool g_bMousePressed = false;
  60. bool g_bMousePressedR = false;
  61. bool g_bMousePressedM = false;
  62. bool g_bMousePressedBoth = false;
  63. float g_fElpasedTime = 0.0f;
  64. D3DCAPS9 g_sCaps;
  65. bool g_bLoadingFinished = false;
  66. HANDLE g_hThreadHandle = NULL;
  67. float g_fWheelPos = -10.0f;
  68. bool g_bLoadingCanceled = false;
  69. IDirect3DTexture9* g_pcTexture = NULL;
  70. bool g_bPlay = false;
  71. double g_dCurrent = 0.;
  72. // default pp steps
  73. unsigned int ppsteps = aiProcess_CalcTangentSpace | // calculate tangents and bitangents if possible
  74. aiProcess_JoinIdenticalVertices | // join identical vertices/ optimize indexing
  75. aiProcess_ValidateDataStructure | // perform a full validation of the loader's output
  76. aiProcess_ImproveCacheLocality | // improve the cache locality of the output vertices
  77. aiProcess_RemoveRedundantMaterials | // remove redundant materials
  78. aiProcess_FindDegenerates | // remove degenerated polygons from the import
  79. aiProcess_FindInvalidData | // detect invalid model data, such as invalid normal vectors
  80. aiProcess_GenUVCoords | // convert spherical, cylindrical, box and planar mapping to proper UVs
  81. aiProcess_TransformUVCoords | // preprocess UV transformations (scaling, translation ...)
  82. aiProcess_FindInstances | // search for instanced meshes and remove them by references to one master
  83. aiProcess_LimitBoneWeights | // limit bone weights to 4 per vertex
  84. aiProcess_OptimizeMeshes | // join small meshes, if possible;
  85. aiProcess_SplitByBoneCount | // split meshes with too many bones. Necessary for our (limited) hardware skinning shader
  86. 0;
  87. unsigned int ppstepsdefault = ppsteps;
  88. bool nopointslines = false;
  89. extern bool g_bWasFlipped /*= false*/;
  90. aiMatrix4x4 g_mWorld;
  91. aiMatrix4x4 g_mWorldRotate;
  92. aiVector3D g_vRotateSpeed = aiVector3D(0.5f,0.5f,0.5f);
  93. // NOTE: The second light direction is now computed from the first
  94. aiVector3D g_avLightDirs[1] =
  95. { aiVector3D(-0.5f,0.6f,0.2f) };
  96. D3DCOLOR g_avLightColors[3] =
  97. {
  98. D3DCOLOR_ARGB(0xFF,0xFF,0xFF,0xFF),
  99. D3DCOLOR_ARGB(0xFF,0xFF,0x00,0x00),
  100. D3DCOLOR_ARGB(0xFF,0x05,0x05,0x05),
  101. };
  102. POINT g_mousePos;
  103. POINT g_LastmousePos;
  104. bool g_bFPSView = false;
  105. bool g_bInvert = false;
  106. EClickPos g_eClick = EClickPos_Circle;
  107. unsigned int g_iCurrentColor = 0;
  108. float g_fLightIntensity = 1.0f;
  109. float g_fLightColor = 1.0f;
  110. RenderOptions g_sOptions;
  111. Camera g_sCamera;
  112. AssetHelper *g_pcAsset = NULL;
  113. //
  114. // Contains the mask image for the HUD
  115. // (used to determine the position of a click)
  116. //
  117. unsigned char* g_szImageMask = NULL;
  118. float g_fLoadTime = 0.0f;
  119. //-------------------------------------------------------------------------------
  120. // Entry point for the loader thread
  121. // The loader thread loads the asset while the progress dialog displays the
  122. // smart progress bar
  123. //-------------------------------------------------------------------------------
  124. DWORD WINAPI LoadThreadProc(LPVOID lpParameter)
  125. {
  126. UNREFERENCED_PARAMETER(lpParameter);
  127. // get current time
  128. double fCur = (double)timeGetTime();
  129. aiPropertyStore* props = aiCreatePropertyStore();
  130. aiSetImportPropertyInteger(props,AI_CONFIG_IMPORT_TER_MAKE_UVS,1);
  131. aiSetImportPropertyFloat(props,AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE,g_smoothAngle);
  132. aiSetImportPropertyInteger(props,AI_CONFIG_PP_SBP_REMOVE,nopointslines ? aiPrimitiveType_LINE | aiPrimitiveType_POINT : 0 );
  133. aiSetImportPropertyInteger(props,AI_CONFIG_GLOB_MEASURE_TIME,1);
  134. //aiSetImportPropertyInteger(props,AI_CONFIG_PP_PTV_KEEP_HIERARCHY,1);
  135. // Call ASSIMPs C-API to load the file
  136. g_pcAsset->pcScene = (aiScene*)aiImportFileExWithProperties(g_szFileName,
  137. ppsteps | /* configurable pp steps */
  138. aiProcess_GenSmoothNormals | // generate smooth normal vectors if not existing
  139. aiProcess_SplitLargeMeshes | // split large, unrenderable meshes into submeshes
  140. aiProcess_Triangulate | // triangulate polygons with more than 3 edges
  141. aiProcess_ConvertToLeftHanded | // convert everything to D3D left handed space
  142. aiProcess_SortByPType | // make 'clean' meshes which consist of a single typ of primitives
  143. 0,
  144. NULL,
  145. props);
  146. aiReleasePropertyStore(props);
  147. // get the end time of zje operation, calculate delta t
  148. double fEnd = (double)timeGetTime();
  149. g_fLoadTime = (float)((fEnd - fCur) / 1000);
  150. g_bLoadingFinished = true;
  151. // check whether the loading process has failed ...
  152. if (NULL == g_pcAsset->pcScene)
  153. {
  154. CLogDisplay::Instance().AddEntry("[ERROR] Unable to load this asset:",
  155. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  156. // print ASSIMPs error string to the log display
  157. CLogDisplay::Instance().AddEntry(aiGetErrorString(),
  158. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. //-------------------------------------------------------------------------------
  164. // load the current asset
  165. // THe path to the asset is specified in the global path variable
  166. //-------------------------------------------------------------------------------
  167. int LoadAsset()
  168. {
  169. // set the world and world rotation matrices to the identity
  170. g_mWorldRotate = aiMatrix4x4();
  171. g_mWorld = aiMatrix4x4();
  172. // char szTemp[MAX_PATH+64];
  173. // sprintf(szTemp,"Starting to load %s",g_szFileName);
  174. CLogWindow::Instance().WriteLine(
  175. "----------------------------------------------------------------------------");
  176. // CLogWindow::Instance().WriteLine(szTemp);
  177. // CLogWindow::Instance().WriteLine(
  178. // "----------------------------------------------------------------------------");
  179. CLogWindow::Instance().SetAutoUpdate(false);
  180. // create a helper thread to load the asset
  181. DWORD dwID;
  182. g_bLoadingCanceled = false;
  183. g_pcAsset = new AssetHelper();
  184. g_hThreadHandle = CreateThread(NULL,0,&LoadThreadProc,NULL,0,&dwID);
  185. if (!g_hThreadHandle)
  186. {
  187. CLogDisplay::Instance().AddEntry(
  188. "[ERROR] Unable to create helper thread for loading",
  189. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  190. return 0;
  191. }
  192. // show the progress bar dialog
  193. DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_LOADDIALOG),
  194. g_hDlg,&ProgressMessageProc);
  195. // update the log window
  196. CLogWindow::Instance().SetAutoUpdate(true);
  197. CLogWindow::Instance().Update();
  198. // now we should have loaded the asset. Check this ...
  199. g_bLoadingFinished = false;
  200. if (!g_pcAsset || !g_pcAsset->pcScene)
  201. {
  202. if (g_pcAsset)
  203. {
  204. delete g_pcAsset;
  205. g_pcAsset = NULL;
  206. }
  207. return 0;
  208. }
  209. // allocate a new MeshHelper array and build a new instance
  210. // for each mesh in the original asset
  211. g_pcAsset->apcMeshes = new AssetHelper::MeshHelper*[g_pcAsset->pcScene->mNumMeshes]();
  212. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  213. g_pcAsset->apcMeshes[i] = new AssetHelper::MeshHelper();
  214. // create animator
  215. g_pcAsset->mAnimator = new SceneAnimator( g_pcAsset->pcScene);
  216. // build a new caption string for the viewer
  217. static const size_t Size = MAX_PATH + 10;
  218. char szOut[Size];
  219. ai_snprintf(szOut, Size,AI_VIEW_CAPTION_BASE " [%s]",g_szFileName);
  220. SetWindowText(g_hDlg,szOut);
  221. // scale the asset vertices to fit into the viewer window
  222. ScaleAsset();
  223. // reset the camera view to the default position
  224. g_sCamera.vPos = aiVector3D(0.0f,0.0f,-10.0f);
  225. g_sCamera.vLookAt = aiVector3D(0.0f,0.0f,1.0f);
  226. g_sCamera.vUp = aiVector3D(0.0f,1.0f,0.0f);
  227. g_sCamera.vRight = aiVector3D(0.0f,1.0f,0.0f);
  228. // build native D3D vertex/index buffers, textures, materials
  229. if( 1 != CreateAssetData())
  230. return 0;
  231. if (!g_pcAsset->pcScene->HasAnimations()) {
  232. EnableWindow(GetDlgItem(g_hDlg,IDC_PLAY),FALSE);
  233. EnableWindow(GetDlgItem(g_hDlg,IDC_SLIDERANIM),FALSE);
  234. }
  235. else {
  236. EnableWindow(GetDlgItem(g_hDlg,IDC_PLAY),TRUE);
  237. EnableWindow(GetDlgItem(g_hDlg,IDC_SLIDERANIM),TRUE);
  238. }
  239. CLogDisplay::Instance().AddEntry("[OK] The asset has been loaded successfully");
  240. CDisplay::Instance().FillDisplayList();
  241. CDisplay::Instance().FillAnimList();
  242. CDisplay::Instance().FillDefaultStatistics();
  243. // render the scene once
  244. CDisplay::Instance().OnRender();
  245. g_pcAsset->iNormalSet = AssetHelper::ORIGINAL;
  246. g_bWasFlipped = false;
  247. return 1;
  248. }
  249. //-------------------------------------------------------------------------------
  250. // Delete the loaded asset
  251. // The function does nothing is no asset is loaded
  252. //-------------------------------------------------------------------------------
  253. int DeleteAsset(void)
  254. {
  255. if (!g_pcAsset)return 0;
  256. // don't anymore know why this was necessary ...
  257. CDisplay::Instance().OnRender();
  258. // delete everything
  259. DeleteAssetData();
  260. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  261. {
  262. delete g_pcAsset->apcMeshes[i];
  263. }
  264. aiReleaseImport(g_pcAsset->pcScene);
  265. delete[] g_pcAsset->apcMeshes;
  266. delete g_pcAsset->mAnimator;
  267. delete g_pcAsset;
  268. g_pcAsset = NULL;
  269. // reset the caption of the viewer window
  270. SetWindowText(g_hDlg,AI_VIEW_CAPTION_BASE);
  271. // clear UI
  272. CDisplay::Instance().ClearAnimList();
  273. CDisplay::Instance().ClearDisplayList();
  274. CMaterialManager::Instance().Reset();
  275. UpdateWindow(g_hDlg);
  276. return 1;
  277. }
  278. //-------------------------------------------------------------------------------
  279. // Calculate the boundaries of a given node and all of its children
  280. // The boundaries are in Worldspace (AABB)
  281. // piNode Input node
  282. // p_avOut Receives the min/max boundaries. Must point to 2 vec3s
  283. // piMatrix Transformation matrix of the graph at this position
  284. //-------------------------------------------------------------------------------
  285. int CalculateBounds(aiNode* piNode, aiVector3D* p_avOut,
  286. const aiMatrix4x4& piMatrix)
  287. {
  288. ai_assert(NULL != piNode);
  289. ai_assert(NULL != p_avOut);
  290. aiMatrix4x4 mTemp = piNode->mTransformation;
  291. mTemp.Transpose();
  292. aiMatrix4x4 aiMe = mTemp * piMatrix;
  293. for (unsigned int i = 0; i < piNode->mNumMeshes;++i)
  294. {
  295. for( unsigned int a = 0; a < g_pcAsset->pcScene->mMeshes[
  296. piNode->mMeshes[i]]->mNumVertices;++a)
  297. {
  298. aiVector3D pc =g_pcAsset->pcScene->mMeshes[piNode->mMeshes[i]]->mVertices[a];
  299. aiVector3D pc1;
  300. D3DXVec3TransformCoord((D3DXVECTOR3*)&pc1,(D3DXVECTOR3*)&pc,
  301. (D3DXMATRIX*)&aiMe);
  302. p_avOut[0].x = min( p_avOut[0].x, pc1.x);
  303. p_avOut[0].y = min( p_avOut[0].y, pc1.y);
  304. p_avOut[0].z = min( p_avOut[0].z, pc1.z);
  305. p_avOut[1].x = max( p_avOut[1].x, pc1.x);
  306. p_avOut[1].y = max( p_avOut[1].y, pc1.y);
  307. p_avOut[1].z = max( p_avOut[1].z, pc1.z);
  308. }
  309. }
  310. for (unsigned int i = 0; i < piNode->mNumChildren;++i)
  311. {
  312. CalculateBounds( piNode->mChildren[i], p_avOut, aiMe );
  313. }
  314. return 1;
  315. }
  316. //-------------------------------------------------------------------------------
  317. // Scale the asset that it fits perfectly into the viewer window
  318. // The function calculates the boundaries of the mesh and modifies the
  319. // global world transformation matrix according to the aset AABB
  320. //-------------------------------------------------------------------------------
  321. int ScaleAsset(void)
  322. {
  323. aiVector3D aiVecs[2] = {aiVector3D( 1e10f, 1e10f, 1e10f),
  324. aiVector3D( -1e10f, -1e10f, -1e10f) };
  325. if (g_pcAsset->pcScene->mRootNode)
  326. {
  327. aiMatrix4x4 m;
  328. CalculateBounds(g_pcAsset->pcScene->mRootNode,aiVecs,m);
  329. }
  330. aiVector3D vDelta = aiVecs[1]-aiVecs[0];
  331. aiVector3D vHalf = aiVecs[0] + (vDelta / 2.0f);
  332. float fScale = 10.0f / vDelta.Length();
  333. g_mWorld = aiMatrix4x4(
  334. 1.0f,0.0f,0.0f,0.0f,
  335. 0.0f,1.0f,0.0f,0.0f,
  336. 0.0f,0.0f,1.0f,0.0f,
  337. -vHalf.x,-vHalf.y,-vHalf.z,1.0f) *
  338. aiMatrix4x4(
  339. fScale,0.0f,0.0f,0.0f,
  340. 0.0f,fScale,0.0f,0.0f,
  341. 0.0f,0.0f,fScale,0.0f,
  342. 0.0f,0.0f,0.0f,1.0f);
  343. return 1;
  344. }
  345. //-------------------------------------------------------------------------------
  346. // Generate a vertex buffer which holds the normals of the asset as
  347. // a list of unconnected lines
  348. // pcMesh Input mesh
  349. // pcSource Source mesh from ASSIMP
  350. //-------------------------------------------------------------------------------
  351. int GenerateNormalsAsLineList(AssetHelper::MeshHelper* pcMesh,const aiMesh* pcSource)
  352. {
  353. ai_assert(NULL != pcMesh);
  354. ai_assert(NULL != pcSource);
  355. if (!pcSource->mNormals)return 0;
  356. // create vertex buffer
  357. if(FAILED( g_piDevice->CreateVertexBuffer(sizeof(AssetHelper::LineVertex) *
  358. pcSource->mNumVertices * 2,
  359. D3DUSAGE_WRITEONLY,
  360. AssetHelper::LineVertex::GetFVF(),
  361. D3DPOOL_DEFAULT, &pcMesh->piVBNormals,NULL)))
  362. {
  363. CLogDisplay::Instance().AddEntry("Failed to create vertex buffer for the normal list",
  364. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  365. return 2;
  366. }
  367. // now fill the vertex buffer with data
  368. AssetHelper::LineVertex* pbData2;
  369. pcMesh->piVBNormals->Lock(0,0,(void**)&pbData2,0);
  370. for (unsigned int x = 0; x < pcSource->mNumVertices;++x)
  371. {
  372. pbData2->vPosition = pcSource->mVertices[x];
  373. ++pbData2;
  374. aiVector3D vNormal = pcSource->mNormals[x];
  375. vNormal.Normalize();
  376. // scalo with the inverse of the world scaling to make sure
  377. // the normals have equal length in each case
  378. // TODO: Check whether this works in every case, I don't think so
  379. vNormal.x /= g_mWorld.a1*4;
  380. vNormal.y /= g_mWorld.b2*4;
  381. vNormal.z /= g_mWorld.c3*4;
  382. pbData2->vPosition = pcSource->mVertices[x] + vNormal;
  383. ++pbData2;
  384. }
  385. pcMesh->piVBNormals->Unlock();
  386. return 1;
  387. }
  388. //-------------------------------------------------------------------------------
  389. // Create the native D3D representation of the asset: vertex buffers,
  390. // index buffers, materials ...
  391. //-------------------------------------------------------------------------------
  392. int CreateAssetData()
  393. {
  394. if (!g_pcAsset)return 0;
  395. // reset all subsystems
  396. CMaterialManager::Instance().Reset();
  397. CDisplay::Instance().Reset();
  398. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  399. {
  400. const aiMesh* mesh = g_pcAsset->pcScene->mMeshes[i];
  401. // create the material for the mesh
  402. if (!g_pcAsset->apcMeshes[i]->piEffect) {
  403. CMaterialManager::Instance().CreateMaterial(
  404. g_pcAsset->apcMeshes[i],mesh);
  405. }
  406. // create vertex buffer
  407. if(FAILED( g_piDevice->CreateVertexBuffer(sizeof(AssetHelper::Vertex) *
  408. mesh->mNumVertices,
  409. D3DUSAGE_WRITEONLY,
  410. 0,
  411. D3DPOOL_DEFAULT, &g_pcAsset->apcMeshes[i]->piVB,NULL))) {
  412. MessageBox(g_hDlg,"Failed to create vertex buffer",
  413. "ASSIMP Viewer Utility",MB_OK);
  414. return 2;
  415. }
  416. DWORD dwUsage = 0;
  417. if (g_pcAsset->apcMeshes[i]->piOpacityTexture || 1.0f != g_pcAsset->apcMeshes[i]->fOpacity)
  418. dwUsage |= D3DUSAGE_DYNAMIC;
  419. unsigned int nidx;
  420. switch (mesh->mPrimitiveTypes) {
  421. case aiPrimitiveType_POINT:
  422. nidx = 1;break;
  423. case aiPrimitiveType_LINE:
  424. nidx = 2;break;
  425. case aiPrimitiveType_TRIANGLE:
  426. nidx = 3;break;
  427. default: ai_assert(false);
  428. };
  429. // check whether we can use 16 bit indices
  430. if (mesh->mNumFaces * 3 >= 65536) {
  431. // create 32 bit index buffer
  432. if(FAILED( g_piDevice->CreateIndexBuffer( 4 *
  433. mesh->mNumFaces * nidx,
  434. D3DUSAGE_WRITEONLY | dwUsage,
  435. D3DFMT_INDEX32,
  436. D3DPOOL_DEFAULT,
  437. &g_pcAsset->apcMeshes[i]->piIB,
  438. NULL)))
  439. {
  440. MessageBox(g_hDlg,"Failed to create 32 Bit index buffer",
  441. "ASSIMP Viewer Utility",MB_OK);
  442. return 2;
  443. }
  444. // now fill the index buffer
  445. unsigned int* pbData;
  446. g_pcAsset->apcMeshes[i]->piIB->Lock(0,0,(void**)&pbData,0);
  447. for (unsigned int x = 0; x < mesh->mNumFaces;++x)
  448. {
  449. for (unsigned int a = 0; a < nidx;++a)
  450. {
  451. *pbData++ = mesh->mFaces[x].mIndices[a];
  452. }
  453. }
  454. }
  455. else {
  456. // create 16 bit index buffer
  457. if(FAILED( g_piDevice->CreateIndexBuffer( 2 *
  458. mesh->mNumFaces * nidx,
  459. D3DUSAGE_WRITEONLY | dwUsage,
  460. D3DFMT_INDEX16,
  461. D3DPOOL_DEFAULT,
  462. &g_pcAsset->apcMeshes[i]->piIB,
  463. NULL)))
  464. {
  465. MessageBox(g_hDlg,"Failed to create 16 Bit index buffer",
  466. "ASSIMP Viewer Utility",MB_OK);
  467. return 2;
  468. }
  469. // now fill the index buffer
  470. uint16_t* pbData;
  471. g_pcAsset->apcMeshes[i]->piIB->Lock(0,0,(void**)&pbData,0);
  472. for (unsigned int x = 0; x < mesh->mNumFaces;++x)
  473. {
  474. for (unsigned int a = 0; a < nidx;++a)
  475. {
  476. *pbData++ = (uint16_t)mesh->mFaces[x].mIndices[a];
  477. }
  478. }
  479. }
  480. g_pcAsset->apcMeshes[i]->piIB->Unlock();
  481. // collect weights on all vertices. Quick and careless
  482. std::vector<std::vector<aiVertexWeight> > weightsPerVertex( mesh->mNumVertices);
  483. for( unsigned int a = 0; a < mesh->mNumBones; a++) {
  484. const aiBone* bone = mesh->mBones[a];
  485. for( unsigned int b = 0; b < bone->mNumWeights; b++)
  486. weightsPerVertex[bone->mWeights[b].mVertexId].push_back( aiVertexWeight( a, bone->mWeights[b].mWeight));
  487. }
  488. // now fill the vertex buffer
  489. AssetHelper::Vertex* pbData2;
  490. g_pcAsset->apcMeshes[i]->piVB->Lock(0,0,(void**)&pbData2,0);
  491. for (unsigned int x = 0; x < mesh->mNumVertices;++x)
  492. {
  493. pbData2->vPosition = mesh->mVertices[x];
  494. if (NULL == mesh->mNormals)
  495. pbData2->vNormal = aiVector3D(0.0f,0.0f,0.0f);
  496. else pbData2->vNormal = mesh->mNormals[x];
  497. if (NULL == mesh->mTangents) {
  498. pbData2->vTangent = aiVector3D(0.0f,0.0f,0.0f);
  499. pbData2->vBitangent = aiVector3D(0.0f,0.0f,0.0f);
  500. }
  501. else {
  502. pbData2->vTangent = mesh->mTangents[x];
  503. pbData2->vBitangent = mesh->mBitangents[x];
  504. }
  505. if (mesh->HasVertexColors( 0)) {
  506. pbData2->dColorDiffuse = D3DCOLOR_ARGB(
  507. ((unsigned char)max( min( mesh->mColors[0][x].a * 255.0f, 255.0f),0.0f)),
  508. ((unsigned char)max( min( mesh->mColors[0][x].r * 255.0f, 255.0f),0.0f)),
  509. ((unsigned char)max( min( mesh->mColors[0][x].g * 255.0f, 255.0f),0.0f)),
  510. ((unsigned char)max( min( mesh->mColors[0][x].b * 255.0f, 255.0f),0.0f)));
  511. }
  512. else pbData2->dColorDiffuse = D3DCOLOR_ARGB(0xFF,0xff,0xff,0xff);
  513. // ignore a third texture coordinate component
  514. if (mesh->HasTextureCoords( 0)) {
  515. pbData2->vTextureUV = aiVector2D(
  516. mesh->mTextureCoords[0][x].x,
  517. mesh->mTextureCoords[0][x].y);
  518. }
  519. else pbData2->vTextureUV = aiVector2D(0.5f,0.5f);
  520. if (mesh->HasTextureCoords( 1)) {
  521. pbData2->vTextureUV2 = aiVector2D(
  522. mesh->mTextureCoords[1][x].x,
  523. mesh->mTextureCoords[1][x].y);
  524. }
  525. else pbData2->vTextureUV2 = aiVector2D(0.5f,0.5f);
  526. // Bone indices and weights
  527. if( mesh->HasBones()) {
  528. unsigned char boneIndices[4] = { 0, 0, 0, 0 };
  529. unsigned char boneWeights[4] = { 0, 0, 0, 0 };
  530. ai_assert( weightsPerVertex[x].size() <= 4);
  531. for( unsigned int a = 0; a < weightsPerVertex[x].size(); a++)
  532. {
  533. boneIndices[a] = weightsPerVertex[x][a].mVertexId;
  534. boneWeights[a] = (unsigned char) (weightsPerVertex[x][a].mWeight * 255.0f);
  535. }
  536. memcpy( pbData2->mBoneIndices, boneIndices, sizeof( boneIndices));
  537. memcpy( pbData2->mBoneWeights, boneWeights, sizeof( boneWeights));
  538. } else
  539. {
  540. memset( pbData2->mBoneIndices, 0, sizeof( pbData2->mBoneIndices));
  541. memset( pbData2->mBoneWeights, 0, sizeof( pbData2->mBoneWeights));
  542. }
  543. ++pbData2;
  544. }
  545. g_pcAsset->apcMeshes[i]->piVB->Unlock();
  546. // now generate the second vertex buffer, holding all normals
  547. if (!g_pcAsset->apcMeshes[i]->piVBNormals) {
  548. GenerateNormalsAsLineList(g_pcAsset->apcMeshes[i],mesh);
  549. }
  550. }
  551. return 1;
  552. }
  553. //-------------------------------------------------------------------------------
  554. // Delete all effects, textures, vertex buffers ... associated with
  555. // an asset
  556. //-------------------------------------------------------------------------------
  557. int DeleteAssetData(bool bNoMaterials)
  558. {
  559. if (!g_pcAsset)return 0;
  560. // TODO: Move this to a proper destructor
  561. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  562. {
  563. if(g_pcAsset->apcMeshes[i]->piVB)
  564. {
  565. g_pcAsset->apcMeshes[i]->piVB->Release();
  566. g_pcAsset->apcMeshes[i]->piVB = NULL;
  567. }
  568. if(g_pcAsset->apcMeshes[i]->piVBNormals)
  569. {
  570. g_pcAsset->apcMeshes[i]->piVBNormals->Release();
  571. g_pcAsset->apcMeshes[i]->piVBNormals = NULL;
  572. }
  573. if(g_pcAsset->apcMeshes[i]->piIB)
  574. {
  575. g_pcAsset->apcMeshes[i]->piIB->Release();
  576. g_pcAsset->apcMeshes[i]->piIB = NULL;
  577. }
  578. // TODO ... unfixed memory leak
  579. // delete storage eventually allocated to hold a copy
  580. // of the original vertex normals
  581. //if (g_pcAsset->apcMeshes[i]->pvOriginalNormals)
  582. //{
  583. // delete[] g_pcAsset->apcMeshes[i]->pvOriginalNormals;
  584. //}
  585. if (!bNoMaterials)
  586. {
  587. if(g_pcAsset->apcMeshes[i]->piEffect)
  588. {
  589. g_pcAsset->apcMeshes[i]->piEffect->Release();
  590. g_pcAsset->apcMeshes[i]->piEffect = NULL;
  591. }
  592. if(g_pcAsset->apcMeshes[i]->piDiffuseTexture)
  593. {
  594. g_pcAsset->apcMeshes[i]->piDiffuseTexture->Release();
  595. g_pcAsset->apcMeshes[i]->piDiffuseTexture = NULL;
  596. }
  597. if(g_pcAsset->apcMeshes[i]->piNormalTexture)
  598. {
  599. g_pcAsset->apcMeshes[i]->piNormalTexture->Release();
  600. g_pcAsset->apcMeshes[i]->piNormalTexture = NULL;
  601. }
  602. if(g_pcAsset->apcMeshes[i]->piSpecularTexture)
  603. {
  604. g_pcAsset->apcMeshes[i]->piSpecularTexture->Release();
  605. g_pcAsset->apcMeshes[i]->piSpecularTexture = NULL;
  606. }
  607. if(g_pcAsset->apcMeshes[i]->piAmbientTexture)
  608. {
  609. g_pcAsset->apcMeshes[i]->piAmbientTexture->Release();
  610. g_pcAsset->apcMeshes[i]->piAmbientTexture = NULL;
  611. }
  612. if(g_pcAsset->apcMeshes[i]->piEmissiveTexture)
  613. {
  614. g_pcAsset->apcMeshes[i]->piEmissiveTexture->Release();
  615. g_pcAsset->apcMeshes[i]->piEmissiveTexture = NULL;
  616. }
  617. if(g_pcAsset->apcMeshes[i]->piOpacityTexture)
  618. {
  619. g_pcAsset->apcMeshes[i]->piOpacityTexture->Release();
  620. g_pcAsset->apcMeshes[i]->piOpacityTexture = NULL;
  621. }
  622. if(g_pcAsset->apcMeshes[i]->piShininessTexture)
  623. {
  624. g_pcAsset->apcMeshes[i]->piShininessTexture->Release();
  625. g_pcAsset->apcMeshes[i]->piShininessTexture = NULL;
  626. }
  627. }
  628. }
  629. return 1;
  630. }
  631. //-------------------------------------------------------------------------------
  632. // Switch beetween zoom/rotate view and the standatd FPS view
  633. // g_bFPSView specifies the view mode to setup
  634. //-------------------------------------------------------------------------------
  635. int SetupFPSView()
  636. {
  637. if (!g_bFPSView)
  638. {
  639. g_sCamera.vPos = aiVector3D(0.0f,0.0f,g_fWheelPos);
  640. g_sCamera.vLookAt = aiVector3D(0.0f,0.0f,1.0f);
  641. g_sCamera.vUp = aiVector3D(0.0f,1.0f,0.0f);
  642. g_sCamera.vRight = aiVector3D(0.0f,1.0f,0.0f);
  643. }
  644. else
  645. {
  646. g_fWheelPos = g_sCamera.vPos.z;
  647. g_sCamera.vPos = aiVector3D(0.0f,0.0f,-10.0f);
  648. g_sCamera.vLookAt = aiVector3D(0.0f,0.0f,1.0f);
  649. g_sCamera.vUp = aiVector3D(0.0f,1.0f,0.0f);
  650. g_sCamera.vRight = aiVector3D(0.0f,1.0f,0.0f);
  651. }
  652. return 1;
  653. }
  654. //-------------------------------------------------------------------------------
  655. // Initialize the IDIrect3D interface
  656. // Called by the WinMain
  657. //-------------------------------------------------------------------------------
  658. int InitD3D(void)
  659. {
  660. if (NULL == g_piD3D)
  661. {
  662. g_piD3D = Direct3DCreate9(D3D_SDK_VERSION);
  663. if (NULL == g_piD3D)return 0;
  664. }
  665. return 1;
  666. }
  667. //-------------------------------------------------------------------------------
  668. // Release the IDirect3D interface.
  669. // NOTE: Assumes that the device has already been deleted
  670. //-------------------------------------------------------------------------------
  671. int ShutdownD3D(void)
  672. {
  673. ShutdownDevice();
  674. if (NULL != g_piD3D)
  675. {
  676. g_piD3D->Release();
  677. g_piD3D = NULL;
  678. }
  679. return 1;
  680. }
  681. template<class TComPtr>
  682. inline
  683. void SafeRelease(TComPtr *ptr) {
  684. if (nullptr != g_piPassThroughEffect) {
  685. g_piPassThroughEffect->Release();
  686. g_piPassThroughEffect = nullptr;
  687. }
  688. }
  689. //-------------------------------------------------------------------------------
  690. // Shutdown the D3D device object and all resources associated with it
  691. // NOTE: Assumes that the asset has already been deleted
  692. //-------------------------------------------------------------------------------
  693. int ShutdownDevice(void)
  694. {
  695. // release other subsystems
  696. CBackgroundPainter::Instance().ReleaseNativeResource();
  697. CLogDisplay::Instance().ReleaseNativeResource();
  698. // release global shaders that have been allocated
  699. SafeRelease(g_piDefaultEffect);
  700. SafeRelease(g_piNormalsEffect);
  701. SafeRelease(g_piPassThroughEffect);
  702. SafeRelease(g_piPatternEffect);
  703. SafeRelease(g_pcTexture);
  704. SafeRelease(gDefaultVertexDecl);
  705. // delete the main D3D device object
  706. SafeRelease(g_piDevice);
  707. // deleted the one channel image allocated to hold the HUD mask
  708. delete[] g_szImageMask;
  709. g_szImageMask = nullptr;
  710. return 1;
  711. }
  712. //-------------------------------------------------------------------------------
  713. //-------------------------------------------------------------------------------
  714. int CreateHUDTexture()
  715. {
  716. // lock the memory resource ourselves
  717. HRSRC res = FindResource(NULL,MAKEINTRESOURCE(IDR_HUD),RT_RCDATA);
  718. HGLOBAL hg = LoadResource(NULL,res);
  719. void* pData = LockResource(hg);
  720. if(FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
  721. pData,SizeofResource(NULL,res),
  722. D3DX_DEFAULT_NONPOW2,
  723. D3DX_DEFAULT_NONPOW2,
  724. 1,
  725. 0,
  726. D3DFMT_A8R8G8B8,
  727. D3DPOOL_MANAGED,
  728. D3DX_DEFAULT,
  729. D3DX_DEFAULT,
  730. 0,
  731. NULL,
  732. NULL,
  733. &g_pcTexture)))
  734. {
  735. CLogDisplay::Instance().AddEntry("[ERROR] Unable to load HUD texture",
  736. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  737. g_pcTexture = NULL;
  738. g_szImageMask = NULL;
  739. FreeResource(hg);
  740. return 0;
  741. }
  742. FreeResource(hg);
  743. D3DSURFACE_DESC sDesc;
  744. g_pcTexture->GetLevelDesc(0,&sDesc);
  745. // lock the memory resource ourselves
  746. res = FindResource(NULL,MAKEINTRESOURCE(IDR_HUDMASK),RT_RCDATA);
  747. hg = LoadResource(NULL,res);
  748. pData = LockResource(hg);
  749. IDirect3DTexture9* pcTex;
  750. if(FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
  751. pData,SizeofResource(NULL,res),
  752. sDesc.Width,
  753. sDesc.Height,
  754. 1,
  755. 0,
  756. D3DFMT_L8,
  757. D3DPOOL_MANAGED, // unnecessary
  758. D3DX_DEFAULT,
  759. D3DX_DEFAULT,
  760. 0,
  761. NULL,
  762. NULL,
  763. &pcTex)))
  764. {
  765. CLogDisplay::Instance().AddEntry("[ERROR] Unable to load HUD mask texture",
  766. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  767. g_szImageMask = NULL;
  768. FreeResource(hg);
  769. return 0;
  770. }
  771. FreeResource(hg);
  772. // lock the texture and copy it to get a pointer
  773. D3DLOCKED_RECT sRect;
  774. pcTex->LockRect(0,&sRect,NULL,D3DLOCK_READONLY);
  775. unsigned char* szOut = new unsigned char[sDesc.Width * sDesc.Height];
  776. unsigned char* _szOut = szOut;
  777. unsigned char* szCur = (unsigned char*) sRect.pBits;
  778. for (unsigned int y = 0; y < sDesc.Height;++y)
  779. {
  780. memcpy(_szOut,szCur,sDesc.Width);
  781. szCur += sRect.Pitch;
  782. _szOut += sDesc.Width;
  783. }
  784. pcTex->UnlockRect(0);
  785. pcTex->Release();
  786. g_szImageMask = szOut;
  787. return 1;
  788. }
  789. //-------------------------------------------------------------------------------
  790. //-------------------------------------------------------------------------------
  791. int CreateDevice (bool p_bMultiSample,bool p_bSuperSample,bool bHW /*= true*/)
  792. {
  793. D3DDEVTYPE eType = bHW ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;
  794. // get the client rectangle of the window.
  795. RECT sRect;
  796. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  797. sRect.right -= sRect.left;
  798. sRect.bottom -= sRect.top;
  799. D3DPRESENT_PARAMETERS sParams;
  800. memset(&sParams,0,sizeof(D3DPRESENT_PARAMETERS));
  801. // get the current display mode
  802. D3DDISPLAYMODE sMode;
  803. g_piD3D->GetAdapterDisplayMode(0,&sMode);
  804. // fill the presentation parameter structure
  805. sParams.Windowed = TRUE;
  806. sParams.hDeviceWindow = GetDlgItem( g_hDlg, IDC_RT );
  807. sParams.EnableAutoDepthStencil = TRUE;
  808. sParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  809. sParams.BackBufferWidth = (UINT)sRect.right;
  810. sParams.BackBufferHeight = (UINT)sRect.bottom;
  811. sParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  812. sParams.BackBufferCount = 1;
  813. // check whether we can use a D32 depth buffer format
  814. if (SUCCEEDED ( g_piD3D->CheckDepthStencilMatch(0,eType,
  815. D3DFMT_X8R8G8B8,D3DFMT_X8R8G8B8,D3DFMT_D32)))
  816. {
  817. sParams.AutoDepthStencilFormat = D3DFMT_D32;
  818. }
  819. else sParams.AutoDepthStencilFormat = D3DFMT_D24X8;
  820. // find the highest multisample type available on this device
  821. D3DMULTISAMPLE_TYPE sMS = D3DMULTISAMPLE_2_SAMPLES;
  822. D3DMULTISAMPLE_TYPE sMSOut = D3DMULTISAMPLE_NONE;
  823. DWORD dwQuality = 0;
  824. if (p_bMultiSample)
  825. {
  826. while ((D3DMULTISAMPLE_TYPE)(D3DMULTISAMPLE_16_SAMPLES + 1) !=
  827. (sMS = (D3DMULTISAMPLE_TYPE)(sMS + 1)))
  828. {
  829. if(SUCCEEDED( g_piD3D->CheckDeviceMultiSampleType(0,eType,
  830. sMode.Format,TRUE,sMS,&dwQuality)))
  831. {
  832. sMSOut = sMS;
  833. }
  834. }
  835. if (0 != dwQuality)dwQuality -= 1;
  836. sParams.MultiSampleQuality = dwQuality;
  837. sParams.MultiSampleType = sMSOut;
  838. }
  839. // preget the device capabilities. If the hardware vertex shader is too old, we prefer software vertex processing
  840. g_piD3D->GetDeviceCaps( 0, D3DDEVTYPE_HAL, &g_sCaps);
  841. DWORD creationFlags = D3DCREATE_MULTITHREADED;
  842. if( g_sCaps.VertexShaderVersion >= D3DVS_VERSION( 2, 0))
  843. creationFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  844. else
  845. creationFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  846. // create the D3D9 device object. with software-vertexprocessing if VS2.0 isn`t supported in hardware
  847. if(FAILED(g_piD3D->CreateDevice(0,eType, g_hDlg, creationFlags ,&sParams,&g_piDevice)))
  848. {
  849. // if hardware fails use software rendering instead
  850. if (bHW)return CreateDevice(p_bMultiSample,p_bSuperSample,false);
  851. return 0;
  852. }
  853. // create a vertex declaration to match the vertex
  854. D3DVERTEXELEMENT9* vdecl = AssetHelper::Vertex::GetDeclarationElements();
  855. if( FAILED( g_piDevice->CreateVertexDeclaration( vdecl, &gDefaultVertexDecl)))
  856. {
  857. MessageBox( g_hDlg, "Failed to create vertex declaration", "Init", MB_OK);
  858. return 0;
  859. }
  860. g_piDevice->SetVertexDeclaration( gDefaultVertexDecl);
  861. // get the capabilities of the device object
  862. g_piDevice->GetDeviceCaps(&g_sCaps);
  863. if(g_sCaps.PixelShaderVersion < D3DPS_VERSION(3,0))
  864. {
  865. EnableWindow(GetDlgItem(g_hDlg,IDC_LOWQUALITY),FALSE);
  866. }
  867. // compile the default material shader (gray gouraud/phong)
  868. ID3DXBuffer* piBuffer = NULL;
  869. if(FAILED( D3DXCreateEffect(g_piDevice,
  870. g_szDefaultShader.c_str(),
  871. (UINT)g_szDefaultShader.length(),
  872. NULL,
  873. NULL,
  874. AI_SHADER_COMPILE_FLAGS,
  875. NULL,
  876. &g_piDefaultEffect,&piBuffer)))
  877. {
  878. if( piBuffer)
  879. {
  880. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  881. piBuffer->Release();
  882. }
  883. return 0;
  884. }
  885. if( piBuffer)
  886. {
  887. piBuffer->Release();
  888. piBuffer = NULL;
  889. }
  890. // use Fixed Function effect when working with shaderless cards
  891. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  892. g_piDefaultEffect->SetTechnique( "DefaultFXSpecular_FF");
  893. // create the shader used to draw the HUD
  894. if(FAILED( D3DXCreateEffect(g_piDevice,
  895. g_szPassThroughShader.c_str(),(UINT)g_szPassThroughShader.length(),
  896. NULL,NULL,AI_SHADER_COMPILE_FLAGS,NULL,&g_piPassThroughEffect,&piBuffer)))
  897. {
  898. if( piBuffer)
  899. {
  900. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  901. piBuffer->Release();
  902. }
  903. return 0;
  904. }
  905. if( piBuffer)
  906. {
  907. piBuffer->Release();
  908. piBuffer = NULL;
  909. }
  910. // use Fixed Function effect when working with shaderless cards
  911. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  912. g_piPassThroughEffect->SetTechnique( "PassThrough_FF");
  913. // create the shader used to visualize normal vectors
  914. if(FAILED( D3DXCreateEffect(g_piDevice,
  915. g_szNormalsShader.c_str(),(UINT)g_szNormalsShader.length(),
  916. NULL,NULL,AI_SHADER_COMPILE_FLAGS,NULL,&g_piNormalsEffect, &piBuffer)))
  917. {
  918. if( piBuffer)
  919. {
  920. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  921. piBuffer->Release();
  922. }
  923. return 0;
  924. }
  925. if( piBuffer)
  926. {
  927. piBuffer->Release();
  928. piBuffer = NULL;
  929. }
  930. //MessageBox( g_hDlg, "Failed to create vertex declaration", "Init", MB_OK);
  931. // use Fixed Function effect when working with shaderless cards
  932. if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0))
  933. g_piNormalsEffect->SetTechnique( "RenderNormals_FF");
  934. g_piDevice->SetRenderState(D3DRS_DITHERENABLE,TRUE);
  935. // create the texture for the HUD
  936. CreateHUDTexture();
  937. CBackgroundPainter::Instance().RecreateNativeResource();
  938. CLogDisplay::Instance().RecreateNativeResource();
  939. g_piPassThroughEffect->SetTexture("TEXTURE_2D",g_pcTexture);
  940. return 1;
  941. }
  942. //-------------------------------------------------------------------------------
  943. //-------------------------------------------------------------------------------
  944. int CreateDevice (void)
  945. {
  946. return CreateDevice(g_sOptions.bMultiSample,
  947. g_sOptions.bSuperSample);
  948. }
  949. //-------------------------------------------------------------------------------
  950. //-------------------------------------------------------------------------------
  951. int GetProjectionMatrix (aiMatrix4x4& p_mOut)
  952. {
  953. const float fFarPlane = 100.0f;
  954. const float fNearPlane = 0.1f;
  955. const float fFOV = (float)(45.0 * 0.0174532925);
  956. const float s = 1.0f / tanf(fFOV * 0.5f);
  957. const float Q = fFarPlane / (fFarPlane - fNearPlane);
  958. RECT sRect;
  959. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  960. sRect.right -= sRect.left;
  961. sRect.bottom -= sRect.top;
  962. const float fAspect = (float)sRect.right / (float)sRect.bottom;
  963. p_mOut = aiMatrix4x4(
  964. s / fAspect, 0.0f, 0.0f, 0.0f,
  965. 0.0f, s, 0.0f, 0.0f,
  966. 0.0f, 0.0f, Q, 1.0f,
  967. 0.0f, 0.0f, -Q * fNearPlane, 0.0f);
  968. return 1;
  969. }
  970. //-------------------------------------------------------------------------------
  971. aiVector3D GetCameraMatrix (aiMatrix4x4& p_mOut)
  972. {
  973. D3DXMATRIX view;
  974. D3DXMatrixIdentity( &view );
  975. D3DXVec3Normalize( (D3DXVECTOR3*)&g_sCamera.vLookAt, (D3DXVECTOR3*)&g_sCamera.vLookAt );
  976. D3DXVec3Cross( (D3DXVECTOR3*)&g_sCamera.vRight, (D3DXVECTOR3*)&g_sCamera.vUp, (D3DXVECTOR3*)&g_sCamera.vLookAt );
  977. D3DXVec3Normalize( (D3DXVECTOR3*)&g_sCamera.vRight, (D3DXVECTOR3*)&g_sCamera.vRight );
  978. D3DXVec3Cross( (D3DXVECTOR3*)&g_sCamera.vUp, (D3DXVECTOR3*)&g_sCamera.vLookAt, (D3DXVECTOR3*)&g_sCamera.vRight );
  979. D3DXVec3Normalize( (D3DXVECTOR3*)&g_sCamera.vUp, (D3DXVECTOR3*)&g_sCamera.vUp );
  980. view._11 = g_sCamera.vRight.x;
  981. view._12 = g_sCamera.vUp.x;
  982. view._13 = g_sCamera.vLookAt.x;
  983. view._14 = 0.0f;
  984. view._21 = g_sCamera.vRight.y;
  985. view._22 = g_sCamera.vUp.y;
  986. view._23 = g_sCamera.vLookAt.y;
  987. view._24 = 0.0f;
  988. view._31 = g_sCamera.vRight.z;
  989. view._32 = g_sCamera.vUp.z;
  990. view._33 = g_sCamera.vLookAt.z;
  991. view._34 = 0.0f;
  992. view._41 = -D3DXVec3Dot( (D3DXVECTOR3*)&g_sCamera.vPos, (D3DXVECTOR3*)&g_sCamera.vRight );
  993. view._42 = -D3DXVec3Dot( (D3DXVECTOR3*)&g_sCamera.vPos, (D3DXVECTOR3*)&g_sCamera.vUp );
  994. view._43 = -D3DXVec3Dot( (D3DXVECTOR3*)&g_sCamera.vPos, (D3DXVECTOR3*)&g_sCamera.vLookAt );
  995. view._44 = 1.0f;
  996. memcpy(&p_mOut,&view,sizeof(aiMatrix4x4));
  997. return g_sCamera.vPos;
  998. }
  999. }