added test/dev tools for AABB and Frustums - added simple AABB calculation and instrumentation

This commit is contained in:
Hartmut Seichter 2024-07-16 15:30:07 +02:00
parent 51bc5203ae
commit 4547b9c6f8
9 changed files with 97 additions and 45 deletions

View file

@ -23,16 +23,32 @@
#ifndef PW_CORE_AABB_HPP
#define PW_CORE_AABB_HPP
#include "vector.hpp"
#include <numeric>
#include <pw/core/vector.hpp>
#include <algorithm>
#include <vector>
namespace pw {
struct aabb {
template <typename Scalar, std::size_t N> struct aabb final {
vector3 min{};
vector3 max{};
using value_type = vector<Scalar, N>;
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<pw::vector<Scalar, N>>& vertices) -> aabb<Scalar, N> {
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

View file

@ -25,6 +25,7 @@
#define PW_CORE_FRUSTUM_HPP
#include <pw/core/globals.hpp>
#include <pw/core/math.hpp>
namespace pw {
@ -35,7 +36,7 @@ template <typename Scalar> 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};

View file

@ -166,6 +166,18 @@ template <typename Scalar, std::size_t N> struct vector final {
return operator+=(-v);
}
constexpr vector min(const vector& v) const noexcept {
return [this, &v]<std::size_t... Ss>(std::index_sequence<Ss...>) {
return pw::vector<Scalar, N>{(std::min(v[Ss], (*this)[Ss]))...};
}(std::make_index_sequence<N>{});
}
constexpr vector max(const vector& v) const noexcept {
return [this, &v]<std::size_t... Ss>(std::index_sequence<Ss...>) {
return pw::vector<Scalar, N>{(std::max(v[Ss], (*this)[Ss]))...};
}(std::make_index_sequence<N>{});
}
constexpr auto project() const noexcept -> vector<Scalar, N - 1> {
return [this]<std::size_t... Ss>(std::index_sequence<Ss...>) {
return vector<Scalar, N - 1>{{(*this)[Ss] / (*this)[N - 1]...}};

View file

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

View file

@ -0,0 +1,20 @@
#include <pw/core/aabb.hpp>
#include <print>
#include <vector>
auto main() -> int {
auto vertices = std::vector<pw::vector<float, 3>>{
{-1.f, 1.f, 0}, {2.f, -1.f, 3.f}, {0, 0, -1.f}};
auto aabb_1 =
pw::aabb<decltype(vertices)::value_type::value_type,
decltype(vertices)::value_type::coefficients>::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());
}

View file

@ -0,0 +1,9 @@
#include <pw/core/frustum.hpp>
#include <vector>
auto main() -> int {
auto frustum = pw::frustum<float>::make_perspective_symmetric(
45.f, float{320} / 240, 0.1f, 1'000.f);
}

View file

@ -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;
}

View file

@ -23,6 +23,11 @@ struct shader final {
bool ready() const;
using uniform_t = std::variant<bool, int, float, double, vector2f, vector3f,
vector4f, matrix4x4f>;
using uniform_entry_t = std::tuple<std::string, uniform_t, int>;
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<bool, int, float, double, vector2f, vector3f,
vector4f, matrix4x4f>;
using uniform_entry_t = std::tuple<std::string, uniform_t, int>;
using uniform_cache_t = std::vector<uniform_entry_t>;

View file

@ -1,70 +1,55 @@
#ifndef PW_VISUAL_TEXTURE_HPP
#define PW_VISUAL_TEXTURE_HPP
#include <pw/core/debug.hpp>
#include <pw/core/globals.hpp>
#include <pw/core/image.hpp>
#include <pw/core/debug.hpp>
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> _impl;
wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge;
struct impl;
unique_ptr<impl> _impl;
};
}
} // namespace pw
#endif