summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromnisci3nce <omniscient.oce@gmail.com>2024-07-19 16:38:56 +1000
committeromnisci3nce <omniscient.oce@gmail.com>2024-07-19 16:38:56 +1000
commit6484c94d4efa391c4cefc9538b2ff74fa3207e00 (patch)
tree98b6f7c4ae864f2b935dbe40a1a4c3911ad437c1
parentefb2d293d5e849fc96e4e386d55a7611795561aa (diff)
fly camera
-rw-r--r--README.md3
-rw-r--r--examples/game_demo/game_demo.c2
-rw-r--r--src/core/camera.c58
-rw-r--r--src/core/camera.h4
-rw-r--r--src/new_render/render.c2
-rw-r--r--src/ral/backends/opengl/backend_opengl.c3
-rw-r--r--src/systems/input.c6
-rw-r--r--src/systems/input.h5
8 files changed, 80 insertions, 3 deletions
diff --git a/README.md b/README.md
index 42c4f46..1040510 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ All third-party dependencies are licensed under their own license.
## TODO
#### Engine
-= [ ] Embedded assets - shaders, textures, et al (probably will use C23's `#embed`?)
+- [ ] Embedded assets - shaders, textures, et al (probably will use C23's `#embed`?)
- [ ] Shader hot-reloading
- [ ] Cross-platform threadpool
- Strings
@@ -71,6 +71,7 @@ All third-party dependencies are licensed under their own license.
- [ ] PCF
- [ ] Cascading shadowmaps (CSM)
- [ ] Point light shadows
+- [ ] Resizing viewport
- [ ] Cel shading
- [ ] Terrain
- [ ] Heightmaps
diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c
index 1f88503..cb969cf 100644
--- a/examples/game_demo/game_demo.c
+++ b/examples/game_demo/game_demo.c
@@ -86,6 +86,8 @@ int main() {
while (!ShouldExit()) {
Frame_Begin();
+ Camera_Update(&cam);
+ SetCamera(cam);
// BEGIN Draw calls
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