diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e3e3a8..2754ca5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,7 @@ macro(blt_add_project name source type) project(tower-defense) endmacro() -project(tower-defense VERSION 0.0.16) +project(tower-defense VERSION 0.0.17) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/map.h b/include/map.h index 2fa892a..6a039b4 100644 --- a/include/map.h +++ b/include/map.h @@ -20,20 +20,48 @@ #define MAP_H #include +#include #include #include namespace td { + struct curve_mesh_data_t + { + struct line_vertex_t + { + blt::vec3 pos; + blt::vec2 uv; + }; + + [[nodiscard]] std::unique_ptr to_vertex_array() const; + void populate_vertex_array(blt::gfx::vertex_array_t& va) const; + + [[nodiscard]] std::vector calculate_vertices() const; + + curve_mesh_data_t& with(const curve_mesh_data_t& mesh) + { + lines.insert(lines.end(), mesh.lines.begin(), mesh.lines.end()); + return *this; + } + + std::vector lines; + }; + class curve_t { public: curve_t(blt::vec2 p0, blt::vec2 p1, blt::vec2 p2); + curve_t(blt::vec2 p0, blt::vec2 p1, blt::vec2 p2, blt::vec2 p3); [[nodiscard]] blt::vec2 get_point(float t) const; + + [[nodiscard]] std::vector to_lines(blt::i32 segments) const; + + [[nodiscard]] curve_mesh_data_t to_mesh(blt::i32 segments) const; private: - blt::vec2 m_p0, m_p1, m_p2; + blt::vec2 m_p0, m_p1, m_p2, m_p3; }; class path_segment_t diff --git a/src/main.cpp b/src/main.cpp index c0a1d9a..bf7e52a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,7 @@ blt::gfx::first_person_camera camera; float t = 0; float dir = 1; -td::curve_t curve{blt::vec2{250, 250}, blt::vec2{500, 500}, blt::vec2{750, 250}}; +td::curve_t curve{blt::vec2{250, 250}, blt::vec2{400, 500}, blt::vec2{600, 500}, blt::vec2{750, 250}}; void init(const blt::gfx::window_data&) { @@ -51,6 +51,9 @@ void update(const blt::gfx::window_data& data) auto pos = curve.get_point(t); renderer_2d.drawRectangleInternal(blt::make_color(1, 0, 0), blt::gfx::rectangle2d_t{pos, blt::vec2{25, 25}}); + auto lines = curve.to_lines(32); + for (const auto& line : lines) + renderer_2d.drawLineInternal(blt::make_color(0, 1,0), line); renderer_2d.render(data.width, data.height); } diff --git a/src/map.cpp b/src/map.cpp index f6a3433..5943db9 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -19,14 +19,57 @@ namespace td { - curve_t::curve_t(const blt::vec2 p0, const blt::vec2 p1, const blt::vec2 p2): m_p0(p0), m_p1(p1), m_p2(p2) + curve_t::curve_t(const blt::vec2 p0, const blt::vec2 p1, const blt::vec2 p2): m_p0(p0), m_p1(p1), m_p2(p1), m_p3(p2) + {} + + curve_t::curve_t(const blt::vec2 p0, const blt::vec2 p1, const blt::vec2 p2, const blt::vec2 p3): m_p0(p0), m_p1(p1), m_p2(p2), m_p3(p3) {} blt::vec2 curve_t::get_point(const float t) const { const auto t_inv = 1.0f - t; const auto t_inv_sq = t_inv * t_inv; + const auto t_inv_cub = t_inv_sq * t_inv; const auto t_sq = t * t; - return t_inv_sq * m_p0 + 2.0f * t_inv * t * m_p1 + t_sq * m_p2; + const auto t_cub = t_sq * t; + return t_inv_cub * m_p0 + 3 * t_inv_sq * t * m_p1 + 3 * t_inv * t_sq * m_p2 + t_cub * m_p3; + } + + std::vector curve_t::to_lines(const blt::i32 segments) const + { + std::vector lines; + float t = 0; + const float diff = 1.0f / static_cast(segments); + + for (blt::i32 i = 0; i < segments; ++i) + { + auto begin = get_point(t); + t += diff; + auto end = get_point(t); + + lines.emplace_back(begin, end); + } + + return lines; + } + + std::unique_ptr curve_mesh_data_t::to_vertex_array() const + {} + + void curve_mesh_data_t::populate_vertex_array(blt::gfx::vertex_array_t& va) const + {} + + std::vector curve_mesh_data_t::calculate_vertices() const + { + std::vector vertices; + + return vertices; + } + + curve_mesh_data_t curve_t::to_mesh(const blt::i32 segments) const + { + curve_mesh_data_t mesh_data; + mesh_data.lines = to_lines(segments); + return mesh_data; } }