lack of lines

main
Brett 2024-04-12 17:37:35 -04:00
parent d40b917c32
commit 8b6686fa3f
7 changed files with 121 additions and 29 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.25)
set(BLT_GRAPHICS_VERSION 0.9.6)
set(BLT_GRAPHICS_VERSION 0.9.7)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})

View File

@ -0,0 +1,28 @@
#ifdef __cplusplus
#include <string>
const std::string shader_2d_textured_cirlce_frag = R"("
#version 300 es
precision mediump float;
out vec4 FragColor;
in vec2 uv;
in vec2 pos;
uniform sampler2D tex;
uniform vec4 color;
uniform vec4 use_texture;
vec4 linear_iter(vec4 i, vec4 p, float factor){
return (i + p) * factor;
}
void main() {
float xs = pos.x * pos.x;
float ys = pos.y * pos.y;
if (xs + ys > 0.5 * 0.5)
discard;
FragColor = (texture(tex, uv) * use_texture) + color;
}
")";
#endif

View File

@ -28,6 +28,8 @@
#include <string_view>
#include <vector>
#include <memory>
#include <map>
#include <variant>
namespace blt::gfx
{
@ -78,6 +80,12 @@ namespace blt::gfx
point2d_t(float x, float y): pos(x, y)
{}
point2d_t(vec2f pos, float scale): pos(pos), scale(scale)
{}
explicit point2d_t(vec2f pos): pos(pos)
{}
};
struct draw_state
@ -101,18 +109,23 @@ namespace blt::gfx
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());
}
};
struct render_object_t
{
std::string texture;
blt::vec4 color;
blt::vec4 texture_blend_factor;
std::variant<rectangle2d_t, point2d_t, line2d_t> t;
};
private:
vertex_array* square_vao = nullptr;
vertex_array* line_vao = nullptr;
shader_t* square_shader = nullptr;
shader_t* line_shader = nullptr;
shader_t* point_shader = nullptr;
resource_manager& resources;
// 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_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_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_t>, vec_hash>, vec_hash>> complex_lines;
// z-index -> draw object
std::map<blt::i32, std::vector<render_object_t>> draw_objects;
size_t draw_count_ = 0;
public:
explicit batch_renderer_2d(resource_manager& resources): resources(resources)
@ -120,58 +133,58 @@ namespace blt::gfx
void create();
inline void drawRectangle(std::string_view texture, const rectangle2d_t& rectangle)
inline void drawRectangle(std::string_view texture, const rectangle2d_t& rectangle, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 1};
const static blt::vec4 full{1, 1, 1, 1};
complex_rectangles[texture][empty][full].push_back(rectangle);
draw_objects[z_index].push_back(render_object_t{std::string(texture), empty, full, rectangle});
}
inline void drawRectangle(const blt::vec4& color, const rectangle2d_t& rectangle)
inline void drawRectangle(const blt::vec4& color, const rectangle2d_t& rectangle, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 0};
complex_rectangles[""][color][empty].push_back(rectangle);
draw_objects[z_index].push_back(render_object_t{"", color, empty, rectangle});
}
inline void drawRectangle(const draw_state& draw_info, const rectangle2d_t& rectangle)
inline void drawRectangle(const draw_state& draw_info, const rectangle2d_t& rectangle, blt::i32 z_index = 0)
{
complex_rectangles[draw_info.texture_name][draw_info.color][draw_info.blend].push_back(rectangle);
draw_objects[z_index].push_back(render_object_t{draw_info.texture_name, draw_info.color, draw_info.blend, rectangle});
}
inline void drawLine(std::string_view texture, const line2d_t& line)
inline void drawLine(std::string_view texture, const line2d_t& line, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 1};
const static blt::vec4 full{1, 1, 1, 1};
complex_lines[texture][empty][full].push_back(line);
draw_objects[z_index].push_back(render_object_t{std::string(texture), empty, full, line});
}
inline void drawLine(const blt::vec4& color, const line2d_t& line)
inline void drawLine(const blt::vec4& color, const line2d_t& line, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 0};
complex_lines[""][color][empty].push_back(line);
draw_objects[z_index].push_back(render_object_t{"", color, empty, line});
}
inline void drawLine(const draw_state& draw_info, const line2d_t& line)
inline void drawLine(const draw_state& draw_info, const line2d_t& line, blt::i32 z_index = 0)
{
complex_lines[draw_info.texture_name][draw_info.color][draw_info.blend].push_back(line);
draw_objects[z_index].push_back(render_object_t{draw_info.texture_name, draw_info.color, draw_info.blend, line});
}
inline void drawPoint(std::string_view texture, const point2d_t& point)
inline void drawPoint(std::string_view texture, const point2d_t& point, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 1};
const static blt::vec4 full{1, 1, 1, 1};
complex_points[texture][empty][full].push_back(point);
draw_objects[z_index].push_back(render_object_t{std::string(texture), empty, full, point});
}
inline void drawPoint(const blt::vec4& color, const point2d_t& point)
inline void drawPoint(const blt::vec4& color, const point2d_t& point, blt::i32 z_index = 0)
{
const static blt::vec4 empty{0, 0, 0, 0};
complex_points[""][color][empty].push_back(point);
draw_objects[z_index].push_back(render_object_t{"", color, empty, point});
}
inline void drawPoint(const draw_state& draw_info, const point2d_t& point)
inline void drawPoint(const draw_state& draw_info, const point2d_t& point, blt::i32 z_index = 0)
{
complex_points[draw_info.texture_name][draw_info.color][draw_info.blend].push_back(point);
draw_objects[z_index].push_back(render_object_t{draw_info.texture_name, draw_info.color, draw_info.blend, point});
}
template<typename T, typename... P>
@ -192,6 +205,24 @@ namespace blt::gfx
drawLine(render_info, {p...});
}
template<typename T, typename... P>
inline void drawRectangle(const T& render_info, blt::i32 z_index, P... p)
{
drawRectangle(render_info, {p...}, z_index);
}
template<typename T, typename... P>
inline void drawPoint(const T& render_info, blt::i32 z_index, P... p)
{
drawPoint(render_info, {p...}, z_index);
}
template<typename T, typename... P>
inline void drawLine(const T& render_info, blt::i32 z_index, P... p)
{
drawLine(render_info, {p...}, z_index);
}
void render(bool transparency = true);
void cleanup();

@ -1 +1 @@
Subproject commit 325508e807f376fb7d287dbb9d80899eddb4e8ff
Subproject commit 9db3f120489ff27aa560e488d82b5ae0d64019df

@ -1 +1 @@
Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5
Subproject commit 231cbee0fc4f59dbe5b8b853a11b08dc84e57c65

@ -1 +1 @@
Subproject commit 6675317107257c2cc16c947b359d557821d85bf2
Subproject commit 111397c71a5f1c2c88e05da9c84edfdba2e472a4

View File

@ -19,6 +19,7 @@
#include <blt/gfx/renderer/2d_textured.vert>
#include <blt/gfx/renderer/2d_line.vert>
#include <blt/gfx/renderer/2d_textured.frag>
#include <blt/gfx/renderer/2d_textured_circle.frag>
#include <blt/gfx/renderer/2d_line.frag>
// https://stackoverflow.com/questions/60440682/drawing-a-line-in-modern-opengl
@ -86,13 +87,16 @@ namespace blt::gfx
square_shader = new shader_t(shader_2d_textured_vert, shader_2d_textured_frag);
square_shader->bindAttribute(0, "vertex");
square_shader->bindAttribute(1, "uv_in");
point_shader = new shader_t(shader_2d_textured_vert, shader_2d_textured_cirlce_frag);
point_shader->bindAttribute(0, "vertex");
point_shader->bindAttribute(1, "uv_in");
}
void batch_renderer_2d::cleanup()
{
delete square_vao;
delete square_shader;
delete line_shader;
delete point_shader;
}
@ -136,7 +140,36 @@ namespace blt::gfx
textures.second.clear();
}
point_shader->bind();
for (auto& textures : complex_points)
{
// resource manager handles the check for empty string
if (auto val = resources.get(textures.first))
val.value()->bind();
for (auto& colors : textures.second)
{
point_shader->setVec4("color", colors.first);
for (auto& blend_factors : colors.second)
{
point_shader->setVec4("use_texture", blend_factors.first);
for (auto& point : blend_factors.second)
{
blt::mat4x4 model;
model.translate(point.pos.x(), point.pos.y(), 0.0f);
model.scale(point.scale, point.scale, 1);
point_shader->setMatrix("model", model);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
draw_count_++;
}
blend_factors.second.clear();
}
colors.second.clear();
}
textures.second.clear();
}
blt::mat4x4 model;
square_shader->bind();
square_shader->setMatrix("model", model);
line_vao->bind();
auto& buf = line_vao->getBuffer(0);