Created
August 22, 2021 13:05
-
-
Save Pikachuxxxx/2e79b0ccdcfebe269814daaa208895fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Camera3D.h" | |
// Constructor with vectors | |
Camera3D::Camera3D(glm::vec3 position, glm::vec3 up, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) | |
{ | |
this->Position = position; | |
this->WorldUp = up; | |
this->Yaw = yaw; | |
this->Pitch = pitch; | |
this->updateCameraVectors(); | |
} | |
// Constructor with scalar values | |
Camera3D::Camera3D(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) | |
{ | |
this->Position = glm::vec3(posX, posY, posZ); | |
this->WorldUp = glm::vec3(upX, upY, upZ); | |
this->Yaw = yaw; | |
this->Pitch = pitch; | |
this->updateCameraVectors(); | |
} | |
void Camera3D::Update(Window& window, float deltaTime) | |
{ | |
if (window.isKeyHeld(GLFW_KEY_W) || window.isKeyHeld(GLFW_KEY_UP)) | |
ProcessKeyboard(FORWARD, deltaTime); | |
else if (window.isKeyHeld(GLFW_KEY_S) || window.isKeyHeld(GLFW_KEY_DOWN)) | |
ProcessKeyboard(BACKWARD, deltaTime); | |
if (window.isKeyHeld(GLFW_KEY_D) || window.isKeyHeld(GLFW_KEY_RIGHT)) | |
ProcessKeyboard(RIGHT, deltaTime); | |
else if (window.isKeyHeld(GLFW_KEY_A) || window.isKeyHeld(GLFW_KEY_LEFT)) | |
ProcessKeyboard(LEFT, deltaTime); | |
if (window.isMouseButtonHeld(GLFW_MOUSE_BUTTON_RIGHT)) | |
{ | |
ProcessMouseMovement(window.deltaMouseX, window.deltaMouseY); | |
window.deltaMouseX = 0.0f; | |
window.deltaMouseY = 0.0f; | |
} | |
} | |
// Returns the view matrix calculated using Euler Angles and the LookAt Matrix | |
glm::mat4 Camera3D::GetViewMatrix() | |
{ | |
return glm::lookAt(this->Position, this->Position + this->Front, this->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 Camera3D::ProcessKeyboard(Camera_Movement direction, float deltaTime) | |
{ | |
float velocity = this->MovementSpeed * deltaTime; | |
if (direction == FORWARD) | |
this->Position += this->Front * velocity; | |
if (direction == BACKWARD) | |
this->Position -= this->Front * velocity; | |
if (direction == LEFT) | |
this->Position -= this->Right * velocity; | |
if (direction == RIGHT) | |
this->Position += this->Right * velocity; | |
} | |
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction. | |
void Camera3D::ProcessMouseMovement(float xoffset, float yoffset, bool constrainPitch) | |
{ | |
xoffset *= this->MouseSensitivity; | |
yoffset *= this->MouseSensitivity; | |
this->Yaw += xoffset; | |
this->Pitch += yoffset; | |
// Make sure that when pitch is out of bounds, screen doesn't get flipped | |
if (constrainPitch) | |
{ | |
if (this->Pitch > 89.0f) | |
this->Pitch = 89.0f; | |
if (this->Pitch < -89.0f) | |
this->Pitch = -89.0f; | |
} | |
// Update Front, Right and Up Vectors using the updated Eular angles | |
this->updateCameraVectors(); | |
} | |
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis | |
void Camera3D::ProcessMouseScroll(float yoffset) | |
{ | |
if (this->Zoom >= 1.0f && this->Zoom <= 45.0f) | |
this->Zoom -= yoffset; | |
if (this->Zoom <= 1.0f) | |
this->Zoom = 1.0f; | |
if (this->Zoom >= 45.0f) | |
this->Zoom = 45.0f; | |
} | |
// Calculates the front vector from the Camera's (updated) Eular Angles | |
void Camera3D::updateCameraVectors() | |
{ | |
// Calculate the new Front vector | |
glm::vec3 front; | |
front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); | |
front.y = sin(glm::radians(this->Pitch)); | |
front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); | |
this->Front = glm::normalize(front); | |
// Also re-calculate the Right and Up vector | |
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. | |
this->Up = glm::normalize(glm::cross(this->Right, this->Front)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
// Std. Includes | |
#include <vector> | |
#include <glm/glm.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include "Window.h" | |
// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods | |
enum Camera_Movement { | |
FORWARD, | |
BACKWARD, | |
LEFT, | |
RIGHT | |
}; | |
// Default camera values | |
const float YAW = -90.0f; | |
const float PITCH = 0.0f; | |
const float SPEED = 0.01f; | |
const float SENSITIVTY = 0.50f; | |
const float ZOOM = 45.0f; | |
class Camera3D | |
{ | |
public: | |
// Camera Attributes | |
glm::vec3 Position; | |
glm::vec3 Front; | |
glm::vec3 Up; | |
glm::vec3 Right; | |
glm::vec3 WorldUp; | |
// Euler Angles | |
float Yaw; | |
float Pitch; | |
// Camera options | |
float MovementSpeed; | |
float MouseSensitivity; | |
float Zoom; | |
public: | |
// Constructor with vectors | |
Camera3D(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); | |
// Constructor with scalar values | |
Camera3D(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch); | |
// Update the camera movement in the world space | |
void Update(Window& window, float deltaTime); | |
// Returns the view matrix calculated using Euler Angles and the LookAt Matrix | |
glm::mat4 GetViewMatrix(); | |
// 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); | |
// 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, bool constrainPitch = true); | |
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis | |
void ProcessMouseScroll(float yoffset); | |
private: | |
// Calculates the front vector from the Camera's (updated) Euler Angles | |
void updateCameraVectors(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment