D3D地形例程

 基于框架(四)的一个例子,代码清单:

  1. //
  2. // 
  3. // File: terrainDriver.cpp
  4. // 
  5. // by tianzhihen
  6. //
  7. // 2008.10.27, MSVC++ 8.0 
  8. //          
  9. //
  10. #include "d3dUtility.h"
  11. #include "terrain.h"
  12. #include "camera.h"
  13. #include "fps.h"
  14. //
  15. // Globals
  16. //
  17. IDirect3DDevice9* Device = 0; 
  18. const int Width  = 640;
  19. const int Height = 480;
  20. Terrain* TheTerrain = 0;
  21. Camera   TheCamera(Camera::LANDOBJECT);
  22. FPSCounter* FPS = 0;
  23. //
  24. // Framework Functions
  25. //
  26. bool Setup()
  27. {
  28.     //
  29.     // 创建地形.
  30.     //
  31.     D3DXVECTOR3 lightDirection(0.0f, 1.0f, 0.0f);
  32.     TheTerrain = new Terrain(Device, "coastMountain64.raw", 64, 64, 10, 0.5f);
  33.     TheTerrain->genTexture(&lightDirection);
  34.     //
  35.     // 创建字体.
  36.     //
  37.     FPS = new FPSCounter(Device);
  38.     //
  39.     // 纹理过滤.
  40.     //
  41.     Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  42.     Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  43.     Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
  44.     //
  45.     // 设置 projection matrix.
  46.     //
  47.     D3DXMATRIX proj;
  48.     D3DXMatrixPerspectiveFovLH(
  49.             &proj,
  50.             D3DX_PI * 0.25f, // 45 - degree
  51.             (float)Width / (float)Height,
  52.             1.0f,
  53.             1000.0f);
  54.     Device->SetTransform(D3DTS_PROJECTION, &proj);
  55.     return true;
  56. }
  57. void Cleanup()
  58. {
  59.     d3d::Delete<Terrain*>(TheTerrain);
  60.     d3d::Delete<FPSCounter*>(FPS);
  61. }
  62. bool Display(float timeDelta)
  63. {
  64.     //
  65.     // Update the scene:
  66.     //
  67.     if( Device )
  68.     {
  69.         if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
  70.             TheCamera.walk(100.0f * timeDelta);
  71.         if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
  72.             TheCamera.walk(-100.0f * timeDelta);
  73.         if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
  74.             TheCamera.yaw(-1.0f * timeDelta);
  75.         
  76.         if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
  77.             TheCamera.yaw(1.0f * timeDelta);
  78.         if( ::GetAsyncKeyState('N') & 0x8000f )
  79.             TheCamera.strafe(-100.0f * timeDelta);
  80.         if( ::GetAsyncKeyState('M') & 0x8000f )
  81.             TheCamera.strafe(100.0f * timeDelta);
  82.         if( ::GetAsyncKeyState('W') & 0x8000f )
  83.             TheCamera.pitch(1.0f * timeDelta);
  84.         if( ::GetAsyncKeyState('S') & 0x8000f )
  85.             TheCamera.pitch(-1.0f * timeDelta);
  86.         D3DXVECTOR3 pos;
  87.         TheCamera.getPosition(&pos);
  88.         float height = TheTerrain->getHeight( pos.x, pos.z );
  89.         pos.y = height + 5.0f; // add height because we're standing up
  90.         TheCamera.setPosition(&pos);
  91.         D3DXMATRIX V;
  92.         TheCamera.getViewMatrix(&V);
  93.         Device->SetTransform(D3DTS_VIEW, &V);
  94.         //
  95.         // Draw the scene:
  96.         //
  97.         Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0);
  98.         Device->BeginScene();
  99.         D3DXMATRIX I;
  100.         D3DXMatrixIdentity(&I);
  101.         if( TheTerrain )
  102.             TheTerrain->draw(&I, false);
  103.         if( FPS )
  104.             FPS->render(0xffffffff, timeDelta);
  105.         Device->EndScene();
  106.         Device->Present(0, 0, 0, 0);
  107.     }
  108.     return true;
  109. }
  110. //
  111. // WndProc
  112. //
  113. LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  114. {
  115.     switch( msg )
  116.     {
  117.     case WM_DESTROY:
  118.         ::PostQuitMessage(0);
  119.         break;
  120.         
  121.     case WM_KEYDOWN:
  122.         if( wParam == VK_ESCAPE )
  123.             ::DestroyWindow(hwnd);
  124.         break;
  125.     }
  126.     return ::DefWindowProc(hwnd, msg, wParam, lParam);
  127. }
  128. //
  129. // WinMain
  130. //
  131. int WINAPI WinMain(HINSTANCE hinstance,
  132.                    HINSTANCE prevInstance, 
  133.                    PSTR cmdLine,
  134.                    int showCmd)
  135. {
  136.     if(!d3d::InitD3D(hinstance,
  137.         Width, Height, true, D3DDEVTYPE_HAL, &Device))
  138.     {
  139.         ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
  140.         return 0;
  141.     }
  142.         
  143.     if(!Setup())
  144.     {
  145.         ::MessageBox(0, "Setup() - FAILED", 0, 0);
  146.         return 0;
  147.     }
  148.     d3d::EnterMsgLoop( Display );
  149.     Cleanup();
  150.     Device->Release();
  151.     return 0;
  152. }

附图:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值