summaryrefslogtreecommitdiff
path: root/src/camera.c
blob: 8aafd14694d8aef42c04ed66dba91e1717a74510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Tuxánci 2 - A first person shooter
 * Copyright (C) 2025-2026  Connor Thomson
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include "ta_math.h"
#include "camera.h"
#include "mouse.h"

ta_camera ta_camera_current;

float ta_camera_sensitivity = 0.0025f;
float ta_camera_fov         = 60.0f;

void ta_camera_init(void) {
    ta_camera_current = (ta_camera){ 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, ta_camera_fov };
}

void ta_camera_update(void) {
    const float pitch_limit = ta_rad(90.0f);

    ta_camera_current.yaw   -= ta_mouse_delta_x * ta_camera_sensitivity;
    ta_camera_current.pitch += ta_mouse_delta_y * ta_camera_sensitivity;

    if (ta_camera_current.pitch > pitch_limit) {
        ta_camera_current.pitch = pitch_limit;
    } else if (ta_camera_current.pitch < -pitch_limit) {
        ta_camera_current.pitch = -pitch_limit;
    }
}

void ta_camera_set_fov(float delta_degrees) {
    ta_camera_current.fov += delta_degrees;

    if (ta_camera_current.fov < 20.0f) {
        ta_camera_current.fov = 20.0f;
    } else if (ta_camera_current.fov > 120.0f) {
        ta_camera_current.fov = 120.0f;
    }
}

void ta_camera_get_view_matrix(float *matrix) {
    float yaw_cosine   = cosf(ta_camera_current.yaw);
    float yaw_sine     = sinf(ta_camera_current.yaw);
    float pitch_cosine = cosf(ta_camera_current.pitch);
    float pitch_sine   = sinf(ta_camera_current.pitch);

    matrix[0]  = yaw_cosine;
    matrix[1]  = -pitch_sine * yaw_sine;
    matrix[2]  = pitch_cosine * yaw_sine;
    matrix[3]  = 0.0f;
    matrix[4]  = 0.0f;
    matrix[5]  = pitch_cosine;
    matrix[6]  = pitch_sine;
    matrix[7]  = 0.0f;
    matrix[8]  = -yaw_sine;
    matrix[9]  = -pitch_sine * yaw_cosine;
    matrix[10] = pitch_cosine * yaw_cosine;
    matrix[11] = 0.0f;
    matrix[12] = -yaw_cosine * ta_camera_current.x + yaw_sine * ta_camera_current.z;
    matrix[13] = pitch_sine * yaw_sine * ta_camera_current.x - pitch_cosine * ta_camera_current.y + pitch_sine * yaw_cosine * ta_camera_current.z;
    matrix[14] = -pitch_cosine * yaw_sine * ta_camera_current.x - pitch_sine * ta_camera_current.y - pitch_cosine * yaw_cosine * ta_camera_current.z;
    matrix[15] = 1.0f;
}