From 4ffb872e48a0e9eada805c6b8f7567319b7e3f53 Mon Sep 17 00:00:00 2001 From: jvech Date: Thu, 29 Jun 2023 08:22:31 -0500 Subject: add: camera module implemented now the camera has its oown properties and functions It should ease its management --- src/camera.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/camera.c (limited to 'src/camera.c') diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..f087d83 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,80 @@ +#include "camera.h" +static void cameraUpdateVectors(struct Camera *camera); + +Mat4 +cameraGetViewMatrix(struct Camera camera) +{ + Vec3 target = linearVec3Add(camera.position, camera.front); + return linearLookAt(camera.position, target, camera.up); +} + +void +cameraProcessKeyboard( + struct Camera *camera, + enum CameraMovement direction, + float deltaTime) +{ + float speed = camera->movementSpeed * deltaTime; + Vec3 tmp; + + switch (direction) { + case CAMDIR_FORWARD: + // pos = pos + front * speed + tmp = linearVec3ScalarMulp(camera->front, speed); + camera->position = linearVec3Add(camera->position, tmp); + break; + + case CAMDIR_BACKWARD: + // pos = pos + front * (-speed) + tmp = linearVec3ScalarMulp(camera->front, -speed); + camera->position = linearVec3Add(camera->position, tmp); + break; + + case CAMDIR_LEFT: + // pos = pos + unit|(up x front)| * (speed) + tmp = linearVec3CrossProduct(camera->front, camera->up); + tmp = linearVec3Normalize(tmp); + tmp = linearVec3ScalarMulp(tmp, -speed); + camera->position = linearVec3Add(camera->position, tmp); + break; + + case CAMDIR_RIGHT: + // pos = pos + unit|(up x front)| * (-speed) + tmp = linearVec3CrossProduct(camera->front, camera->up); + tmp = linearVec3Normalize(tmp); + tmp = linearVec3ScalarMulp(tmp, speed); + camera->position = linearVec3Add(camera->position, tmp); + break; + + default: + break; + } +} + +void +cameraProcessMouseMovement(struct Camera *camera, float xoffset, float yoffset) +{ + camera->yaw += camera->mouseSensivity * xoffset; + camera->pitch += camera->mouseSensivity * yoffset; + + if (camera->pitch > 89.0f) + camera->pitch = 89.0f; + else if (camera->pitch < -89.0f) + camera->pitch = - 89.0f; + cameraUpdateVectors(camera); +} + +void +cameraUpdateVectors(struct Camera *camera) +{ + Vec3 tmp; + float rpitch = M_PI / 180 * camera->pitch; + float ryaw = M_PI / 180 * camera->yaw; + + tmp = linearVec3( + cosf(ryaw) * cosf(-rpitch), + sinf(-rpitch), + sinf(ryaw) * cosf(-rpitch)); + camera->front = linearVec3Normalize(tmp); + +} -- cgit v1.2.3-70-g09d2