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

250 lines
9.3 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/renderer/resource_manager.h"
#include <blt/std/hashmap.h>
#include <blt/std/memory_util.h>
#include <blt/math/vectors.h>
#include <string_view>
#include <vector>
2024-02-17 14:38:18 -05:00
#include <memory>
2024-04-12 17:37:35 -04:00
#include <map>
#include <variant>
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-04-11 20:40:28 -04:00
blt::vec2f pos, size;
2024-04-14 17:34:56 -04:00
blt::f32 rotation = 0;
2024-04-14 17:34:56 -04:00
rectangle2d_t(blt::f32 x, blt::f32 y, blt::f32 width, blt::f32 height, blt::f32 rotation): pos(x, y), size(width, height), rotation(rotation)
{}
2024-04-14 17:34:56 -04:00
rectangle2d_t(blt::f32 x, blt::f32 y, blt::f32 width, blt::f32 height): pos(x, y), size(width, height)
2024-04-11 20:40:28 -04:00
{}
2024-04-14 17:34:56 -04:00
rectangle2d_t(blt::vec2f pos, blt::vec2f size, blt::f32 rotation): pos(pos), size(size), rotation(rotation)
2024-04-11 20:40:28 -04:00
{}
rectangle2d_t(blt::vec2f pos, blt::vec2f size): pos(pos), size(size)
{}
};
struct line2d_t
{
blt::vec2f p1;
blt::vec2f p2;
2024-04-14 17:34:56 -04:00
blt::f32 thickness = 1;
2024-04-11 20:40:28 -04:00
2024-04-14 17:34:56 -04:00
line2d_t(blt::f32 px1, blt::f32 py1, blt::f32 px2, blt::f32 py2): p1(px1, py1), p2(px2, py2)
2024-04-11 20:40:28 -04:00
{}
line2d_t(blt::vec2f p1, blt::vec2f p2): p1(p1), p2(p2)
{}
2024-04-12 00:10:29 -04:00
2024-04-14 17:34:56 -04:00
line2d_t(blt::f32 px1, blt::f32 py1, blt::f32 px2, blt::f32 py2, blt::f32 thickness): p1(px1, py1), p2(px2, py2), thickness(thickness)
2024-04-12 00:10:29 -04:00
{}
2024-04-14 17:34:56 -04:00
line2d_t(blt::vec2f p1, blt::vec2f p2, blt::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
{
blt::vec2f pos;
float scale = 1;
point2d_t(float x, float y, float scale): pos(x, y), scale(scale)
{}
point2d_t(float x, float y): pos(x, y)
{}
2024-04-12 17:37:35 -04:00
point2d_t(vec2f pos, float scale): pos(pos), scale(scale)
{}
explicit point2d_t(vec2f pos): pos(pos)
{}
};
2023-12-30 03:26:06 -05:00
struct draw_state
2024-01-01 22:15:30 -05:00
{
// texture to use
std::string texture_name;
// color to use
blt::color4 color;
// how much to blend the texture into the color? note blending is always additive!
blt::color4 blend;
// should we outline this object?
bool outline;
// what color should we outline with?
blt::color4 outline_color;
2024-01-01 22:15:30 -05:00
};
class batch_renderer_2d
{
private:
struct vec_hash
{
std::size_t operator()(const blt::vec4& key) const
{
using namespace blt::mem;
2024-04-11 20:40:28 -04:00
return type_cast<blt::i32>(key.x()) ^ type_cast<blt::i32>(key.y()) ^ type_cast<blt::i32>(key.y()) ^ type_cast<blt::i32>(key.z());
}
};
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-04-14 17:34:56 -04:00
blt::f32 z_index;
T obj;
render_object_t(float z_index, T obj): z_index(z_index), obj(obj)
{}
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>;
private:
vertex_array* square_vao = nullptr;
2024-04-11 20:40:28 -04:00
vertex_array* line_vao = nullptr;
shader_t* square_shader = nullptr;
shader_t* point_shader = nullptr;
resource_manager& resources;
2024-04-14 17:34:56 -04:00
// texture -> color -> blend factor -> list of rectangles
blt::hashmap_t<std::string, blt::hashmap_t<blt::vec4, blt::hashmap_t<blt::vec4, std::vector<rectangle2d_obj_t>, vec_hash>, vec_hash>> complex_rectangles;
blt::hashmap_t<std::string, blt::hashmap_t<blt::vec4, blt::hashmap_t<blt::vec4, std::vector<point2d_obj_t>, vec_hash>, vec_hash>> complex_points;
blt::hashmap_t<std::string, blt::hashmap_t<blt::vec4, blt::hashmap_t<blt::vec4, std::vector<line2d_obj_t>, vec_hash>, vec_hash>> complex_lines;
size_t draw_count_ = 0;
2024-01-01 22:15:30 -05:00
public:
explicit batch_renderer_2d(resource_manager& resources): resources(resources)
{}
void create();
inline void drawRectangleInternal(std::string_view texture, const rectangle2d_t& rectangle, blt::f32 z_index = 0)
{
2024-01-18 14:14:13 -05:00
const static blt::vec4 empty{0, 0, 0, 1};
2024-01-02 13:03:51 -05:00
const static blt::vec4 full{1, 1, 1, 1};
2024-04-14 17:37:17 -04:00
complex_rectangles[texture][empty][full].emplace_back(-z_index, rectangle);
}
inline void drawRectangleInternal(const blt::vec4& color, const rectangle2d_t& rectangle, blt::f32 z_index = 0)
{
2024-01-02 13:03:51 -05:00
const static blt::vec4 empty{0, 0, 0, 0};
2024-04-14 17:37:17 -04:00
complex_rectangles[""][color][empty].emplace_back(-z_index, rectangle);
}
inline void drawRectangleInternal(const draw_state& draw_info, const rectangle2d_t& rectangle, blt::f32 z_index = 0)
{
2024-04-14 17:37:17 -04:00
complex_rectangles[draw_info.texture_name][draw_info.color][draw_info.blend].emplace_back(-z_index, rectangle);
}
inline void drawLineInternal(std::string_view texture, const line2d_t& line, blt::f32 z_index = 0)
2024-04-12 00:10:29 -04:00
{
const static blt::vec4 empty{0, 0, 0, 1};
const static blt::vec4 full{1, 1, 1, 1};
2024-04-14 17:37:17 -04:00
complex_lines[texture][empty][full].emplace_back(-z_index, line);
2024-04-12 00:10:29 -04:00
}
inline void drawLineInternal(const blt::vec4& color, const line2d_t& line, blt::f32 z_index = 0)
2024-04-11 20:40:28 -04:00
{
const static blt::vec4 empty{0, 0, 0, 0};
2024-04-14 17:37:17 -04:00
complex_lines[""][color][empty].emplace_back(-z_index, line);
2024-04-11 20:40:28 -04:00
}
inline void drawLineInternal(const draw_state& draw_info, const line2d_t& line, blt::f32 z_index = 0)
2024-04-11 20:40:28 -04:00
{
2024-04-14 17:37:17 -04:00
complex_lines[draw_info.texture_name][draw_info.color][draw_info.blend].emplace_back(-z_index, line);
2024-04-11 20:40:28 -04:00
}
inline void drawPointInternal(std::string_view texture, const point2d_t& point, blt::f32 z_index = 0)
2024-04-11 20:40:28 -04:00
{
const static blt::vec4 empty{0, 0, 0, 1};
const static blt::vec4 full{1, 1, 1, 1};
2024-04-14 17:37:17 -04:00
complex_points[texture][empty][full].emplace_back(-z_index, point);
2024-04-11 20:40:28 -04:00
}
inline void drawPointInternal(const blt::vec4& color, const point2d_t& point, blt::f32 z_index = 0)
2024-04-11 20:40:28 -04:00
{
const static blt::vec4 empty{0, 0, 0, 0};
2024-04-14 17:37:17 -04:00
complex_points[""][color][empty].emplace_back(-z_index, point);
2024-04-11 20:40:28 -04:00
}
inline void drawPointInternal(const draw_state& draw_info, const point2d_t& point, blt::f32 z_index = 0)
2024-04-11 20:40:28 -04:00
{
2024-04-14 17:37:17 -04:00
complex_points[draw_info.texture_name][draw_info.color][draw_info.blend].emplace_back(-z_index, point);
2024-04-11 20:40:28 -04:00
}
2024-01-02 13:03:51 -05:00
template<typename T, typename... P>
inline 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>
inline 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>
inline 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-04-14 17:34:56 -04:00
inline void drawRectangle(const T& render_info, blt::f32 z_index, P... p)
2024-04-12 17:37:35 -04:00
{
drawRectangleInternal(render_info, {p...}, z_index);
2024-04-12 17:37:35 -04:00
}
template<typename T, typename... P>
2024-04-14 17:34:56 -04:00
inline void drawPoint(const T& render_info, blt::f32 z_index, P... p)
2024-04-12 17:37:35 -04:00
{
drawPointInternal(render_info, {p...}, z_index);
2024-04-12 17:37:35 -04:00
}
template<typename T, typename... P>
2024-04-14 17:34:56 -04:00
inline void drawLine(const T& render_info, blt::f32 z_index, P... p)
2024-04-12 17:37:35 -04:00
{
drawLineInternal(render_info, {p...}, z_index);
2024-04-12 17:37:35 -04:00
}
2024-01-02 13:03:51 -05:00
void render(bool transparency = true);
2024-01-01 22:15:30 -05:00
void cleanup();
2024-04-11 20:40:28 -04:00
[[nodiscard]] inline size_t draw_count() const
2024-01-02 13:03:51 -05:00
{
return 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