MFC中使用D3D

1. 新建单文档类型的MFC应用程序

2. 添加三个lib文件:d3d9.lib d3dx9.lib winmm.lib

3. 在D3D9MFCView.h文件中添加如下代码:

包含头文件:#include <d3dx9.h> //88888

添加成员函数和成员变量:

[c-sharp] view plain copy print ?
  1. //88888 
  2. private
  3.     HRESULT initD3D( 
  4.         HWND hwnd, 
  5.         int width, 
  6.         int height, 
  7.         bool windowed, 
  8.         D3DDEVTYPE deviceType); 
  9.     HRESULT setup(int width, int height); 
  10.     HRESULT cleanup(); 
  11.  
  12. public
  13.     HRESULT update(float timeDelta); 
  14.     HRESULT render(); 
  15.  
  16. private
  17.     IDirect3DDevice9* _device; 
  18.     D3DPRESENT_PARAMETERS _d3dpp; 
  19.     ID3DXMesh* _teapot; 
  20.  
  21. public
  22.     virtual void OnInitialUpdate(); 
  23.     afx_msg void OnSize(UINT nType, int cx, int cy); 
  24. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  25.  
  26. //88888 

对应函数实现:

[c-sharp] view plain copy print ?
  1. CD3D9MFCView::CD3D9MFCView():_device(0),_teapot(0) 
  2.     // TODO: add construction code here 
  3.     ::ZeroMemory(&_d3dpp,sizeof(_d3dpp));//88888 
  4.  
  5. CD3D9MFCView::~CD3D9MFCView() 
  6.     if (_teapot) 
  7.         _teapot->Release(); 
  8.     if(_device) 
  9.         _device->Release(); 
  10.          
  11. HRESULT CD3D9MFCView::initD3D(HWND hwnd, 
  12.                                    int width, 
  13.                                    int height, 
  14.                                    bool windowed, 
  15.                                    D3DDEVTYPE deviceType) 
  16.     HRESULT hr = 0; 
  17.     // Step 1: Create the IDirect3D9 object. 
  18.     IDirect3D9* d3d9 = 0; 
  19.     d3d9 = Direct3DCreate9(D3D_SDK_VERSION); 
  20.     if( !d3d9 ) 
  21.     { 
  22.         ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0); 
  23.         return E_FAIL; 
  24.     } 
  25.     // Step 2: Check for hardware vp. 
  26.     D3DCAPS9 caps; 
  27.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); 
  28.     int vp = 0; 
  29.     if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) 
  30.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; 
  31.     else 
  32.         vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 
  33.     // Step 3: Fill out the D3DPRESENT_PARAMETERS structure. 
  34.     _d3dpp.BackBufferWidth = width; 
  35.     _d3dpp.BackBufferHeight = height; 
  36.     _d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 
  37.     _d3dpp.BackBufferCount = 1; 
  38.     _d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; 
  39.     _d3dpp.MultiSampleQuality = 0; 
  40.     _d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
  41.     _d3dpp.hDeviceWindow = hwnd; 
  42.     _d3dpp.Windowed = windowed; 
  43.     _d3dpp.EnableAutoDepthStencil = true
  44.     _d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 
  45.     _d3dpp.Flags = 0; 
  46.     _d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; 
  47.     _d3dpp.PresentationInterval = 
  48.         D3DPRESENT_INTERVAL_IMMEDIATE; 
  49.     // Step 4: Create the device. 
  50.     hr = d3d9->CreateDevice( 
  51.         D3DADAPTER_DEFAULT, // primary adapter 
  52.         deviceType, // device type 
  53.         hwnd, // window associated with device 
  54.         vp, // vertex processing 
  55.         &_d3dpp, // present parameters 
  56.         &_device); // return created device 
  57.     if( FAILED(hr) ) 
  58.     { 
  59.         // try again using a safer configuration. 
  60.         _d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 
  61.         hr = d3d9->CreateDevice( 
  62.             D3DADAPTER_DEFAULT, 
  63.             deviceType, 
  64.             hwnd, 
  65.             D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
  66.             &_d3dpp, 
  67.             &_device); 
  68.         if( FAILED(hr) ) 
  69.         { 
  70.             d3d9->Release(); // done with d3d9 object 
  71.             ::MessageBox(0, "CreateDevice() - FAILED", 0, 0); 
  72.             return hr; 
  73.         } 
  74.     } 
  75.     d3d9->Release(); // done with d3d9 object 
  76.     return S_OK; 
  77.  
  78. HRESULT CD3D9MFCView::setup(int width, int height) 
  79.     if( _device ) 
  80.     { 
  81.         // Set view matrix. 
  82.         D3DXMATRIX V; 
  83.         D3DXVECTOR3 pos (0.0f, 0.0f, -6.0f); 
  84.         D3DXVECTOR3 target (0.0f, 0.0f, 0.0f); 
  85.         D3DXVECTOR3 up (0.0f, 1.0f, 0.0f); 
  86.         D3DXMatrixLookAtLH(&V, &pos, &target, &up); 
  87.         _device->SetTransform(D3DTS_VIEW, &V); 
  88.         // Create the teapot. 
  89.         if( !_teapot) 
  90.             D3DXCreateTeapot(_device, &_teapot, 0); 
  91.         // Use wireframe mode and turn off lighting. 
  92.         _device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); 
  93.         _device->SetRenderState(D3DRS_LIGHTING, false); 
  94.         // Size the viewport based on window dimensions. 
  95.         D3DVIEWPORT9 vp = {0, 0, width, height, 0.0f, 1.0f}; 
  96.         _device->SetViewport( &vp ); 
  97.         // Set the projection matrix based on the 
  98.         // window dimensions. 
  99.         D3DXMATRIX P; 
  100.         D3DXMatrixPerspectiveFovLH( 
  101.             &P, 
  102.             D3DX_PI * 0.25f,//45-degree field of view 
  103.             (float)width / (float)height, 
  104.             1.0f, 
  105.             1000.0f); 
  106.         _device->SetTransform(D3DTS_PROJECTION, &P); 
  107.     } 
  108.     return S_OK; 
  109.  
  110.  
  111. HRESULT CD3D9MFCView::cleanup() 
  112.     // Nothing to Destroy. 
  113.     return S_OK; 
  114.  
  115. HRESULT CD3D9MFCView::update(float timeDelta) 
  116.     if( _device ) 
  117.     { 
  118.         // 
  119.         // Spin the teapot around the y-axis. 
  120.         // 
  121.         static float angle = 0.0f; 
  122.         D3DXMATRIX yRotationMatrix; 
  123.         D3DXMatrixRotationY(&yRotationMatrix, angle); 
  124.         _device->SetTransform(D3DTS_WORLD, &yRotationMatrix); 
  125.         angle += timeDelta; 
  126.         if(angle >= D3DX_PI * 2.0f) 
  127.             angle = 0.0f; 
  128.     } 
  129.     return S_OK; 
  130.  
  131. HRESULT CD3D9MFCView::render() 
  132.     if( _device ) 
  133.     { 
  134.         // 
  135.         // Draw the scene. 
  136.         // 
  137.         _device->Clear(0, 0, 
  138.             D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
  139.             0x00000000, 1.0f, 0); 
  140.         _device->BeginScene(); 
  141.         // Draw the teapot. 
  142.         _teapot->DrawSubset(0); 
  143.         _device->EndScene(); 
  144.         _device->Present(0, 0, 0, 0); 
  145.     } 
  146.     return S_OK; 
  147.  
  148.  
  149. void CD3D9MFCView::OnInitialUpdate() 
  150.     CView::OnInitialUpdate(); 
  151.  
  152.     // TODO: Add your specialized code here and/or call the base class 
  153.     CRect rect; 
  154.     GetClientRect(&rect); 
  155.     // Initialize Direct3D (e.g. acquire a IDirect3DDevice9 poniter). 
  156.     HRESULT hr = initD3D( 
  157.         GetSafeHwnd(), 
  158.         rect.right, 
  159.         rect.bottom, 
  160.         true
  161.         D3DDEVTYPE_HAL); 
  162.     if(FAILED(hr)) 
  163.     { 
  164.         MessageBox("initD3D() - Failed", "Error"); 
  165.         ::PostQuitMessage(0); 
  166.     } 
  167.     // Setup the application. 
  168.     hr = setup(rect.right, rect.bottom); 
  169.     if(FAILED(hr)) 
  170.     { 
  171.         MessageBox("setup() - Failed", "Error"); 
  172.         ::PostQuitMessage(0); 
  173.     } 
  174.  
  175. void CD3D9MFCView::OnSize(UINT nType, int cx, int cy) 
  176.     CView::OnSize(nType, cx, cy); 
  177.  
  178.     // TODO: Add your message handler code here 
  179.     if( _device ) 
  180.     { 
  181.         HRESULT hr = 0; 
  182.         // On a resize we must change the dimensions of the 
  183.         // back buffers to match the new window size. 
  184.         _d3dpp.BackBufferWidth = cx; 
  185.         _d3dpp.BackBufferHeight = cy; 
  186.         // We are about to call Reset, free any resources 
  187.         // that need to be freed prior to a Reset. 
  188.         hr = cleanup(); 
  189.         if(FAILED(hr)) 
  190.         { 
  191.             MessageBox("destroy() - Failed", "Error"); 
  192.             ::PostQuitMessage(0); 
  193.         } 
  194.         // Reset the flipping chain with the new window dimensions. 
  195.         // Note that all device states are reset to the default 
  196.         // after this call. 
  197.         hr = _device->Reset(&_d3dpp); 
  198.         if(FAILED(hr)) 
  199.         { 
  200.             MessageBox("Reset() - Failed", "Error"); 
  201.             ::PostQuitMessage(0); 
  202.         } 
  203.         // Reinitialize resource and device states since we 
  204.         // Reset everything. 
  205.         hr = setup(cx, cy); 
  206.         if(FAILED(hr)) 
  207.         { 
  208.             MessageBox("setup() - Failed", "Error"); 
  209.             ::PostQuitMessage(0); 
  210.         } 
  211.     } 
  212.  
  213. BOOL CD3D9MFCView::OnEraseBkgnd(CDC* pDC) 
  214.     // TODO: Add your message handler code here and/or call default 
  215.  
  216.     return FALSE; 
  217.     //return CView::OnEraseBkgnd(pDC); 

3. 在类CD3D9MFCApp中添加如下代码。

包含头文件:#include "mmsystem.h"//88888

添加重载函数:

[c-sharp] view plain copy print ?
  1. BOOL CD3D9MFCApp::OnIdle(LONG lCount) 
  2.     // TODO: Add your specialized code here and/or call the base class 
  3.  
  4.     CWinApp::OnIdle(lCount); 
  5.     CWnd* mainFrame = AfxGetMainWnd(); 
  6.     CD3D9MFCView* cview = 
  7.         (CD3D9MFCView*)mainFrame->GetWindow(GW_CHILD); 
  8.     // Save last time. 
  9.     static float lastTime = (float)timeGetTime(); 
  10.     // Compute time now. 
  11.     float currentTime = (float)timeGetTime(); 
  12.     // Compute the difference: time elapsed in seconds. 
  13.     float deltaTime = (currentTime - lastTime) * 0.001f; 
  14.     // Last time is now current time. 
  15.     lastTime = currentTime; 
  16.     cview->update(deltaTime); 
  17.     cview->render(); 
  18.     return TRUE; 
  19.  
  20.     //return CWinApp::OnIdle(lCount); 

效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值