From a4aca2a3c3084882eb2a3912ef3f87e02153c74a Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 22 Jun 2023 18:10:26 -0400 Subject: implemented matrix stacks --- include/rodeo/gfx.h | 25 ++++++++++++++++ include/rodeo/math_t.h | 2 ++ src/gfx/irodeo_gfx_t.h | 8 ++++- src/gfx/rodeo_gfx.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/include/rodeo/gfx.h b/include/rodeo/gfx.h index afa99a2..15ce3ad 100644 --- a/include/rodeo/gfx.h +++ b/include/rodeo/gfx.h @@ -4,6 +4,7 @@ // public #include "rodeo/gfx_t.h" #include "rodeo/math/vec2_t.h" +#include "rodeo/math/mat4_t.h" // -- external -- #include "stc/cstr.h" @@ -122,6 +123,30 @@ rodeo_gfx_scissor_begin(rodeo_rectangle_t rectangle); void rodeo_gfx_scissor_end(void); +void +irodeo_gfx_matrix_init(void); + +void +irodeo_gfx_matrix_deinit(void); + +void +rodeo_gfx_matrix_set(rodeo_math_mat4_t matrix); + +rodeo_math_mat4_t +rodeo_gfx_matrix_get(void); + +void +rodeo_gfx_matrix_push(void); + +void +rodeo_gfx_matrix_pop(void); + +uint32_t +rodeo_gfx_matrix_size(void); + +uint32_t +rodeo_gfx_matrix_capacity(void); + #define \ mrodeo_gfx_scissor_do(rectangle) \ mrodeo_defer_do( \ diff --git a/include/rodeo/math_t.h b/include/rodeo/math_t.h index 9374c2a..19fe4bf 100644 --- a/include/rodeo/math_t.h +++ b/include/rodeo/math_t.h @@ -3,3 +3,5 @@ // --- internal --- // public #include "rodeo/math/vec2_t.h" +#include "rodeo/math/vec3_t.h" +#include "rodeo/math/mat4_t.h" diff --git a/src/gfx/irodeo_gfx_t.h b/src/gfx/irodeo_gfx_t.h index e6b2555..1aa1778 100644 --- a/src/gfx/irodeo_gfx_t.h +++ b/src/gfx/irodeo_gfx_t.h @@ -3,7 +3,7 @@ // -- internal -- // public #include "rodeo/gfx_t.h" -#include "rodeo/math.h" +#include "rodeo/math_t.h" // -- external -- #include "bgfx/c99/bgfx.h" @@ -22,6 +22,10 @@ struct } irodeo_gfx_dimensions_extra_t; +#define i_type stc_gfx_matrix_stack +#define i_val rodeo_math_mat4_t +#include + typedef struct { @@ -55,6 +59,8 @@ struct uint32_t frame_end; float frame_time; irodeo_gfx_dimensions_extra_t dimensions_extra; + rodeo_math_mat4_t matrix_stack_top; + stc_gfx_matrix_stack matrix_stack; } irodeo_gfx_state_t; diff --git a/src/gfx/rodeo_gfx.c b/src/gfx/rodeo_gfx.c index 52345dc..b702c26 100644 --- a/src/gfx/rodeo_gfx.c +++ b/src/gfx/rodeo_gfx.c @@ -182,12 +182,16 @@ rodeo_gfx_init(float width, float height) BGFX_TEXTURE_FORMAT_COUNT ); + irodeo_gfx_matrix_init(); + irodeo_gfx_state.frame_end = (uint32_t)SDL_GetPerformanceCounter(); } void rodeo_gfx_deinit(void) { + irodeo_gfx_matrix_deinit(); + free(irodeo_gfx_state.default_texture.data); bgfx_destroy_program(irodeo_gfx_state.program_shader); @@ -456,7 +460,14 @@ rodeo_gfx_renderer_flush(void) //bgfx_set_dynamic_index_buffer(irodeo_gfx_state.index_buffer_handle, 0, irodeo_gfx_state.index_size); //const bgfx_memory_t* ibm = bgfx_copy(irodeo_gfx_state.batched_indices, sizeof(uint16_t) * irodeo_gfx_state.index_size); //bgfx_update_dynamic_index_buffer(irodeo_gfx_state.index_buffer_handle, 0, ibm); - + + // apply matrices + //rodeo_math_mat4_t result_matrix = rodeo_math_mat4_multiply( + // irodeo_gfx_state.proj_matrix, + // irodeo_gfx_state.matrix_stack_top + //); + //bgfx_set_transform(&result_matrix.raw, 1); + bgfx_set_transform(&irodeo_gfx_state.matrix_stack_top, 1); // submit vertices & batches bgfx_submit(0, irodeo_gfx_state.program_shader, 0, BGFX_DISCARD_NONE); @@ -921,6 +932,73 @@ rodeo_gfx_scissor_end(void) bgfx_set_scissor_cached(UINT16_MAX); } +// matrix design: by default start with first one being identity and pushed +// popping past the first one resets it to idenity + +void +irodeo_gfx_matrix_init(void) +{ + irodeo_gfx_state.matrix_stack = stc_gfx_matrix_stack_with_capacity(32); + irodeo_gfx_state.matrix_stack_top = rodeo_math_mat4_identity(); + stc_gfx_matrix_stack_push(&irodeo_gfx_state.matrix_stack, rodeo_gfx_matrix_get()); + +} + +void +irodeo_gfx_matrix_deinit(void) +{ + stc_gfx_matrix_stack_drop(&irodeo_gfx_state.matrix_stack); +} + +void +rodeo_gfx_matrix_set(rodeo_math_mat4_t matrix) +{ + rodeo_gfx_renderer_flush(); + //*stc_gfx_matrix_stack_end(&irodeo_gfx_state.matrix_stack).ref = matrix; + irodeo_gfx_state.matrix_stack_top = matrix; +} + +rodeo_math_mat4_t +rodeo_gfx_matrix_get(void) +{ + //return *stc_gfx_matrix_stack_top(&irodeo_gfx_state.matrix_stack); + return irodeo_gfx_state.matrix_stack_top; +} + +void +rodeo_gfx_matrix_push(void) +{ + stc_gfx_matrix_stack_push( + &irodeo_gfx_state.matrix_stack, + rodeo_gfx_matrix_get() + ); +} + +void +rodeo_gfx_matrix_pop(void) +{ + // submit to render + rodeo_gfx_renderer_flush(); + irodeo_gfx_state.matrix_stack_top = *stc_gfx_matrix_stack_top(&irodeo_gfx_state.matrix_stack); + if(stc_gfx_matrix_stack_size(&irodeo_gfx_state.matrix_stack) > 0) + { + stc_gfx_matrix_stack_pop(&irodeo_gfx_state.matrix_stack); + //stc_gfx_matrix_stack_push(&irodeo_gfx_state.matrix_stack, rodeo_math_mat4_identity()); + } +} + +uint32_t +rodeo_gfx_matrix_size(void) +{ + return (uint32_t)stc_gfx_matrix_stack_size(&irodeo_gfx_state.matrix_stack); +} + +uint32_t +rodeo_gfx_matrix_capacity(void) +{ + return (uint32_t)stc_gfx_matrix_stack_capacity(&irodeo_gfx_state.matrix_stack); +} + bgfx_shader_handle_t irodeo_gfx_shader_load(const cstr path) { -- cgit v1.2.3