afCamera.cpp
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:3k
源码类别:

其他游戏

开发平台:

Visual C++

  1. // afCamera.cpp: implementation of the afCamera class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "afCamera.h"
  5. #include "afPlane.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. //-----------------------------------------------------------------------------
  10. // Name:
  11. // Desc:
  12. //-----------------------------------------------------------------------------
  13. afCamera::afCamera()
  14. {
  15.     // Set attributes for the view matrix
  16.     D3DXVECTOR3 vEyePt(0.0f,0.0f,0.0f);
  17.     D3DXVECTOR3 vLookatPt(0.0f,0.0f,1.0f);
  18.     D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f);
  19.     SetViewParams( vEyePt, vLookatPt, vUpVec );
  20.     // Set attributes for the projection matrix
  21.     SetProjParams( D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Name:
  25. // Desc:
  26. //-----------------------------------------------------------------------------
  27. VOID afCamera::SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
  28.                                 D3DXVECTOR3& vUpVec )
  29. {
  30.     // Set attributes for the view matrix
  31.     m_vEyePt    = vEyePt;
  32.     m_vLookatPt = vLookatPt;
  33.     m_vUpVec    = vUpVec;
  34.     D3DXVECTOR3 vDir = m_vLookatPt - m_vEyePt;
  35.     D3DXVec3Normalize( &m_vView, &vDir );
  36.     D3DXVec3Cross( &m_vCross, &m_vView, &m_vUpVec );
  37.     D3DXMatrixLookAtLH( &m_matView, &m_vEyePt, &m_vLookatPt, &m_vUpVec );
  38.     D3DXMatrixInverse( &m_matBillboard, NULL, &m_matView );
  39.     m_matBillboard._41 = 0.0f;
  40.     m_matBillboard._42 = 0.0f;
  41.     m_matBillboard._43 = 0.0f;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Name:
  45. // Desc:
  46. //-----------------------------------------------------------------------------
  47. VOID afCamera::SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
  48.                                    FLOAT fFarPlane )
  49. {
  50.     // Set attributes for the projection matrix
  51.     m_fFOV        = fFOV;
  52.     m_fAspect     = fAspect;
  53.     m_fNearPlane  = fNearPlane;
  54.     m_fFarPlane   = fFarPlane;
  55.     D3DXMatrixPerspectiveFovLH( &m_matProj, fFOV, fAspect, fNearPlane, fFarPlane );
  56. }
  57. bool afCamera::getUpdateVisibility()
  58. {
  59. return m_updateVisibility;
  60. }
  61. void afCamera::SetUpdateVisibility(bool visible)
  62. {
  63. m_updateVisibility=visible;
  64. }
  65. void afCamera::createClippingPlanes(afPlane nPlanes[6])
  66. {
  67. float nx,ny,nz;
  68. // near plane
  69. nPlanes[0].set(0.0f, 0.0f, -1.0f, -m_fNearPlane);
  70. // far plane
  71. nPlanes[1].set(0.0f, 0.0f, 1.0f, m_fFarPlane);
  72. nx = (float)cos(GetFOVX()/2.0f);
  73. nz = (float)sin(GetFOVX()/2.0f);
  74. // right plane
  75. nPlanes[2].set(nx, 0.0f, -nz, 0.0f);
  76. // left plane
  77. nPlanes[3].set(-nx, 0.0f, -nz, 0.0f);
  78. ny = (float)cos(GetFOVY()/2.0f);
  79. nz = (float)sin(GetFOVY()/2.0f);
  80. // up plane
  81. nPlanes[4].set(0.0f, ny, -nz, 0.0f);
  82. // down plane
  83. nPlanes[5].set(0.0f, -ny, -nz, 0.0f);
  84. }