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

DirextX编程

开发平台:

Visual C++

  1. // Mipmap.cpp : Defines the class behaviors for the application.
  2. #include "stdafx.h"
  3. #include "Mipmap.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 = 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. D3DVERTEX g_Mesh[4]; // Rendering vertices
  27. CMipmapApp::CMipmapApp(void)
  28. {
  29. m_dwAppInitFlags &= ~CDirectXApp::DXAPPIF_DD;
  30. m_dwAppInitFlags |= CDirectXApp::DXAPPIF_D3DIM;
  31. SetFullScreen(g_bFullScreen);
  32. SetPackFileName(_T("Data"), _T("gui"));
  33. // Called during initial app startup, this function
  34. // performs all the permanent initialization.
  35. m_pD3DDevice = NULL;
  36. m_pD3DMaterial = NULL;
  37. // add your permanent init code here !
  38. }
  39. // Called before the app exits, this function gives the app
  40. // the chance to cleanup after itself.
  41. CMipmapApp::~CMipmapApp()
  42. {
  43. // add your cleaup code here !
  44. }
  45. bool CMipmapApp::GetDXInitSettings(void)
  46. {
  47. if (m_pDirectSound != NULL)
  48. {
  49. if (m_pDirectSound->SelectDSDriver(0) == false)
  50. return  false;
  51. }
  52. if (m_pDirectMusic != NULL)
  53. {
  54. if (m_pDirectMusic->SelectDefaultDMusPort() == false)
  55. return  false;
  56. }
  57. if ((g_bUseVoodoo12 == false)
  58. || m_pDirectDraw->SelectDDDevice(0, 1) == false)
  59. {
  60. if (m_pDirectDraw->SelectDDDevice(0) == false)
  61. return  false;
  62. }
  63. if (g_bUseZBuffer)
  64. m_dwDDInitFlags |= CDDDevice::D3DIF_ZBUFFER;
  65. if (GetFirstDDDevice()->IsCanRenderWindowed() == false)
  66. {
  67. m_dwDDInitFlags &= ~CDDDevice::DDIF_WINDOWED;
  68. m_dwDDInitFlags &= ~CDDDevice::DDIF_NO_FLIP;
  69. }
  70. else
  71. {
  72. m_dwDDInitFlags |= CDDDevice::DDIF_NO_FLIP;
  73. }
  74. if (g_lpGuidD3DDevice == NULL)
  75. {
  76. if (GetFirstDDDevice()->SelectDefaultD3DDevice() == false)
  77. return  false;
  78. }
  79. else
  80. {
  81. if (GetFirstDDDevice()->SelectD3DDevice(g_lpGuidD3DDevice) == false)
  82. return  false;
  83. }
  84. if (GetFirstDDDevice()->SelectDisplayMode(
  85. IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
  86. return  false;
  87. return  true;
  88. }
  89. // Called during device intialization, this code checks the device
  90. // for some minimum set of capabilities, Initialize scene objects.
  91. bool CMipmapApp::InitDXObjects(void)
  92. {
  93. m_pD3DDevice = GetFirstD3DDevice();
  94. ASSERT(m_pD3DDevice != NULL);
  95. m_pD3DMaterial = m_pD3DDevice->GetD3DMaterial();
  96. // add your init code here !
  97. // Check Texture modes for MIPLINEAR MipMapping
  98. if (!(m_pD3DDevice->GetTriCaps()->dwTextureFilterCaps & D3DPTFILTERCAPS_LINEARMIPLINEAR))
  99. return  false;
  100. // Create the mipmaps (loads bitmaps from "brickN.bmp" files)
  101. m_pBrickTexture = new  CD3DTexture;
  102. if (m_pBrickTexture->Create(m_pD3DDevice, _T("BRICK"), m_pPackFileManager, 0, 0) == false)
  103. {
  104. return  false;
  105. }
  106. // Initialize mesh used to render the mipmaps
  107. g_Mesh[0] = D3DVERTEX(D3DVECTOR(-1,-1, 0), D3DVECTOR(0,0,1), 0.0f, 1.0f);
  108. g_Mesh[1] = D3DVERTEX(D3DVECTOR(-1, 1, 0), D3DVECTOR(0,0,1), 0.0f, 0.0f);
  109. g_Mesh[2] = D3DVERTEX(D3DVECTOR( 1,-1, 0), D3DVECTOR(0,0,1), 1.0f, 1.0f);
  110. g_Mesh[3] = D3DVERTEX(D3DVECTOR( 1, 1, 0), D3DVECTOR(0,0,1), 1.0f, 0.0f);
  111. m_pD3DMaterial->SetColor(1.0f, 1.0f, 1.0f, 0.0f);
  112. m_pD3DMaterial->Set();
  113. // Build the mipmap device surfaces and textures.
  114. m_pBrickTexture->SetAsCurrent();
  115. m_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
  116. m_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
  117. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE);
  118. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
  119. // Set the transform matrices.
  120. CD3DMatrix matProj;
  121. matProj.SetProjection(1.57f, 1.0f, 1.0f, 100.0f);
  122. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
  123. // This simple sample uses only ambient light
  124. m_pD3DDevice->SetRenderState(D3DRENDERSTATE_AMBIENT, 0xFFFFFFFFL);
  125. return  true;
  126. }
  127. // Called when the app is exitting, or the device is being changed,
  128. // this function deletes any device dependant objects.
  129. bool CMipmapApp::DestroyDXObjects(void)
  130. {
  131. // add your destroy code here !
  132. if (m_pBrickTexture != NULL)
  133. {
  134. delete  m_pBrickTexture;
  135. m_pBrickTexture = NULL;
  136. }
  137. m_pD3DMaterial = NULL;
  138. m_pD3DDevice = NULL;
  139. return  CDirectXApp::DestroyDXObjects();
  140. }
  141. // Called once per frame, the call is the entry point for
  142. // animating the scene. This function sets up render states,
  143. // clears the viewport, and renders the scene.
  144. bool CMipmapApp::UpdateFrame(void)
  145. {
  146. // add your code here !
  147. static FLOAT fTimeKey = 0.0f;
  148. fTimeKey += 0.002f;
  149. // add your code here !
  150. D3DVECTOR vEyePt = D3DVECTOR(0, 0, (FLOAT)(13*sin(fTimeKey) - 15));
  151. D3DVECTOR vLookatPt = D3DVECTOR(0, 0, 0);
  152. D3DVECTOR vUpVec = D3DVECTOR(0, 1, 0);
  153. CD3DMatrix matView;
  154. matView.SetView(vEyePt, vLookatPt, vUpVec);
  155. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
  156. //Clear the viewport
  157. m_pD3DDevice->Clear(RGBA_MAKE(0, 0, 0xFF, 0));
  158. // Begin the scene
  159. if (SUCCEEDED(m_pD3DDevice->BeginScene()))
  160. {
  161. CD3DMatrix matWorld;
  162. // Draw the left quad. Set renderstates for bilinear filtering
  163. matWorld.SetTranslate(-1.1f, 0.0f, 0.0f);
  164. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  165. m_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTFP_NONE);
  166. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX,
  167. g_Mesh, 4, 0);
  168. // Draw the right quad. Set renderstates for mipmapping
  169. matWorld.SetTranslate(+1.1f, 0.0f, 0.0f);
  170. m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  171. m_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTFP_LINEAR);
  172. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX,
  173. g_Mesh, 4, 0);
  174. // End the scene.
  175. m_pD3DDevice->EndScene();
  176. }
  177. return  CDirectXApp::UpdateFrame();
  178. }
  179. // ---- add your functions ! ----
  180. // ----
  181. #if _MSC_VER >= 1200 && _MSC_VER < 1400
  182. #ifdef _DEBUG
  183. #pragma comment(lib, "DXGuideD_VC6.lib")
  184. #else
  185. #pragma comment(lib, "DXGuide_VC6.lib")
  186. #endif // _DEBUG
  187. #endif // _MSC_VER
  188. #if _MSC_VER >= 1000 && _MSC_VER < 1200
  189. #ifdef _DEBUG
  190. #pragma comment(lib, "DXGuideD_VC5.lib")
  191. #else
  192. #pragma comment(lib, "DXGuide_VC5.lib")
  193. #endif // _DEBUG
  194. #endif // _MSC_VER
  195. BEGIN_MESSAGE_MAP(CMipmapApp, CDirectXApp)
  196. //{{AFX_MSG_MAP(CMipmapApp)
  197. //}}AFX_MSG_MAP
  198. END_MESSAGE_MAP()
  199. CMipmapApp theApp;