From 86e4aa6265ade205aba94494a7a31a83b5686387 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 3 Jan 2018 13:26:37 +0000 Subject: [Engine] [Example] Reenabled vsync and working on example. --- yage/core/window.cpp | 2 +- yage/data/renderbatch.h | 17 ------------- yage/render/spritebatch.cpp | 48 ++++++++++++++---------------------- yage/render/spritebatch.h | 60 +++++++++++++++++++++++++-------------------- 4 files changed, 54 insertions(+), 73 deletions(-) delete mode 100644 yage/data/renderbatch.h (limited to 'yage') diff --git a/yage/core/window.cpp b/yage/core/window.cpp index d8f3a7e8..5db75074 100644 --- a/yage/core/window.cpp +++ b/yage/core/window.cpp @@ -58,7 +58,7 @@ void Window::create(std::string window_name, int width, int height) glfwSetKeyCallback(window_, key_callback); // set vsync on - glfwSwapInterval(0); + glfwSwapInterval(1); // set the clear colour to black glClearColor(0.18f, 0.18f, 0.18f, 1.f); diff --git a/yage/data/renderbatch.h b/yage/data/renderbatch.h deleted file mode 100644 index 51521ec5..00000000 --- a/yage/data/renderbatch.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef YAGE_CORE_RENDERBATCH_H -#define YAGE_CORE_RENDERBATCH_H - -#include - -struct RenderBatch { - GLint offset; - GLsizei num_vertices; - GLuint texture; - - RenderBatch(GLint offset_i, GLsizei num_vertices_i, GLuint texture_i) - : offset(offset_i), num_vertices(num_vertices_i), texture(texture_i) - { - } -}; - -#endif diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp index fb8eb605..14e30ccd 100644 --- a/yage/render/spritebatch.cpp +++ b/yage/render/spritebatch.cpp @@ -8,10 +8,10 @@ #include "spritebatch.h" -#include -#include #include "../core/logger.h" +#include #include +#include using std::cout; @@ -22,15 +22,6 @@ namespace yage const int SpriteBatch::NUM_VERTICES; -Glyph::Glyph(GLuint texture, float depth, const Vertex &top_left, - const Vertex &top_right, const Vertex &bottom_right, - const Vertex &bottom_left) - : texture_(texture), depth_(depth), top_left_(top_left), - top_right_(top_right), bottom_right_(bottom_right), - bottom_left_(bottom_left) -{ -} - SpriteBatch::SpriteBatch() : vao_(0), vbo_(0) { glGenVertexArrays(1, &vao_); @@ -59,9 +50,6 @@ SpriteBatch::SpriteBatch() : vao_(0), vbo_(0) glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); - - // unbind vertex array object - glBindVertexArray(0); } SpriteBatch::~SpriteBatch() @@ -121,11 +109,12 @@ void SpriteBatch::render() { glBindVertexArray(vao_); // sort and create render batches - sortGlyphs(); createRenderBatches(); glActiveTexture(GL_TEXTURE0); for (auto &&batch : render_batches_) { + yLog << "Drawing " << batch.num_vertices + << " vertices bound to texture: " << batch.texture; glBindTexture(GL_TEXTURE_2D, batch.texture); glDrawArrays(GL_TRIANGLES, batch.offset, batch.num_vertices); } @@ -146,22 +135,22 @@ void SpriteBatch::createRenderBatches() vertices.reserve(glyph_ptrs_.size() * NUM_VERTICES); for (int i = 0; i < (int)glyph_ptrs_.size(); ++i) { - if (i == 0 || (i > 0 && (glyph_ptrs_[i]->texture() != - glyph_ptrs_[i - 1]->texture()))) { + if (i == 0 || (i > 0 && (glyph_ptrs_[i]->texture != + glyph_ptrs_[i - 1]->texture))) { render_batches_.emplace_back(i * NUM_VERTICES, NUM_VERTICES, - glyph_ptrs_[i]->texture()); + glyph_ptrs_[i]->texture); } else { render_batches_.back().num_vertices += NUM_VERTICES; } - 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]->top_left); + vertices.push_back(glyph_ptrs_[i]->top_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()); + vertices.push_back(glyph_ptrs_[i]->top_right); + vertices.push_back(glyph_ptrs_[i]->bottom_right); + vertices.push_back(glyph_ptrs_[i]->bottom_left); } // orphan the buffer @@ -174,16 +163,17 @@ void SpriteBatch::createRenderBatches() void SpriteBatch::sortGlyphs() { glyph_ptrs_.reserve(glyphs_.size()); - for(auto &glyph : glyphs_) { + for (auto &glyph : glyphs_) { glyph_ptrs_.push_back(&glyph); } + // sort using introsort or quicksort std::sort(glyph_ptrs_.begin(), glyph_ptrs_.end(), - [](Glyph *a, Glyph *b) -> bool { - if (a->depth() == b->depth()) { - return a->texture() < b->texture(); + [](details::Glyph *a, details::Glyph *b) -> bool { + if (a->depth == b->depth) { + return a->texture < b->texture; } - return a->depth() < b->depth(); + return a->depth < b->depth; }); } diff --git a/yage/render/spritebatch.h b/yage/render/spritebatch.h index 35d9a1b1..fb9f6337 100644 --- a/yage/render/spritebatch.h +++ b/yage/render/spritebatch.h @@ -9,7 +9,6 @@ #ifndef YAGE_SPRITE_BATCH_H #define YAGE_SPRITE_BATCH_H -#include "../data/renderbatch.h" #include "../data/vertex.h" #include "batch.h" @@ -21,33 +20,42 @@ namespace yage { -class SpriteBatch; +namespace details +{ + +struct RenderBatch { + GLint offset; + GLsizei num_vertices; + GLuint texture; + + RenderBatch(GLint offset_i, GLsizei num_vertices_i, GLuint texture_i) + : offset(offset_i), num_vertices(num_vertices_i), texture(texture_i) + { + } +}; /** Glyph with information of the texture. */ -class Glyph -{ -private: - GLuint texture_; - float depth_; - Vertex top_left_; - Vertex top_right_; - Vertex bottom_right_; - Vertex bottom_left_; - -public: - Glyph(GLuint texture, float depth, const Vertex &top_left, - const Vertex &top_right, const Vertex &bottom_right, - const Vertex &bottom_left); - - GLuint texture() const { return texture_; } - float depth() const { return depth_; } - Vertex top_left() const { return top_left_; } - Vertex top_right() const { return top_right_; } - Vertex bottom_right() const { return bottom_right_; } - Vertex bottom_left() const { return bottom_left_; } +struct Glyph { + GLuint texture; + float depth; + Vertex top_left; + Vertex top_right; + Vertex bottom_right; + Vertex bottom_left; + + Glyph(GLuint texture_i, float depth_i, const Vertex &top_left_i, + const Vertex &top_right_i, const Vertex &bottom_right_i, + const Vertex &bottom_left_i) + : texture(texture_i), depth(depth_i), top_left(top_left_i), + top_right(top_right_i), bottom_right(bottom_right_i), + bottom_left(bottom_left_i) + { + } }; +} // namespace details + class SpriteBatch { public: @@ -56,9 +64,9 @@ public: private: GLuint vao_; GLuint vbo_; - std::vector glyphs_; - std::vector glyph_ptrs_; - std::vector render_batches_; + std::vector glyphs_; + std::vector glyph_ptrs_; + std::vector render_batches_; public: SpriteBatch(); -- cgit