diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a9dc5e..de7d632 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/include/blt/gfx/renderer/2d_textured_circle.frag b/include/blt/gfx/renderer/2d_textured_circle.frag new file mode 100644 index 0000000..14d7031 --- /dev/null +++ b/include/blt/gfx/renderer/2d_textured_circle.frag @@ -0,0 +1,28 @@ +#ifdef __cplusplus +#include +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 \ No newline at end of file diff --git a/include/blt/gfx/renderer/batch_2d_renderer.h b/include/blt/gfx/renderer/batch_2d_renderer.h index 678b8c4..1864671 100644 --- a/include/blt/gfx/renderer/batch_2d_renderer.h +++ b/include/blt/gfx/renderer/batch_2d_renderer.h @@ -28,6 +28,8 @@ #include #include #include +#include +#include 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(key.x()) ^ type_cast(key.y()) ^ type_cast(key.y()) ^ type_cast(key.z()); } }; - + + struct render_object_t + { + std::string texture; + blt::vec4 color; + blt::vec4 texture_blend_factor; + std::variant 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, vec_hash>, vec_hash>> complex_rectangles; - blt::hashmap_t, vec_hash>, vec_hash>> complex_points; - blt::hashmap_t, vec_hash>, vec_hash>> complex_lines; + // z-index -> draw object + std::map> 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 @@ -192,6 +205,24 @@ namespace blt::gfx drawLine(render_info, {p...}); } + template + inline void drawRectangle(const T& render_info, blt::i32 z_index, P... p) + { + drawRectangle(render_info, {p...}, z_index); + } + + template + inline void drawPoint(const T& render_info, blt::i32 z_index, P... p) + { + drawPoint(render_info, {p...}, z_index); + } + + template + 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(); diff --git a/libraries/BLT b/libraries/BLT index 325508e..9db3f12 160000 --- a/libraries/BLT +++ b/libraries/BLT @@ -1 +1 @@ -Subproject commit 325508e807f376fb7d287dbb9d80899eddb4e8ff +Subproject commit 9db3f120489ff27aa560e488d82b5ae0d64019df diff --git a/libraries/imgui b/libraries/imgui index a1b0682..231cbee 160000 --- a/libraries/imgui +++ b/libraries/imgui @@ -1 +1 @@ -Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5 +Subproject commit 231cbee0fc4f59dbe5b8b853a11b08dc84e57c65 diff --git a/libraries/openal-soft b/libraries/openal-soft index 6675317..111397c 160000 --- a/libraries/openal-soft +++ b/libraries/openal-soft @@ -1 +1 @@ -Subproject commit 6675317107257c2cc16c947b359d557821d85bf2 +Subproject commit 111397c71a5f1c2c88e05da9c84edfdba2e472a4 diff --git a/src/blt/gfx/renderer/batch_2d_renderer.cpp b/src/blt/gfx/renderer/batch_2d_renderer.cpp index 8d3c5f0..94d8f6a 100644 --- a/src/blt/gfx/renderer/batch_2d_renderer.cpp +++ b/src/blt/gfx/renderer/batch_2d_renderer.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include // 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);