80 lines
2.5 KiB
C
80 lines
2.5 KiB
C
//
|
|
// Created by Tristan on 10/16/2025.
|
|
//
|
|
|
|
#include "camera.h"
|
|
#include "../..//core/logger/log.h"
|
|
|
|
void camera_init(Camera *self, vec3 initial_position, int width, int height) {
|
|
// copy initial_pos vector
|
|
glm_vec3_copy(initial_position, self->Position);
|
|
|
|
// initialize orientation vector
|
|
glm_vec3_copy((vec3){0.0f, 0.0f, -1.0f}, self->Orientation);
|
|
|
|
// initialize up vector
|
|
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, self->Up);
|
|
|
|
// initialize controls and viewport
|
|
self->speed = 0.1f;
|
|
self->sensitivity = 100.0f;
|
|
self->FOV_deg = 45.0f;
|
|
|
|
self->width = width;
|
|
self->height = height;
|
|
|
|
// Init view matrix to identity (for first frame)
|
|
glm_mat4_identity(self->View);
|
|
}
|
|
|
|
/*
|
|
Camera l_camera = {
|
|
.Position = { 0.0f, 0.0f, 0.0f },
|
|
.Orientation = { 0.0f, 0.0f, -1.0f },
|
|
.Up = { 0.0f, 1.0f, 0.0f }
|
|
};*/
|
|
|
|
void camera_update_view_matrix(Camera *self) {
|
|
// calc the center or target point (Pos + Orientation)
|
|
vec3 target;
|
|
glm_vec3_add(self->Position, self->Orientation, target);
|
|
|
|
// create view matrix
|
|
glm_lookat(self->Position, target, self->Up, self->View);
|
|
}
|
|
|
|
void camera_update_projection(Camera *self, float nearPlane, float farPlane, mat4 dest) {
|
|
float aspect = (float)self->width / (float)self->height;
|
|
glm_perspective(glm_rad(self->FOV_deg), aspect, nearPlane, farPlane, dest);
|
|
}
|
|
|
|
// TODO: Update matrix function signature to accept camera entity?
|
|
// change name to camera_upload_matrix for clarity?
|
|
void camera_matrix_to_shader(Camera *self, GLuint shader_program_ID, const char *uniform_name) {
|
|
// update the view matrix first, camera position might have moved
|
|
camera_update_view_matrix(self);
|
|
|
|
// calculate projection matrix (P) with temporary matrix 'projection' for the result
|
|
mat4 projection;
|
|
camera_update_projection(self, 0.1f, 100.0f, projection);
|
|
|
|
// combine the matrices -- P * V -> PV
|
|
mat4 view_projection;
|
|
glm_mat4_mul(projection, self->View, view_projection);
|
|
|
|
// get uniform location
|
|
GLint uniform_location = glGetUniformLocation(shader_program_ID, uniform_name);
|
|
|
|
if (uniform_location == -1) {
|
|
log_error("Warning: Uniform '%s' not found in shader program %u.\n", uniform_name, shader_program_ID);
|
|
return;
|
|
}
|
|
|
|
// upload combined matrix to the shader
|
|
// view_projection[0] array pointer decays to first value
|
|
glUniformMatrix4fv(uniform_location, 1, GL_FALSE, view_projection[0]);
|
|
}
|
|
|
|
void camera_inputs(Camera *self, SDL_Event *event) {
|
|
// TODO: implementation for handling keyboard/mouse inputs for camera
|
|
} |