From c322c17a69fc756b494124d4deab6909ee9134cb Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 22 Jun 2023 20:30:29 -0400 Subject: implement 2d camera system --- include/rodeo/gfx.h | 13 +++++++++++++ include/rodeo/gfx_t.h | 11 +++++++++++ src/gfx/irodeo_gfx.h | 6 ++++++ src/gfx/irodeo_gfx_t.h | 1 + src/gfx/rodeo_gfx.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/include/rodeo/gfx.h b/include/rodeo/gfx.h index 15ce3ad..07d8430 100644 --- a/include/rodeo/gfx.h +++ b/include/rodeo/gfx.h @@ -147,6 +147,19 @@ rodeo_gfx_matrix_size(void); uint32_t rodeo_gfx_matrix_capacity(void); +void +rodeo_gfx_camera_2d_begin(rodeo_gfx_camera_2d_t camera); + +void +rodeo_gfx_camera_2d_end(void); + +#define \ +mrodeo_gfx_camera_do(camera) \ + mrodeo_defer_do( \ + rodeo_gfx_camera_2d_begin(camera), \ + rodeo_gfx_camera_2d_end() \ + ) + #define \ mrodeo_gfx_scissor_do(rectangle) \ mrodeo_defer_do( \ diff --git a/include/rodeo/gfx_t.h b/include/rodeo/gfx_t.h index 63811c5..3d206c2 100644 --- a/include/rodeo/gfx_t.h +++ b/include/rodeo/gfx_t.h @@ -3,6 +3,7 @@ // -- internal -- // public #include "rodeo_types.h" +#include "rodeo/math/vec2_t.h" // -- system -- #include @@ -67,3 +68,13 @@ rodeo_gfx_vertex_t; typedef uint16_t rodeo_gfx_index_t; +typedef +struct +{ + rodeo_math_vec2_t target; + float turns; + float zoom; + rodeo_math_vec2_t offset; +} +rodeo_gfx_camera_2d_t; + diff --git a/src/gfx/irodeo_gfx.h b/src/gfx/irodeo_gfx.h index 25a65e4..5634f91 100644 --- a/src/gfx/irodeo_gfx.h +++ b/src/gfx/irodeo_gfx.h @@ -24,6 +24,12 @@ irodeo_gfx_dimensions_extra_update(void); irodeo_gfx_dimensions_extra_t irodeo_gfx_dimensions_extra_get(void); +void +irodeo_gfx_camera_2d_init(void); + +void +irodeo_gfx_camera_2d_deinit(void); + #define \ mrodeo_bgfx_vertex_layout_do(vertex_layout) \ mrodeo_defer_do( \ diff --git a/src/gfx/irodeo_gfx_t.h b/src/gfx/irodeo_gfx_t.h index 1aa1778..7616db3 100644 --- a/src/gfx/irodeo_gfx_t.h +++ b/src/gfx/irodeo_gfx_t.h @@ -61,6 +61,7 @@ struct irodeo_gfx_dimensions_extra_t dimensions_extra; rodeo_math_mat4_t matrix_stack_top; stc_gfx_matrix_stack matrix_stack; + rodeo_math_mat4_t camera_2d_matrix; } irodeo_gfx_state_t; diff --git a/src/gfx/rodeo_gfx.c b/src/gfx/rodeo_gfx.c index b702c26..f922675 100644 --- a/src/gfx/rodeo_gfx.c +++ b/src/gfx/rodeo_gfx.c @@ -5,6 +5,7 @@ #include "rodeo/gfx.h" #include "rodeo/window.h" #include "rodeo/log.h" +#include "rodeo/math.h" // private #include "gfx/irodeo_gfx.h" #include "window/irodeo_window.h" @@ -183,6 +184,7 @@ rodeo_gfx_init(float width, float height) ); irodeo_gfx_matrix_init(); + irodeo_gfx_camera_2d_init(); irodeo_gfx_state.frame_end = (uint32_t)SDL_GetPerformanceCounter(); } @@ -190,6 +192,7 @@ rodeo_gfx_init(float width, float height) void rodeo_gfx_deinit(void) { + irodeo_gfx_camera_2d_deinit(); irodeo_gfx_matrix_deinit(); free(irodeo_gfx_state.default_texture.data); @@ -407,6 +410,8 @@ irodeo_gfx_frame_stall(void) } } +#include "math/irodeo_mat4.h" + void rodeo_gfx_renderer_flush(void) { @@ -467,7 +472,13 @@ rodeo_gfx_renderer_flush(void) // irodeo_gfx_state.matrix_stack_top //); //bgfx_set_transform(&result_matrix.raw, 1); - bgfx_set_transform(&irodeo_gfx_state.matrix_stack_top, 1); + + //irodeo_print_matrix(irodeo_gfx_state.camera_2d_matrix); + rodeo_math_mat4_t transform = rodeo_math_mat4_multiply( + irodeo_gfx_state.matrix_stack_top, + irodeo_gfx_state.camera_2d_matrix + ); + bgfx_set_transform(&transform, 1); // submit vertices & batches bgfx_submit(0, irodeo_gfx_state.program_shader, 0, BGFX_DISCARD_NONE); @@ -999,6 +1010,42 @@ rodeo_gfx_matrix_capacity(void) return (uint32_t)stc_gfx_matrix_stack_capacity(&irodeo_gfx_state.matrix_stack); } +void +irodeo_gfx_camera_2d_init(void) +{ + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_identity(); +} + +void +irodeo_gfx_camera_2d_deinit(void) +{ + // nothing to do +} + +void +rodeo_gfx_camera_2d_begin(rodeo_gfx_camera_2d_t camera) +{ + rodeo_gfx_renderer_flush(); + + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_translate(irodeo_gfx_state.camera_2d_matrix, (rodeo_math_vec3_t){ .val.x = -camera.target.val.x, .val.y = -camera.target.val.y, .val.z = 0.0f }); + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_rotate(irodeo_gfx_state.camera_2d_matrix, camera.turns, (rodeo_math_vec3_t){ .val.z = 1 }); + //rodeo_math_mat4_t matRotation = MatrixRotate((rodeo_math_vec3_t){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_scale(irodeo_gfx_state.camera_2d_matrix, (rodeo_math_vec3_t){ .val.x = camera.zoom, .val.y = camera.zoom, .val.z = 1.0f }); + //rodeo_math_mat4_t matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_translate(irodeo_gfx_state.camera_2d_matrix, (rodeo_math_vec3_t){ .val.x = camera.offset.val.x, .val.y = camera.offset.val.y }); + //rodeo_math_mat4_t matTranslation = MatrixTranslate(camera.offset.x, camera.offset.y, 0.0f); + + // build matrix +} + +void +rodeo_gfx_camera_2d_end(void) +{ + rodeo_gfx_renderer_flush(); + + irodeo_gfx_state.camera_2d_matrix = rodeo_math_mat4_identity(); +} + bgfx_shader_handle_t irodeo_gfx_shader_load(const cstr path) { -- cgit v1.2.3