Advertisement
Guest User

Shader.h

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