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.
 
 
 
 
 
 

471 lines
12 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. namespace AssimpView {
  37. // From: U3D build 1256 (src\kernel\graphic\scenegraph\SkyBox.cpp)
  38. // ------------------------------------------------------------------------------
  39. /** \brief Vertex structure for the skybox
  40. */
  41. // ------------------------------------------------------------------------------
  42. struct SkyBoxVertex
  43. {
  44. float x,y,z;
  45. float u,v,w;
  46. };
  47. // ------------------------------------------------------------------------------
  48. /** \brief Vertices for the skybox
  49. */
  50. // ------------------------------------------------------------------------------
  51. SkyBoxVertex g_cubeVertices_indexed[] =
  52. {
  53. { -1.0f, 1.0f, -1.0f, -1.0f,1.0f,-1.0f }, // 0
  54. { 1.0f, 1.0f, -1.0f, 1.0f,1.0f,-1.0f }, // 1
  55. { -1.0f, -1.0f, -1.0f, -1.0f,-1.0f,-1.0f }, // 2
  56. { 1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f }, // 3
  57. {-1.0f, 1.0f, 1.0f, -1.0f,1.0f,1.0f }, // 4
  58. {-1.0f,-1.0f, 1.0f, -1.0f,-1.0f,1.0f }, // 5
  59. { 1.0f, 1.0f, 1.0f, 1.0f,1.0f,1.0f }, // 6
  60. { 1.0f,-1.0f, 1.0f, 1.0f,-1.0f,1.0f } // 7
  61. };
  62. // ------------------------------------------------------------------------------
  63. /** \brief Indices for the skybox
  64. */
  65. // ------------------------------------------------------------------------------
  66. unsigned short g_cubeIndices[] =
  67. {
  68. 0, 1, 2, 3, 2, 1,4, 5, 6,
  69. 7, 6, 5, 4, 6, 0, 1, 6, 0,
  70. 5, 2, 7,3, 2, 7, 1, 6, 3,
  71. 7, 3, 6, 0, 2, 4, 5, 4, 2,
  72. };
  73. CBackgroundPainter CBackgroundPainter::s_cInstance;
  74. //-------------------------------------------------------------------------------
  75. void CBackgroundPainter::SetColor (D3DCOLOR p_clrNew)
  76. {
  77. if (TEXTURE_CUBE == eMode)
  78. RemoveSBDeps();
  79. clrColor = p_clrNew;
  80. eMode = SIMPLE_COLOR;
  81. if (pcTexture)
  82. {
  83. pcTexture->Release();
  84. pcTexture = NULL;
  85. }
  86. }
  87. //-------------------------------------------------------------------------------
  88. void CBackgroundPainter::RemoveSBDeps()
  89. {
  90. MODE e = eMode;
  91. eMode = SIMPLE_COLOR;
  92. if (g_pcAsset && g_pcAsset->pcScene)
  93. {
  94. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  95. {
  96. if (aiShadingMode_Gouraud != g_pcAsset->apcMeshes[i]->eShadingMode)
  97. {
  98. CMaterialManager::Instance().DeleteMaterial(g_pcAsset->apcMeshes[i]);
  99. CMaterialManager::Instance().CreateMaterial(
  100. g_pcAsset->apcMeshes[i],g_pcAsset->pcScene->mMeshes[i]);
  101. }
  102. }
  103. }
  104. eMode = e;
  105. }
  106. //-------------------------------------------------------------------------------
  107. void CBackgroundPainter::ResetSB()
  108. {
  109. mMatrix = aiMatrix4x4();
  110. }
  111. //-------------------------------------------------------------------------------
  112. void CBackgroundPainter::SetCubeMapBG (const char* p_szPath)
  113. {
  114. bool bHad = false;
  115. if (pcTexture)
  116. {
  117. pcTexture->Release();
  118. pcTexture = NULL;
  119. if(TEXTURE_CUBE ==eMode)bHad = true;
  120. }
  121. eMode = TEXTURE_CUBE;
  122. szPath = std::string( p_szPath );
  123. // ARRRGHH... ugly. TODO: Rewrite this!
  124. aiString sz;
  125. sz.Set(szPath);
  126. CMaterialManager::Instance().FindValidPath(&sz);
  127. szPath = std::string( sz.data );
  128. // now recreate all native resources
  129. RecreateNativeResource();
  130. if (SIMPLE_COLOR != this->eMode)
  131. {
  132. // this influences all material with specular components
  133. if (!bHad)
  134. {
  135. if (g_pcAsset && g_pcAsset->pcScene)
  136. {
  137. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  138. {
  139. if (aiShadingMode_Phong == g_pcAsset->apcMeshes[i]->eShadingMode)
  140. {
  141. CMaterialManager::Instance().DeleteMaterial(g_pcAsset->apcMeshes[i]);
  142. CMaterialManager::Instance().CreateMaterial(
  143. g_pcAsset->apcMeshes[i],g_pcAsset->pcScene->mMeshes[i]);
  144. }
  145. }
  146. }
  147. }
  148. else
  149. {
  150. if (g_pcAsset && g_pcAsset->pcScene)
  151. {
  152. for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
  153. {
  154. if (aiShadingMode_Phong == g_pcAsset->apcMeshes[i]->eShadingMode)
  155. {
  156. g_pcAsset->apcMeshes[i]->piEffect->SetTexture(
  157. "lw_tex_envmap",CBackgroundPainter::Instance().GetTexture());
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. //-------------------------------------------------------------------------------
  165. void CBackgroundPainter::RotateSB(const aiMatrix4x4* pm)
  166. {
  167. this->mMatrix = mMatrix * (*pm);
  168. }
  169. //-------------------------------------------------------------------------------
  170. void CBackgroundPainter::SetTextureBG (const char* p_szPath)
  171. {
  172. if (TEXTURE_CUBE == this->eMode)this->RemoveSBDeps();
  173. if (pcTexture)
  174. {
  175. pcTexture->Release();
  176. pcTexture = NULL;
  177. }
  178. eMode = TEXTURE_2D;
  179. szPath = std::string( p_szPath );
  180. // ARRRGHH... ugly. TODO: Rewrite this!
  181. aiString sz;
  182. sz.Set(szPath);
  183. CMaterialManager::Instance().FindValidPath(&sz);
  184. szPath = std::string( sz.data );
  185. // now recreate all native resources
  186. RecreateNativeResource();
  187. }
  188. //-------------------------------------------------------------------------------
  189. void CBackgroundPainter::OnPreRender()
  190. {
  191. if (SIMPLE_COLOR != eMode)
  192. {
  193. // clear the z-buffer only (in wireframe mode we must also clear
  194. // the color buffer )
  195. if (g_sOptions.eDrawMode == RenderOptions::WIREFRAME)
  196. {
  197. g_piDevice->Clear(0,NULL,D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET,
  198. D3DCOLOR_ARGB(0xff,100,100,100),1.0f,0);
  199. }
  200. else
  201. {
  202. g_piDevice->Clear(0,NULL,D3DCLEAR_ZBUFFER,0,1.0f,0);
  203. }
  204. if (TEXTURE_2D == eMode)
  205. {
  206. RECT sRect;
  207. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  208. sRect.right -= sRect.left;
  209. sRect.bottom -= sRect.top;
  210. struct SVertex
  211. {
  212. float x,y,z,w,u,v;
  213. };
  214. UINT dw;
  215. this->piSkyBoxEffect->Begin(&dw,0);
  216. this->piSkyBoxEffect->BeginPass(0);
  217. SVertex as[4];
  218. as[1].x = 0.0f;
  219. as[1].y = 0.0f;
  220. as[1].z = 0.2f;
  221. as[1].w = 1.0f;
  222. as[1].u = 0.0f;
  223. as[1].v = 0.0f;
  224. as[3].x = (float)sRect.right;
  225. as[3].y = 0.0f;
  226. as[3].z = 0.2f;
  227. as[3].w = 1.0f;
  228. as[3].u = 1.0f;
  229. as[3].v = 0.0f;
  230. as[0].x = 0.0f;
  231. as[0].y = (float)sRect.bottom;
  232. as[0].z = 0.2f;
  233. as[0].w = 1.0f;
  234. as[0].u = 0.0f;
  235. as[0].v = 1.0f;
  236. as[2].x = (float)sRect.right;
  237. as[2].y = (float)sRect.bottom;
  238. as[2].z = 0.2f;
  239. as[2].w = 1.0f;
  240. as[2].u = 1.0f;
  241. as[2].v = 1.0f;
  242. as[0].x -= 0.5f;as[1].x -= 0.5f;as[2].x -= 0.5f;as[3].x -= 0.5f;
  243. as[0].y -= 0.5f;as[1].y -= 0.5f;as[2].y -= 0.5f;as[3].y -= 0.5f;
  244. DWORD dw2;g_piDevice->GetFVF(&dw2);
  245. g_piDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
  246. g_piDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,
  247. &as,sizeof(SVertex));
  248. piSkyBoxEffect->EndPass();
  249. piSkyBoxEffect->End();
  250. g_piDevice->SetFVF(dw2);
  251. }
  252. return;
  253. }
  254. // clear both the render target and the z-buffer
  255. g_piDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  256. clrColor,1.0f,0);
  257. }
  258. //-------------------------------------------------------------------------------
  259. void CBackgroundPainter::OnPostRender()
  260. {
  261. if (TEXTURE_CUBE == eMode)
  262. {
  263. aiMatrix4x4 pcProj;
  264. GetProjectionMatrix(pcProj);
  265. aiMatrix4x4 pcCam;
  266. aiVector3D vPos = GetCameraMatrix(pcCam);
  267. aiMatrix4x4 aiMe;
  268. aiMe[3][0] = vPos.x;
  269. aiMe[3][1] = vPos.y;
  270. aiMe[3][2] = vPos.z;
  271. aiMe = mMatrix * aiMe;
  272. pcProj = (aiMe * pcCam) * pcProj;
  273. piSkyBoxEffect->SetMatrix("WorldViewProjection",
  274. (const D3DXMATRIX*)&pcProj);
  275. UINT dwPasses;
  276. piSkyBoxEffect->Begin(&dwPasses,0);
  277. piSkyBoxEffect->BeginPass(0);
  278. DWORD dw2;
  279. g_piDevice->GetFVF(&dw2);
  280. g_piDevice->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0));
  281. g_piDevice->DrawIndexedPrimitiveUP(
  282. D3DPT_TRIANGLELIST,0,8,12,g_cubeIndices,D3DFMT_INDEX16,
  283. g_cubeVertices_indexed,sizeof(SkyBoxVertex));
  284. g_piDevice->SetFVF(dw2);
  285. piSkyBoxEffect->EndPass();
  286. piSkyBoxEffect->End();
  287. }
  288. }
  289. //-------------------------------------------------------------------------------
  290. void CBackgroundPainter::ReleaseNativeResource()
  291. {
  292. if ( piSkyBoxEffect)
  293. {
  294. piSkyBoxEffect->Release();
  295. piSkyBoxEffect = NULL;
  296. }
  297. if (pcTexture)
  298. {
  299. pcTexture->Release();
  300. pcTexture = NULL;
  301. }
  302. }
  303. //-------------------------------------------------------------------------------
  304. void CBackgroundPainter::RecreateNativeResource()
  305. {
  306. if (SIMPLE_COLOR == eMode)return;
  307. if (TEXTURE_CUBE == eMode)
  308. {
  309. // many skyboxes are 16bit FP format which isn't supported
  310. // with bilinear filtering on older cards
  311. D3DFORMAT eFmt = D3DFMT_UNKNOWN;
  312. if(FAILED(g_piD3D->CheckDeviceFormat(0,D3DDEVTYPE_HAL,
  313. D3DFMT_X8R8G8B8,D3DUSAGE_QUERY_FILTER,D3DRTYPE_CUBETEXTURE,D3DFMT_A16B16G16R16F)))
  314. {
  315. eFmt = D3DFMT_A8R8G8B8;
  316. }
  317. if (FAILED(D3DXCreateCubeTextureFromFileEx(
  318. g_piDevice,
  319. szPath.c_str(),
  320. D3DX_DEFAULT,
  321. 0,
  322. 0,
  323. eFmt,
  324. D3DPOOL_MANAGED,
  325. D3DX_DEFAULT,
  326. D3DX_DEFAULT,
  327. 0,
  328. NULL,
  329. NULL,
  330. (IDirect3DCubeTexture9**)&pcTexture)))
  331. {
  332. const char* szEnd = strrchr(szPath.c_str(),'\\');
  333. if (!szEnd)szEnd = strrchr(szPath.c_str(),'/');
  334. if (!szEnd)szEnd = szPath.c_str()-1;
  335. char szTemp[1024];
  336. sprintf(szTemp,"[ERROR] Unable to load background cubemap %s",szEnd+1);
  337. CLogDisplay::Instance().AddEntry(szTemp,
  338. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  339. eMode = SIMPLE_COLOR;
  340. return;
  341. }
  342. else CLogDisplay::Instance().AddEntry("[OK] The skybox has been imported successfully",
  343. D3DCOLOR_ARGB(0xFF,0,0xFF,0));
  344. }
  345. else
  346. {
  347. if (FAILED(D3DXCreateTextureFromFileEx(
  348. g_piDevice,
  349. szPath.c_str(),
  350. D3DX_DEFAULT,
  351. D3DX_DEFAULT,
  352. 0,
  353. 0,
  354. D3DFMT_A8R8G8B8,
  355. D3DPOOL_MANAGED,
  356. D3DX_DEFAULT,
  357. D3DX_DEFAULT,
  358. 0,
  359. NULL,
  360. NULL,
  361. (IDirect3DTexture9**)&pcTexture)))
  362. {
  363. const char* szEnd = strrchr(szPath.c_str(),'\\');
  364. if (!szEnd)szEnd = strrchr(szPath.c_str(),'/');
  365. if (!szEnd)szEnd = szPath.c_str()-1;
  366. char szTemp[1024];
  367. sprintf(szTemp,"[ERROR] Unable to load background texture %s",szEnd+1);
  368. CLogDisplay::Instance().AddEntry(szTemp,
  369. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  370. eMode = SIMPLE_COLOR;
  371. return;
  372. }
  373. else CLogDisplay::Instance().AddEntry("[OK] The background texture has been imported successfully",
  374. D3DCOLOR_ARGB(0xFF,0,0xFF,0));
  375. }
  376. if (!piSkyBoxEffect)
  377. {
  378. ID3DXBuffer* piBuffer = NULL;
  379. if(FAILED( D3DXCreateEffect(
  380. g_piDevice,
  381. g_szSkyboxShader.c_str(),
  382. (UINT)g_szSkyboxShader.length(),
  383. NULL,
  384. NULL,
  385. AI_SHADER_COMPILE_FLAGS,
  386. NULL,
  387. &piSkyBoxEffect,&piBuffer)))
  388. {
  389. // failed to compile the shader
  390. if( piBuffer) {
  391. MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK);
  392. piBuffer->Release();
  393. }
  394. CLogDisplay::Instance().AddEntry("[ERROR] Unable to compile skybox shader",
  395. D3DCOLOR_ARGB(0xFF,0xFF,0,0));
  396. eMode = SIMPLE_COLOR;
  397. return ;
  398. }
  399. }
  400. // commit the correct textures to the shader
  401. if (TEXTURE_CUBE == eMode)
  402. {
  403. piSkyBoxEffect->SetTexture("lw_tex_envmap",pcTexture);
  404. piSkyBoxEffect->SetTechnique("RenderSkyBox");
  405. }
  406. else if (TEXTURE_2D == eMode)
  407. {
  408. piSkyBoxEffect->SetTexture("TEXTURE_2D",pcTexture);
  409. piSkyBoxEffect->SetTechnique("RenderImage2D");
  410. }
  411. }
  412. };