From 1497ffa54e502844e8cd8db1b1d99a0b96c4dcc9 Mon Sep 17 00:00:00 2001 From: Harrand Date: Tue, 26 Nov 2024 14:02:54 +0000 Subject: [PATCH] [trs] trs rotation fixes, and quad_renderer now supports rotation --- include/tz/ren/quad.hpp | 3 +++ src/tz/core/trs.cpp | 2 +- src/tz/ren/quad.cpp | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/tz/ren/quad.hpp b/include/tz/ren/quad.hpp index e0ee1a2798..316bc3c599 100644 --- a/include/tz/ren/quad.hpp +++ b/include/tz/ren/quad.hpp @@ -156,6 +156,9 @@ namespace tz::ren */ void set_quad_layer(quad_renderer_handle renh, quad_handle quad, short layer); + float get_quad_rotation(quad_renderer_handle renh, quad_handle quad); + void set_quad_rotation(quad_renderer_handle renh, quad_handle quad, float rotation); + /** * @ingroup tz_ren_quad diff --git a/src/tz/core/trs.cpp b/src/tz/core/trs.cpp index 41abfe3f5d..420efff57b 100644 --- a/src/tz/core/trs.cpp +++ b/src/tz/core/trs.cpp @@ -72,7 +72,7 @@ namespace tz tz::m4f trs::matrix() const { - return matrix_scale(this->scale) * matrix_translate(this->translate) * this->rotate.matrix(); + return matrix_scale(this->scale) * this->rotate.matrix() * matrix_translate(this->translate); } trs trs::from_matrix(tz::m4f mat) diff --git a/src/tz/ren/quad.cpp b/src/tz/ren/quad.cpp index 61cc5b2f05..30d98f516f 100644 --- a/src/tz/ren/quad.cpp +++ b/src/tz/ren/quad.cpp @@ -262,6 +262,24 @@ namespace tz::ren tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&layer_value, 1)), offset); } + float get_quad_rotation(quad_renderer_handle renh, quad_handle quad) + { + const auto& ren = renderers[renh.peek()]; + tz::quat rot = ren.internals[quad.peek()].transform.rotate; + // assume rot is axis angle {0, 0, 1} and some angle (so acos(w) * 2) + return std::acos(rot[3]) * 2.0f; + } + + void set_quad_rotation(quad_renderer_handle renh, quad_handle quad, float rotation) + { + auto& ren = renderers[renh.peek()]; + auto& internal = ren.internals[quad.peek()]; + internal.transform.rotate = tz::quat::from_axis_angle({0.0f, 0.0f, 1.0f}, rotation); + 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()];