72 lines
2.1 KiB
C++
72 lines
2.1 KiB
C++
// Camera.hpp
|
|
#pragma once
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
namespace vapp{
|
|
enum Camera_Movement{
|
|
FORWARD,
|
|
BACKWARD,
|
|
LEFT,
|
|
RIGHT,
|
|
UP,
|
|
DOWN,
|
|
};
|
|
|
|
class Camera{
|
|
public:
|
|
float Yaw = -90.0f, Pitch = 0.0f;
|
|
float MovementSpeed = 2.5f;
|
|
float MouseSensitivity = 0.1f;
|
|
float Zoom = 45.0f;
|
|
|
|
glm::vec3 Position, Front, Up, Right, WorldUp;
|
|
|
|
Camera(glm::vec3 pos = {0, 0, 3}, glm::vec3 up = {0, 1, 0})
|
|
: Position(pos), WorldUp(up), Front({0, 0, -1}){ updateCameraVectors(); }
|
|
|
|
glm::mat4 GetViewMatrix(){
|
|
return glm::lookAt(Position, Position + Front, Up);
|
|
}
|
|
|
|
void ProcessKeyboard(Camera_Movement dir, float dt){
|
|
float v = MovementSpeed * dt;
|
|
if(dir == FORWARD) Position += Front * v;
|
|
if(dir == BACKWARD) Position -= Front * v;
|
|
if(dir == LEFT) Position -= Right * v;
|
|
if(dir == RIGHT) Position += Right * v;
|
|
if(dir == UP) Position += Up * v;
|
|
if(dir == DOWN) Position -= Up * v;
|
|
}
|
|
|
|
void ProcessMouseMovement(float xoff, float yoff, bool constrainPitch = true){
|
|
xoff *= MouseSensitivity;
|
|
yoff *= MouseSensitivity;
|
|
Yaw += xoff;
|
|
Pitch += yoff;
|
|
if(constrainPitch){
|
|
if(Pitch > 89.0f) Pitch = 89.0f;
|
|
if(Pitch < -89.0f) Pitch = -89.0f;
|
|
}
|
|
updateCameraVectors();
|
|
}
|
|
|
|
void ProcessMouseScroll(float yoff){
|
|
Zoom -= yoff;
|
|
if(Zoom < 1.0f) Zoom = 1.0f;
|
|
if(Zoom > 45.0f) Zoom = 45.0f;
|
|
}
|
|
|
|
private:
|
|
void updateCameraVectors(){
|
|
glm::vec3 f;
|
|
f.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
|
f.y = sin(glm::radians(Pitch));
|
|
f.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
|
Front = glm::normalize(f);
|
|
Right = glm::normalize(glm::cross(Front, WorldUp));
|
|
Up = glm::normalize(glm::cross(Right, Front));
|
|
}
|
|
};
|
|
}
|