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

Starting a new rendering system

parent ad9344f3
No related branches found
No related tags found
No related merge requests found
Showing
with 189 additions and 263 deletions
......@@ -6,21 +6,20 @@ in vec2 Texture;
uniform sampler2D uSampler[32];
uniform uint uTexturesCount;
uniform vec3 uClearColor;
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 != uClearColor.r) {
if(tcolor.r != 0.0) {
color.r = tcolor.r;
}
if(tcolor.g != uClearColor.g) {
if(tcolor.g != 0.0) {
color.g = tcolor.g;
}
if(tcolor.b != uClearColor.b) {
if(tcolor.b != 0.0) {
color.b = tcolor.b;
}
}
......
#version 450 core
out vec4 FragColor;
uniform sampler2D uSampler[32];
uniform int uTextures[128];
uniform vec4 uFrames[128];
uniform vec2 uSizes[128];
in vec2 Texture;
flat in int Id;
void main() {
vec2 coord = vec2(
(uFrames[Id].x / uSizes[Id].x) + (uFrames[Id].z / uSizes[Id].x) * Texture.x,
(uFrames[Id].y / uSizes[Id].y) + (uFrames[Id].w / uSizes[Id].y) * Texture.y
);
FragColor = texture(uSampler[uTextures[Id]], coord);
}
\ No newline at end of file
#version 450 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTex;
uniform mat4 uModel[128];
uniform mat4 uView;
uniform mat4 uProj;
out vec2 Texture;
flat out int Id;
void main() {
Texture = aTex;
Id = gl_InstanceID;
gl_Position = uProj * uView * uModel[gl_InstanceID] * vec4(aPos.x, aPos.y, 0.0, 1.0);
}
\ No newline at end of file
assets/textures/Neera.png

8.11 KiB

......@@ -4,23 +4,21 @@
namespace megu {
GraphicEngine::GraphicEngine(Window & window, float w, float h)
: _window(window), _renderer(w, h) {
: _window(window), _view(0, 0, w, h) {
glViewport(0, 0, window.width(), window.height());
this->_renderer.setClearColor(NORMALIZE(135.f), NORMALIZE(170.f), NORMALIZE(255.f));
}
GraphicEngine::GraphicEngine(Window & window)
: _window(window), _renderer(window.width(), window.height()) {
: _window(window), _view(0, 0, window.width(), window.height()) {
glViewport(0, 0, window.width(), window.height());
this->_renderer.setClearColor(NORMALIZE(135.f), NORMALIZE(170.f), NORMALIZE(255.f));
}
void GraphicEngine::push(Priority priority, const Renderer & renderer) {
void GraphicEngine::push(Priority priority, Renderer & renderer) {
this->_layers[priority] = std::make_unique<Layer>(renderer);
}
void GraphicEngine::push(Priority layer_priority, Priority draw_priority, const DrawGroup & group) {
this->_layers[layer_priority]->push(draw_priority, group);
void GraphicEngine::push(Priority layer_priority, Priority object_priority, Renderable & object) {
this->_layers[layer_priority].get()->push(object_priority, object);
}
void GraphicEngine::remove(Priority priority) {
......@@ -30,13 +28,6 @@ namespace megu {
}
}
void GraphicEngine::remove(Priority layer_priority, Priority draw_priority) {
auto it = this->_layers.find(layer_priority);
if(it != this->_layers.end()) {
it->second->remove(draw_priority);
}
}
std::optional<std::reference_wrapper<const Layer>> GraphicEngine::get(Priority priority) const {
const auto it = this->_layers.find(priority);
if(it != this->_layers.end()) {
......@@ -45,37 +36,27 @@ namespace megu {
return {};
}
std::optional<std::reference_wrapper<const DrawGroup>> GraphicEngine::get(Priority layer_priority, Priority draw_priority) const {
const auto it = this->_layers.find(layer_priority);
if(it != this->_layers.end()) {
return it->second->get(draw_priority);
}
return {};
}
void GraphicEngine::step() {
if(this->_window.isOpen()) {
// Draw Layers
TextureArray textures;
for(auto & [priority, layer] : this->_layers) {
if(!layer.get()->empty()) {
const glm::vec2 & dimension = layer->renderer().dimension();
glViewport(0, 0, static_cast<GLsizei>(dimension.x), static_cast<GLsizei>(dimension.y));
textures.push_back(layer->draw(this->_window, textures));
}
}
// Merge Textures
FrameBuffer::BindDefaultFrameBuffer();
glViewport(0, 0, static_cast<GLsizei>(this->_window.width()), static_cast<GLsizei>(this->_window.height()));
this->_renderer.render(this->_window, this->_group, textures);
glClear(GL_COLOR_BUFFER_BIT);
std::any any = 1;
this->_module.render(this->_window, this->_view, any, textures);
this->_window.swapBuffers();
}
}
void GraphicEngine::setClearColor(float x, float y, float z) {
this->_renderer.setClearColor(x, y, z);
this->_group.setClearColor(x, y, z);
for(auto & [priority, layer] : this->_layers) {
layer.get()->renderer().setClearColor(x, y, z);
}
......
......@@ -2,10 +2,10 @@
#include <map>
#include <optional>
#include <engine/graphics/front/group/DrawGroup.hpp>
#include <engine/graphics/front/group/FrameBufferGroup.hpp>
#include "Renderer.hpp"
#include <engine/graphics/front/module/FrameBufferModule.hpp>
#include <engine/graphics/back/cameras/View.hpp>
#include "Layer.hpp"
namespace megu {
......@@ -16,17 +16,14 @@ namespace megu {
GraphicEngine(Window &);
~GraphicEngine() = default;
void push(Priority, const Renderer &);
void push(Priority, Priority, const DrawGroup &);
void push(Priority, Renderer &);
void push(Priority, Priority, Renderable &);
void remove(Priority);
void remove(Priority, Priority);
std::optional<std::reference_wrapper<const Layer>> get(Priority) const;
std::optional<std::reference_wrapper<const DrawGroup>> get(Priority, Priority) const;
inline bool empty() const {return this->_layers.empty();}
inline const Renderer & renderer() const {return this->_renderer;}
inline const std::map<Priority, std::unique_ptr<Layer>> & layers() const {return this->_layers;}
void step();
......@@ -34,9 +31,9 @@ namespace megu {
private:
std::map<Priority, std::unique_ptr<Layer>> _layers;
Renderer _renderer;
FrameBufferGroup _group;
FrameBuffer_Module _module;
Window & _window;
View _view;
};
}
\ No newline at end of file
......@@ -3,37 +3,10 @@
#include <iostream>
namespace megu {
Layer::Layer(const Renderer & renderer)
: _renderer(renderer),
_frameBuffer(static_cast<GLuint>(renderer.dimension().x), static_cast<GLuint>(renderer.dimension().y)) {}
Layer::Layer(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;
void Layer::push(Priority priority, Renderable & object) {
this->_objects[typeid(object)].insert(object);
}
void Layer::remove(Priority priority) {
auto it = this->_objects.find(priority);
if(it != this->_objects.end()) {
this->_objects.erase(it);
}
}
std::optional<std::reference_wrapper<const DrawGroup>> Layer::get(Priority priority) const {
const auto it = this->_objects.find(priority);
if(it != this->_objects.end()) {
return *it->second;
}
return {};
}
const Texture & Layer::draw(const Window & window, const TextureArray & textures) const {
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
......@@ -2,8 +2,12 @@
#include <map>
#include <optional>
#include <typeindex>
#include <any>
#include <engine/graphics/back/buffers/FrameBuffer.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp>
#include <engine/graphics/front/object/Renderable.hpp>
#include <engine/graphics/utility/reference_sorter.hpp>
#include "Priority.hpp"
#include "Renderer.hpp"
......@@ -12,22 +16,29 @@ namespace megu {
class Layer {
public:
Layer() = delete;
Layer(const Renderer &);
Layer(Renderer &);
~Layer() = default;
void push(Priority, const DrawGroup &);
void remove(Priority);
std::optional<std::reference_wrapper<const DrawGroup>> get(Priority) const;
inline bool empty() const {return this->_objects.empty();}
inline const Renderer & renderer() const {return this->_renderer;}
inline const std::map<Priority, DrawGroup const *> & groups() const {return this->_objects;}
virtual const Texture & draw(const Window &, const TextureArray &) const;
void push(Priority, Renderable &);
const Texture & draw(const Window & w, const TextureArray & a) {
this->_frameBuffer.bind();
for(const auto & [type, objects] : this->_objects) {
if(!objects.empty()) {
//std::cout << typeid((*objects.begin()).get()).name() << std::endl;
this->_renderer.render(w, (*objects.begin()).get(), a);
}
}
return this->_frameBuffer.texture();
}
private:
const Renderer & _renderer;
Renderer & _renderer;
FrameBuffer _frameBuffer;
std::map<Priority, DrawGroup const*> _objects;
std::map<std::type_index, std::set<std::reference_wrapper<Renderable>>> _objects;
};
}
\ No newline at end of file
......@@ -11,4 +11,7 @@ namespace megu {
return p1 != p2 ? p1 > p2 : &p1 > &p2;
}
};
template <class T>
using priority_map = std::map<Priority, T>;
}
\ No newline at end of file
......@@ -7,10 +7,6 @@ namespace megu {
glEnable(GL_BLEND);
}
void Renderer::render(const Window & window, const DrawGroup & group, const TextureArray & textures) const {
group.draw(window, this->_view, textures);
}
void Renderer::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
......
......@@ -6,7 +6,7 @@
#include <engine/graphics/back/buffers/FrameBuffer.hpp>
#include <engine/graphics/back/cameras/View.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp>
#include <engine/graphics/front/module/ImageModule.hpp>
namespace megu {
class Renderer {
......@@ -15,7 +15,10 @@ namespace megu {
Renderer(float, float);
~Renderer() = default;
virtual void render(const Window &, const DrawGroup &, const TextureArray &) const;
void render(const Window & w, Renderable & r, const TextureArray & a) {
this->clear();
r.accept(w, this->_view, this->_module, a);
}
const glm::vec2 dimension() const {return this->_view.dimension();}
......@@ -26,5 +29,6 @@ namespace megu {
private:
View _view;
Image_Module _module;
};
}
\ No newline at end of file
#include "DrawGroup.hpp"
namespace megu {
}
\ No newline at end of file
#include "ImageGroup.hpp"
#include <tuple>
#include <engine/graphics/utility/array_generator.hpp>
namespace megu {
ImageGroup::ImageGroup()
: _vbo(this->_vao, Quads::Layout(), Quads::Vertices().size(), megu::EditMode::STATIC) {
Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX);
this->_program.attach(vert);
Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT);
this->_program.attach(frag);
this->_program.link();
this->_vbo << Quads::Vertices();
vert.release();
frag.release();
}
void ImageGroup::add(const Image & image) {
this->_images.insert(image);
}
void ImageGroup::update() {
std::set<std::reference_wrapper<const Image>, isometric_sorter> source = this->_images;
this->_images.clear();
for(auto & i : source) {
this->_images.insert(i);
}
}
void ImageGroup::draw(const Window & window, const Camera & camera, const TextureArray &) const {
this->_vao.bind();
this->_program.use();
this->_program.setUniform("uProj", camera.projection());
this->_program.setUniform("uView", camera.view());
std::vector<std::reference_wrapper<const Texture>> textures;
std::vector<glm::mat4> uModels;
std::vector<GLint> uTextures;
static auto uSampler = array_generator<GLint, 32>().array;
this->_program.setUniform("uSampler", uSampler);
for(auto & image : this->_images) {
std::vector<std::reference_wrapper<const Texture>>::iterator it = std::find(textures.begin(), textures.end(), image.get().texture());
if(it != textures.end()) {
uModels.push_back(image.get().transformation().model());
uTextures.push_back(static_cast<GLint>(it - textures.begin()));
}
else {
if(textures.size() >= 8 || uModels.size() >= 124) {
this->_program.setUniform("uModel", uModels);
this->_program.setUniform("uTextures", uTextures);
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size()));
textures.clear();
uModels.clear();
uTextures.clear();
}
textures.push_back(image.get().texture());
image.get().texture().bind(static_cast<GLint>(textures.size()-1));
uTextures.push_back(static_cast<GLint>(textures.size()-1));
uModels.push_back(image.get().transformation().model());
}
}
if(!textures.empty()) {
this->_program.setUniform("uModel", uModels);
this->_program.setUniform("uTextures", uTextures);
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size()));
}
}
}
\ No newline at end of file
#include "VertexArrayGroup.hpp"
#include <engine/graphics/utility/array_generator.hpp>
namespace megu {
VertexArrayGroup::VertexArrayGroup()
: _vbo(this->_vao, Quads::Layout(), Quads::Vertices(), megu::EditMode::STATIC) {
Source vert("assets/shaders/Grid-Instanced.vert", Source::Categorie::VERTEX);
this->_program.attach(vert);
Source frag("assets/shaders/Grid-Instanced.frag", Source::Categorie::FRAGMENT);
this->_program.attach(frag);
this->_program.link();
vert.release();
frag.release();
}
void VertexArrayGroup::add(const Texture & texture, const VerticesArray & grid) {
this->_objects[texture].push_back(grid);
}
void VertexArrayGroup::draw(const Window &, const Camera & camera, const TextureArray &) const {
this->_vao.bind();
this->_program.use();
this->_program.setUniform("uProj", camera.projection());
this->_program.setUniform("uView", camera.view());
for(const auto & [texture, grids] : this->_objects) {
texture.get().bind(0);
for(const auto & grid : grids) {
this->_program.setUniform("uModel", grid.get().transformation().model());
this->_program.setUniform("uSampler", 0);
this->_program.setUniform("uData", static_cast<const void *>(grid.get().vertices().data()), grid.get().size());
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(grid.get().size()));
}
}
}
}
\ No newline at end of file
#pragma once
#include "DrawGroup.hpp"
#include <map>
#include <optional>
#include <engine/graphics/back/textures/Texture.hpp>
#include <engine/graphics/back/buffers/VertexArray.hpp>
#include <engine/graphics/back/buffers/VerticeBuffer.hpp>
#include <engine/graphics/back/shaders/Program.hpp>
#include <engine/graphics/front/object/VerticesArray.hpp>
#include <engine/graphics/utility/texture_comparator.hpp>
namespace megu {
class VertexArrayGroup : public DrawGroup {
public:
VertexArrayGroup();
~VertexArrayGroup() = default;
void add(const Texture &, const VerticesArray &);
void draw(const Window &, const Camera &, const TextureArray &) const override;
private:
std::map<std::reference_wrapper<const Texture>, std::vector<std::reference_wrapper<const VerticesArray>>, texture_comparator> _objects;
VertexArray _vao;
VerticeBuffer _vbo;
Program _program;
};
}
\ No newline at end of file
#include "FrameBufferGroup.hpp"
#include "FrameBufferModule.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <engine/graphics/front/geometry/Plane.hpp>
#include <engine/graphics/utility/array_generator.hpp>
namespace megu {
FrameBufferGroup::FrameBufferGroup()
: _vbo(this->_vao, Plane::Layout(), Plane::Vertices(), megu::EditMode::STATIC), _clear(0.f) {
FrameBuffer_Module::FrameBuffer_Module()
: _vbo(this->_vao, Plane::Layout(), Plane::Vertices(), megu::EditMode::STATIC) {
megu::Source vert("assets/shaders/FrameBuffer-Instanced.vert", Source::Categorie::VERTEX);
this->_program.attach(vert);
......@@ -19,7 +18,7 @@ namespace megu {
frag.release();
}
void FrameBufferGroup::draw(const Window & window, const Camera & camera, const TextureArray & textures) const {
void FrameBuffer_Module::render(const Window & window, const Camera & camera, std::any & any, const TextureArray & textures) {
this->_program.use();
this->_vao.bind();
......@@ -31,7 +30,6 @@ namespace megu {
this->_program.setUniform("uSampler", uSampler);
this->_program.setUniform("uTexturesCount", static_cast<GLuint>(textures.size()));
this->_program.setUniform("uClearColor", this->_clear);
glDrawArrays(Plane::Primitive(), 0, static_cast<GLsizei>(Plane::Vertices().size()));
}
}
\ No newline at end of file
#pragma once
#include "DrawGroup.hpp"
#include <map>
#include <list>
#include <set>
#include <engine/graphics/utility/isometric_sorter.hpp>
#include <any>
#include <engine/graphics/back/buffers/VertexArray.hpp>
#include <engine/graphics/back/buffers/VerticeBuffer.hpp>
#include <engine/graphics/back/shaders/Program.hpp>
#include <engine/graphics/front/object/Image.hpp>
#include <engine/graphics/utility/module.hpp>
namespace megu {
class ImageGroup : public DrawGroup {
class FrameBuffer_Module : public Module<std::any> {
public:
ImageGroup();
~ImageGroup() = default;
FrameBuffer_Module();
~FrameBuffer_Module() = default;
void draw(const Window &, const Camera &, const TextureArray &) const override;
void add(const Image &);
void update();
void render(const Window &, const Camera &, std::any &, const TextureArray &) override;
private:
std::set<std::reference_wrapper<const Image>, isometric_sorter> _images;
VertexArray _vao;
VerticeBuffer _vbo;
Program _program;
......
#include "ImageModule.hpp"
#include <engine/graphics/utility/array_generator.hpp>
namespace megu {
Image_Module::Image_Module()
: _vbo(this->_vao, Quads::Layout(), Quads::Vertices().size(), megu::EditMode::STATIC) {
Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX);
this->_program.attach(vert);
Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT);
this->_program.attach(frag);
this->_program.link();
this->_vbo << Quads::Vertices();
vert.release();
frag.release();
}
void Image_Module::render(const Window &, const Camera & camera, Image & image, const TextureArray &) {
this->_vao.bind();
this->_program.use();
image.texture().bind();
this->_program.setUniform("uProj", camera.projection());
this->_program.setUniform("uView", camera.view());
static auto uSampler = array_generator<GLint, 32>().array;
this->_program.setUniform("uSampler", uSampler);
std::vector<glm::mat4> uModels;
std::vector<GLint> uTextures;
uModels.push_back(image.transformation().model());
uTextures.push_back(0);
this->_program.setUniform("uModel", uModels);
this->_program.setUniform("uTextures", uTextures);
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), 1);
}
}
\ No newline at end of file
......@@ -4,21 +4,20 @@
#include <engine/graphics/back/buffers/VerticeBuffer.hpp>
#include <engine/graphics/back/shaders/Program.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp>
#include <engine/graphics/front/object/Image.hpp>
#include <engine/graphics/utility/module.hpp>
namespace megu {
class FrameBufferGroup : public DrawGroup {
class Image_Module : public Module<Image> {
public:
FrameBufferGroup();
~FrameBufferGroup() = default;
Image_Module();
~Image_Module() = default;
virtual void draw(const Window &, const Camera &, const TextureArray &) const;
inline void setClearColor(float x, float y, float z) {this->_clear = {x, y, z};}
void render(const Window &, const Camera &, Image &, const TextureArray &) override;
private:
VertexArray _vao;
VerticeBuffer _vbo;
Program _program;
glm::vec3 _clear;
};
}
\ No newline at end of file
#pragma once
#include "Renderable.hpp"
#include <filesystem>
#include <glm/vec2.hpp>
......@@ -10,7 +12,7 @@
#include <engine/graphics/utility/type.hpp>
namespace megu {
class Image {
class Image : public Renderable {
public:
Image() = default;
Image(const std::filesystem::path &);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment