make square shader accessable
parent
ac79aa80df
commit
f60ce3dc0b
|
@ -1,7 +1,7 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
include(FetchContent)
|
||||
|
||||
set(BLT_GRAPHICS_VERSION 1.1.7)
|
||||
set(BLT_GRAPHICS_VERSION 1.1.8)
|
||||
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
|
||||
|
||||
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})
|
||||
|
|
|
@ -32,236 +32,256 @@
|
|||
|
||||
namespace blt::gfx
|
||||
{
|
||||
struct rectangle2d_t
|
||||
{
|
||||
vec2f pos, size;
|
||||
f32 rotation = 0;
|
||||
|
||||
rectangle2d_t(const f32 x, const f32 y, const f32 width, const f32 height, const f32 rotation): pos(x, y), size(width, height),
|
||||
rotation(rotation)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const f32 x, const f32 y, const f32 width, const f32 height): pos(x, y), size(width, height)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const vec2f pos, const vec2f size, const f32 rotation): pos(pos), size(size), rotation(rotation)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const vec2f pos, const vec2f size): pos(pos), size(size)
|
||||
{}
|
||||
};
|
||||
|
||||
struct line2d_t
|
||||
{
|
||||
vec2f p1;
|
||||
vec2f p2;
|
||||
f32 thickness = 1;
|
||||
|
||||
line2d_t(const f32 px1, const f32 py1, const f32 px2, const f32 py2): p1(px1, py1), p2(px2, py2)
|
||||
{}
|
||||
|
||||
line2d_t(const vec2f p1, const vec2f p2): p1(p1), p2(p2)
|
||||
{}
|
||||
|
||||
line2d_t(const f32 px1, const f32 py1, const f32 px2, const f32 py2, const f32 thickness): p1(px1, py1), p2(px2, py2), thickness(thickness)
|
||||
{}
|
||||
|
||||
line2d_t(const vec2f p1, const vec2f p2, const f32 thickness): p1(p1), p2(p2), thickness(thickness)
|
||||
{}
|
||||
};
|
||||
|
||||
struct point2d_t
|
||||
{
|
||||
vec2f pos;
|
||||
float scale = 1;
|
||||
|
||||
point2d_t(const float x, const float y, const float scale): pos(x, y), scale(scale)
|
||||
{}
|
||||
|
||||
point2d_t(const float x, const float y): pos(x, y)
|
||||
{}
|
||||
|
||||
point2d_t(const vec2f pos, const float scale): pos(pos), scale(scale)
|
||||
{}
|
||||
|
||||
explicit point2d_t(const vec2f pos): pos(pos)
|
||||
{}
|
||||
|
||||
point2d_t apply_scale(float s) const
|
||||
{
|
||||
return {pos, scale * s};
|
||||
}
|
||||
};
|
||||
|
||||
struct render_info_t
|
||||
{
|
||||
private:
|
||||
constexpr static color4 disabled = color4(0, 0, 0, 0);
|
||||
constexpr static vec4 empty{0, 0, 0, 1};
|
||||
constexpr static vec4 full{1, 1, 1, 1};
|
||||
|
||||
render_info_t(const std::string_view texture, color4 color, color4 blend): texture_name(texture), color(color), blend(blend)
|
||||
{}
|
||||
|
||||
public:
|
||||
// texture to use
|
||||
std::string texture_name;
|
||||
// color to use
|
||||
color4 color;
|
||||
// how much to blend the texture into the color? note blending is always additive!
|
||||
color4 blend;
|
||||
|
||||
render_info_t() = default;
|
||||
|
||||
static render_info_t make_info(const std::string_view texture)
|
||||
{
|
||||
render_info_t info{texture, empty, full};
|
||||
return info;
|
||||
}
|
||||
|
||||
static render_info_t make_info(const color4 color)
|
||||
{
|
||||
render_info_t info{"", color, empty};
|
||||
return info;
|
||||
}
|
||||
|
||||
static render_info_t make_info(const std::string_view texture, const color4 color, const color4 blend)
|
||||
{
|
||||
render_info_t info{texture, color, blend};
|
||||
return info;
|
||||
}
|
||||
};
|
||||
|
||||
class batch_renderer_2d
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<pp_engine_t> engine;
|
||||
|
||||
template<typename T>
|
||||
struct render_object_t
|
||||
{
|
||||
f32 z_index;
|
||||
T obj;
|
||||
|
||||
render_object_t(const float z_index, const T obj): z_index(z_index), obj(obj)
|
||||
{}
|
||||
};
|
||||
|
||||
using rectangle2d_obj_t = render_object_t<rectangle2d_t>;
|
||||
using point2d_obj_t = render_object_t<point2d_t>;
|
||||
using line2d_obj_t = render_object_t<line2d_t>;
|
||||
|
||||
template<typename T>
|
||||
using object_container = hashmap_t<std::string, std::vector<std::pair<render_info_t, T>>>;
|
||||
|
||||
private:
|
||||
std::unique_ptr<vertex_array_t> square_vao;
|
||||
std::unique_ptr<vertex_array_t> line_vao;
|
||||
std::unique_ptr<shader_t> square_shader;
|
||||
std::unique_ptr<shader_t> point_shader;
|
||||
resource_manager& resources;
|
||||
matrix_state_manager& state;
|
||||
// texture name -> draw info
|
||||
struct
|
||||
{
|
||||
object_container<rectangle2d_obj_t> complex_rectangles;
|
||||
object_container<point2d_obj_t> complex_points;
|
||||
object_container<line2d_obj_t> complex_lines;
|
||||
size_t draw_count = 0;
|
||||
f32 z_min = std::numeric_limits<f32>::max();
|
||||
f32 z_max = std::numeric_limits<f32>::min();
|
||||
} draw;
|
||||
|
||||
template<typename E>
|
||||
static void insert_obj(object_container<E>& map, const render_info_t& info, const E& obj)
|
||||
{
|
||||
map[info.texture_name].emplace_back(info, obj);
|
||||
}
|
||||
|
||||
// called at the end of the render function
|
||||
void render_reset();
|
||||
|
||||
// called before invocation of draw_objects
|
||||
void pre_reset();
|
||||
|
||||
// called after draw_objects()
|
||||
void post_reset();
|
||||
|
||||
void draw_points(const f32 denominator);
|
||||
|
||||
void draw_lines(const f32 denominator);
|
||||
|
||||
void draw_rectangles(const f32 denominator);
|
||||
|
||||
void draw_objects();
|
||||
|
||||
inline void update_z_index(f32 z_index)
|
||||
{
|
||||
draw.z_min = std::min(draw.z_min, -z_index);
|
||||
draw.z_max = std::max(draw.z_max, -z_index);
|
||||
}
|
||||
|
||||
public:
|
||||
explicit batch_renderer_2d(resource_manager& resources, matrix_state_manager& state): resources(resources), state(state)
|
||||
{
|
||||
engine = pp_engine_t::make_multi_pp(std::make_unique<pp_render_target_t>());
|
||||
}
|
||||
|
||||
explicit batch_renderer_2d(resource_manager& resources, matrix_state_manager& state, std::unique_ptr<pp_engine_t> ppEngine):
|
||||
engine(std::move(ppEngine)), resources(resources), state(state)
|
||||
{}
|
||||
|
||||
void create();
|
||||
|
||||
void drawRectangleInternal(std::string_view texture, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawRectangleInternal(const vec4& color, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawRectangleInternal(const render_info_t& draw_info, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(std::string_view texture, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(const vec4& color, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(const render_info_t& draw_info, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(std::string_view texture, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(const vec4& color, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(const render_info_t& draw_info, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawRectangle(const T& render_info, P... p)
|
||||
{ drawRectangleInternal(render_info, {p...}); }
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawPoint(const T& render_info, P... p)
|
||||
{ drawPointInternal(render_info, {p...}); }
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawLine(const T& render_info, P... p)
|
||||
{ drawLineInternal(render_info, {p...}); }
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawRectangle(const T& render_info, f32 z_index, P... p)
|
||||
{ drawRectangleInternal(render_info, {p...}, z_index); }
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawPoint(const T& render_info, f32 z_index, P... p)
|
||||
{ drawPointInternal(render_info, {p...}, z_index); }
|
||||
|
||||
template<typename T, typename... P>
|
||||
void drawLine(const T& render_info, f32 z_index, P... p)
|
||||
{ drawLineInternal(render_info, {p...}, z_index); }
|
||||
|
||||
void render(i32 width, i32 height, bool transparency = true);
|
||||
|
||||
void cleanup();
|
||||
|
||||
[[nodiscard]] size_t draw_count() const
|
||||
{ return draw.draw_count; }
|
||||
};
|
||||
struct rectangle2d_t
|
||||
{
|
||||
vec2f pos, size;
|
||||
f32 rotation = 0;
|
||||
|
||||
rectangle2d_t(const f32 x, const f32 y, const f32 width, const f32 height, const f32 rotation): pos(x, y), size(width, height),
|
||||
rotation(rotation)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const f32 x, const f32 y, const f32 width, const f32 height): pos(x, y), size(width, height)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const vec2f pos, const vec2f size, const f32 rotation): pos(pos), size(size), rotation(rotation)
|
||||
{}
|
||||
|
||||
rectangle2d_t(const vec2f pos, const vec2f size): pos(pos), size(size)
|
||||
{}
|
||||
};
|
||||
|
||||
struct line2d_t
|
||||
{
|
||||
vec2f p1;
|
||||
vec2f p2;
|
||||
f32 thickness = 1;
|
||||
|
||||
line2d_t(const f32 px1, const f32 py1, const f32 px2, const f32 py2): p1(px1, py1), p2(px2, py2)
|
||||
{}
|
||||
|
||||
line2d_t(const vec2f p1, const vec2f p2): p1(p1), p2(p2)
|
||||
{}
|
||||
|
||||
line2d_t(const f32 px1, const f32 py1, const f32 px2, const f32 py2, const f32 thickness): p1(px1, py1), p2(px2, py2), thickness(thickness)
|
||||
{}
|
||||
|
||||
line2d_t(const vec2f p1, const vec2f p2, const f32 thickness): p1(p1), p2(p2), thickness(thickness)
|
||||
{}
|
||||
};
|
||||
|
||||
struct point2d_t
|
||||
{
|
||||
vec2f pos;
|
||||
float scale = 1;
|
||||
|
||||
point2d_t(const float x, const float y, const float scale): pos(x, y), scale(scale)
|
||||
{}
|
||||
|
||||
point2d_t(const float x, const float y): pos(x, y)
|
||||
{}
|
||||
|
||||
point2d_t(const vec2f pos, const float scale): pos(pos), scale(scale)
|
||||
{}
|
||||
|
||||
explicit point2d_t(const vec2f pos): pos(pos)
|
||||
{}
|
||||
|
||||
point2d_t apply_scale(float s) const
|
||||
{
|
||||
return {pos, scale * s};
|
||||
}
|
||||
};
|
||||
|
||||
struct render_info_t
|
||||
{
|
||||
private:
|
||||
constexpr static color4 disabled = color4(0, 0, 0, 0);
|
||||
constexpr static vec4 empty{0, 0, 0, 1};
|
||||
constexpr static vec4 full{1, 1, 1, 1};
|
||||
|
||||
render_info_t(const std::string_view texture, color4 color, color4 blend): texture_name(texture), color(color), blend(blend)
|
||||
{}
|
||||
|
||||
public:
|
||||
// texture to use
|
||||
std::string texture_name;
|
||||
// color to use
|
||||
color4 color;
|
||||
// how much to blend the texture into the color? note blending is always additive!
|
||||
color4 blend;
|
||||
|
||||
render_info_t() = default;
|
||||
|
||||
static render_info_t make_info(const std::string_view texture)
|
||||
{
|
||||
render_info_t info{texture, empty, full};
|
||||
return info;
|
||||
}
|
||||
|
||||
static render_info_t make_info(const color4 color)
|
||||
{
|
||||
render_info_t info{"", color, empty};
|
||||
return info;
|
||||
}
|
||||
|
||||
static render_info_t make_info(const std::string_view texture, const color4 color, const color4 blend)
|
||||
{
|
||||
render_info_t info{texture, color, blend};
|
||||
return info;
|
||||
}
|
||||
};
|
||||
|
||||
class batch_renderer_2d
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<pp_engine_t> engine;
|
||||
|
||||
template <typename T>
|
||||
struct render_object_t
|
||||
{
|
||||
f32 z_index;
|
||||
T obj;
|
||||
|
||||
render_object_t(const float z_index, const T obj): z_index(z_index), obj(obj)
|
||||
{}
|
||||
};
|
||||
|
||||
using rectangle2d_obj_t = render_object_t<rectangle2d_t>;
|
||||
using point2d_obj_t = render_object_t<point2d_t>;
|
||||
using line2d_obj_t = render_object_t<line2d_t>;
|
||||
|
||||
template <typename T>
|
||||
using object_container = hashmap_t<std::string, std::vector<std::pair<render_info_t, T>>>;
|
||||
|
||||
private:
|
||||
std::unique_ptr<vertex_array_t> square_vao;
|
||||
std::unique_ptr<vertex_array_t> line_vao;
|
||||
std::unique_ptr<shader_t> square_shader;
|
||||
std::unique_ptr<shader_t> point_shader;
|
||||
resource_manager& resources;
|
||||
matrix_state_manager& state;
|
||||
|
||||
// texture name -> draw info
|
||||
struct
|
||||
{
|
||||
object_container<rectangle2d_obj_t> complex_rectangles;
|
||||
object_container<point2d_obj_t> complex_points;
|
||||
object_container<line2d_obj_t> complex_lines;
|
||||
size_t draw_count = 0;
|
||||
f32 z_min = std::numeric_limits<f32>::max();
|
||||
f32 z_max = std::numeric_limits<f32>::min();
|
||||
} draw;
|
||||
|
||||
template <typename E>
|
||||
static void insert_obj(object_container<E>& map, const render_info_t& info, const E& obj)
|
||||
{
|
||||
map[info.texture_name].emplace_back(info, obj);
|
||||
}
|
||||
|
||||
// called at the end of the render function
|
||||
void render_reset();
|
||||
|
||||
// called before invocation of draw_objects
|
||||
void pre_reset();
|
||||
|
||||
// called after draw_objects()
|
||||
void post_reset();
|
||||
|
||||
void draw_points(const f32 denominator);
|
||||
|
||||
void draw_lines(const f32 denominator);
|
||||
|
||||
void draw_rectangles(const f32 denominator);
|
||||
|
||||
void draw_objects();
|
||||
|
||||
inline void update_z_index(f32 z_index)
|
||||
{
|
||||
draw.z_min = std::min(draw.z_min, -z_index);
|
||||
draw.z_max = std::max(draw.z_max, -z_index);
|
||||
}
|
||||
|
||||
public:
|
||||
explicit batch_renderer_2d(resource_manager& resources, matrix_state_manager& state): resources(resources), state(state)
|
||||
{
|
||||
engine = pp_engine_t::make_multi_pp(std::make_unique<pp_render_target_t>());
|
||||
}
|
||||
|
||||
explicit batch_renderer_2d(resource_manager& resources, matrix_state_manager& state, std::unique_ptr<pp_engine_t> ppEngine):
|
||||
engine(std::move(ppEngine)), resources(resources), state(state)
|
||||
{}
|
||||
|
||||
void create();
|
||||
|
||||
void drawRectangleInternal(std::string_view texture, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawRectangleInternal(const vec4& color, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawRectangleInternal(const render_info_t& draw_info, const rectangle2d_t& rectangle, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(std::string_view texture, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(const vec4& color, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawLineInternal(const render_info_t& draw_info, const line2d_t& line, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(std::string_view texture, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(const vec4& color, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
void drawPointInternal(const render_info_t& draw_info, const point2d_t& point, f32 z_index = 0);
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawRectangle(const T& render_info, P... p)
|
||||
{
|
||||
drawRectangleInternal(render_info, {p...});
|
||||
}
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawPoint(const T& render_info, P... p)
|
||||
{
|
||||
drawPointInternal(render_info, {p...});
|
||||
}
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawLine(const T& render_info, P... p)
|
||||
{
|
||||
drawLineInternal(render_info, {p...});
|
||||
}
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawRectangle(const T& render_info, f32 z_index, P... p)
|
||||
{
|
||||
drawRectangleInternal(render_info, {p...}, z_index);
|
||||
}
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawPoint(const T& render_info, f32 z_index, P... p)
|
||||
{
|
||||
drawPointInternal(render_info, {p...}, z_index);
|
||||
}
|
||||
|
||||
template <typename T, typename... P>
|
||||
void drawLine(const T& render_info, f32 z_index, P... p)
|
||||
{
|
||||
drawLineInternal(render_info, {p...}, z_index);
|
||||
}
|
||||
|
||||
void render(i32 width, i32 height, bool transparency = true);
|
||||
|
||||
void cleanup();
|
||||
|
||||
[[nodiscard]] shader_t& get_square_shader() const
|
||||
{
|
||||
return *square_shader;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t draw_count() const
|
||||
{
|
||||
return draw.draw_count;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H
|
||||
|
|
Loading…
Reference in New Issue