diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-03 13:49:44 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-03 13:49:44 +0100 |
commit | a1e93038af0a739b9af57bd40ffbee6963dc3242 (patch) | |
tree | eebceb8f833c55fe8c9a1e15d4c26afd7defdfbc /camera.c | |
parent | 362f70b04da8f8e9c0c159527bce7da65591c260 (diff) | |
download | faqe-a1e93038af0a739b9af57bd40ffbee6963dc3242.tar.gz faqe-a1e93038af0a739b9af57bd40ffbee6963dc3242.tar.xz faqe-a1e93038af0a739b9af57bd40ffbee6963dc3242.zip |
Add a simple camera struct and camera controls
Diffstat (limited to 'camera.c')
-rw-r--r-- | camera.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/camera.c b/camera.c new file mode 100644 index 0000000..6335d0e --- /dev/null +++ b/camera.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com> + * SPDX-License-Identifier: MIT + */ +#include <assert.h> +#include <math.h> +#include <stdlib.h> + +#include "camera.h" +#include "math.h" + +#define PITCH_LIMIT (PI * 0.99) + +void camera_clamp(struct camera *cam) +{ + assert(cam != NULL); + + cam->yaw = fmod(cam->yaw, 2 * PI); + if (cam->pitch > PITCH_LIMIT / 2) + cam->pitch = PITCH_LIMIT / 2; + else if (cam->pitch < -PITCH_LIMIT / 2) + cam->pitch = -PITCH_LIMIT / 2; +} +void camera_dir(vec3 out, struct camera *cam) +{ + assert(out != NULL); + assert(cam != NULL); + + out[0] = cosf(cam->pitch) * cosf(cam->yaw); + out[1] = sinf(cam->pitch); + out[2] = cosf(cam->pitch) * sinf(cam->yaw); +} +void camera_lookat(mat4x4 out, struct camera *cam) +{ + vec3 at, dir; + + assert(out != NULL); + assert(cam != NULL); + + camera_dir(dir, cam); + vec3_add(at, cam->pos, dir); + mat4x4_look_at(out, cam->pos, at, (vec3){ 0, 1, 0 }); +} |