From ff825f1e18860d5aa0c761496f1dc0670422715c Mon Sep 17 00:00:00 2001 From: omnisci3nce Date: Fri, 19 Jul 2024 16:38:56 +1000 Subject: fly camera --- src/core/camera.c | 58 ++++++++++++++++++++++++++++++++ src/core/camera.h | 4 +++ src/new_render/render.c | 2 +- src/ral/backends/opengl/backend_opengl.c | 3 +- src/systems/input.c | 6 ++++ src/systems/input.h | 5 +++ 6 files changed, 76 insertions(+), 2 deletions(-) (limited to 'src') 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 diff --git a/src/new_render/render.c b/src/new_render/render.c index b5311c8..07913f8 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -138,7 +138,7 @@ void Render_RenderEntities(RenderEnt* entities, size_t entity_count) { Renderer* ren = get_renderer(); RenderScene scene = ren->scene; - // TOOD: -- Shadows + // TODO: -- Shadows // f32 near_plane = 1.0, far_plane = 10.0; // Mat4 light_projection = mat4_orthographic(-10.0, 10.0, -10.0, 10.0, near_plane, far_plane); // Vec3 pos = vec3_negate(scene.sun.direction); diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c index 4316e75..10a21d1 100644 --- a/src/ral/backends/opengl/backend_opengl.c +++ b/src/ral/backends/opengl/backend_opengl.c @@ -52,7 +52,8 @@ bool GPU_Backend_Init(const char* window_name, struct GLFWwindow* window, } // All of these are no-ops in OpenGL -void GPU_Backend_Shutdown() {} +void GPU_Backend_Shutdown() { /* TODO */ +} bool GPU_Device_Create(GPU_Device* out_device) { return true; } void GPU_Device_Destroy(GPU_Device* device) {} bool GPU_Swapchain_Create(GPU_Swapchain* out_swapchain) { return true; } diff --git a/src/systems/input.c b/src/systems/input.c index a371ecd..87a0678 100644 --- a/src/systems/input.c +++ b/src/systems/input.c @@ -96,3 +96,9 @@ bool key_is_pressed(keycode key) { return g_input->depressed_keys[key]; } bool key_just_pressed(keycode key) { return g_input->just_pressed_keys[key]; } bool key_just_released(keycode key) { return g_input->just_released_keys[key]; } + +bool MouseBtn_Held() { + return g_input->mouse.prev_left_btn_pressed && g_input->mouse.left_btn_pressed; +} + +mouse_state Input_GetMouseState() { return g_input->mouse; } \ No newline at end of file diff --git a/src/systems/input.h b/src/systems/input.h index f928ed1..ce6e190 100644 --- a/src/systems/input.h +++ b/src/systems/input.h @@ -34,9 +34,14 @@ bool key_just_pressed(keycode key); /** @brief `key` was just released */ bool key_just_released(keycode key); +// TODO: right btn as well +bool MouseBtn_Held(); + // --- Lifecycle bool Input_Init(Input_State *input, struct GLFWwindow *window); void Input_Shutdown(Input_State *input); void Input_Update(Input_State *state); // must be run once per main loop + +PUB mouse_state Input_GetMouseState(); \ No newline at end of file -- cgit v1.2.3-70-g09d2