BumpMap.cpp
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:12k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. // BumpMap.cpp : Defines the class behaviors for the application.
  2. #include "stdafx.h"
  3. #include "BumpMap.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. const bool g_bFullScreen
  10. #ifdef _DEBUG
  11. = false; // true;
  12. #else
  13. = true; // false;
  14. #endif // _DEBUG
  15. const bool g_bUseZBuffer = true; // false;
  16. const DWORD g_dwWidth = 640; // 800; // 1024;
  17. const DWORD g_dwHeight = 480; // 600; // 768;
  18. const DWORD g_dwBPP = 16; // 32;
  19. const bool g_bUseVoodoo12 = false; // true;
  20. const _GUID* const g_lpGuidD3DDevice
  21. // = NULL;
  22. // = &IID_IDirect3DTnLHalDevice;
  23. // = &IID_IDirect3DRGBDevice;
  24. // = &IID_IDirect3DHALDevice;
  25. = &IID_IDirect3DRefDevice;
  26. bool g_bTextureOn = true;
  27. bool g_bBumpMapOn = true;
  28. bool g_bEnvMapOn = true;
  29. CD3DTexture::BUMPMAPFORMAT g_BumpMapFormat = CD3DTexture::BUMPMAP_U5V5L6;
  30. CBumpMapApp::CBumpMapApp(void)
  31. {
  32. m_dwAppInitFlags &= ~CDirectXApp::DXAPPIF_DD;
  33. m_dwAppInitFlags |= CDirectXApp::DXAPPIF_D3DIM;
  34. SetFullScreen(g_bFullScreen);
  35. SetPackFileName(_T("Data"), _T("gui"));
  36. // Called during initial app startup, this function
  37. // performs all the permanent initialization.
  38. m_pD3DDevice = NULL;
  39. m_pD3DMaterial = NULL;
  40. // add your permanent init code here !
  41. m_pBlockTexture = m_pEarthTexture = m_pEarthBumpTexture
  42. = m_pEarthEnvMapTexture = NULL;
  43. m_pLight = NULL;
  44. // Initialize geometry
  45. // Sets up the vertices for a bump-mapped sphere.
  46. m_pEllipse = new  CD3DEllipse(14, 14,
  47. CD3DUN2Vertex::TypeDesc);
  48. }
  49. // Called before the app exits, this function gives the app
  50. // the chance to cleanup after itself.
  51. CBumpMapApp::~CBumpMapApp()
  52. {
  53. // add your cleaup code here !
  54. if (m_pEllipse != NULL)
  55. {
  56. delete  m_pEllipse;
  57. m_pEllipse = NULL;
  58. }
  59. }
  60. bool CBumpMapApp::GetDXInitSettings(void)
  61. {
  62. if (m_pDirectSound != NULL)
  63. {
  64. if (m_pDirectSound->SelectDSDriver(0) == false)
  65. return  false;
  66. }
  67. if (m_pDirectMusic != NULL)
  68. {
  69. if (m_pDirectMusic->SelectDefaultDMusPort() == false)
  70. return  false;
  71. }
  72. if ((g_bUseVoodoo12 == false)
  73. || m_pDirectDraw->SelectDDDevice(0, 1) == false)
  74. {
  75. if (m_pDirectDraw->SelectDDDevice(0) == false)
  76. return  false;
  77. }
  78. if (g_bUseZBuffer)
  79. m_dwDDInitFlags |= CDDDevice::D3DIF_ZBUFFER;
  80. if (GetFirstDDDevice()->IsCanRenderWindowed() == false)
  81. {
  82. m_dwDDInitFlags &= ~CDDDevice::DDIF_WINDOWED;
  83. m_dwDDInitFlags &= ~CDDDevice::DDIF_NO_FLIP;
  84. }
  85. else
  86. {
  87. m_dwDDInitFlags |= CDDDevice::DDIF_NO_FLIP;
  88. }
  89. if (g_lpGuidD3DDevice == NULL)
  90. {
  91. if (GetFirstDDDevice()->SelectDefaultD3DDevice() == false)
  92. return  false;
  93. }
  94. else
  95. {
  96. if (GetFirstDDDevice()->SelectD3DDevice(g_lpGuidD3DDevice) == false)
  97. return  false;
  98. }
  99. if (GetFirstDDDevice()->SelectDisplayMode(
  100. IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
  101. return  false;
  102. return  true;
  103. }
  104. // Called during device intialization, this code checks the device
  105. // for some minimum set of capabilities, Initialize scene objects.
  106. bool CBumpMapApp::InitDXObjects(void)
  107. {
  108. m_pD3DDevice = GetFirstD3DDevice();
  109. ASSERT(m_pD3DDevice != NULL);
  110. m_pD3DMaterial = m_pD3DDevice->GetD3DMaterial();
  111. // add your init code here !
  112. DWORD dwTextureOpCaps = m_pD3DDevice->GetD3DDeviceDesc()->dwTextureOpCaps;
  113. if ((dwTextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP) == 0
  114. || (dwTextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE) == 0)
  115. return  false;
  116. // Load texture maps
  117. m_pBlockTexture = new  CD3DTexture;
  118. if (m_pBlockTexture->Create(m_pD3DDevice,
  119. _T("block.bmp"), m_pPackFileManager, 0, 0) == false)
  120. return  false;
  121. m_pEarthTexture = new  CD3DTexture;
  122. if (m_pEarthTexture->Create(m_pD3DDevice,
  123. _T("earth.bmp"), m_pPackFileManager, 0, 0) == false)
  124. return  false;
  125. m_pEarthBumpTexture = new  CD3DTexture;
  126. if (m_pEarthBumpTexture->CreateBumpMapTexture(m_pD3DDevice,
  127. _T("earthbump.bmp"), m_pPackFileManager, g_BumpMapFormat, 1) == false)
  128. return  false;
  129. m_pEarthEnvMapTexture = new  CD3DTexture;
  130. if (m_pEarthEnvMapTexture->Create(m_pD3DDevice,
  131. _T("EarthEnvMap.bmp"), m_pPackFileManager, 0, 0) == false)
  132. return  false;
  133. for (DWORD  t = 0; t < 3; ++ t)
  134. {
  135. m_pD3DDevice->SetTextureStageState(t, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);
  136. m_pD3DDevice->SetTextureStageState(t, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
  137. m_pD3DDevice->SetTextureStageState(t, D3DTSS_MINFILTER, D3DTFN_LINEAR);
  138. }
  139. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE);
  140. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
  141. // Set matrices
  142. D3DVECTOR vEyePt(0.0f, 2.0f, 10.0f);
  143. D3DVECTOR vLookatPt(0.0f, 0.0f, 0.0f);
  144. D3DVECTOR vUpVec(0.0f, 1.0f, 0.0f);
  145. CD3DMatrix matScale, matWorld, matView, matProj;
  146. matView.SetView(vEyePt, vLookatPt, vUpVec);
  147. matProj.SetProjection(g_PI/4, 1.0f, 1.0f, 20.0f);
  148. matScale.SetScale(3.0f, 3.0f, 3.0f );
  149. matWorld.SetRotateY(80.0f * (g_PI/180) );
  150. matWorld *= matScale;
  151. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  152. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
  153. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
  154. // Set a material
  155. m_pD3DMaterial->SetColor(1.0f, 1.0f, 1.0f, 1.0f, D3D_ML_DIFFUSE);
  156. m_pD3DMaterial->Set();
  157. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_AMBIENT, 0x000000FF);
  158. // Add a directional light
  159. m_pLight = new  CD3DDirectionalLight(0.0f, 0.0f, 1.0f);
  160. m_pLight->SetColor(0.9f, 0.9f, 0.9f);
  161. if (m_pLight->Create(m_pD3DDevice) == false)
  162. return  false;
  163. if (m_pLight->Enable(true) == false)
  164. return  false;
  165. return  true;
  166. }
  167. // Called when the app is exitting, or the device is being changed,
  168. // this function deletes any device dependant objects.
  169. bool CBumpMapApp::DestroyDXObjects(void)
  170. {
  171. // add your destroy code here !
  172. if (m_pLight != NULL)
  173. {
  174. delete  m_pLight;
  175. m_pLight = NULL;
  176. }
  177. if (m_pBlockTexture != NULL)
  178. {
  179. delete  m_pBlockTexture;
  180. m_pBlockTexture = NULL;
  181. }
  182. if (m_pEarthTexture != NULL)
  183. {
  184. delete  m_pEarthTexture;
  185. m_pEarthTexture = NULL;
  186. }
  187. if (m_pEarthBumpTexture != NULL)
  188. {
  189. delete  m_pEarthBumpTexture;
  190. m_pEarthBumpTexture = NULL;
  191. }
  192. if (m_pEarthEnvMapTexture != NULL)
  193. {
  194. delete  m_pEarthEnvMapTexture;
  195. m_pEarthEnvMapTexture = NULL;
  196. }
  197. m_pD3DMaterial = NULL;
  198. m_pD3DDevice = NULL;
  199. return  CDirectXApp::DestroyDXObjects();
  200. }
  201. // Called once per frame, the call is the entry point for
  202. // animating the scene. This function sets up render states,
  203. // clears the viewport, and renders the scene.
  204. bool CBumpMapApp::UpdateFrame(void)
  205. {
  206. // add your code here !
  207. const FLOAT fAngle = 30.0f;
  208. CD3DMatrix matWorld, matRotate;
  209. matRotate.SetRotateY(-fAngle * (g_PI/180));
  210. m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  211. matWorld = matRotate * matWorld;
  212. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  213. ApplyEnivronmentMap(
  214. static_cast<CD3DUN2Vertex*>(m_pEllipse->GetVertices()),
  215. m_pEllipse->GetNumVertices());
  216. m_pD3DDevice->Clear(RGBA_MAKE(0, 0, 0xFF, 0));
  217. if (SUCCEEDED(m_pD3DDevice->BeginScene()))
  218. {
  219. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_WRAP0, D3DWRAP_U | D3DWRAP_V);
  220. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  221. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  222. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  223. m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  224. m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  225. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  226. if (g_bTextureOn)
  227. m_pEarthTexture->SetAsCurrent();
  228. else
  229. m_pBlockTexture->SetAsCurrent();
  230.     
  231. m_pD3DDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 1);
  232. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  233. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  234. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  235. if (g_bBumpMapOn)
  236. {
  237. //m_pEarthBumpTexture->SetStage(1);
  238. m_pEarthBumpTexture->SetAsCurrent();
  239. m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
  240. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE);
  241. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  242. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  243. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT00, CDirectX::FLOAT2DWORD(0.5f));
  244. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT01, CDirectX::FLOAT2DWORD(0.0f));
  245. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT10, CDirectX::FLOAT2DWORD(0.0f));
  246. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT11, CDirectX::FLOAT2DWORD(0.5f));
  247. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVLSCALE, CDirectX::FLOAT2DWORD(1.0f));
  248. m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVLOFFSET, CDirectX::FLOAT2DWORD(0.0f));
  249. if (g_bEnvMapOn)
  250. {
  251. m_pEarthEnvMapTexture->SetStage(2);
  252. m_pEarthEnvMapTexture->SetAsCurrent();
  253. m_pD3DDevice->SetTextureStageState(2, D3DTSS_TEXCOORDINDEX, 0);
  254. m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_ADD);
  255. m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  256. m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_CURRENT);
  257. }
  258. else
  259. m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
  260. }
  261. else
  262. {
  263. if (g_bEnvMapOn)
  264. {
  265. m_pEarthEnvMapTexture->SetStage(1);
  266. m_pEarthEnvMapTexture->SetAsCurrent();
  267. m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
  268. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
  269. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  270. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  271. }
  272. else
  273. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  274. m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
  275. }
  276. m_pEllipse->Render(m_pD3DDevice);
  277. m_pD3DDevice->EndScene();
  278. }
  279. return  CDirectXApp::UpdateFrame();
  280. }
  281. // ---- add your functions ! ----
  282. // Performs a calculation on each of the vertices' normals to determine
  283. // what the texture coordinates should be for the environment map.
  284. void CBumpMapApp::ApplyEnivronmentMap(CD3DUN2Vertex*  pv,
  285. DWORD  dwNumVertices)
  286. {
  287. // Get the World-View(WV) and Projection(P) matrices
  288. CD3DMatrix W, V, WV;
  289. m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_VIEW, &V);
  290. m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_WORLD, &W);
  291. m_pD3DDevice->MultiplyTransform(D3DTRANSFORMSTATE_VIEW, &W);
  292. m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_VIEW, &WV);
  293. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &V);
  294. // Loop through the vertices, transforming each one and calculating
  295. // the correct texture coordinates.
  296. for (WORD  i = 0; i < dwNumVertices; ++ i)
  297. {
  298. FLOAT nx = pv[i].m_dvNX;
  299. FLOAT ny = pv[i].m_dvNY;
  300. FLOAT nz = pv[i].m_dvNZ;
  301. FLOAT nxv = nx * WV._11 + ny * WV._21 + nz * WV._31 + WV._41;
  302. FLOAT nyv = nx * WV._12 + ny * WV._22 + nz * WV._32 + WV._42;
  303. FLOAT nzv = nx * WV._13 + ny * WV._23 + nz * WV._33 + WV._43;
  304. FLOAT nlen = (FLOAT)::sqrt(nxv * nxv + nyv * nyv + nzv * nzv);
  305. pv[i].m_dvTU1 = 0.5f + 1.2f * nxv / nlen;
  306. pv[i].m_dvTV1 = 0.5f - 1.2f * nyv / nlen;
  307. }
  308. }
  309. // ----
  310. #if _MSC_VER >= 1200 && _MSC_VER < 1400
  311. #ifdef _DEBUG
  312. #pragma comment(lib, "DXGuideD_VC6.lib")
  313. #else
  314. #pragma comment(lib, "DXGuide_VC6.lib")
  315. #endif // _DEBUG
  316. #endif // _MSC_VER
  317. #if _MSC_VER >= 1000 && _MSC_VER < 1200
  318. #ifdef _DEBUG
  319. #pragma comment(lib, "DXGuideD_VC5.lib")
  320. #else
  321. #pragma comment(lib, "DXGuide_VC5.lib")
  322. #endif // _DEBUG
  323. #endif // _MSC_VER
  324. BEGIN_MESSAGE_MAP(CBumpMapApp, CDirectXApp)
  325. //{{AFX_MSG_MAP(CBumpMapApp)
  326. //}}AFX_MSG_MAP
  327. END_MESSAGE_MAP()
  328. CBumpMapApp theApp;