diff options
author | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-19 16:38:56 +1000 |
---|---|---|
committer | omnisci3nce <omniscient.oce@gmail.com> | 2024-07-19 16:41:04 +1000 |
commit | ff825f1e18860d5aa0c761496f1dc0670422715c (patch) | |
tree | bc88851579492007237b125b68811cef947397c0 /src/core | |
parent | efb2d293d5e849fc96e4e386d55a7611795561aa (diff) |
fly camera
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/camera.c | 58 | ||||
-rw-r--r-- | src/core/camera.h | 4 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/core/camera.c b/src/core/camera.c index 428a50d..c0228ba 100644 --- a/src/core/camera.c +++ b/src/core/camera.c @@ -1,5 +1,7 @@ #include "camera.h" +#include "input.h" +#include "keys.h" #include "maths.h" Camera Camera_Create(Vec3 pos, Vec3 front, Vec3 up, f32 fov) { @@ -19,3 +21,59 @@ Mat4 Camera_ViewProj(Camera *c, f32 lens_height, f32 lens_width, Mat4 *out_view, } return mat4_mult(view, proj); } + +void Camera_Update(Camera *camera) { + static f32 yaw = 0.0; + static f32 pitch = 0.0; + + // Keyboard + f32 speed = 0.25; + Vec3 horizontal = vec3_cross(camera->front, camera->up); + if (key_is_pressed(KEYCODE_A) || key_is_pressed(KEYCODE_KEY_LEFT)) { + Vec3 displacement = vec3_mult(horizontal, -speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_D) || key_is_pressed(KEYCODE_KEY_RIGHT)) { + Vec3 displacement = vec3_mult(horizontal, speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_W) || key_is_pressed(KEYCODE_KEY_UP)) { + Vec3 displacement = vec3_mult(camera->front, speed); + camera->position = vec3_add(camera->position, displacement); + } + if (key_is_pressed(KEYCODE_S) || key_is_pressed(KEYCODE_KEY_DOWN)) { + Vec3 displacement = vec3_mult(camera->front, -speed); + camera->position = vec3_add(camera->position, displacement); + } + + // Mouse + if (MouseBtn_Held()) { + mouse_state mouse = Input_GetMouseState(); + // printf("Delta x: %d Delta y %d\n",mouse.x_delta, mouse.y_delta ); + + f32 x_offset = mouse.x_delta; + f32 y_offset = -mouse.y_delta; + + f32 sensitivity = 0.7f; // change this value to your liking + x_offset *= sensitivity; + y_offset *= sensitivity; + + yaw += x_offset; + pitch += y_offset; + + // make sure that when pitch is out of bounds, screen doesn't get flipped + if (pitch > 89.0f) pitch = 89.0f; + if (pitch < -89.0f) pitch = -89.0f; + + Vec3 front; + front.x = cos(deg_to_rad(yaw) * cos(deg_to_rad(pitch))); + front.y = sin(deg_to_rad(pitch)); + front.z = sin(deg_to_rad(yaw)) * cos(deg_to_rad(pitch)); + front = vec3_normalise(front); + camera->front.x = front.x; + camera->front.y = front.y; + camera->front.z = front.z; + } + + // TODO: Right mouse => pan in screen space +}
\ No newline at end of file diff --git a/src/core/camera.h b/src/core/camera.h index 7d2c7ed..450f81e 100644 --- a/src/core/camera.h +++ b/src/core/camera.h @@ -29,6 +29,10 @@ PUB Mat4 Camera_ViewProj(Camera* c, f32 lens_height, f32 lens_width, Mat4* out_v /** @brief Get 2D camera transform matrix */ PUB Mat4 Camera_View2D(Camera* c); // TODO: 2D cameras +struct Input_State; + +void Camera_Update(Camera* camera); + // TODO: (HIGH) Basic reusable camera controls /* Right click + move = pan |