From 5c1a57b3672ec1e0777d8d0878c6a7ae93ebfdca Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 30 Dec 2017 16:07:41 +0000 Subject: [Code] Fixed spritebatch and optimised it. --- yage/core/logger.cpp | 4 +-- yage/core/logger.h | 2 +- yage/render/rectangle.cpp | 1 + yage/render/spritebatch.cpp | 81 ++++++++++++++++++++------------------------- yage/render/spritebatch.h | 10 +++--- 5 files changed, 43 insertions(+), 55 deletions(-) (limited to 'yage') diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp index f661e826..3c80cf1b 100644 --- a/yage/core/logger.cpp +++ b/yage/core/logger.cpp @@ -65,9 +65,9 @@ void Logger::clear() Logger &Logger::instance() { - static Logger gLogger; + static Logger yLogger; - return gLogger; + return yLogger; } } // namespace yage diff --git a/yage/core/logger.h b/yage/core/logger.h index 95af73b0..98c64b88 100644 --- a/yage/core/logger.h +++ b/yage/core/logger.h @@ -41,6 +41,6 @@ private: } // namespace yage -#define gLog (yage::Logger::instance()(__FILE__, __LINE__)) +#define yLog (yage::Logger::instance()(__FILE__, __LINE__)) #endif diff --git a/yage/render/rectangle.cpp b/yage/render/rectangle.cpp index 365d058d..632c7ceb 100644 --- a/yage/render/rectangle.cpp +++ b/yage/render/rectangle.cpp @@ -35,3 +35,4 @@ void Rectangle::render() const } } // namepsace yage + diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp index c1bf4a28..bce5986f 100644 --- a/yage/render/spritebatch.cpp +++ b/yage/render/spritebatch.cpp @@ -26,9 +26,37 @@ Glyph::Glyph(GLuint texture, float depth, const Vertex &top_left, { } -SpriteBatch::SpriteBatch() +SpriteBatch::SpriteBatch() : vao_(0), vbo_(0) { - createVertexArray(); + glGenVertexArrays(1, &vao_); + if (vao_ == 0) { + throw std::runtime_error("failed to generate VAO"); + } + // bind vertex array object + glBindVertexArray(vao_); + + glGenBuffers(1, &vbo_); + if (vbo_ == 0) { + throw std::runtime_error("failed to generate VBO"); + } + // bind vertex buffer object + glBindBuffer(GL_ARRAY_BUFFER, vbo_); + + // set the vertex attribute pointers + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, position)); + glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), + (void *)offsetof(Vertex, colour)); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, uv)); + + // enable vertex attribute arrays + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + + // unbind vertex array object + glBindVertexArray(0); } SpriteBatch::~SpriteBatch() @@ -88,10 +116,10 @@ void SpriteBatch::draw(const glm::vec4 &destination_rect, void SpriteBatch::render() { + glBindVertexArray(vao_); // sort and create render batches - sortGlyphs(); createRenderBatches(); - glBindVertexArray(vao_); + sortGlyphs(); for (auto &&batch : render_batches_) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, batch.texture); @@ -104,43 +132,6 @@ void SpriteBatch::render() render_batches_.clear(); } -void SpriteBatch::createVertexArray() -{ - if (vao_ == 0) { - glGenVertexArrays(1, &vao_); - if (vao_ == 0) { - throw std::runtime_error("glGenVertexArrays failed"); - } - } - // bind vertex array object - glBindVertexArray(vao_); - - if (vbo_ == 0) { - glGenBuffers(1, &vbo_); - if (vbo_ == 0) { - throw std::runtime_error("glGenBuffers failed"); - } - } - // bind vertex buffer object - glBindBuffer(GL_ARRAY_BUFFER, vbo_); - - // set the vertex attribute pointers - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (void *)offsetof(Vertex, position)); - glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), - (void *)offsetof(Vertex, colour)); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (void *)offsetof(Vertex, uv)); - - // enable vertex attribute arrays - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - // unbind vertex array object - glBindVertexArray(0); -} - void SpriteBatch::createRenderBatches() { std::vector vertices; @@ -163,12 +154,11 @@ void SpriteBatch::createRenderBatches() vertices.push_back(glyph_ptrs_[i]->bottom_left()); vertices.push_back(glyph_ptrs_[i]->top_left()); vertices.push_back(glyph_ptrs_[i]->top_right()); - vertices.push_back(glyph_ptrs_[i]->bottom_left()); - vertices.push_back(glyph_ptrs_[i]->bottom_right()); + vertices.push_back(glyph_ptrs_[i]->top_right()); + vertices.push_back(glyph_ptrs_[i]->bottom_right()); + vertices.push_back(glyph_ptrs_[i]->bottom_left()); - // bind vbo - glBindBuffer(GL_ARRAY_BUFFER, vbo_); // orphan the buffer glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr, GL_DYNAMIC_DRAW); @@ -176,7 +166,6 @@ void SpriteBatch::createRenderBatches() glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), vertices.data()); // unbind buffer - glBindBuffer(GL_ARRAY_BUFFER, 0); } } diff --git a/yage/render/spritebatch.h b/yage/render/spritebatch.h index e82268b8..35d9a1b1 100644 --- a/yage/render/spritebatch.h +++ b/yage/render/spritebatch.h @@ -9,9 +9,9 @@ #ifndef YAGE_SPRITE_BATCH_H #define YAGE_SPRITE_BATCH_H -#include "batch.h" -#include "../data/vertex.h" #include "../data/renderbatch.h" +#include "../data/vertex.h" +#include "batch.h" #include #include @@ -48,15 +48,14 @@ public: Vertex bottom_left() const { return bottom_left_; } }; - class SpriteBatch { public: static const int NUM_VERTICES = 6; private: - GLuint vbo_ = 0; - GLuint vao_ = 0; + GLuint vao_; + GLuint vbo_; std::vector glyphs_; std::vector glyph_ptrs_; std::vector render_batches_; @@ -81,7 +80,6 @@ public: void render(); private: - void createVertexArray(); void createRenderBatches(); void sortGlyphs(); }; -- cgit