aboutsummaryrefslogtreecommitdiff
path: root/src/camera.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/camera.c')
-rw-r--r--src/camera.c80
1 files changed, 80 insertions, 0 deletions
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);
+
+}
Feel free to download, copy and edit any repo