BLT-With-Graphics-Template/include/blt/gfx/renderer/batch_2d_renderer.h

278 lines
11 KiB
C
Raw Normal View History

2023-12-29 19:26:41 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H
#define BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H
2024-01-01 22:15:30 -05:00
#include "blt/gfx/model.h"
#include "blt/gfx/shader.h"
#include "blt/gfx/framebuffer.h"
#include "blt/gfx/renderer/resource_manager.h"
2024-05-03 22:14:48 -04:00
#include "blt/gfx/renderer/postprocess.h"
#include <blt/std/hashmap.h>
#include <blt/std/memory_util.h>
#include <blt/math/vectors.h>
#include <string_view>
#include <vector>
2024-01-01 22:15:30 -05:00
2023-12-29 19:26:41 -05:00
namespace blt::gfx
{
2024-01-02 17:10:29 -05:00
struct rectangle2d_t
{
2024-05-03 16:25:02 -04:00
vec2f pos, size;
f32 rotation = 0;
2024-05-03 16:25:02 -04:00
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)
{}
2024-05-03 16:25:02 -04:00
rectangle2d_t(const f32 x, const f32 y, const f32 width, const f32 height): pos(x, y), size(width, height)
2024-04-11 20:40:28 -04:00
{}
2024-05-03 16:25:02 -04:00
rectangle2d_t(const vec2f pos, const vec2f size, const f32 rotation): pos(pos), size(size), rotation(rotation)
2024-04-11 20:40:28 -04:00
{}
2024-05-03 16:25:02 -04:00
rectangle2d_t(const vec2f pos, const vec2f size): pos(pos), size(size)
2024-04-11 20:40:28 -04:00
{}
};
struct line2d_t
{
2024-05-03 16:25:02 -04:00
vec2f p1;
vec2f p2;
f32 thickness = 1;
2024-04-11 20:40:28 -04:00
2024-05-03 16:25:02 -04:00
line2d_t(const f32 px1, const f32 py1, const f32 px2, const f32 py2): p1(px1, py1), p2(px2, py2)
2024-04-11 20:40:28 -04:00
{}
2024-05-03 16:25:02 -04:00
line2d_t(const vec2f p1, const vec2f p2): p1(p1), p2(p2)
2024-04-11 20:40:28 -04:00
{}
2024-04-12 00:10:29 -04:00
2024-05-03 16:25:02 -04:00
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)
2024-04-12 00:10:29 -04:00
{}
2024-05-03 16:25:02 -04:00
line2d_t(const vec2f p1, const vec2f p2, const f32 thickness): p1(p1), p2(p2), thickness(thickness)
2024-04-12 00:10:29 -04:00
{}
2024-04-11 20:40:28 -04:00
};
struct point2d_t
{
2024-05-03 16:25:02 -04:00
vec2f pos;
2024-04-11 20:40:28 -04:00
float scale = 1;
2024-05-03 16:25:02 -04:00
point2d_t(const float x, const float y, const float scale): pos(x, y), scale(scale)
2024-04-11 20:40:28 -04:00
{}
2024-05-03 16:25:02 -04:00
point2d_t(const float x, const float y): pos(x, y)
{}
2024-04-12 17:37:35 -04:00
2024-05-03 16:25:02 -04:00
point2d_t(const vec2f pos, const float scale): pos(pos), scale(scale)
2024-04-12 17:37:35 -04:00
{}
2024-05-03 16:25:02 -04:00
explicit point2d_t(const vec2f pos): pos(pos)
2024-04-12 17:37:35 -04:00
{}
};
2023-12-30 03:26:06 -05:00
2024-05-03 16:25:02 -04:00
struct render_info_t
2024-01-01 22:15:30 -05:00
{
2024-05-03 16:25:02 -04:00
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;
// should we outline this object?
bool outline = false;
// what color should we outline with?
color4 outline_color;
render_info_t() = default;
static render_info_t make_info(const std::string_view texture, const color4 outline = disabled)
{
render_info_t info{texture, empty, full};
if (outline != disabled)
{
info.outline = true;
info.outline_color = outline;
}
return info;
}
static render_info_t make_info(const color4 color, const color4 outline = disabled)
{
render_info_t info{"", color, empty};
if (outline != disabled)
{
info.outline = true;
info.outline_color = outline;
}
return info;
}
static render_info_t make_info(const std::string_view texture, const color4 color, const color4 blend, const color4 outline = disabled)
{
render_info_t info{texture, color, blend};
if (outline != disabled)
{
info.outline = true;
info.outline_color = outline;
}
return info;
}
2024-01-01 22:15:30 -05:00
};
class batch_renderer_2d
{
private:
2024-05-05 02:29:45 -04:00
std::unique_ptr<pp_engine_t> engine;
2024-04-12 17:37:35 -04:00
2024-04-14 17:34:56 -04:00
template<typename T>
2024-04-12 17:37:35 -04:00
struct render_object_t
{
2024-05-03 16:25:02 -04:00
f32 z_index;
2024-04-14 17:34:56 -04:00
T obj;
2024-05-03 16:25:02 -04:00
render_object_t(const float z_index, const T obj): z_index(z_index), obj(obj)
2024-04-14 17:34:56 -04:00
{}
2024-04-12 17:37:35 -04:00
};
2024-04-14 17:34:56 -04:00
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>;
2024-05-03 16:25:02 -04:00
template<typename T>
using object_container = hashmap_t<std::string, std::vector<std::pair<render_info_t, T>>>;
private:
vertex_array_t* square_vao = nullptr;
vertex_array_t* line_vao = nullptr;
2024-04-11 20:40:28 -04:00
shader_t* square_shader = nullptr;
shader_t* point_shader = nullptr;
resource_manager& resources;
2024-05-05 17:03:50 -04:00
matrix_state_manager& state;
2024-05-03 16:25:02 -04:00
// 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_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);
}
2024-01-01 22:15:30 -05:00
public:
2024-05-05 17:03:50 -04:00
explicit batch_renderer_2d(resource_manager& resources, matrix_state_manager& state): resources(resources), state(state)
2024-05-05 02:29:45 -04:00
{
2024-05-05 17:03:50 -04:00
engine = pp_engine_t::make_multi_pp(std::make_unique<pp_outline_target_t>(),
std::make_unique<pp_blur_step_inplace_t>(state, frame_buffer_t::attachment_t::COLOR1),
std::make_unique<pp_expansion_step_inplace_t>(frame_buffer_t::attachment_t::COLOR1,
vec4{4, 4, 4, 1}),
std::make_unique<pp_overlay_blur_step_t>(frame_buffer_t::attachment_t::COLOR1, 2, 2),
2024-05-05 17:03:50 -04:00
std::make_unique<pp_outline_step_t>(state)
);
2024-05-05 02:29:45 -04:00
}
void create();
2024-05-03 16:25:02 -04:00
void drawRectangleInternal(std::string_view texture, const rectangle2d_t& rectangle, f32 z_index = 0);
2024-05-03 16:25:02 -04:00
void drawRectangleInternal(const vec4& color, const rectangle2d_t& rectangle, f32 z_index = 0);
2024-05-03 16:25:02 -04:00
void drawRectangleInternal(const render_info_t& draw_info, const rectangle2d_t& rectangle, f32 z_index = 0);
2024-05-03 16:25:02 -04:00
void drawLineInternal(std::string_view texture, const line2d_t& line, f32 z_index = 0);
2024-04-12 00:10:29 -04:00
2024-05-03 16:25:02 -04:00
void drawLineInternal(const vec4& color, const line2d_t& line, f32 z_index = 0);
2024-04-11 20:40:28 -04:00
2024-05-03 16:25:02 -04:00
void drawLineInternal(const render_info_t& draw_info, const line2d_t& line, f32 z_index = 0);
2024-04-11 20:40:28 -04:00
2024-05-03 16:25:02 -04:00
void drawPointInternal(std::string_view texture, const point2d_t& point, f32 z_index = 0);
2024-04-11 20:40:28 -04:00
2024-05-03 16:25:02 -04:00
void drawPointInternal(const vec4& color, const point2d_t& point, f32 z_index = 0);
2024-04-11 20:40:28 -04:00
2024-05-03 16:25:02 -04:00
void drawPointInternal(const render_info_t& draw_info, const point2d_t& point, f32 z_index = 0);
2024-04-11 20:40:28 -04:00
2024-01-02 13:03:51 -05:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawRectangle(const T& render_info, P... p)
{ drawRectangleInternal(render_info, {p...}); }
2024-01-02 13:03:51 -05:00
2024-04-11 20:40:28 -04:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawPoint(const T& render_info, P... p)
{ drawPointInternal(render_info, {p...}); }
2024-04-11 20:40:28 -04:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawLine(const T& render_info, P... p)
{ drawLineInternal(render_info, {p...}); }
2024-04-11 20:40:28 -04:00
2024-04-12 17:37:35 -04:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawRectangle(const T& render_info, f32 z_index, P... p)
{ drawRectangleInternal(render_info, {p...}, z_index); }
2024-04-12 17:37:35 -04:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawPoint(const T& render_info, f32 z_index, P... p)
{ drawPointInternal(render_info, {p...}, z_index); }
2024-04-12 17:37:35 -04:00
template<typename T, typename... P>
2024-05-03 16:25:02 -04:00
void drawLine(const T& render_info, f32 z_index, P... p)
{ drawLineInternal(render_info, {p...}, z_index); }
2024-04-12 17:37:35 -04:00
void render(i32 width, i32 height, bool transparency = true);
2024-01-01 22:15:30 -05:00
void cleanup();
2024-05-03 16:25:02 -04:00
[[nodiscard]] size_t draw_count() const
{ return draw.draw_count; }
2024-01-01 22:15:30 -05:00
};
2023-12-29 19:26:41 -05:00
}
#endif //BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H