assimp_view.cpp 40 KB

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