diff options
Diffstat (limited to 'src/camera.c')
| -rw-r--r-- | src/camera.c | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/src/camera.c b/src/camera.c index ffb63cb..2699841 100644 --- a/src/camera.c +++ b/src/camera.c @@ -17,21 +17,52 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include <stdbool.h> +#include "ta_math.h" #include "camera.h" -#include "ta_sdl.h" +#include "mouse.h" ta_camera ta_camera_current; +float ta_camera_sensitivity = 0.0025f; + void ta_camera_init(void) { - ta_camera_current = (ta_camera){ 0.0f, 0.0f, 5.0f }; + ta_camera_current = (ta_camera){ 0.0f, 0.0f, 5.0f, 0.0f, 0.0f }; } -void ta_camera_get_view_matrix(float *matrix) { - for (int index = 0; index < 16; index++) { - matrix[index] = index % 5 == 0 ? 1.0f : 0.0f; +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_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[12] = -ta_camera_current.x; - matrix[13] = -ta_camera_current.y; - matrix[14] = -ta_camera_current.z; + 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; }
\ No newline at end of file |