topical media & game development

talk show tell print

graphic-directx-game-18-Culling-CullingDemo.cpp / cpp



  //=============================================================================
  // CullingDemo.cpp by Frank Luna (C) 2005 All Rights Reserved.
  //
  // Demonstrates how to view frustum cull axis aligned bounding boxes.
  //
  // Controls: Use mouse to look and 'W', 'S', 'A', and 'D' keys to move.
  //=============================================================================
  
  include <d3dApp.h>
  include <DirectInput.h>
  include <crtdbg.h>
  include <GfxStats.h>
  include <list>
  include <Terrain.h>
  include <Camera.h>
  
  class CullingDemo : public D3DApp
  {
  public:
          CullingDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
          ~CullingDemo();
  
          bool checkDeviceCaps();
          void onLostDevice();
          void onResetDevice();
          void updateScene(float dt);
          void drawScene();
  
  private:
          GfxStats* mGfxStats;
          Terrain*  mTerrain;
  };
  
  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
  
          // Construct camera before application, since the application uses the camera.
          Camera camera;
          gCamera = &camera;
  
          CullingDemo app(hInstance, "Culling Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
          gd3dApp = &app;
  
          DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
          gDInput = &di;
  
      return gd3dApp->run();
  }
  
  CullingDemo::CullingDemo(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();
          
          mTerrain = new Terrain(513, 513, 4.0f, 4.0f, 
                  "coastMountain513.raw",  
                  "grass.dds",
                  "dirt.dds",
                  "rock.dds",
                  "blend_coastal.dds",
                  1.5f, 0.0f);
  
          D3DXVECTOR3 toSun(1.0f, 1.0f, 1.0f);
          D3DXVec3Normalize(&toSun, &toSun);
          mTerrain->setDirToSunW(toSun);
  
          // Initialize camera.
          gCamera->pos().y = 250.0f;
          gCamera->setSpeed(50.0f);
  
          mGfxStats->addVertices(mTerrain->getNumVertices());
          mGfxStats->addTriangles(mTerrain->getNumTriangles());
  
          onResetDevice();
  }
  
  CullingDemo::~CullingDemo()
  {
          delete mGfxStats;
          delete mTerrain;
  
          DestroyAllVertexDeclarations();
  }
  
  bool CullingDemo::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 CullingDemo::onLostDevice()
  {
          mGfxStats->onLostDevice();
          mTerrain->onLostDevice();
  }
  
  void CullingDemo::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.
          float w = (float)md3dPP.BackBufferWidth;
          float h = (float)md3dPP.BackBufferHeight;
          gCamera->setLens(D3DX_PI * 0.25f, w/h, 1.0f, 2000.0f);
  }
  
  void CullingDemo::updateScene(float dt)
  {
          mGfxStats->update(dt);
  
          gDInput->poll();
  
          gCamera->update(dt, mTerrain, 2.5f);
  }
  
  void CullingDemo::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();
          
          mGfxStats->display();
  
          HR(gd3dDevice->EndScene());
  
          // Present the backbuffer.
          HR(gd3dDevice->Present(0, 0, 0, 0));
  }
  


(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.