assimp_view.cpp 40 KB

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