Select Git revision
ImageGroup.cpp

BATON Theau authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
ImageGroup.cpp 3.21 KiB
#include "ImageGroup.hpp"
#include <tuple>
namespace megu {
ImageGroup::ImageGroup()
: _vbo(this->_vao, Quads::Layout(), 400) {
{
Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX);
Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT);
this->_program << vert;
this->_program << frag;
this->_program.link();
vert.release();
frag.release();
}
this->_vbo << Quads::Vertices();
}
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 {
this->_vao.bind();
this->_program.use();
std::vector<std::reference_wrapper<const Texture>> textures;
std::vector<glm::mat4> uModels;
std::vector<GLint> uTextures;
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("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()));
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());
}
}
//std::cout << "---------------" << std::endl;
if(!textures.empty()) {
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()));
}
}
}