121 lines
No EOL
3.9 KiB
C++
121 lines
No EOL
3.9 KiB
C++
|
|
#ifndef ENGINE2026_CAMERA_H
|
|
#define ENGINE2026_CAMERA_H
|
|
|
|
#include <glad/glad.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
// Movement keys/buttons
|
|
enum Camera_Movement {
|
|
FORWARD,
|
|
BACKWARD,
|
|
LEFT,
|
|
RIGHT
|
|
};
|
|
|
|
// Default camera values
|
|
const float YAW = -90.0f;
|
|
const float PITCH = 0.0f;
|
|
const float SPEED = 2.5f;
|
|
const float SENSITIVITY = 0.1f;
|
|
const float ZOOM = 45.0f;
|
|
|
|
class Camera
|
|
{
|
|
public:
|
|
glm::vec3 position, front, up, right, worldUp;
|
|
float yaw, pitch;
|
|
float movementSpeed, mouseSensitivity, zoom;
|
|
|
|
// constructor with vectors
|
|
Camera(glm::vec3 newPosition = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float newYaw = YAW, float newPitch = PITCH) : front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM)
|
|
{
|
|
position = newPosition;
|
|
worldUp = up;
|
|
yaw = newYaw;
|
|
pitch = newPitch;
|
|
updateCameraVectors();
|
|
}
|
|
// constructor with scalar values
|
|
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float newYaw, float newPitch) : front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM)
|
|
{
|
|
position = glm::vec3(posX, posY, posZ);
|
|
worldUp = glm::vec3(upX, upY, upZ);
|
|
yaw = newYaw;
|
|
pitch = newPitch;
|
|
updateCameraVectors();
|
|
}
|
|
|
|
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
|
|
glm::mat4 GetViewMatrix()
|
|
{
|
|
return glm::lookAt(position, position + front, up);
|
|
}
|
|
|
|
// 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)
|
|
void processKeyboard(Camera_Movement direction, float deltaTime)
|
|
{
|
|
float velocity = movementSpeed * deltaTime;
|
|
if (direction == FORWARD)
|
|
position += front * velocity;
|
|
if (direction == BACKWARD)
|
|
position -= front * velocity;
|
|
if (direction == LEFT)
|
|
position -= right * velocity;
|
|
if (direction == RIGHT)
|
|
position += right * velocity;
|
|
}
|
|
|
|
// processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
|
void processMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
|
{
|
|
xoffset *= mouseSensitivity;
|
|
yoffset *= mouseSensitivity;
|
|
|
|
yaw += xoffset;
|
|
pitch += yoffset;
|
|
|
|
// make sure that when pitch is out of bounds, screen doesn't get flipped
|
|
if (constrainPitch)
|
|
{
|
|
if (pitch > 89.0f)
|
|
pitch = 89.0f;
|
|
|
|
if (pitch < -89.0f)
|
|
pitch = -89.0f;
|
|
}
|
|
|
|
// update Front, Right and Up Vectors using the updated Euler angles
|
|
updateCameraVectors();
|
|
}
|
|
|
|
// processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
|
|
void processMouseScroll(float yoffset)
|
|
{
|
|
zoom -= (float)yoffset;
|
|
if (zoom < 1.0f)
|
|
zoom = 1.0f;
|
|
if (zoom > 45.0f)
|
|
zoom = 45.0f;
|
|
}
|
|
|
|
|
|
private:
|
|
// calculates the front vector from the Camera's (updated) Euler Angles
|
|
void updateCameraVectors()
|
|
{
|
|
// calculate the new Front vector
|
|
glm::vec3 newFront;
|
|
newFront.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
newFront.y = sin(glm::radians(pitch));
|
|
newFront.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
front = glm::normalize(newFront);
|
|
|
|
// also re-calculate the Right and Up vector
|
|
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.
|
|
up = glm::normalize(glm::cross(right, front));
|
|
}
|
|
};
|
|
|
|
#endif |