assimp_view.cpp 40 KB

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