diff --git a/src/tz/core/trs.cpp b/src/tz/core/trs.cpp index c63d8a6657..aa09d84048 100644 --- a/src/tz/core/trs.cpp +++ b/src/tz/core/trs.cpp @@ -32,7 +32,7 @@ namespace tz tz::m4f trs::matrix() const { - return matrix_translate(this->translate) * this->rotate.matrix() * matrix_scale(this->scale); + return matrix_scale(this->scale) * matrix_translate(this->translate) * this->rotate.matrix(); } trs trs::from_matrix(tz::m4f mat) diff --git a/src/tz/ren/quad.cpp b/src/tz/ren/quad.cpp index b81457521f..c8cfa93f44 100644 --- a/src/tz/ren/quad.cpp +++ b/src/tz/ren/quad.cpp @@ -1,5 +1,6 @@ #include "tz/ren/quad.hpp" #include "tz/core/matrix.hpp" +#include "tz/core/trs.hpp" #include "tz/topaz.hpp" #include "tz/gpu/resource.hpp" #include "tz/gpu/pass.hpp" @@ -12,6 +13,11 @@ namespace tz::ren { + struct quad_internal_data + { + tz::trs transform; + }; + struct quad_renderer_data { quad_renderer_info info = {}; @@ -19,6 +25,7 @@ namespace tz::ren tz::gpu::resource_handle camera_buffer = tz::nullhand; tz::gpu::pass_handle main_pass = tz::nullhand; tz::gpu::graph_handle graph = tz::nullhand; + std::vector internals = {}; std::size_t quad_count = 0; std::size_t texture_count = 0; unsigned int window_width_cache; @@ -27,8 +34,8 @@ namespace tz::ren struct quad_data { - tz::v4f pos_scale = {0.0f, 0.0f, 1.0f, 1.0f}; - tz::v3f colour_tint = {1.0f, 1.0f, 1.0f}; + tz::m4f model = tz::m4f::iden(); + tz::v3f colour = {1.0f, 1.0f, 1.0f}; std::uint32_t texture_id = -1; }; @@ -123,13 +130,21 @@ namespace tz::ren std::expected quad_renderer_create_quad(quad_renderer_handle renh, quad_info info) { + auto& ren = renderers[renh.peek()]; + + quad_internal_data& internal = ren.internals.emplace_back(); + internal.transform = + { + .translate = {info.position[0], info.position[1], 0.0f}, + .rotate = tz::quat::from_axis_angle({0.0f, 0.0f, 1.0f}, info.rotation), + .scale = {info.scale[0], info.scale[1], 1.0f} + }; + quad_data new_data; - new_data.pos_scale = {info.position[0], info.position[1], info.scale[0], info.scale[1]}; - new_data.colour_tint = info.colour; + new_data.model = internal.transform.matrix(); + new_data.colour = info.colour; new_data.texture_id = info.texture_id; - auto& ren = renderers[renh.peek()]; - if(info.texture_id != static_cast(-1)) { if(info.texture_id >= ren.texture_count) @@ -158,46 +173,48 @@ namespace tz::ren tz::v2f get_quad_position(quad_renderer_handle renh, quad_handle quad) { const auto& ren = renderers[renh.peek()]; - auto quad_data_array = tz::gpu::resource_read(ren.data_buffer); - tz::v4f pos_scale = *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale)); - return {pos_scale[0], pos_scale[1]}; + tz::v3f pos = ren.internals[quad.peek()].transform.translate; + return {pos[0], pos[1]}; } void set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position) { auto& ren = renderers[renh.peek()]; - std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale); + auto& internal = ren.internals[quad.peek()]; + internal.transform.translate = {position[0], position[1]}; - tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&position, 1)), offset); + tz::m4f model = internal.transform.matrix(); + tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model)); } tz::v2f get_quad_scale(quad_renderer_handle renh, quad_handle quad) { const auto& ren = renderers[renh.peek()]; - auto quad_data_array = tz::gpu::resource_read(ren.data_buffer); - tz::v4f pos_scale = *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale)); - return {pos_scale[2], pos_scale[3]}; + tz::v3f scale = ren.internals[quad.peek()].transform.scale; + return {scale[0], scale[1]}; } void set_quad_scale(quad_renderer_handle renh, quad_handle quad, tz::v2f scale) { auto& ren = renderers[renh.peek()]; - std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale) + sizeof(tz::v2f); + auto& internal = ren.internals[quad.peek()]; + internal.transform.scale = {scale[0], scale[1]}; - tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&scale, 1)), offset); + tz::m4f model = internal.transform.matrix(); + tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model)); } tz::v3f get_quad_colour(quad_renderer_handle renh, quad_handle quad) { const auto& ren = renderers[renh.peek()]; auto quad_data_array = tz::gpu::resource_read(ren.data_buffer); - return *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint)); + return *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour)); } void set_quad_colour(quad_renderer_handle renh, quad_handle quad, tz::v3f colour) { auto& ren = renderers[renh.peek()]; - std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint); + std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour); tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&colour, 1)), offset); } diff --git a/src/tz/ren/quad.vertex.tzsl b/src/tz/ren/quad.vertex.tzsl index c5b6ba8828..60febd4972 100644 --- a/src/tz/ren/quad.vertex.tzsl +++ b/src/tz/ren/quad.vertex.tzsl @@ -2,10 +2,8 @@ shader(type = vertex); struct quad_data { - vec4 pos_scale; - float tint_red; - float tint_green; - float tint_blue; + mat4 model; + vec3 colour; uint texture_id; }; @@ -39,10 +37,8 @@ void main() vec2 local_pos = quad_positions[in::vertex_id % 6]; quad_data cur_quad = quad.data[quad_id]; - vec2 position_worldspace = cur_quad.pos_scale.xy + (local_pos * cur_quad.pos_scale.zw); - - out::position = camera.projection * vec4(position_worldspace, 0, 1); - out::tint = vec3(cur_quad.tint_red, cur_quad.tint_green, cur_quad.tint_blue); + out::position = camera.projection * cur_quad.model * vec4(local_pos, 0, 1); + out::tint = vec3(cur_quad.colour); out::uv = quad_texcoords[in::vertex_id % 6]; out::texture_id = cur_quad.texture_id; } \ No newline at end of file