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. --- examples/shooter/main.cpp | 43 ++---------------------- tests/log/test.cpp | 4 +-- 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 +++--- 7 files changed, 47 insertions(+), 98 deletions(-) diff --git a/examples/shooter/main.cpp b/examples/shooter/main.cpp index fe98f300..12cf218d 100644 --- a/examples/shooter/main.cpp +++ b/examples/shooter/main.cpp @@ -2,59 +2,20 @@ #include "glad/glad.h" -#include - using std::cout; int main(int argc, char **argv) { - cout << "Starting Shooter example...\n"; - yage::Window window; window.create("Shooter example", 800, 600); - yage::Shader shader("examples/resources/colourshader.vert", - "examples/resources/colourshader.frag"); - - GLfloat vertices[] = { - 0.0f, 0.5f, 1.f, 0.f, 0.f, // Vertex 1 (X, Y, R, G, B) - 0.5f, -0.5f, 0.f, 1.f, 0.f, // Vertex 2 (X, Y, R, G, B) - -0.5f, -0.5f, 0.f, 0.f, 1.f, // Vertex 3 (X, Y, R, G, B) - }; - - // create vertex array - GLuint rect_vao, rect_vbo; - - // bind vertex array object - glGenVertexArrays(1, &rect_vao); - glBindVertexArray(rect_vao); - - // bind vertex buffer object - glGenBuffers(1, &rect_vbo); - glBindBuffer(GL_ARRAY_BUFFER, rect_vbo); - - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - shader.use(); - - // enable vertex attribute arrays - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - - // set the vertex attribute pointers - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), - (void *)(2 * sizeof(GLfloat))); + yage::Shader shader("examples/resources/textureshader.vert", + "examples/resources/textureshader.frag"); while (!window.shouldClose()) { window.pollEvents(); window.clearBuffer(); - glDrawArrays(GL_TRIANGLES, 0, 3); - window.swapBuffer(); } - - glDeleteBuffers(1, &rect_vbo); - - glDeleteVertexArrays(1, &rect_vao); } diff --git a/tests/log/test.cpp b/tests/log/test.cpp index 53c2894b..bf087855 100644 --- a/tests/log/test.cpp +++ b/tests/log/test.cpp @@ -12,8 +12,8 @@ int main() { - gLog << "First message"; + yLog << "First message"; - gLog << "Second Message"; + yLog << "Second Message"; std::cout << "COUT\n"; } 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