You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1189 lines
36 KiB

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