diff options
| author | Aaditya Dhruv <[email protected]> | 2026-01-28 14:20:23 -0600 |
|---|---|---|
| committer | Aaditya Dhruv <[email protected]> | 2026-01-28 14:20:23 -0600 |
| commit | 17d2d2589694030f79f2a37732d72d4e433fd745 (patch) | |
| tree | 918cc9f51276c7401c273fe05ad12d10edfaa40f /src/camera.c | |
| parent | 77dddef4153688218bec0b50f547622daa18903d (diff) | |
Add camera handling and movement
Camera related movements have been moved to camera.c - it tracks the
matrices as well so we don't have to call it in block_update
The player class is empty right now, but the camera will be a child of
the player, and the player a child of the engine
Diffstat (limited to 'src/camera.c')
| -rw-r--r-- | src/camera.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..e409b06 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,44 @@ +#include "camera.h" +#include "cglm/cam.h" +#include "cglm/io.h" +#include "cglm/mat4.h" +#include "cglm/vec3.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void camera_init(struct camera** camera) { + struct camera* cam = malloc(sizeof(struct camera)); + memset(cam, 0, sizeof(struct camera)); + vec3 camera_direction = { 0.0f, -1.0f, -5.0f }; + vec3 camera_up = { 0.0f, 1.0f, 0.0f }; + memcpy(cam->direction, camera_direction, sizeof(vec3)); + memcpy(cam->up, camera_up, sizeof(vec3)); + glm_mat4_identity(cam->view); + glm_mat4_identity(cam->perspective); + cam->fov = glm_rad(45.0f); + *camera = cam; +} +void camera_set_position(struct camera* camera, vec3 pos) { + memcpy(camera->position, pos, sizeof(vec3)); +} + +void camera_update(struct camera* camera, struct shader* shader) { + // vec3 camera = { 8.0f, 10.0f, 15.0f }; + // vec3 cam_pivot = { 8.0f, 0.0f, -8.0f }; + glm_look(camera->position, camera->direction, camera->up, camera->view); + // glm_lookat(camera, cam_pivot, axis_y, blk->view); + // glm_rotate_at(blk->view, cam_pivot, angle, axis_y); + // Projection (perspective) matrix + glm_perspective(camera->fov, 800.0f / 600.0f, 0.1f, -10.0f, camera->perspective); + set_uniform_mat4("view", shader, camera->view); + set_uniform_mat4("perspective", shader, camera->perspective); + // fprintf(stderr, "==== Block View ====\n"); + // glm_mat4_print(camera->view, stderr); + // fprintf(stderr, "==== Block Perspective ====\n"); + // glm_mat4_print(camera->perspective, stderr); +} + +void camera_move(struct camera *camera, float *move) { + glm_vec3_add(camera->position, move, camera->position); +} |
