From 199eeb4ee1f7df513fa1cd109df59f53c9433575 Mon Sep 17 00:00:00 2001 From: Warwick Date: Mon, 18 Aug 2025 12:52:44 +0100 Subject: [PATCH] Created a camera system that has changing mvp and setting the uniform Now to correct whatever is wrong with it. --- CMakeLists.txt | 1 + shaders/vert.glsl | 4 ++- src/camera.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++ src/camera.h | 21 ++++++++++++ src/main.c | 17 ++++++++- 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 src/camera.c create mode 100644 src/camera.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a88bdd..43108ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,6 +88,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE ) target_link_libraries(${PROJECT_NAME} PRIVATE + m ${SDL3_LIBRARIES} ${SDL3_image_LIBRARIES} ${CGLM_LIBRARIES} diff --git a/shaders/vert.glsl b/shaders/vert.glsl index 2242931..b69fe72 100644 --- a/shaders/vert.glsl +++ b/shaders/vert.glsl @@ -1,8 +1,10 @@ #version 330 core layout (location = 0) in vec3 aPos; +uniform mat4 MVP; + void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0) * MVP; } diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..037be68 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,87 @@ +#include "camera.h" +#include "SDL3/SDL_keyboard.h" +#include "cglm/mat4.h" +#include "cglm/cam.h" +#include "cglm/vec3.h" +#include +#include +#include + +static float mouse_x, mouse_y; + +Camera Camera_default = { + .position = {0.0f, 0.0f, 3.0f}, + .forward = {0.0f, 0.0f, -1.0f}, + .yaw = -1.5707963268f, // -90 deg in radians + .pitch = 0 +}; + +void camera_tick(Camera *camera) { + // Mouse input + SDL_GetRelativeMouseState(&mouse_x, &mouse_y); + camera->yaw += mouse_x * glm_rad(MOUSE_SENSITIVITY); + camera->pitch += mouse_y * glm_rad(MOUSE_SENSITIVITY); + // Restrict pitch + if (camera->pitch > 89.0f) + camera->pitch = 89.0f; + if (camera->pitch < -89.0f) + camera->pitch = -89.0f; + + // Camera Direction + camera->forward[0] = cos(camera->yaw) * cos(camera->pitch); + camera->forward[1] = sin(camera->pitch); + camera->forward[2] = sin(camera->yaw) * cos(camera->pitch); + glm_normalize(camera->forward); + vec3 camera_right = GLM_VEC3_ZERO_INIT; + vec3 up = GLM_VEC3_ZERO_INIT; + up[2] = 1.0f; + glm_cross(up, camera->forward, camera_right); + glm_normalize(camera_right); + + // Move camera based on wasd; + const bool *keyboardState = SDL_GetKeyboardState(NULL); + if (keyboardState[SDL_SCANCODE_W]) { + vec3 calc_buff; + glm_vec3_scale(camera->forward, MOVEMENT_SPEED, calc_buff); + glm_vec3_add(camera->position, calc_buff, camera->position); + } + if (keyboardState[SDL_SCANCODE_S]) { + vec3 calc_buff; + glm_vec3_scale(camera->forward, MOVEMENT_SPEED, calc_buff); + glm_vec3_sub(camera->position, calc_buff, camera->position); + } + if (keyboardState[SDL_SCANCODE_A]) { + vec3 calc_buff; + glm_vec3_scale(camera_right, MOVEMENT_SPEED, calc_buff); + glm_vec3_add(camera->position, calc_buff, camera->position); + } + if (keyboardState[SDL_SCANCODE_D]) { + vec3 calc_buff; + glm_vec3_scale(camera_right, MOVEMENT_SPEED, calc_buff); + glm_vec3_sub(camera->position, calc_buff, camera->position); + } +} + +void calc_camera_mvp(mat4 *mvp_result, Camera *camera) { + // Get position to look and up vector + vec3 camera_target; + glm_vec3_add(camera->position , camera->forward, camera_target); + vec3 up = GLM_VEC3_ZERO_INIT; + up[2] = 1.0f; + + mat4 model; + glm_mat4_identity(model); + + mat4 view; + glm_lookat(camera->position, camera_target, up, view); + + mat4 projection; + float fov = glm_rad(45.0f); // fov y + float aspect = 800.0f / 600.0f; + float near = 0.1f; + float far = 100.0f; + glm_perspective(fov, aspect, near, far, projection); + + glm_mat4_mul(model, view, *mvp_result); + glm_mat4_mul(*mvp_result, projection, *mvp_result); +} diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 0000000..0cc6e4c --- /dev/null +++ b/src/camera.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +#define MOVEMENT_SPEED 0.02f +#define MOUSE_SENSITIVITY 0.05f + +struct Camera_s { + vec3 position; + vec3 forward; + float yaw; + float pitch; +}; +typedef struct Camera_s Camera; +extern Camera Camera_default; + +void calc_camera_mvp (mat4 *mvp_result, Camera *camera); + +void get_camera_position (vec3 *position_result, Camera *camera); + +void camera_tick(Camera *camera); diff --git a/src/main.c b/src/main.c index f4abd11..4665fec 100644 --- a/src/main.c +++ b/src/main.c @@ -3,9 +3,11 @@ #include #include #include +#include #include -#include "arena_allocator.h" +#include "camera.h" +#include "cglm/io.h" // settings const unsigned int SCR_WIDTH = 800; @@ -142,6 +144,9 @@ int main(int argc, char *argv[]) { // VBOs) when it's not directly necessary. glBindVertexArray(0); + // Initialise camera + Camera camera = Camera_default; + while (1) { // handle input SDL_PollEvent(&event); @@ -149,6 +154,16 @@ int main(int argc, char *argv[]) { break; } + // Handle camera + camera_tick(&camera); + + // TODO: make shader file + mat4 mvp; + calc_camera_mvp(&mvp, &camera); + glm_mat4_print(mvp,stdout); + int mvp_uniform_location = glGetUniformLocation(shaderProgram, "MVP"); + glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]); + // render glClearColor(0.1f, 0.3f, 0.4f, 1.f); glClear(GL_COLOR_BUFFER_BIT);