feat: establish stable OpenGL 3.3 core graphics conxtext
initializes the full graphics context pipeline required for modern OpenGL rendering
This commit is contained in:
@@ -3,34 +3,78 @@
|
||||
//
|
||||
|
||||
#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 matrix(float FOVdeg, float nearPlane, float farPlane, Shader* shader_instance, const char* uniform) {
|
||||
mat4 view, projection;
|
||||
glm_mat4_identity(view);
|
||||
glm_mat4_identity(projection);
|
||||
|
||||
// store the result of the vector addition
|
||||
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);
|
||||
|
||||
// cglm function to perform vector addition
|
||||
glm_vec3_add(l_camera.Position, l_camera.Orientation, target);
|
||||
|
||||
mat4 target_2;
|
||||
|
||||
glm_mat4_mul(projection[0], view[0], target_2);
|
||||
|
||||
// now we can use the addition of the two vec3's in the glm_lookat() function
|
||||
glm_lookat(l_camera.Position, target, l_camera.Up, view);
|
||||
|
||||
glm_perspective(glm_rad(FOVdeg), (float)(l_camera.width / l_camera.height), nearPlane, farPlane, projection);
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader_instance->ID, uniform), 1, GL_FALSE, target_2);
|
||||
// create view matrix
|
||||
glm_lookat(self->Position, target, self->Up, self->View);
|
||||
}
|
||||
|
||||
void inputs(SDL_Window* window);
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user