107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
|
|
#include "Camera.h"
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
// constructor with vectors
|
|
Camera::Camera(glm::vec3 newPosition, glm::vec3 up, float newYaw, float newPitch) : 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::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 Camera::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 Camera::processKeyboard(Camera_Movement direction, float deltaTime)
|
|
{
|
|
float velocity = movementSpeed * deltaTime;
|
|
if (sprinting)
|
|
velocity *= 8;
|
|
|
|
if (direction == FORWARD)
|
|
position += front * velocity;
|
|
if (direction == BACKWARD)
|
|
position -= front * velocity;
|
|
if (direction == LEFT)
|
|
position -= right * velocity;
|
|
if (direction == RIGHT)
|
|
position += right * velocity;
|
|
if (direction == SPRINT)
|
|
sprinting = !sprinting;
|
|
}
|
|
|
|
// processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
|
void Camera::processMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch)
|
|
{
|
|
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 Camera::processMouseScroll(float yoffset)
|
|
{
|
|
zoom -= (float)yoffset;
|
|
if (zoom < 1.0f)
|
|
zoom = 1.0f;
|
|
if (zoom > 45.0f)
|
|
zoom = 45.0f;
|
|
}
|
|
|
|
void Camera::Update(Shader shader, int screenWidth, int screenHeight)
|
|
{
|
|
glm::mat4 projection = glm::perspective(glm::radians(zoom), (float)screenWidth / (float)screenHeight, 0.1f, 100.0f);
|
|
|
|
shader.setMat4("projection", projection);
|
|
shader.setMat4("view", GetViewMatrix());
|
|
shader.setVec3("viewPos", position);
|
|
}
|
|
|
|
// calculates the front vector from the Camera's (updated) Euler Angles
|
|
void Camera::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));
|
|
}
|
|
|
|
|