Dx基本Camera类

#ifndef __CameraH__
#define __CameraH__

#include <d3d9.h>
#include <d3dx9.h>
#include <Windows.h>

class CCamera
{
private:
 IDirect3DDevice9* m_pDevice;
 D3DXVECTOR3 m_vPos;
 POINT m_LastPoint;
 float m_fYaw, m_fPitch, m_fVelocity;
 bool m_bIsRot;
private:
 void SetView();
public:
 void SetCamera(IDirect3DDevice9* pDevice, D3DXVECTOR3 vPos, float fYaw, float fPitch, float fVelocity);
 void Logic(float fElapsedTime);
 void Handle(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
void CCamera::SetCamera(IDirect3DDevice9* pDevice, D3DXVECTOR3 vPos, float fYaw, float fPitch, float fVelocity)
{
 m_pDevice = pDevice; m_vPos = vPos; m_fYaw = fYaw; m_fPitch = fPitch; m_fVelocity = fVelocity;
 m_bIsRot = false;
 SetView();
}
void CCamera::Logic(float fElapsedTime)
{
 D3DXVECTOR3 vDelta(0.0f, 0.0f, 0.0f);
 bool bOk = false;
 float fDistance = m_fVelocity * fElapsedTime;
 if(GetAsyncKeyState('W') & 0x8000) { vDelta.z += fDistance; bOk = true; }
 if(GetAsyncKeyState('S') & 0x8000) { vDelta.z -= fDistance; bOk = true; }
 if(GetAsyncKeyState('A') & 0x8000) { vDelta.x -= fDistance; bOk = true; }
 if(GetAsyncKeyState('D') & 0x8000) { vDelta.x += fDistance; bOk = true; }
 if(GetAsyncKeyState(VK_HOME) & 0x8000) { vDelta.y += fDistance; bOk = true; }
 if(GetAsyncKeyState(VK_END) & 0x8000) { vDelta.y -= fDistance; bOk = true; }
 if(bOk)
 {
  D3DXMATRIX matRot;
  D3DXMatrixRotationYawPitchRoll(&matRot, m_fYaw, m_fPitch, 0.0f);
  D3DXVec3TransformNormal(&vDelta, &vDelta, &matRot);
  m_vPos += vDelta;
 }
 if(m_bIsRot)
 {
  POINT CurrentPoint;
  GetCursorPos(&CurrentPoint);
  POINT DeltaPoint;
  DeltaPoint.x = CurrentPoint.x - m_LastPoint.x;
  DeltaPoint.y = CurrentPoint.y - m_LastPoint.y;
  m_LastPoint = CurrentPoint;
  m_fYaw += DeltaPoint.x * 0.01f;
  m_fPitch += DeltaPoint.y * 0.01f;
 }
 SetView();
}
void CCamera::Handle(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
 if(uMsg == WM_RBUTTONDOWN)
 {
  m_bIsRot = true;
  GetCursorPos(&m_LastPoint);
  SetCapture(hWnd);
 }
 else if(uMsg == WM_RBUTTONUP)
 {
  m_bIsRot = false;
  ReleaseCapture();
 }
}
void CCamera::SetView()
{
 D3DXVECTOR3 vPos = m_vPos;
 D3DXMATRIX matRot;
 D3DXMatrixRotationYawPitchRoll(&matRot, m_fYaw, m_fPitch, 0.0f);
 D3DXVECTOR3 vDir;
 D3DXVec3TransformNormal(&vDir, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), &matRot);
 D3DXVECTOR3 vLookAt = vPos + vDir;
 D3DXVECTOR3 vUp;
 D3DXVec3TransformNormal(&vUp, &D3DXVECTOR3(0.0f, 1.0f, 0.0f), &matRot);
 D3DXMATRIX matView;
 D3DXMatrixLookAtLH(&matView, &vPos, &vLookAt, &vUp);
 m_pDevice->SetTransform(D3DTS_VIEW, &matView);
}
#endif

posted on 2012-05-01 21:59  紫澜  阅读(423)  评论(0编辑  收藏  举报