From 4547b9c6f85a8f308e554539c180806421ba1827 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 16 Jul 2024 15:30:07 +0200 Subject: [PATCH] added test/dev tools for AABB and Frustums - added simple AABB calculation and instrumentation --- src/core/include/pw/core/aabb.hpp | 24 ++++++++-- src/core/include/pw/core/frustum.hpp | 3 +- src/core/include/pw/core/vector.hpp | 12 +++++ src/core/tests/CMakeLists.txt | 2 + src/core/tests/pwcore_test_aabb.cpp | 20 ++++++++ src/core/tests/pwcore_test_frustum.cpp | 9 ++++ src/core/tests/pwcore_test_vector.cpp | 5 ++ src/visual/include/pw/visual/shader.hpp | 8 ++-- src/visual/include/pw/visual/texture.hpp | 59 +++++++++--------------- 9 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 src/core/tests/pwcore_test_aabb.cpp create mode 100644 src/core/tests/pwcore_test_frustum.cpp diff --git a/src/core/include/pw/core/aabb.hpp b/src/core/include/pw/core/aabb.hpp index 4a36324..712a15a 100644 --- a/src/core/include/pw/core/aabb.hpp +++ b/src/core/include/pw/core/aabb.hpp @@ -23,16 +23,32 @@ #ifndef PW_CORE_AABB_HPP #define PW_CORE_AABB_HPP +#include "vector.hpp" +#include #include +#include +#include + namespace pw { -struct aabb { +template struct aabb final { - vector3 min{}; - vector3 max{}; + using value_type = vector; - constexpr vector3 dimension() const noexcept { return max - min; } + value_type min{}; + value_type max{}; + + constexpr auto dimension() const noexcept { return max - min; } + + static constexpr auto make( + const std::vector>& vertices) -> aabb { + return std::accumulate(std::begin(vertices), std::end(vertices), + aabb{.min{vertices.front()}, .max{vertices.front()}}, + [](const auto& prev, const auto& e) { + return aabb{.min{e.min(prev.min)}, .max{e.max(prev.max)}}; + }); + } }; } // namespace pw diff --git a/src/core/include/pw/core/frustum.hpp b/src/core/include/pw/core/frustum.hpp index 58c3922..4526dea 100644 --- a/src/core/include/pw/core/frustum.hpp +++ b/src/core/include/pw/core/frustum.hpp @@ -25,6 +25,7 @@ #define PW_CORE_FRUSTUM_HPP #include +#include namespace pw { @@ -35,7 +36,7 @@ template struct frustum final { Scalar aspect_ratio, Scalar z_near, Scalar z_far) -> frustum { - const auto tangent_half{std::tan(pw::deg_to_rad(fov_h_deg / 2))}; + const auto tangent_half{std::tan(pw::deg_to_rad(fov_h_deg / Scalar{2}))}; const auto top{tangent_half * z_near}; const auto right{top * aspect_ratio}; diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 0008c25..f409eba 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -166,6 +166,18 @@ template struct vector final { return operator+=(-v); } + constexpr vector min(const vector& v) const noexcept { + return [this, &v](std::index_sequence) { + return pw::vector{(std::min(v[Ss], (*this)[Ss]))...}; + }(std::make_index_sequence{}); + } + + constexpr vector max(const vector& v) const noexcept { + return [this, &v](std::index_sequence) { + return pw::vector{(std::max(v[Ss], (*this)[Ss]))...}; + }(std::make_index_sequence{}); + } + constexpr auto project() const noexcept -> vector { return [this](std::index_sequence) { return vector{{(*this)[Ss] / (*this)[N - 1]...}}; diff --git a/src/core/tests/CMakeLists.txt b/src/core/tests/CMakeLists.txt index 0140b6d..a527a0c 100644 --- a/src/core/tests/CMakeLists.txt +++ b/src/core/tests/CMakeLists.txt @@ -9,4 +9,6 @@ make_test(pwcore_test_axisangle) make_test(pwcore_test_quaternion) make_test(pwcore_test_color) make_test(pwcore_test_transform_tools) +make_test(pwcore_test_aabb) +make_test(pwcore_test_frustum) make_test(pwcore_test_mesh) diff --git a/src/core/tests/pwcore_test_aabb.cpp b/src/core/tests/pwcore_test_aabb.cpp new file mode 100644 index 0000000..cd1d078 --- /dev/null +++ b/src/core/tests/pwcore_test_aabb.cpp @@ -0,0 +1,20 @@ +#include + +#include +#include + +auto main() -> int { + + auto vertices = std::vector>{ + {-1.f, 1.f, 0}, {2.f, -1.f, 3.f}, {0, 0, -1.f}}; + + auto aabb_1 = + pw::aabb::make(vertices); + + std::print("aabb::min -> {} {} {}\n", aabb_1.min.x(), aabb_1.min.y(), + aabb_1.min.z()); + + std::print("aabb::max -> {} {} {}\n", aabb_1.max.x(), aabb_1.max.y(), + aabb_1.max.z()); +} diff --git a/src/core/tests/pwcore_test_frustum.cpp b/src/core/tests/pwcore_test_frustum.cpp new file mode 100644 index 0000000..32911d6 --- /dev/null +++ b/src/core/tests/pwcore_test_frustum.cpp @@ -0,0 +1,9 @@ + +#include +#include + +auto main() -> int { + + auto frustum = pw::frustum::make_perspective_symmetric( + 45.f, float{320} / 240, 0.1f, 1'000.f); +} diff --git a/src/core/tests/pwcore_test_vector.cpp b/src/core/tests/pwcore_test_vector.cpp index 446671d..2aa13ed 100644 --- a/src/core/tests/pwcore_test_vector.cpp +++ b/src/core/tests/pwcore_test_vector.cpp @@ -71,5 +71,10 @@ auto main() -> int { auto& [x, y, z] = vec3f; std::print("x:{} y:{} z:{}\n", x, y, z); + auto min_ab{vec3f.min(pw::vector{1.3f, 0.9f, 2.3f})}; + + std::print("min((1.3f,0.9f,2.3f),({} {} {})) -> ({} {} {})\n", vec3f.x(), + vec3f.y(), vec3f.z(), min_ab.x(), min_ab.y(), min_ab.z()); + return 0; } diff --git a/src/visual/include/pw/visual/shader.hpp b/src/visual/include/pw/visual/shader.hpp index 970566c..a40cc09 100644 --- a/src/visual/include/pw/visual/shader.hpp +++ b/src/visual/include/pw/visual/shader.hpp @@ -23,6 +23,11 @@ struct shader final { bool ready() const; + using uniform_t = std::variant; + using uniform_entry_t = std::tuple; + + shader& set_uniform_at_location(int location, float v); shader& set_uniform_at_location(int location, uint32_t v); shader& set_uniform_at_location(int location, int32_t v); @@ -59,9 +64,6 @@ struct shader final { void use(); - using uniform_t = std::variant; - using uniform_entry_t = std::tuple; using uniform_cache_t = std::vector; diff --git a/src/visual/include/pw/visual/texture.hpp b/src/visual/include/pw/visual/texture.hpp index d040844..1c7add2 100644 --- a/src/visual/include/pw/visual/texture.hpp +++ b/src/visual/include/pw/visual/texture.hpp @@ -1,70 +1,55 @@ #ifndef PW_VISUAL_TEXTURE_HPP #define PW_VISUAL_TEXTURE_HPP +#include #include #include -#include namespace pw { class texture { -public: - enum class data_type { - color, - normal - }; + public: + enum class data_type { color, normal }; - enum class data_layout { - shape_1d, - shape_2d, - shape_3d - }; + enum class data_layout { shape_1d, shape_2d, shape_3d }; - enum texture_dimension { - dimension_r, - dimension_s, - dimension_t - }; + enum texture_dimension { dimension_r, dimension_s, dimension_t }; - enum wrap_mode { - wrap_repeat, - wrap_clamp, - wrap_clamp_to_edge, - wrap_clamp_to_repeat - }; + enum wrap_mode { + wrap_repeat, + wrap_clamp, + wrap_clamp_to_edge, + wrap_clamp_to_repeat + }; texture(); - texture(const image& i,data_layout s); + texture(const image& i, data_layout s); ~texture(); void set_shape(data_layout s); - data_layout shape() const { return _shape; } + data_layout shape() const { return _shape; } - void set_wrap(wrap_mode w); - wrap_mode wrap() const { return _wrap; } + void set_wrap(wrap_mode w); + wrap_mode wrap() const { return _wrap; } uint32_t native_handle() const; - bool create(const image &i); - void update(const image &i); + bool create(const image& i); + void update(const image& i); void bind(); void unbind(); bool valid() const; -protected: - + protected: data_layout _shape = data_layout::shape_2d; - wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge; - - struct impl; - unique_ptr _impl; - + wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge; + struct impl; + unique_ptr _impl; }; -} - +} // namespace pw #endif