Advertisement
Nightdra

Camera.h

Jul 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #ifndef CAMERA_H
  2. #define CAMERA_H
  3.  
  4. #include <glad/glad.h>
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7.  
  8. #include <vector>
  9.  
  10. // Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
  11. enum Camera_Movement {
  12.     FORWARD,
  13.     BACKWARD,
  14.     LEFT,
  15.     RIGHT,
  16.     UP,
  17.     DOWN
  18. };
  19.  
  20. // Default camera values
  21. const float YAW        = -90.0f;
  22. const float PITCH      =  0.0f;
  23. const float SPEED      =  2.5f;
  24. const float SENSITIVTY =  0.05f;
  25. const float ZOOM       =  45.0f;
  26.  
  27.  
  28. // An abstract camera class that processes input and calculates the corresponding Eular Angles, Vectors and Matrices for use in OpenGL
  29. class Camera
  30. {
  31. public:
  32.     // Camera Attributes
  33.     glm::vec3 Position;
  34.     glm::vec3 Front;
  35.     glm::vec3 moveFront;
  36.     glm::vec3 Up;
  37.     glm::vec3 Right;
  38.     glm::vec3 WorldUp;
  39.     // Eular Angles
  40.     float Yaw;
  41.     float Pitch;
  42.     // Camera options
  43.     float MovementSpeed;
  44.     float MouseSensitivity;
  45.     float Zoom;
  46.  
  47.     // Constructor with vectors
  48.     Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
  49.     {
  50.         Position = position;
  51.         WorldUp = up;
  52.         Yaw = yaw;
  53.         Pitch = pitch;
  54.         updateCameraVectors();
  55.     }
  56.     // Constructor with scalar values
  57.     Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
  58.     {
  59.         Position = glm::vec3(posX, posY, posZ);
  60.         WorldUp = glm::vec3(upX, upY, upZ);
  61.         Yaw = yaw;
  62.         Pitch = pitch;
  63.         updateCameraVectors();
  64.     }
  65.  
  66.     // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
  67.     glm::mat4 GetViewMatrix()
  68.     {
  69.         return glm::lookAt(Position, Position + Front, Up);
  70.     }
  71.  
  72.     // Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
  73.     void ProcessKeyboard(Camera_Movement direction, float deltaTime)
  74.     {
  75.         float velocity = MovementSpeed * deltaTime;
  76.         if (direction == FORWARD)
  77.             Position += moveFront * velocity;
  78.         if (direction == BACKWARD)
  79.             Position -= moveFront * velocity;
  80.         if (direction == LEFT)
  81.             Position -= Right * velocity;
  82.         if (direction == RIGHT)
  83.             Position += Right * velocity;
  84.         if (direction == DOWN)
  85.             Position -= glm::vec3(0.0, 1.0, 0.0) * velocity;
  86.         if (direction == UP)
  87.             Position += glm::vec3(0.0, 1.0, 0.0) * velocity;
  88.     }
  89.  
  90.     // Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
  91.     void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
  92.     {
  93.         xoffset *= MouseSensitivity;
  94.         yoffset *= MouseSensitivity;
  95.  
  96.         Yaw   += xoffset;
  97.         Pitch += yoffset;
  98.  
  99.         // Make sure that when pitch is out of bounds, screen doesn't get flipped
  100.         if (constrainPitch)
  101.         {
  102.             if (Pitch > 89.0f)
  103.                 Pitch = 89.0f;
  104.             if (Pitch < -89.0f)
  105.                 Pitch = -89.0f;
  106.         }
  107.  
  108.         // Update Front, Right and Up Vectors using the updated Eular angles
  109.         updateCameraVectors();
  110.     }
  111.  
  112.     // Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
  113.     void ProcessMouseScroll(float yoffset)
  114.     {
  115.         if (Zoom >= 1.0f && Zoom <= 45.0f)
  116.             Zoom -= yoffset;
  117.         if (Zoom <= 1.0f)
  118.             Zoom = 1.0f;
  119.         if (Zoom >= 45.0f)
  120.             Zoom = 45.0f;
  121.     }
  122.  
  123. private:
  124.     // Calculates the front vector from the Camera's (updated) Eular Angles
  125.     void updateCameraVectors()
  126.     {
  127.         // Calculate the new Front vector
  128.         glm::vec3 front;
  129.         front.x = cos(glm::radians(Yaw));
  130.         front.y = 0.0f;
  131.         front.z = sin(glm::radians(Yaw));
  132.         moveFront = glm::normalize(front);
  133.  
  134.         front.x *= cos(glm::radians(Pitch));
  135.         front.y = sin(glm::radians(Pitch));
  136.         front.z *= cos(glm::radians(Pitch));
  137.         Front = glm::normalize(front);
  138.  
  139.         // Also re-calculate the Right and Up vector
  140.         Right = glm::normalize(glm::cross(Front, WorldUp));  // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
  141.         Up    = glm::normalize(glm::cross(Right, Front));
  142.     }
  143. };
  144. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement