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

Change render group rendering algo

parent e73eee7a
Branches
No related tags found
No related merge requests found
Showing
with 206 additions and 114 deletions
#version 450 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTex;
uniform mat4 uProj;
uniform mat4 uView;
uniform mat4 uModel[128];
out flat int Id;
out vec2 Texture;
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
#version 450 core
out vec4 FragColor;
in flat int Id;
in vec2 Texture;
uniform sampler2D uSampler[8];
uniform int uTextures[128];
void main() {
FragColor = texture(uSampler[uTextures[Id]], Texture);
}
\ No newline at end of file
assets/textures/Cube_Blue_Outlined.png

329 B

assets/textures/Cube_Test.png

359 B

...@@ -3,6 +3,6 @@ Pos=60,60 ...@@ -3,6 +3,6 @@ Pos=60,60
Size=400,400 Size=400,400
[Window][Isometric] [Window][Isometric]
Pos=138,21 Pos=135,21
Size=477,101 Size=477,101
...@@ -154,6 +154,10 @@ namespace megu { ...@@ -154,6 +154,10 @@ namespace megu {
glUniform1iv(this->_locations.at(name), static_cast<GLsizei>(value.size()), value.data()); glUniform1iv(this->_locations.at(name), static_cast<GLsizei>(value.size()), value.data());
} }
void Program::setUniform(const std::string & name, const std::vector<GLuint> & value) const {
glUniform1uiv(this->_locations.at(name), static_cast<GLsizei>(value.size()), value.data());
}
void Program::setUniform(const std::string & name, const std::vector<GLfloat> & value) const { void Program::setUniform(const std::string & name, const std::vector<GLfloat> & value) const {
glUniform1fv(this->_locations.at(name), static_cast<GLsizei>(value.size()), value.data()); glUniform1fv(this->_locations.at(name), static_cast<GLsizei>(value.size()), value.data());
} }
......
...@@ -63,6 +63,7 @@ namespace megu { ...@@ -63,6 +63,7 @@ namespace megu {
void setUniform(const std::string &, const glm::mat4 &) const; void setUniform(const std::string &, const glm::mat4 &) const;
void setUniform(const std::string &, const std::vector<GLint> &) const; void setUniform(const std::string &, const std::vector<GLint> &) const;
void setUniform(const std::string &, const std::vector<GLuint> &) const;
void setUniform(const std::string &, const std::vector<GLfloat> &) const; void setUniform(const std::string &, const std::vector<GLfloat> &) const;
void setUniform(const std::string &, const std::vector<glm::vec2> &) const; void setUniform(const std::string &, const std::vector<glm::vec2> &) const;
void setUniform(const std::string &, const std::vector<glm::vec4> &) const; void setUniform(const std::string &, const std::vector<glm::vec4> &) const;
......
...@@ -115,9 +115,9 @@ namespace megu { ...@@ -115,9 +115,9 @@ namespace megu {
return this->_id == texture._id; return this->_id == texture._id;
} }
bool Texture::operator!=(const Texture & texture) const { /*bool Texture::operator!=(const Texture & texture) const {
return this->_id != texture._id; return this->_id != texture._id;
} }*/
bool Texture::operator>=(const Texture & texture) const { bool Texture::operator>=(const Texture & texture) const {
return this->_id >= texture._id; return this->_id >= texture._id;
......
...@@ -55,7 +55,7 @@ namespace megu { ...@@ -55,7 +55,7 @@ namespace megu {
bool valid() const; bool valid() const;
bool operator==(const Texture &) const; bool operator==(const Texture &) const;
bool operator!=(const Texture &) const; //bool operator!=(const Texture &) const;
bool operator>=(const Texture &) const; bool operator>=(const Texture &) const;
bool operator<=(const Texture &) const; bool operator<=(const Texture &) const;
bool operator>(const Texture &) const; bool operator>(const Texture &) const;
......
#include "Engine.hpp"
namespace megu {
GraphicEngine::GraphicEngine(Window & window,float w, float h)
: _renderer(w, h), _window(window) {
glViewport(0, 0, window.width(), window.height());
glClearColor(0.f, 0.f, 0.f, 1.f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
void GraphicEngine::step() {
if(this->_window.isOpen()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->_renderer.render(this->_window);
this->_window.swapBuffers();
}
}
}
\ No newline at end of file
#pragma once
#include "Renderer.hpp"
namespace megu {
class GraphicEngine {
public:
GraphicEngine() = delete;
GraphicEngine(Window &, float, float);
~GraphicEngine() = default;
void step();
inline void setClearColor(float r, float g, float b, float a = 1.f) const {glClearColor(r, g, b, a);}
Renderer & tmp_getRenderer() {return this->_renderer;}
private:
Window & _window;
Renderer _renderer;
};
}
\ No newline at end of file
#include "Renderer.hpp"
namespace megu {
Renderer::Renderer(float x, float y)
: _view(0, 0, y, x) {
}
void Renderer::add(DrawGroup & group) {
this->_groups.insert(&group);
}
void Renderer::render(const Window & window) const {
for(auto & group : this->_groups) {
group->draw(window, this->_view);
}
}
}
\ No newline at end of file
#pragma once
#include <set>
#include <any>
#include <engine/graphics/utility/reference_sorter.hpp>
#include <engine/graphics/back/cameras/View.hpp>
#include <engine/graphics/front/group/DrawGroup.hpp>
namespace megu {
class Renderer {
public:
Renderer() = delete;
Renderer(float, float);
~Renderer() = default;
void add(DrawGroup & group);
virtual void render(const Window &) const;
private:
std::set<DrawGroup *> _groups;
View _view;
};
}
\ No newline at end of file
...@@ -6,8 +6,8 @@ namespace megu { ...@@ -6,8 +6,8 @@ namespace megu {
ImageGroup::ImageGroup() ImageGroup::ImageGroup()
: _vbo(this->_vao, Quads::Layout(), 400) { : _vbo(this->_vao, Quads::Layout(), 400) {
{ {
Source vert("assets/shaders/Image-Instanced.vert", Source::Categorie::VERTEX); Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX);
Source frag("assets/shaders/Image.frag", Source::Categorie::FRAGMENT); Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT);
this->_program << vert; this->_program << vert;
this->_program << frag; this->_program << frag;
...@@ -36,21 +36,51 @@ namespace megu { ...@@ -36,21 +36,51 @@ namespace megu {
this->_vao.bind(); this->_vao.bind();
this->_program.use(); this->_program.use();
std::map<std::reference_wrapper<const Texture>, std::vector<glm::mat4>, std::greater<megu::Texture>> data; std::vector<std::reference_wrapper<const Texture>> textures;
std::vector<glm::mat4> uModels;
std::vector<GLint> uTextures;
for(auto & image : this->_images) { for(auto & image : this->_images) {
data[image.get().texture()].push_back(image.get().transformation().model());
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("uProj", camera.projection());
this->_program.setUniform("uView", camera.view());
this->_program.setUniform("uSampler", std::vector<GLint>({0, 1, 2, 3, 4, 5, 6, 7}));
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()));
for(auto &[texture, models] : data) { textures.clear();
texture.get().bind(0); 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());
}
}
//std::cout << "---------------" << std::endl;
if(!textures.empty()) {
this->_program.setUniform("uProj", camera.projection()); this->_program.setUniform("uProj", camera.projection());
this->_program.setUniform("uView", camera.view()); this->_program.setUniform("uView", camera.view());
this->_program.setUniform("uSampler", 0);
this->_program.setUniform("uModel", models);
glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(models.size())); this->_program.setUniform("uSampler", std::vector<GLint>({0, 1, 2, 3, 4, 5, 6, 7}));
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
#pragma once #pragma once
#include "DrawGroup.hpp" #include "DrawGroup.hpp"
#include "reference_sorter.hpp"
#include "isometric_sorter.hpp"
#include <map> #include <map>
#include <list> #include <list>
#include <set> #include <set>
#include <engine/graphics/utility/isometric_sorter.hpp>
#include <engine/graphics/back/buffers/VertexArray.hpp> #include <engine/graphics/back/buffers/VertexArray.hpp>
#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>
......
...@@ -9,6 +9,7 @@ namespace megu { ...@@ -9,6 +9,7 @@ namespace megu {
friend bool operator<(const T &, const T &); friend bool operator<(const T &, const T &);
bool operator()(const T &, const T &) const; bool operator()(const T &, const T &) const;
bool operator()(const T *, const T *) const;
}; };
} }
......
...@@ -10,4 +10,9 @@ namespace megu { ...@@ -10,4 +10,9 @@ namespace megu {
bool reference_sorter<T>::operator()(const T & obj_1, const T & obj_2) const { bool reference_sorter<T>::operator()(const T & obj_1, const T & obj_2) const {
return &obj_1 < &obj_2; return &obj_1 < &obj_2;
} }
template <class T>
bool reference_sorter<T>::operator()(const T * obj_1, const T * obj_2) const {
return obj_1 < obj_2;
}
} }
\ No newline at end of file
...@@ -7,13 +7,15 @@ ...@@ -7,13 +7,15 @@
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#define WINDOW_WIDTH 1200 #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 720 #define WINDOW_HEIGHT 640
#include <engine/io/Window.hpp> #include <engine/io/Window.hpp>
#include <engine/graphics/back/cameras/View.hpp> #include <engine/graphics/back/cameras/View.hpp>
#include <engine/graphics/front/object/Image.hpp> #include <engine/graphics/front/object/Image.hpp>
#include <engine/graphics/front/group/ImageGroup.hpp> #include <engine/graphics/front/group/ImageGroup.hpp>
#include <engine/graphics/front/Renderer.hpp>
#include <engine/graphics/front/Engine.hpp>
#include <engine/graphics/errors.hpp> #include <engine/graphics/errors.hpp>
#define NORMALIZE(X) X/255.f #define NORMALIZE(X) X/255.f
...@@ -39,10 +41,6 @@ int main(int argc, const char * argv[]) { ...@@ -39,10 +41,6 @@ int main(int argc, const char * argv[]) {
std::cout << "Window Inited" << std::endl; std::cout << "Window Inited" << std::endl;
//? Camera
megu::View view(0, 0, 320, 240);
std::cout << "View Initied" << std::endl;
//? Group //? Group
megu::ImageGroup group; megu::ImageGroup group;
...@@ -50,16 +48,16 @@ int main(int argc, const char * argv[]) { ...@@ -50,16 +48,16 @@ int main(int argc, const char * argv[]) {
//? Image //? Image
std::vector<int> map = { std::vector<int> map = {
0, 0, 0, 0, 1, 2, 3, 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
0, 0, 0, 2, 0, 3, 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
0, 0, 1, 3, 3, 0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 2, 1, 0, 0, 1, 0, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 3, 1, 0, 0, 3, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 3, 0, 2, 3, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 0, 0, 1, 2, 3, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 2, 0, 0, 0, 1, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
0, 0, 3, 1, 2, 0, 0, 0, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
}; };
size_t x = 0; size_t x = 0;
...@@ -67,8 +65,11 @@ int main(int argc, const char * argv[]) { ...@@ -67,8 +65,11 @@ int main(int argc, const char * argv[]) {
std::vector<std::unique_ptr<megu::Image>> images; std::vector<std::unique_ptr<megu::Image>> images;
megu::Texture texture; megu::Texture texture_1;
texture.store(megu::TextureBuffer("assets/textures/Cube_Grass.png")); texture_1.store(megu::TextureBuffer("assets/textures/Cube_Blue.png"));
megu::Texture texture_2;
texture_2.store(megu::TextureBuffer("assets/textures/Cube_Red.png"));
for(auto id : map) { for(auto id : map) {
if(x == 10) { if(x == 10) {
...@@ -76,16 +77,20 @@ int main(int argc, const char * argv[]) { ...@@ -76,16 +77,20 @@ int main(int argc, const char * argv[]) {
++y; ++y;
} }
images.push_back(std::make_unique<megu::Image>(texture));
if(id != 0) {
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); glm::vec2 pos = to_screen_coordinate({x, y}, 32.f, 32.f);
images.back()->setPosition(pos.x + 160, pos.y); images.back()->setPosition(pos.x + 160, pos.y);
}
++x; ++x;
} }
for(auto & i : images) { for(auto & i : images) {
group.add(*i); group.add(*i);
i.get()->setSize(32.f, 32.f);
} }
std::cout << "Image Inited" << std::endl; std::cout << "Image Inited" << std::endl;
...@@ -95,88 +100,19 @@ int main(int argc, const char * argv[]) { ...@@ -95,88 +100,19 @@ int main(int argc, const char * argv[]) {
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
ImGui_ImplGlfw_InitForOpenGL(window.ptr(), true); ImGui_ImplGlfw_InitForOpenGL(window.ptr(), true);
//? Render Loop //? Engines
glClearColor(1.f, 1.f, 1.f, 1.f); megu::GraphicEngine engine(window, 320, 320);
engine.setClearColor(0.f, 0.f, 0.f);
double lastTime = glfwGetTime();
int nbFrames = 0;
//? Render Loop
std::cout << "Render Loop Begin !" << std::endl; std::cout << "Render Loop Begin !" << std::endl;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glm::vec2 xy = {0, 0}; glm::vec2 xy = {0, 0};
while(window.isOpen()) { engine.tmp_getRenderer().add(group);
double currentTime = glfwGetTime();
nbFrames++;
if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago
// printf and reset timer
window.setTitle(std::to_string(nbFrames));
nbFrames = 0;
lastTime += 1.0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); while(window.isOpen()) {
window.pollEvents(); window.pollEvents();
group.draw(window, view); engine.step();
/*ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if(ImGui::Begin("Isometric")) {
ImGui::Text("Position : %i / %i", static_cast<int>(xy.x), static_cast<int>(xy.y));
ImGui::Text("x : ");
ImGui::SameLine();
if(ImGui::Button("+")) {
++xy.x;
glm::vec2 pos = to_screen_coordinate(xy, 32, 32, 0.f);
images[1]->setPosition(pos.x, pos.y);
group.update();
}
ImGui::SameLine();
if(ImGui::Button("-")) {
--xy.x;
glm::vec2 pos = to_screen_coordinate(xy, 32, 32, 0.f);
images[1]->setPosition(pos.x, pos.y);
group.update();
}
ImGui::PushID(1);
ImGui::Text("y : ");
ImGui::SameLine();
if(ImGui::Button("+")) {
++xy.y;
glm::vec2 pos = to_screen_coordinate(xy, 32, 32, 0.f);
images[1]->setPosition(pos.x, pos.y);
group.update();
}
ImGui::SameLine();
if(ImGui::Button("-")) {
--xy.y;
glm::vec2 pos = to_screen_coordinate(xy, 32, 32, 0.f);
images[1]->setPosition(pos.x, pos.y);
group.update();
}
ImGui::PopID();
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());*/
window.swapBuffers();
} }
std::cout << "Render Loop End !" << std::endl; std::cout << "Render Loop End !" << std::endl;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment