home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / Camera.h < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  2.9 KB  |  98 lines

  1. //***************************************************************************************
  2. // Camera.h by Frank Luna (C) 2011 All Rights Reserved.
  3. //   
  4. // Simple first person style camera class that lets the viewer explore the 3D scene.
  5. //   -It keeps track of the camera coordinate system relative to the world space
  6. //    so that the view matrix can be constructed.  
  7. //   -It keeps track of the viewing frustum of the camera so that the projection
  8. //    matrix can be obtained.
  9. //***************************************************************************************
  10.  
  11. #ifndef CAMERA_H
  12. #define CAMERA_H
  13.  
  14. #include "d3dUtil.h"
  15.  
  16. class Camera
  17. {
  18. public:
  19.  
  20.     Camera();
  21.     ~Camera();
  22.  
  23.     // Get/Set world camera position.
  24.     DirectX::XMVECTOR GetPosition()const;
  25.     DirectX::XMFLOAT3 GetPosition3f()const;
  26.     void SetPosition(float x, float y, float z);
  27.     void SetPosition(const DirectX::XMFLOAT3& v);
  28.     
  29.     // Get camera basis vectors.
  30.     DirectX::XMVECTOR GetRight()const;
  31.     DirectX::XMFLOAT3 GetRight3f()const;
  32.     DirectX::XMVECTOR GetUp()const;
  33.     DirectX::XMFLOAT3 GetUp3f()const;
  34.     DirectX::XMVECTOR GetLook()const;
  35.     DirectX::XMFLOAT3 GetLook3f()const;
  36.  
  37.     // Get frustum properties.
  38.     float GetNearZ()const;
  39.     float GetFarZ()const;
  40.     float GetAspect()const;
  41.     float GetFovY()const;
  42.     float GetFovX()const;
  43.  
  44.     // Get near and far plane dimensions in view space coordinates.
  45.     float GetNearWindowWidth()const;
  46.     float GetNearWindowHeight()const;
  47.     float GetFarWindowWidth()const;
  48.     float GetFarWindowHeight()const;
  49.     
  50.     // Set frustum.
  51.     void SetLens(float fovY, float aspect, float zn, float zf);
  52.  
  53.     // Define camera space via LookAt parameters.
  54.     void LookAt(DirectX::FXMVECTOR pos, DirectX::FXMVECTOR target, DirectX::FXMVECTOR worldUp);
  55.     void LookAt(const DirectX::XMFLOAT3& pos, const DirectX::XMFLOAT3& target, const DirectX::XMFLOAT3& up);
  56.  
  57.     // Get View/Proj matrices.
  58.     DirectX::XMMATRIX GetView()const;
  59.     DirectX::XMMATRIX GetProj()const;
  60.  
  61.     DirectX::XMFLOAT4X4 GetView4x4f()const;
  62.     DirectX::XMFLOAT4X4 GetProj4x4f()const;
  63.  
  64.     // Strafe/Walk the camera a distance d.
  65.     void Strafe(float d);
  66.     void Walk(float d);
  67.  
  68.     // Rotate the camera.
  69.     void Pitch(float angle);
  70.     void RotateY(float angle);
  71.  
  72.     // After modifying camera position/orientation, call to rebuild the view matrix.
  73.     void UpdateViewMatrix();
  74.  
  75. private:
  76.  
  77.     // Camera coordinate system with coordinates relative to world space.
  78.     DirectX::XMFLOAT3 mPosition = { 0.0f, 0.0f, 0.0f };
  79.     DirectX::XMFLOAT3 mRight = { 1.0f, 0.0f, 0.0f };
  80.     DirectX::XMFLOAT3 mUp = { 0.0f, 1.0f, 0.0f };
  81.     DirectX::XMFLOAT3 mLook = { 0.0f, 0.0f, 1.0f };
  82.  
  83.     // Cache frustum properties.
  84.     float mNearZ = 0.0f;
  85.     float mFarZ = 0.0f;
  86.     float mAspect = 0.0f;
  87.     float mFovY = 0.0f;
  88.     float mNearWindowHeight = 0.0f;
  89.     float mFarWindowHeight = 0.0f;
  90.  
  91.     bool mViewDirty = true;
  92.  
  93.     // Cache View/Proj matrices.
  94.     DirectX::XMFLOAT4X4 mView = MathHelper::Identity4x4();
  95.     DirectX::XMFLOAT4X4 mProj = MathHelper::Identity4x4();
  96. };
  97.  
  98. #endif // CAMERA_H