Skip to content

Commit

Permalink
[trs] trs rotation fixes, and quad_renderer now supports rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Nov 26, 2024
1 parent a3ce3e5 commit 1497ffa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/tz/core/trs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ namespace tz::ren
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const std::int32_t>(&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<const tz::m4f>(&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()];
Expand Down

0 comments on commit 1497ffa

Please sign in to comment.