Skip to content
Snippets Groups Projects
Commit b3f711b1 authored by BATON Theau's avatar BATON Theau
Browse files

Add per layer rendering

parent 8bc0d3ba
No related branches found
No related tags found
No related merge requests found
Showing
with 212 additions and 39 deletions
#version 450 core
out vec4 FragColor;
in flat int Id;
in vec2 Texture;
uniform sampler2D uSampler[32];
uniform uint uTexturesCount;
void main() {
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
for(uint i = 0; i < uTexturesCount; ++i) {
vec4 tcolor = texture(uSampler[i], Texture);
if(tcolor.r != 0.0) {
color.r = tcolor.r;
}
if(tcolor.g != 0.0) {
color.g = tcolor.g;
}
if(tcolor.b != 0.0) {
color.b = tcolor.b;
}
}
FragColor = color;
}
\ No newline at end of file
#version 450 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTex;
out vec2 Texture;
void main() {
Texture = aTex;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}
\ No newline at end of file
...@@ -13,6 +13,7 @@ namespace megu { ...@@ -13,6 +13,7 @@ namespace megu {
this->_texture.setWraping(Texture::Wraping::CLAMP_TO_EDGE, Texture::Axis::S | Texture::Axis::T); this->_texture.setWraping(Texture::Wraping::CLAMP_TO_EDGE, Texture::Axis::S | Texture::Axis::T);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->_texture.identifier(), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->_texture.identifier(), 0);
glGenRenderbuffers(1, &this->_rbo_id); glGenRenderbuffers(1, &this->_rbo_id);
glBindRenderbuffer(GL_RENDERBUFFER, this->_rbo_id); glBindRenderbuffer(GL_RENDERBUFFER, this->_rbo_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
......
#include "Engine.hpp" #include "Engine.hpp"
#define NORMALIZE(X) X/255.f
namespace megu { namespace megu {
GraphicEngine::GraphicEngine(Window & window,float w, float h) GraphicEngine::GraphicEngine(Window & window)
: _renderer(w, h), _window(window) { : _window(window), _renderer(window.width(), window.height()) {
glViewport(0, 0, window.width(), window.height()); glViewport(0, 0, window.width(), window.height());
this->_renderer.setClearColor(0.f, 0.f, 0.f); this->_renderer.setClearColor(NORMALIZE(135.f), NORMALIZE(170.f), NORMALIZE(255.f));
}
void GraphicEngine::push(Priority priority, const Renderer & renderer) {
this->_layers[priority] = std::make_unique<Layer>(renderer);
} }
void GraphicEngine::push(DrawGroup & group, Priority priority) { void GraphicEngine::push(Priority layer_priority, Priority draw_priority, const DrawGroup & group) {
this->_objects[priority] = &group; this->_layers[layer_priority]->push(draw_priority, group);
} }
void GraphicEngine::step() { void GraphicEngine::step() {
if(this->_window.isOpen() && !this->_objects.empty()) { if(this->_window.isOpen()) {
this->_renderer.clear(); // Draw Layers
for(auto & [priority, groups] : this->_objects) { TextureArray textures;
this->_renderer.render(this->_window, *groups, {}); for(auto & [priority, layer] : this->_layers) {
textures.push_back(layer->draw(this->_window, textures));
} }
// Merge Textures
FrameBuffer::BindDefaultFrameBuffer();
this->_renderer.clear();
this->_renderer.render(this->_window, this->_group, textures);
this->_window.swapBuffers(); this->_window.swapBuffers();
} }
} }
......
...@@ -2,26 +2,27 @@ ...@@ -2,26 +2,27 @@
#include <map> #include <map>
#include <engine/graphics/front/group/DrawGroup.hpp> #include <engine/graphics/front/group/DrawGroup.hpp>
#include <engine/graphics/front/group/FrameBufferGroup.hpp>
#include "Renderer.hpp" #include "Renderer.hpp"
#include "FrameBufferGroup.hpp" #include "Layer.hpp"
namespace megu { namespace megu {
class GraphicEngine { class GraphicEngine {
public: public:
GraphicEngine() = delete; GraphicEngine() = delete;
GraphicEngine(Window &, float, float); GraphicEngine(Window &);
~GraphicEngine() = default; ~GraphicEngine() = default;
void push(DrawGroup &, Priority); void push(Priority, const Renderer &);
void push(Priority, Priority, const DrawGroup &);
void step(); void step();
Renderer & tmp_renderer() {return this->_renderer;}
private: private:
std::map<Priority, std::unique_ptr<Layer>> _layers;
Renderer _renderer; Renderer _renderer;
std::map<Priority, DrawGroup *> _objects; FrameBufferGroup _group;
Window & _window; Window & _window;
}; };
......
#include "Layer.hpp"
#include <iostream>
namespace megu {
Layer::Layer(const Renderer & renderer)
: _renderer(renderer),
_frameBuffer(static_cast<GLuint>(renderer.dimension().x), static_cast<GLuint>(renderer.dimension().y)) {}
void Layer::push(Priority priority, const DrawGroup & group) {
this->_objects[priority] = &group;
}
const Texture & Layer::draw(const Window & window, const TextureArray & textures) const {
if(this->_frameBuffer.usable()) {
this->_renderer.setClearColor(0.f, 0.f, 0.f, 0.f);
this->_renderer.clear();
this->_frameBuffer.bind();
for(auto &[priority, group] : this->_objects) {
this->_renderer.render(window, *group, textures);
}
}
return this->_frameBuffer.texture();
}
}
\ No newline at end of file
#pragma once
#include <map>
#include <engine/graphics/back/buffers/FrameBuffer.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp>
#include "Priority.hpp"
#include "Renderer.hpp"
namespace megu {
class Layer {
public:
Layer() = delete;
Layer(const Renderer &);
~Layer() = default;
void push(Priority, const DrawGroup &);
virtual const Texture & draw(const Window &, const TextureArray &) const;
private:
const Renderer & _renderer;
FrameBuffer _frameBuffer;
std::map<Priority, DrawGroup const*> _objects;
};
}
\ No newline at end of file
#pragma once
#include <stddef.h>
namespace megu {
using Priority = size_t;
struct priority_sorter {
bool operator()(const Priority & p1, const Priority & p2) {
return p1 != p2 ? p1 > p2 : &p1 > &p2;
}
};
}
\ No newline at end of file
...@@ -11,11 +11,11 @@ namespace megu { ...@@ -11,11 +11,11 @@ namespace megu {
group.draw(window, this->_view, textures); group.draw(window, this->_view, textures);
} }
void Renderer::clear() { void Renderer::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
void Renderer::setClearColor(float r, float g, float b, float a) { void Renderer::setClearColor(float r, float g, float b, float a) const {
glClearColor(r, g, b, a); glClearColor(r, g, b, a);
} }
} }
\ No newline at end of file
...@@ -17,8 +17,10 @@ namespace megu { ...@@ -17,8 +17,10 @@ namespace megu {
virtual void render(const Window &, const DrawGroup &, const TextureArray &) const; virtual void render(const Window &, const DrawGroup &, const TextureArray &) const;
void clear(); const glm::vec2 dimension() const {return this->_view.dimension();}
void setClearColor(float, float, float, float = 1.f);
void clear() const;
void setClearColor(float, float, float, float = 1.f) const;
private: private:
View _view; View _view;
......
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
#include <engine/graphics/back/textures/Texture.hpp> #include <engine/graphics/back/textures/Texture.hpp>
namespace megu { namespace megu {
using TextureArray = std::set<std::reference_wrapper<const Texture>>; using TextureArray = std::vector<std::reference_wrapper<const Texture>>;
} }
\ No newline at end of file
#include "Plane.hpp"
namespace megu {
Vertices_t Plane::_Vertices = {
-1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f
};
}
\ No newline at end of file
#pragma once
#include <engine/graphics/back/geometry/Geometry.hpp>
namespace megu {
class Plane : public Static_Geometry<Plane, QUADS, layout::FLAT, layout::TEXTURE> {
public:
inline static const Vertices_t & Vertices() {return Plane::_Vertices;}
private:
static Vertices_t _Vertices;
};
}
...@@ -5,14 +5,6 @@ ...@@ -5,14 +5,6 @@
#include <engine/io/Window.hpp> #include <engine/io/Window.hpp>
namespace megu { namespace megu {
using Priority = size_t;
struct priority_sorter {
bool operator()(const Priority & p1, const Priority & p2) {
return p1 != p2 ? p1 > p2 : &p1 > &p2;
}
};
class DrawGroup { class DrawGroup {
public: public:
virtual void draw(const Window &, const Camera &, const TextureArray &) const = 0; virtual void draw(const Window &, const Camera &, const TextureArray &) const = 0;
......
#include "FrameBufferGroup.hpp" #include "FrameBufferGroup.hpp"
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <engine/graphics/front/geometry/Plane.hpp>
namespace megu { namespace megu {
FrameBufferGroup::FrameBufferGroup() FrameBufferGroup::FrameBufferGroup()
: _vbo(this->_vao, Quads::Layout(), Quads::Vertices(), EditMode::STATIC) { : _vbo(this->_vao, Plane::Layout(), Plane::Vertices().size()) {
megu::Source vert("assets/shaders/FrameBuffer-Instanced.vert", Source::Categorie::VERTEX); megu::Source vert("assets/shaders/FrameBuffer-Instanced.vert", Source::Categorie::VERTEX);
this->_program.attach(vert); this->_program.attach(vert);
...@@ -13,45 +14,24 @@ namespace megu { ...@@ -13,45 +14,24 @@ namespace megu {
this->_program.link(); this->_program.link();
this->_vbo << Plane::Vertices();
vert.release(); vert.release();
frag.release(); frag.release();
} }
void FrameBufferGroup::draw(const Window & window, const Camera & camera, const TextureArray & textures) const { void FrameBufferGroup::draw(const Window & window, const Camera & camera, const TextureArray & textures) const {
if(window.isOpen()) { if(window.isOpen()) {
std::vector<glm::mat4> uModels;
std::vector<GLint> uTextures;
this->_vao.bind();
size_t i = 1;
GLint slotsCount = Texture::CountSlots();
for(auto & texture : textures) {
if(i % slotsCount == 0) {
this->_program.use(); this->_program.use();
this->_vao.bind();
this->_program.setUniform("uModels", uModels); for(GLuint i = 0; i < static_cast<GLuint>(textures.size()); ++i) {
this->_program.setUniform("uTextures", uTextures); textures[i].get().bind(static_cast<GLuint>(i));
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(Quads::Vertices().size()), static_cast<GLsizei>(i));
uModels.clear();
uModels.clear();
}
texture.get().bind(i-1 % slotsCount);
uTextures.push_back(i-1);
uModels.push_back(glm::translate(glm::mat4(1), glm::vec3(0.f, 0.f, static_cast<float>(i))));
} }
if(uModels.empty()) { this->_program.setUniform("uSampler", std::vector<GLint>{0, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10});
this->_program.use(); this->_program.setUniform("uTexturesCount", static_cast<GLuint>(textures.size()));
glDrawArrays(Plane::Primitive(), 0, static_cast<GLsizei>(Plane::Vertices().size()));
this->_program.setUniform("uModels", uModels);
this->_program.setUniform("uTextures", uTextures);
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(Quads::Vertices().size()), static_cast<GLsizei>(i));
}
} }
} }
} }
\ No newline at end of file
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include <engine/graphics/back/buffers/VerticeBuffer.hpp> #include <engine/graphics/back/buffers/VerticeBuffer.hpp>
#include <engine/graphics/back/shaders/Program.hpp> #include <engine/graphics/back/shaders/Program.hpp>
#include <engine/graphics/front/geometry/Quads.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp> #include <engine/graphics/front/group/DrawGroup.hpp>
namespace megu { namespace megu {
......
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#include <engine/graphics/front/engine/Engine.hpp> #include <engine/graphics/front/engine/Engine.hpp>
#include <engine/graphics/errors.hpp> #include <engine/graphics/errors.hpp>
#define NORMALIZE(X) X/255.f
const float i_x = 1.f; const float i_x = 1.f;
const float i_y = 0.5f; const float i_y = 0.5f;
const float j_x = -1.f; const float j_x = -1.f;
...@@ -83,7 +81,7 @@ int main(int argc, const char * argv[]) { ...@@ -83,7 +81,7 @@ int main(int argc, const char * argv[]) {
images.push_back(std::make_unique<megu::Image>(id == 1 ? texture_1 : texture_2)); images.push_back(std::make_unique<megu::Image>(id == 1 ? texture_1 : texture_2));
glm::vec2 pos = to_screen_coordinate({x, y}, 32.f, 32.f, 1.f); glm::vec2 pos = to_screen_coordinate({x, y}, 32.f, 32.f, 1.f);
images.back()->setPosition({pos.x + 160, pos.y}); images.back()->setPosition({pos.x + 25, pos.y});
} }
++x; ++x;
...@@ -129,7 +127,7 @@ int main(int argc, const char * argv[]) { ...@@ -129,7 +127,7 @@ int main(int argc, const char * argv[]) {
images_2.push_back(std::make_unique<megu::Image>(texture_3)); images_2.push_back(std::make_unique<megu::Image>(texture_3));
glm::vec2 pos = to_screen_coordinate({x_2, y_2}, 32.f, 32.f); glm::vec2 pos = to_screen_coordinate({x_2, y_2}, 32.f, 32.f);
images_2.back()->setPosition({pos.x + 160, pos.y}); images_2.back()->setPosition({pos.x + 25, pos.y});
} }
++x_2; ++x_2;
...@@ -148,16 +146,19 @@ int main(int argc, const char * argv[]) { ...@@ -148,16 +146,19 @@ int main(int argc, const char * argv[]) {
ImGui_ImplGlfw_InitForOpenGL(window.ptr(), true); ImGui_ImplGlfw_InitForOpenGL(window.ptr(), true);
//? Engines //? Engines
megu::GraphicEngine engine(window, 320, 320); megu::GraphicEngine engine(window);
engine.tmp_renderer().setClearColor(NORMALIZE(135), NORMALIZE(150), NORMALIZE(255)); megu::Renderer basic_renderer(320, 320);
engine.push(0, basic_renderer);
engine.push(1, basic_renderer);
engine.push(1, 0, group);
engine.push(0, 0, group_2);
//? Render Loop //? Render Loop
std::cout << "Render Loop Begin !" << std::endl; std::cout << "Render Loop Begin !" << std::endl;
glm::vec2 xy = {0, 0}; glm::vec2 xy = {0, 0};
engine.push(group, 1);
engine.push(group_2, 0);
double previousTime = megu::Window::Time(); double previousTime = megu::Window::Time();
int frameCount = 0; int frameCount = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment