start work on efficient curve renderer

main
Brett 2025-03-18 17:10:46 -04:00
parent be5ca99866
commit 3647c8be48
4 changed files with 79 additions and 5 deletions

View File

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

View File

@ -20,20 +20,48 @@
#define MAP_H
#include <blt/math/vectors.h>
#include <blt/gfx/renderer/batch_2d_renderer.h>
#include <fwddecl.h>
#include <enemies.h>
namespace td
{
struct curve_mesh_data_t
{
struct line_vertex_t
{
blt::vec3 pos;
blt::vec2 uv;
};
[[nodiscard]] std::unique_ptr<blt::gfx::vertex_array_t> to_vertex_array() const;
void populate_vertex_array(blt::gfx::vertex_array_t& va) const;
[[nodiscard]] std::vector<line_vertex_t> 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<blt::gfx::line2d_t> 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<blt::gfx::line2d_t> 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

View File

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

View File

@ -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<blt::gfx::line2d_t> curve_t::to_lines(const blt::i32 segments) const
{
std::vector<blt::gfx::line2d_t> lines;
float t = 0;
const float diff = 1.0f / static_cast<float>(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<blt::gfx::vertex_array_t> 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::line_vertex_t> curve_mesh_data_t::calculate_vertices() const
{
std::vector<line_vertex_t> 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;
}
}