Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. namespace Engine
  2. {
  3.     /// <summary>
  4.     /// Generic camera class
  5.     /// </summary>
  6.     class Camera
  7.     {
  8.     protected:
  9.         /// <summary>Camera view matrix</summary>
  10.         mat4 mViewMatrix;
  11.  
  12.         /// <summary>Camera projection matrix</summary>
  13.         mat4 mProjectionMatrix;
  14.  
  15.         /// <summary>Camera position</summary>
  16.         float4 mPosition;
  17.  
  18.         /// <summary>Camera look-at target</summary>
  19.         float4 mTarget;
  20.  
  21.         /// <summary>Camera forward vector</summary>
  22.         float4 mForward;
  23.  
  24.         /// <summary>Camera up vector</summary>
  25.         float4 mUp;
  26.  
  27.         /// <summary>Camera movement direction, so that our forward+strafe is not faster</summary>
  28.         float4 mDirection;
  29.  
  30.         /// <summary>Camera movement speed</summary>
  31.         float mSpeed;
  32.  
  33.         /// <summary>Is our view matrix really representing current state</summary>
  34.         bool mValid;
  35.  
  36.         /// <summary>Pointer to frustum of the camera</summary>
  37.         Frustum* mFrustum;
  38.  
  39.         /// <summary>Updates frustum of the camera</summary>
  40.         virtual void UpdateFrustum() = 0;
  41.  
  42.     public:
  43.         /// <summary>Constructor</summary>
  44.         /// <param name="position">Camera position</param>
  45.         /// <param name="target">Camera look-at target</param>
  46.         /// <param name="up">Camera up vector</param>
  47.         /// <param name="speed">Camera translation speed</param>
  48.         Camera( const float4& position,
  49.                 const float4& target,
  50.                 const float4& up,
  51.                 float speed = 1.0f)
  52.         {
  53.             mPosition = position;
  54.             mTarget = target;
  55.             mUp = up;
  56.             mSpeed = speed;
  57.             mForward = normalize(target - position);
  58.             mValid = false;
  59.             mFrustum = new Frustum();
  60.         }
  61.  
  62.         /// <summary>Virtual destructor</summary>
  63.         virtual ~Camera()
  64.         {
  65.             delete mFrustum;
  66.         }
  67.  
  68.         /// <summary>Strafe camera into side</summary>
  69.         /// <param name="dir">Whether into positive or negative direction</param>
  70.         void Strafe(float dir = 1.0f)
  71.         {
  72.             float4 ortho = normalize(float4(mForward.z, 0.0f, -mForward.x, 0.0f));
  73.             mDirection += ortho * dir;
  74.         }
  75.  
  76.         /// <summary>Move camera</summary>
  77.         /// <param name="dir">Whether into positive or negative direction</param>
  78.         void Move(float dir = 1.0f)
  79.         {
  80.             mDirection += mForward * dir;
  81.         }
  82.  
  83.         /// <summary>Rotate camera direction</summary>
  84.         /// <param name="yaw">Delta yaw angle</param>
  85.         /// <param name="pitch">Delta pitch angle</param>
  86.         void Rotate(float yaw, float pitch)
  87.         {
  88.             // Calculate quaternion rotation of delta yaw and delta pitch
  89.             quat rotation;
  90.             float4 ortho = normalize(float4(mForward.z, 0.0f, -mForward.x, 0.0f));
  91.             rotation = from_axisangle(float4(0, 1, 0, 0), yaw);
  92.             rotation = rotation * from_axisangle(ortho, -pitch);
  93.  
  94.             // Apply this rotation to camera forward vector
  95.             mForward = rotate(rotation, mForward);
  96.  
  97.             // Update camera target position
  98.             mTarget = mPosition + mForward;
  99.  
  100.             // Invalidate matrices
  101.             mValid = false;
  102.         }
  103.  
  104.         /// <summary>
  105.         /// Camera process step
  106.         ///
  107.         /// Moves camera by distance computed as ds = v * dt - e.g. using camera speed constant and
  108.         /// delta time (between 2 steps of game engine).
  109.         /// </summary>
  110.         /// <param name="up">Camera up vector</param>
  111.         void Process(float timeStep)
  112.         {
  113.             // Normalize direction of the camera
  114.             if (length(mDirection) > 0.0f)
  115.             {
  116.                 mDirection = normalize(mDirection);
  117.             }
  118.  
  119.             // Perform differential step
  120.             mPosition += mDirection * mSpeed * timeStep;
  121.             mTarget += mDirection * mSpeed * timeStep;
  122.  
  123.             // Zero-out direction and invalidate matrices
  124.             mDirection = float4(0.0f, 0.0f, 0.0f, 0.0f);
  125.             mValid = false;
  126.         }
  127.  
  128.         /// <summary>Getter for camera view matrix</summary>
  129.         virtual const mat4& GetViewMatrix()
  130.         {
  131.             mViewMatrix = lookat(mPosition, mTarget, mUp);
  132.             //mViewMatrix = LookAtLH(mPosition, mTarget, mUp);
  133.             mValid = true;
  134.  
  135.             return mViewMatrix;
  136.         }
  137.  
  138.         inline float4 GetPosition()
  139.         {
  140.             return mPosition;
  141.         }
  142.  
  143.         inline Frustum* GetFrustum()
  144.         {
  145.             UpdateFrustum();
  146.             return mFrustum;
  147.         }
  148.  
  149.         inline float4 GetForward()
  150.         {
  151.             return mForward;
  152.         }
  153.  
  154.         inline float4 GetRight()
  155.         {
  156.             return cross(mForward, mUp);
  157.         }
  158.  
  159.         inline float4 GetUp()
  160.         {
  161.             return mUp;
  162.         }
  163.  
  164.         /// <summary>Getter for camera projection matrix (abstract)</summary>
  165.         virtual const mat4& GetProjectionMatrix() = 0;
  166.  
  167.         ALIGNED_NEW_DELETE("Engine::Camera")
  168.     };
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement