feat(camera): add projection matrix and accessors

This commit refactors the Camera class to add clarity and better logic:

- Integrates variables directly from constructor to the header for cleaner code
- Adds getters and setters for fov, speed, cameraSensitivity and position
- Duplicates projection matrix from cube to camera
- Adds comments to the header file
- TODO: the projection matrix from the cube loop function must now be replaced by the call to the `glm::mat4 getProjectionMatrix()` camera function
This commit is contained in:
2025-12-08 11:20:05 +01:00
parent a349be7064
commit 0f15a015d4
2 changed files with 75 additions and 23 deletions

View File

@@ -9,23 +9,37 @@ struct GLFWwindow;
class Camera class Camera
{ {
private: private:
int screenWidth; int width;
int screenHeight; int height;
double mousePosX; double mousePosX;
double mousePosY; double mousePosY;
// first mouse detection
bool firstMouse = true; bool firstMouse = true;
float cameraYaw; float cameraYaw = -90.f; // horizontal angle
float cameraPitch; float cameraPitch = 10.f; // vertical angle
glm::vec3 cameraFront; // front camera vector (where the camera is pointed.)
glm::vec3 cameraFront = glm::vec3(0.f, 0.f, -1.f);
// up world vector (absolute up pos)
glm::vec3 worldUp = glm::vec3(0.f, 1.f, 0.f);
// up camera vector (relative up from the camera)
glm::vec3 cameraUp; glm::vec3 cameraUp;
// right camera vector (relative right from the camera)
glm::vec3 cameraRight; glm::vec3 cameraRight;
glm::vec3 worldUp;
// camera position
glm::vec3 cameraPosition = glm::vec3(0.f, 0.f, 0.f);
// glfw window
GLFWwindow* window; GLFWwindow* window;
float speed = 3.f; // move speed
float cameraSensitivity; // mouse sensitivity
float fov = 45.f; // Field Of View
void processInput(float deltaTime); void processInput(float deltaTime);
void processMouseMovement(); void processMouseMovement();
void updateCameraVectors(); void updateCameraVectors();
@@ -34,13 +48,21 @@ class Camera
Camera(int width, int height, GLFWwindow* window, float sensitivity); Camera(int width, int height, GLFWwindow* window, float sensitivity);
void update(float deltaTime); void update(float deltaTime);
glm::mat4 getViewMatrix();
float speed; // view matrix
float cameraSensitivity; glm::mat4 getViewMatrix() const;
float fov; // projection matrix
glm::mat4 getProjectionMatrix() const;
glm::vec3 cameraPosition; float getFov() const { return fov; }
float getSpeed() const { return speed; }
float getCameraSensitivity() const { return cameraSensitivity; }
glm::vec3 getPosition() const { return cameraPosition; }
void setSpeed(float newSpeed);
void setCameraSensitivity(float newSensitivity);
void setFov(float newFov);
void setPosition(const glm::vec3& newPosition);
}; };
#endif #endif

View File

@@ -4,18 +4,10 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity) Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
: screenWidth(width), : width(width),
screenHeight(height), height(height),
window(window), window(window),
cameraYaw(-90.0f), cameraSensitivity(sensitivity)
cameraPitch(10.0f),
speed(3.0f),
cameraSensitivity(sensitivity),
fov(45.0f),
cameraPosition(0.0f, 0.0f, 0.0f),
cameraFront(0.0f, 0.0f, -1.0f),
worldUp(0.0f, 1.0f, 0.0f),
firstMouse(true)
{ {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
updateCameraVectors(); updateCameraVectors();
@@ -28,6 +20,7 @@ void Camera::update(float deltaTime)
updateCameraVectors(); updateCameraVectors();
} }
// TODO: callback management
void Camera::processInput(float deltaTime) void Camera::processInput(float deltaTime)
{ {
float velocity = speed * deltaTime; float velocity = speed * deltaTime;
@@ -97,7 +90,44 @@ void Camera::updateCameraVectors()
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront)); cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
} }
glm::mat4 Camera::getViewMatrix() glm::mat4 Camera::getViewMatrix() const
{ {
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp); return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
} }
glm::mat4 Camera::getProjectionMatrix() const
{
return glm::perspective(
glm::radians(this->fov),
static_cast<float>(this->width) / static_cast<float>(this->height), 0.1f,
100.0f);
}
void Camera::setSpeed(float newSpeed)
{
if (newSpeed > 0.0f)
{
speed = newSpeed;
}
}
void Camera::setCameraSensitivity(float newSensitivity)
{
if (newSensitivity > 0.0f)
{
cameraSensitivity = newSensitivity;
}
}
void Camera::setFov(float newFov)
{
if (newFov > 1.0f && newFov < 179.0f)
{
fov = newFov;
}
}
void Camera::setPosition(const glm::vec3& newPosition)
{
cameraPosition = newPosition;
}