topical media & game development

talk show tell print

graphic-directx-game-17-Terrain-Demo-TerrainDemo.cpp / cpp



  //=============================================================================
  // TerrainDemo.cpp by Frank Luna (C) 2005 All Rights Reserved.
  //
  // Demonstrates how to render a terrain with multi-texturing.
  //
  // Controls: Use mouse to orbit and zoom; use the 'W' and 'S' keys to 
  //           alter the height of the camera.
  //=============================================================================
  
  include <d3dApp.h>
  include <DirectInput.h>
  include <crtdbg.h>
  include <GfxStats.h>
  include <list>
  include <Terrain.h>
   
  
  class TerrainDemo : public D3DApp
  {
  public:
          TerrainDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
          ~TerrainDemo();
  
          bool checkDeviceCaps();
          void onLostDevice();
          void onResetDevice();
          void updateScene(float dt);
          void drawScene();
  
          // Helper methods
          void buildViewMtx();
          void buildProjMtx();
  
  private:
          GfxStats* mGfxStats;
          Terrain*  mTerrain;
  
          float mCameraRotationY;
          float mCameraRadius;
          float mCameraHeight;
  
          D3DXMATRIX mWorld; // Not used
          D3DXMATRIX mView;
          D3DXMATRIX mProj;
  };
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
                                     PSTR cmdLine, int showCmd)
  {
          // Enable run-time memory check for debug builds.
          #if defined(DEBUG) | defined(_DEBUG)
                  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
          #endif
  
          TerrainDemo app(hInstance, "Terrain Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
          gd3dApp = &app;
  
          DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
          gDInput = &di;
  
      return gd3dApp->run();
  }
  
  TerrainDemo::TerrainDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
  : D3DApp(hInstance, winCaption, devType, requestedVP)
  {
          if(!checkDeviceCaps())
          {
                  MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
                  PostQuitMessage(0);
          }
  
          InitAllVertexDeclarations();
  
          mGfxStats = new GfxStats();
          
          mCameraRadius    = 75.0f;
          mCameraRotationY = 1.3f * D3DX_PI;
          mCameraHeight    = 35.0f;
  
          mTerrain = new Terrain(257, 257, 0.5f, 0.5f, 
                  "heightmap17_257.raw",  
                  "grass.dds",
                  "dirt.dds",
                  "stone.dds",
                  "blend_hm17.dds",
                  0.2f, 0.0f);
  
          mTerrain->setDirToSunW(D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  
          // Scale the mesh down.
          D3DXMatrixIdentity(&mWorld);
          
          mGfxStats->addVertices(mTerrain->getNumVertices());
          mGfxStats->addTriangles(mTerrain->getNumTriangles());
  
          onResetDevice();
  }
  
  TerrainDemo::~TerrainDemo()
  {
          delete mGfxStats;
          delete mTerrain;
  
          DestroyAllVertexDeclarations();
  }
  
  bool TerrainDemo::checkDeviceCaps()
  {
          D3DCAPS9 caps;
          HR(gd3dDevice->GetDeviceCaps(&caps));
  
          // Check for vertex shader version 2.0 support.
          if( caps.VertexShaderVersion < D3DVS_VERSION(2, 0) )
                  return false;
  
          // Check for pixel shader version 2.0 support.
          if( caps.PixelShaderVersion < D3DPS_VERSION(2, 0) )
                  return false;
  
          return true;
  }
  
  void TerrainDemo::onLostDevice()
  {
          mGfxStats->onLostDevice();
          mTerrain->onLostDevice();
  }
  
  void TerrainDemo::onResetDevice()
  {
          mGfxStats->onResetDevice();
          mTerrain->onResetDevice();
  
          // The aspect ratio depends on the backbuffer dimensions, which can 
          // possibly change after a reset.  So rebuild the projection matrix.
          buildProjMtx();
  }
  
  void TerrainDemo::updateScene(float dt)
  {
          mGfxStats->update(dt);
  
          // Get snapshot of input devices.
          gDInput->poll();
  
          // Check input.
          if( gDInput->keyDown(DIK_W) )         
                  mCameraHeight   += 25.0f * dt;
          if( gDInput->keyDown(DIK_S) )         
                  mCameraHeight   -= 25.0f * dt;
  
          // Divide by 50 to make mouse less sensitive. 
          mCameraRotationY += gDInput->mouseDX() / 100.0f;
          mCameraRadius    += gDInput->mouseDY() / 25.0f;
  
          // If we rotate over 360 degrees, just roll back to 0
          if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI ) 
                  mCameraRotationY = 0.0f;
  
          // Don't let radius get too small.
          if( mCameraRadius < 2.0f )
                  mCameraRadius = 2.0f;
  
          // The camera position/orientation relative to world space can 
          // change every frame based on input, so we need to rebuild the
          // view matrix every frame with the latest changes.
          buildViewMtx();
  }
  
  void TerrainDemo::drawScene()
  {
          // Clear the backbuffer and depth buffer.
          HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
  
          HR(gd3dDevice->BeginScene());
  
          mTerrain->draw(mView, mProj);
          
          mGfxStats->display();
  
          HR(gd3dDevice->EndScene());
  
          // Present the backbuffer.
          HR(gd3dDevice->Present(0, 0, 0, 0));
  }
  
  void TerrainDemo::buildViewMtx()
  {
          float x = mCameraRadius * cosf(mCameraRotationY);
          float z = mCameraRadius * sinf(mCameraRotationY);
          D3DXVECTOR3 pos(x, mCameraHeight, z);
          D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
          D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
          D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
  }
  
  void TerrainDemo::buildProjMtx()
  {
          float w = (float)md3dPP.BackBufferWidth;
          float h = (float)md3dPP.BackBufferHeight;
          D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.