topical media & game development

talk show tell print

graphic-directx-game-09-ColoredWavesDemo-ColoredWavesDemo.cpp / cpp



  //=============================================================================
  // ColoredWavesDemo.cpp by Frank Luna (C) 2005 All Rights Reserved.
  //
  // Shows how to animate vertices in a vertex shader and how to color
  // vertices in a vertex shader procedurally based on the height of 
  // the vertex.
  //
  // 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 <Vertex.h>
  
  class ColoredWavesDemo : public D3DApp
  {
  public:
          ColoredWavesDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
          ~ColoredWavesDemo();
  
          bool checkDeviceCaps();
          void onLostDevice();
          void onResetDevice();
          void updateScene(float dt);
          void drawScene();
  
          // Helper methods
          void buildGeoBuffers();
          void buildFX();
          void buildViewMtx();
          void buildProjMtx();
  
  private:
          GfxStats* mGfxStats;
  
          DWORD mNumVertices;
          DWORD mNumTriangles;
  
          IDirect3DVertexBuffer9* mVB;
          IDirect3DIndexBuffer9*  mIB;
          ID3DXEffect*            mFX;
          D3DXHANDLE              mhTech;
          D3DXHANDLE              mhWVP;
          D3DXHANDLE              mhTime;
  
          float mTime;
  
          float mCameraRotationY;
          float mCameraRadius;
          float mCameraHeight;
  
          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
  
          ColoredWavesDemo app(hInstance, "Colored Waves Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
          gd3dApp = &app;
  
          DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
          gDInput = &di;
  
      return gd3dApp->run();
  }
  
  ColoredWavesDemo::ColoredWavesDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
  : D3DApp(hInstance, winCaption, devType, requestedVP)
  {
          if(!checkDeviceCaps())
          {
                  MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
                  PostQuitMessage(0);
          }
  
          mGfxStats = new GfxStats();
  
          mCameraRadius    = 25.0f;
          mCameraRotationY = 1.2 * D3DX_PI;
          mCameraHeight    = 15.0f;
          mTime            = 0.0f;
  
          buildGeoBuffers();
          buildFX();
  
          onResetDevice();
  
          InitAllVertexDeclarations();
  }
  
  ColoredWavesDemo::~ColoredWavesDemo()
  {
          delete mGfxStats;
          ReleaseCOM(mVB);
          ReleaseCOM(mIB);
          ReleaseCOM(mFX);
  
          DestroyAllVertexDeclarations();
  }
  
  bool ColoredWavesDemo::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 ColoredWavesDemo::onLostDevice()
  {
          mGfxStats->onLostDevice();
          HR(mFX->OnLostDevice());
  }
  
  void ColoredWavesDemo::onResetDevice()
  {
          mGfxStats->onResetDevice();
          HR(mFX->OnResetDevice());
  
          // The aspect ratio depends on the backbuffer dimensions, which can 
          // possibly change after a reset.  So rebuild the projection matrix.
          buildProjMtx();
  }
  
  void ColoredWavesDemo::updateScene(float dt)
  {
          mGfxStats->setVertexCount(mNumVertices);
          mGfxStats->setTriCount(mNumTriangles);
          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 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 < 5.0f )
                  mCameraRadius = 5.0f;
  
          // Accumulate time for simulation.  
          mTime += dt;
  
          // 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 ColoredWavesDemo::drawScene()
  {
          // Clear the backbuffer and depth buffer.
          HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));
  
          HR(gd3dDevice->BeginScene());
  
          // Let Direct3D know the vertex buffer, index buffer and vertex 
          // declaration we are using.
          HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos)));
          HR(gd3dDevice->SetIndices(mIB));
          HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl));
  
          // Setup the rendering FX
          HR(mFX->SetTechnique(mhTech));
          HR(mFX->SetMatrix(mhWVP, &(mView*mProj)));
          HR(mFX->SetFloat(mhTime, mTime));
  
          // Begin passes.
          UINT numPasses = 0;
          HR(mFX->Begin(&numPasses, 0));
          for(UINT i = 0; i < numPasses; ++i)
          {
                  HR(mFX->BeginPass(i));
                  HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mNumVertices, 0, mNumTriangles));
                  HR(mFX->EndPass());
          }
          HR(mFX->End());
  
          
          mGfxStats->display();
  
          HR(gd3dDevice->EndScene());
  
          // Present the backbuffer.
          HR(gd3dDevice->Present(0, 0, 0, 0));
  }
   
  void ColoredWavesDemo::buildGeoBuffers()
  {
          std::vector<D3DXVECTOR3> verts;
          std::vector<DWORD> indices;
  
          GenTriGrid(100, 100, 0.5f, 0.5f, 
                  D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);
  
          // Save vertex count and triangle count for DrawIndexedPrimitive arguments.
          mNumVertices  = 100*100;
          mNumTriangles = 99*99*2;
  
          // Obtain a pointer to a new vertex buffer.
          HR(gd3dDevice->CreateVertexBuffer(mNumVertices * sizeof(VertexPos), 
                  D3DUSAGE_WRITEONLY,        0, D3DPOOL_MANAGED, &mVB, 0));
  
          // Now lock it to obtain a pointer to its internal data, and write the
          // grid's vertex data.
          VertexPos* v = 0;
          HR(mVB->Lock(0, 0, (void**)&v, 0));
  
          for(DWORD i = 0; i < mNumVertices; ++i)
                  v[i] = verts[i];
  
          HR(mVB->Unlock());
  
          // Obtain a pointer to a new index buffer.
          HR(gd3dDevice->CreateIndexBuffer(mNumTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
                  D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
  
          // Now lock it to obtain a pointer to its internal data, and write the
          // grid's index data.
  
          WORD* k = 0;
          HR(mIB->Lock(0, 0, (void**)&k, 0));
  
          for(DWORD i = 0; i < mNumTriangles*3; ++i)
                  k[i] = (WORD)indices[i];
  
          HR(mIB->Unlock());
  }
  
  void ColoredWavesDemo::buildFX()
  {
          // Create the FX from a .fx file.
          ID3DXBuffer* errors = 0;
          HR(D3DXCreateEffectFromFile(gd3dDevice, "heightcolor.fx", 
                  0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors));
          if( errors )
                  MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
  
          // Obtain handles.
          mhTech = mFX->GetTechniqueByName("HeightColorTech");
          mhWVP  = mFX->GetParameterByName(0, "gWVP");
          mhTime = mFX->GetParameterByName(0, "gTime");
  }
  
  void ColoredWavesDemo::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 ColoredWavesDemo::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.