Skip to content

Commit

Permalink
[core.vector] scalars are no longer implicitly convertible to vectors…
Browse files Browse the repository at this point in the history
…, and vector*=scalar,vector/-scalar,vector*scalar,vector/scalar is now defined. previously this was causing the scalar to implicitly convert to vec{scalar, 0...} which was very wrong
  • Loading branch information
harrand committed Oct 31, 2024
1 parent f12d7c2 commit 36fae7d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
8 changes: 7 additions & 1 deletion include/tz/core/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace tz
template<typename... Ts>
requires(
sizeof...(Ts) <= N
&& sizeof...(Ts) > 0
&& sizeof...(Ts) > 1
&& std::is_convertible_v<std::tuple_element_t<0, std::tuple<Ts...>>, T>)
constexpr vector(Ts&&... ts) :
arr({ std::forward<Ts>(ts)... }) {}
Expand Down Expand Up @@ -72,6 +72,12 @@ namespace tz
*/
T& operator[](std::size_t idx);

// Multiply vector by scalar
vector<T, N>& operator*=(T scalar);
vector<T, N>& operator/=(T scalar);
vector<T, N> operator*(T scalar) const{auto cpy = *this; return cpy *= scalar;}
vector<T, N> operator/(T scalar) const{auto cpy = *this; return cpy /= scalar;}

/// Add one vector to another.
vector<T, N>& operator+=(const vector<T, N>& rhs);
/// Subtract one vector from another.
Expand Down
7 changes: 5 additions & 2 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ namespace tz::ren
};
std::expected<quad_handle, tz::error_code> quad_renderer_create_quad(quad_renderer_handle renh, quad_info info);

tz::v2f quad_renderer_get_quad_position(quad_renderer_handle renh, quad_handle quad);
void quad_renderer_set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position);
tz::v2f get_quad_position(quad_renderer_handle renh, quad_handle quad);
void set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position);

tz::v2f get_quad_scale(quad_renderer_handle renh, quad_handle quad);
void set_quad_scale(quad_renderer_handle renh, quad_handle quad, tz::v2f scale);

tz::gpu::graph_handle quad_renderer_graph(quad_renderer_handle renh);
}
Expand Down
20 changes: 20 additions & 0 deletions src/tz/core/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ namespace tz
return this->arr[idx];
}

VECTOR_TEMPLATE_IMPL
vector<T, N>& vector<T, N>::operator*=(T scalar)
{
for(std::size_t i = 0; i < N; i++)
{
this->arr[i] *= scalar;
}
return *this;
}

VECTOR_TEMPLATE_IMPL
vector<T, N>& vector<T, N>::operator/=(T scalar)
{
for(std::size_t i = 0; i < N; i++)
{
this->arr[i] /= scalar;
}
return *this;
}

VECTOR_TEMPLATE_IMPL
vector<T, N>& vector<T, N>::operator+=(const vector<T, N>& rhs)
{
Expand Down
20 changes: 18 additions & 2 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,38 @@ namespace tz::ren
return static_cast<tz::hanval>(ren.quad_count++);
}

tz::v2f quad_renderer_get_quad_position(quad_renderer_handle renh, quad_handle quad)
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<const tz::v4f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale));
return {pos_scale[0], pos_scale[1]};
}

void quad_renderer_set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position)
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);

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v2f>(&position, 1)), offset);
}

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<const tz::v4f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale));
return {pos_scale[2], pos_scale[3]};
}

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);

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v2f>(&scale, 1)), offset);
}

tz::gpu::graph_handle quad_renderer_graph(quad_renderer_handle renh)
{
return renderers[renh.peek()].graph;
Expand Down
22 changes: 17 additions & 5 deletions test/tz/ren_quad_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,38 @@ int main()

tz::ren::quad_handle quad1 = tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = tz::v2f::zero(), .scale = {0.2f, 0.2f}}));

tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = {-0.5f, -0.5f}, .scale = {0.15f, 0.15f}}));

while(tz::os::window_is_open())
{
tz::os::window_update();
tz::gpu::execute(tz::ren::quad_renderer_graph(ren));
tz::v2f pos = tz::ren::quad_renderer_get_quad_position(ren, quad1);
tz::v2f pos = tz::ren::get_quad_position(ren, quad1);
tz::v2f scale = tz::ren::get_quad_scale(ren, quad1);
if(tz::os::is_key_pressed(tz::os::key::a))
{
tz::ren::quad_renderer_set_quad_position(ren, quad1, pos += {-0.002f, 0.0f});
tz::ren::set_quad_position(ren, quad1, pos += {-0.002f, 0.0f});
}
if(tz::os::is_key_pressed(tz::os::key::d))
{
tz::ren::quad_renderer_set_quad_position(ren, quad1, pos += {0.002f, 0.0f});
tz::ren::set_quad_position(ren, quad1, pos += {0.002f, 0.0f});
}
if(tz::os::is_key_pressed(tz::os::key::w))
{
tz::ren::quad_renderer_set_quad_position(ren, quad1, pos += {0.0f, 0.002f});
tz::ren::set_quad_position(ren, quad1, pos += {0.0f, 0.002f});
}
if(tz::os::is_key_pressed(tz::os::key::s))
{
tz::ren::quad_renderer_set_quad_position(ren, quad1, pos += {0.0f, -0.002f});
tz::ren::set_quad_position(ren, quad1, pos += {0.0f, -0.002f});
}

if(tz::os::is_key_pressed(tz::os::key::i))
{
tz::ren::set_quad_scale(ren, quad1, scale * 1.001f);
}
if(tz::os::is_key_pressed(tz::os::key::k))
{
tz::ren::set_quad_scale(ren, quad1, scale * 0.999f);
}
}

Expand Down

0 comments on commit 36fae7d

Please sign in to comment.