From f949692714e72a0e2d45ebb6a5d698424ab71dee Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 25 Dec 2017 13:54:09 +0000 Subject: [Broken] Reorganising and fixing. --- yage/core/glslprogram.cpp | 215 ---------------------------------------------- yage/core/glslprogram.h | 58 ------------- yage/core/input.h | 151 -------------------------------- yage/core/sprite.cpp | 95 -------------------- yage/core/sprite.h | 49 ----------- yage/core/spritebatch.cpp | 200 ------------------------------------------ yage/core/spritebatch.h | 110 ------------------------ yage/core/texture.h | 32 ------- yage/core/vertex.h | 84 ------------------ 9 files changed, 994 deletions(-) delete mode 100644 yage/core/glslprogram.cpp delete mode 100644 yage/core/glslprogram.h delete mode 100644 yage/core/input.h delete mode 100644 yage/core/sprite.cpp delete mode 100644 yage/core/sprite.h delete mode 100644 yage/core/spritebatch.cpp delete mode 100644 yage/core/spritebatch.h delete mode 100644 yage/core/texture.h delete mode 100644 yage/core/vertex.h (limited to 'yage/core') diff --git a/yage/core/glslprogram.cpp b/yage/core/glslprogram.cpp deleted file mode 100644 index 13abbba3..00000000 --- a/yage/core/glslprogram.cpp +++ /dev/null @@ -1,215 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: glslprogram.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "glslprogram.h" - -#include -#include -#include - -namespace yage -{ - -GlslProgram::~GlslProgram() -{ - /// cleans up all the shaders and the program - if (fragment_shader_id_ != 0) { - glDeleteShader(fragment_shader_id_); - } - - if (vertex_shader_id_ != 0) { - glDeleteShader(vertex_shader_id_); - } - - if (program_id_ != 0) { - glDeleteProgram(program_id_); - } -} - -void GlslProgram::compileShader(GLuint shader, const std::string &shaderContent) -{ - // cast source to a c string to get the address of it and input it for - // compilation - const auto *vertex_source = (const GLchar *)shaderContent.c_str(); - glShaderSource(shader, 1, &vertex_source, nullptr); - glCompileShader(shader); - - // check if compilation was successful - GLint is_compiled = 0; - glGetShaderiv(shader, GL_COMPILE_STATUS, (int *)&is_compiled); - - // if it isn't compiled throw exception to clean up - if (is_compiled == GL_FALSE) { - GLint max_length = 0; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length); - - std::vector error_log(max_length); - glGetShaderInfoLog(shader, max_length, &max_length, &error_log[0]); - std::string error_log_str((const char *)&error_log[0]); - - std::string shaderName; - if (shader == vertex_shader_id_) - shaderName = "vertex shader"; - else - shaderName = "fragment shader"; - - throw std::runtime_error("Couldn't compile the " + shaderName + " : " + - error_log_str); - } -} - -void GlslProgram::compileShaderFromFile(GLuint shader, - const std::string &file_path) -{ - // get a string with the input from the shader file - std::ifstream file(file_path); - if (!file.is_open()) { - throw std::runtime_error("Failed to open '" + file_path + "'"); - } - - std::string content = ""; - std::string line; - - while (std::getline(file, line)) { - content += line + "\n"; - } - file.close(); - - compileShader(shader, content); -} - -void GlslProgram::initShaderId() -{ - // create the program that will be run on GPU - program_id_ = glCreateProgram(); - - // create vertex shader - vertex_shader_id_ = glCreateShader(GL_VERTEX_SHADER); - if (vertex_shader_id_ == 0) { - throw std::runtime_error("Vertex shader failed to be created"); - } - - // create fragment shader - fragment_shader_id_ = glCreateShader(GL_FRAGMENT_SHADER); - if (fragment_shader_id_ == 0) { - throw std::runtime_error("Fragment shader failed to be created"); - } -} - -void GlslProgram::compileShaders(const std::string &vertexShader, - const std::string fragmentShader) -{ - initShaderId(); - - compileShader(vertex_shader_id_, vertexShader); - compileShader(fragment_shader_id_, fragmentShader); -} - -void GlslProgram::compileShadersFromFile( - const std::string &vertex_shader_path, - const std::string &fragment_shader_path) -{ - initShaderId(); - - // compile the two shaders - compileShaderFromFile(vertex_shader_id_, vertex_shader_path); - compileShaderFromFile(fragment_shader_id_, fragment_shader_path); -} - -void GlslProgram::linkShaders() -{ - // attach the shaders that we want - glAttachShader(program_id_, vertex_shader_id_); - glAttachShader(program_id_, fragment_shader_id_); - - // link our program - glLinkProgram(program_id_); - - GLint is_linked = 0; - glGetProgramiv(program_id_, GL_LINK_STATUS, (int *)&is_linked); - if (is_linked == GL_FALSE) { - GLint max_length = 0; - glGetProgramiv(program_id_, GL_INFO_LOG_LENGTH, &max_length); - - std::vector error_log(max_length); - glGetProgramInfoLog(program_id_, max_length, &max_length, - &error_log[0]); - - std::string error_log_str((const char *)&error_log[0]); - - throw std::runtime_error("Could not link program: " + error_log_str); - } - - // detach shaders after successful link - glDetachShader(program_id_, fragment_shader_id_); - glDetachShader(program_id_, vertex_shader_id_); - - // we can then delete the shaders once we have the program - glDeleteShader(fragment_shader_id_); - glDeleteShader(vertex_shader_id_); -} - -void GlslProgram::addAttribute(const std::string &attribute_name) -{ - glBindAttribLocation(program_id_, attribute_index_++, - attribute_name.c_str()); -} - -GLint GlslProgram::getUniformLocation(const std::string &uniform_name) -{ - GLint location = glGetUniformLocation(program_id_, uniform_name.c_str()); - if ((GLuint)location == GL_INVALID_INDEX) { - throw std::runtime_error("'" + uniform_name + "' not found"); - } - return location; -} - -void GlslProgram::use() -{ - glUseProgram(program_id_); - for (int i = 0; i < attribute_index_; ++i) { - glEnableVertexAttribArray(i); - } -} - -void GlslProgram::unuse() -{ - for (int i = 0; i < attribute_index_; ++i) { - glDisableVertexAttribArray(i); - } - glUseProgram(0); -} - -void GlslProgram::defaultSetup() -{ - std::string vertexShader = - "#version 130\n\nin vec2 vertex_position;\nin vec4 vertex_colour;\nin " - "vec2 vertex_uv;\n\nout vec2 fragment_position;\nout vec4 " - "fragment_colour;\nout vec2 fragment_uv;\n\nuniform mat4 P;\n\nvoid " - "main()\n{\n gl_Position.xy = (P*vec4(vertex_position, 0.0, " - "1.0)).xy;\n gl_Position.z = 0.0;\n gl_Position.w = 1.0;\n\n " - "fragment_position = vertex_position;\n fragment_colour = " - "vertex_colour;\n fragment_uv = vec2(vertex_uv.x, " - "1-vertex_uv.y);\n\n}"; - - std::string fragmentShader = - "#version 130\n\nin vec2 fragment_position;\nin vec4 " - "fragment_colour;\nin vec2 fragment_uv;\n\nout vec4 colour;\n\nuniform " - "sampler2D texture_sampler;\n\nvoid main()\n{\n vec4 texture_color " - "= texture(texture_sampler, fragment_uv);\n\n colour = " - "texture_color;\n}"; - - compileShaders(vertexShader, fragmentShader); - addAttribute("vertex_position"); - addAttribute("vertex_colour"); - addAttribute("vertex_uv"); - - linkShaders(); -} - -} // namespace yage diff --git a/yage/core/glslprogram.h b/yage/core/glslprogram.h deleted file mode 100644 index 0617bc1e..00000000 --- a/yage/core/glslprogram.h +++ /dev/null @@ -1,58 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: glslprogram.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#ifndef GLSL_PROGRAM_H -#define GLSL_PROGRAM_H - -#include - -#include - -namespace yage -{ - -class GlslProgram -{ -public: - GlslProgram() = default; - GlslProgram(const GlslProgram &) = delete; - GlslProgram(GlslProgram &&) = delete; - ~GlslProgram(); - - GlslProgram &operator=(const GlslProgram &) = delete; - GlslProgram &operator=(GlslProgram &&) = delete; - - /// compiles vertex and fragment shader - void compileShaders(const std::string &vertexShader, - const std::string fragmentShader); - void compileShadersFromFile(const std::string &vertex_shader_path, - const std::string &fragment_shader_path); - void linkShaders(); - void addAttribute(const std::string &attribute_name); - GLint getUniformLocation(const std::string &uniform_name); - void use(); - void unuse(); - - void defaultSetup(); - -private: - /// compiled shader program id - GLuint program_id_ = 0; - GLuint vertex_shader_id_ = 0; - GLuint fragment_shader_id_ = 0; - int attribute_index_ = 0; - - /// compiles one shader - void compileShader(GLuint shader, const std::string &shaderContent); - void compileShaderFromFile(GLuint shader, const std::string &file_path); - void initShaderId(); -}; - -} // namespace yage - -#endif diff --git a/yage/core/input.h b/yage/core/input.h deleted file mode 100644 index 866793d8..00000000 --- a/yage/core/input.h +++ /dev/null @@ -1,151 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: input.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#ifndef YAGE_CORE_INPUT_H -#define YAGE_CORE_INPUT_H - -#define GLFW_INCLUDE_NONE -#include - -namespace yage -{ - -enum class key { - // unknown key - UNKNOWN = GLFW_KEY_UNKNOWN, - - // actual keys - SPACE = GLFW_KEY_SPACE, - APOSTROPHE = GLFW_KEY_APOSTROPHE, /* ' */ - COMMA = GLFW_KEY_COMMA, /* , */ - MINUS = GLFW_KEY_MINUS, /* - */ - PERIOD = GLFW_KEY_PERIOD, /* . */ - SLASH = GLFW_KEY_SLASH, /* / */ - NUM0 = GLFW_KEY_0, - NUM1 = GLFW_KEY_1, - NUM2 = GLFW_KEY_2, - NUM3 = GLFW_KEY_3, - NUM4 = GLFW_KEY_4, - NUM5 = GLFW_KEY_5, - NUM6 = GLFW_KEY_6, - NUM7 = GLFW_KEY_7, - NUM8 = GLFW_KEY_8, - NUM9 = GLFW_KEY_9, - SEMICOLON = GLFW_KEY_SEMICOLON, /* ; */ - EQUAL = GLFW_KEY_EQUAL, /* = */ - A = GLFW_KEY_A, - B = GLFW_KEY_B, - C = GLFW_KEY_C, - D = GLFW_KEY_D, - E = GLFW_KEY_E, - F = GLFW_KEY_F, - G = GLFW_KEY_G, - H = GLFW_KEY_H, - I = GLFW_KEY_I, - J = GLFW_KEY_J, - K = GLFW_KEY_K, - L = GLFW_KEY_L, - M = GLFW_KEY_M, - N = GLFW_KEY_N, - O = GLFW_KEY_O, - P = GLFW_KEY_P, - Q = GLFW_KEY_Q, - R = GLFW_KEY_R, - S = GLFW_KEY_S, - T = GLFW_KEY_T, - U = GLFW_KEY_U, - V = GLFW_KEY_V, - W = GLFW_KEY_W, - X = GLFW_KEY_X, - Y = GLFW_KEY_Y, - Z = GLFW_KEY_Z, - LEfT_BRACKET = GLFW_KEY_LEFT_BRACKET, /* [ */ - BACKSLASH = GLFW_KEY_BACKSLASH, /* \ */ - RIGHT_BRACKET = GLFW_KEY_RIGHT_BRACKET, /* ] */ - GRAVE_ACCENT = GLFW_KEY_GRAVE_ACCENT, /* ` */ - WORLD_1 = GLFW_KEY_WORLD_1, /* non-US #1 */ - WORLD_2 = GLFW_KEY_WORLD_2, /* non-US #2 */ - - // function keys - ESCAPE = GLFW_KEY_ESCAPE, - ENTER = GLFW_KEY_ENTER, - TAB = GLFW_KEY_TAB, - BACKSPACE = GLFW_KEY_BACKSPACE, - INSERT = GLFW_KEY_INSERT, - DELETE = GLFW_KEY_DELETE, - RIGHT = GLFW_KEY_RIGHT, - LEFT = GLFW_KEY_LEFT, - DOWN = GLFW_KEY_DOWN, - UP = GLFW_KEY_UP, - PAGE_UP = GLFW_KEY_PAGE_UP, - PAGE_DOWN = GLFW_KEY_PAGE_DOWN, - HOME = GLFW_KEY_HOME, - END = GLFW_KEY_END, - CAPS_LOCK = GLFW_KEY_CAPS_LOCK, - SCROLL_LOCK = GLFW_KEY_SCROLL_LOCK, - NUM_LOCK = GLFW_KEY_NUM_LOCK, - PRINT_SCREEN = GLFW_KEY_PRINT_SCREEN, - PAUSE = GLFW_KEY_PAUSE, - F1 = GLFW_KEY_F1, - F2 = GLFW_KEY_F2, - F3 = GLFW_KEY_F3, - F4 = GLFW_KEY_F4, - F5 = GLFW_KEY_F5, - F6 = GLFW_KEY_F6, - F7 = GLFW_KEY_F7, - F8 = GLFW_KEY_F8, - F9 = GLFW_KEY_F9, - F10 = GLFW_KEY_F10, - F11 = GLFW_KEY_F11, - F12 = GLFW_KEY_F12, - F13 = GLFW_KEY_F13, - F14 = GLFW_KEY_F14, - F15 = GLFW_KEY_F15, - F16 = GLFW_KEY_F16, - F17 = GLFW_KEY_F17, - F18 = GLFW_KEY_F18, - F19 = GLFW_KEY_F19, - F20 = GLFW_KEY_F20, - F21 = GLFW_KEY_F21, - F22 = GLFW_KEY_F22, - F23 = GLFW_KEY_F23, - F24 = GLFW_KEY_F24, - F25 = GLFW_KEY_F25, - KP_0 = GLFW_KEY_KP_0, - KP_1 = GLFW_KEY_KP_1, - KP_2 = GLFW_KEY_KP_2, - KP_3 = GLFW_KEY_KP_3, - KP_4 = GLFW_KEY_KP_4, - KP_5 = GLFW_KEY_KP_5, - KP_6 = GLFW_KEY_KP_6, - KP_7 = GLFW_KEY_KP_7, - KP_8 = GLFW_KEY_KP_8, - KP_9 = GLFW_KEY_KP_9, - KP_DECIMAL = GLFW_KEY_KP_DECIMAL, - KP_DIVIDE = GLFW_KEY_KP_DIVIDE, - KP_MULTIPLY = GLFW_KEY_KP_MULTIPLY, - KP_SUBTRACT = GLFW_KEY_KP_SUBTRACT, - KP_ADD = GLFW_KEY_KP_ADD, - KP_ENTER = GLFW_KEY_KP_ENTER, - KP_EQUAL = GLFW_KEY_KP_EQUAL, - LEFT_SHIFT = GLFW_KEY_LEFT_SHIFT, - LEFT_CONTROL = GLFW_KEY_LEFT_CONTROL, - LEFT_ALT = GLFW_KEY_LEFT_ALT, - LEFT_SUPER = GLFW_KEY_LEFT_SUPER, - RIGHT_SHIFT = GLFW_KEY_RIGHT_SHIFT, - RIGHT_CONTROL = GLFW_KEY_RIGHT_CONTROL, - RIGHT_ALT = GLFW_KEY_RIGHT_ALT, - RIGHT_SUPER = GLFW_KEY_RIGHT_SUPER, - MENU = GLFW_KEY_MENU, - - LAST = GLFW_KEY_LAST, -}; - -} // namepsace yage - -#endif diff --git a/yage/core/sprite.cpp b/yage/core/sprite.cpp deleted file mode 100644 index 6862f910..00000000 --- a/yage/core/sprite.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: sprite.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include -#include -#include - -#include - -namespace yage -{ - -Sprite::~Sprite() -{ - if (vbo_id_ != 0) { - glDeleteBuffers(1, &vbo_id_); - } -} - -void Sprite::init(float x, float y, float width, float height, - const std::string &texture_path) -{ - x_ = x; - y_ = y; - width_ = width; - height_ = height; - texture_ = ResourceManager::getTexture(texture_path); - - if (vbo_id_ == 0) { - glGenBuffers(1, &vbo_id_); - } - - Vertex vertex_data[6]; - - vertex_data[0].setPosition(x + width, y + height); - vertex_data[0].setUv(1.f, 1.f); - - vertex_data[1].setPosition(x, y + height); - vertex_data[1].setUv(0.f, 1.f); - - vertex_data[2].setPosition(x, y); - vertex_data[2].setUv(0.f, 0.f); - - vertex_data[3].setPosition(x, y); - vertex_data[3].setUv(0.f, 0.f); - - vertex_data[4].setPosition(x + width, y + height); - vertex_data[4].setUv(1.f, 1.f); - - vertex_data[5].setPosition(x + width, y); - vertex_data[5].setUv(1.f, 0.f); - - for (auto &i : vertex_data) { - i.setColour(255, 0, 255, 255); - } - - vertex_data[1].setColour(0, 255, 255, 255); - vertex_data[4].setColour(255, 0, 0, 255); - - glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, - GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -void Sprite::draw() -{ - glBindTexture(GL_TEXTURE_2D, texture_.id); - glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); - - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - 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)); - glDrawArrays(GL_TRIANGLES, 0, 6); - - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -} // namespace yage diff --git a/yage/core/sprite.h b/yage/core/sprite.h deleted file mode 100644 index 852f4f28..00000000 --- a/yage/core/sprite.h +++ /dev/null @@ -1,49 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: sprite.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#ifndef SPRITE_H -#define SPRITE_H - -#include "texture.h" - -#include - -#include - -namespace yage -{ - -/** @deprecated Use SpriteBatch instead - */ -class Sprite -{ -private: - float x_; - float y_; - float width_; - float height_; - GLuint vbo_id_ = 0; - Texture texture_; - -public: - Sprite() = default; - Sprite(const Sprite &) = delete; - Sprite(Sprite &&) = delete; - ~Sprite(); - - Sprite &operator=(const Sprite &) = delete; - Sprite &operator=(Sprite &&) = delete; - - void init(float x, float y, float width, float height, - const std::string &texture_path); - void draw(); -}; - -} // namespace yage - -#endif diff --git a/yage/core/spritebatch.cpp b/yage/core/spritebatch.cpp deleted file mode 100644 index 2159aeec..00000000 --- a/yage/core/spritebatch.cpp +++ /dev/null @@ -1,200 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: spritebatch.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include - -#include -#include - -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) -{ -} - -RenderBatch::RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture) - : num_vertices_(num_vertices), offset_(offset), texture_(texture) -{ -} - -SpriteBatch::SpriteBatch() -{ - createVertexArray(); -} - -SpriteBatch::~SpriteBatch() -{ - if (vao_ != 0) { - glDeleteVertexArrays(1, &vao_); - } - - if (vbo_ != 0) { - glDeleteVertexArrays(1, &vbo_); - } -} - -void SpriteBatch::begin() -{ - glyphs_.clear(); - glyph_ptrs_.clear(); - render_batches_.clear(); -} - -void SpriteBatch::end() -{ - sortGlyphs(); - createRenderBatches(); -} - -void SpriteBatch::draw(const yage::Vector4f &destination_rect, - const yage::Vector4f &uv_rect, GLuint texture, - const Colour &colour, float depth) -{ - Vertex top_left, top_right, bottom_right, bottom_left; - - top_left.colour = colour; - top_left.setPosition(destination_rect.x, - destination_rect.y + destination_rect.w); - top_left.setUv(uv_rect.x, uv_rect.y + uv_rect.w); - - top_right.colour = colour; - top_right.setPosition(destination_rect.x + destination_rect.z, - destination_rect.y + destination_rect.w); - top_right.setUv(uv_rect.x + uv_rect.z, uv_rect.y + uv_rect.w); - - bottom_right.colour = colour; - bottom_right.setPosition(destination_rect.x + destination_rect.z, - destination_rect.y); - bottom_right.setUv(uv_rect.x + uv_rect.z, uv_rect.y); - - bottom_left.colour = colour; - bottom_left.setPosition(destination_rect.x, destination_rect.y); - bottom_left.setUv(uv_rect.x, uv_rect.y); - - // deal with fragmenting by creating vector of pointers - glyphs_.emplace_back(texture, depth, top_left, top_right, bottom_right, - bottom_left); - glyph_ptrs_.push_back(&glyphs_.back()); -} - -void SpriteBatch::render() -{ - // sort and create render batches - sortGlyphs(); - createRenderBatches(); - - glBindVertexArray(vao_); - for (auto &&batch : render_batches_) { - glBindTexture(GL_TEXTURE_2D, batch.texture()); - glDrawArrays(GL_TRIANGLES, batch.offset(), batch.num_vertices()); - } - glBindVertexArray(0); - - // clear and reset the vectors - glyphs_.clear(); - glyph_ptrs_.clear(); - 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_); - - // enable vertex attribute arrays - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - // 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)); - glDrawArrays(GL_TRIANGLES, 0, 6); - - // unbind vertex array object - glBindVertexArray(0); -} - -void SpriteBatch::createRenderBatches() -{ - std::vector vertices; - if (glyph_ptrs_.empty()) { - return; - } - - render_batches_.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()))) { - render_batches_.emplace_back(i * NUM_VERTICES, NUM_VERTICES, - 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]->bottom_right()); - vertices.push_back(glyph_ptrs_[i]->top_right()); - - // bind vbo - glBindBuffer(GL_ARRAY_BUFFER, vbo_); - // orphan the buffer - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr, - GL_DYNAMIC_DRAW); - // upload the data - glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), - vertices.data()); - // unbind buffer - glBindBuffer(GL_ARRAY_BUFFER, 0); - } -} - -void SpriteBatch::sortGlyphs() -{ - // 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(); - } - return a->depth() < b->depth(); - }); -} - -} // namespace yage diff --git a/yage/core/spritebatch.h b/yage/core/spritebatch.h deleted file mode 100644 index c16b44f6..00000000 --- a/yage/core/spritebatch.h +++ /dev/null @@ -1,110 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: spritebatch.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -/** @file - */ - -#ifndef YAGE_SPRITE_BATCH_H -#define YAGE_SPRITE_BATCH_H - -#include "vertex.h" - -#include -#include -#include - -#include - -namespace yage -{ - -class SpriteBatch; - -/** 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_; } -}; - -class RenderBatch -{ - friend SpriteBatch; - -private: - GLsizei num_vertices_; - GLint offset_; - GLuint texture_; - -public: - RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture); - - GLint offset() const { return offset_; } - GLsizei num_vertices() const { return num_vertices_; } - GLuint texture() const { return texture_; } -}; - -class SpriteBatch -{ -public: - static const int NUM_VERTICES = 6; - -private: - GLuint vbo_ = 0; - GLuint vao_ = 0; - std::vector glyphs_; - std::vector glyph_ptrs_; - std::vector render_batches_; - -public: - SpriteBatch(); - SpriteBatch(const SpriteBatch &) = delete; - SpriteBatch(SpriteBatch &&) = delete; - ~SpriteBatch(); - - SpriteBatch &operator=(const SpriteBatch &) = delete; - SpriteBatch &operator=(SpriteBatch &&) = delete; - - // initialize vaos and vbos - void begin(); - void end(); - - // adds a sprite to the sprite batch to be rendered later - void draw(const yage::Vector4f &destination_rect, - const yage::Vector4f &uv_rect, GLuint texture, - const Colour &colour, float depth); - // render the batch - void render(); - -private: - void createVertexArray(); - void createRenderBatches(); - void sortGlyphs(); -}; - -} // namespace yage - -#endif diff --git a/yage/core/texture.h b/yage/core/texture.h deleted file mode 100644 index aec7b906..00000000 --- a/yage/core/texture.h +++ /dev/null @@ -1,32 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: texture.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#ifndef YAGE_CORE_TEXTURE_H -#define YAGE_CORE_TEXTURE_H - -#include - -namespace yage -{ - -struct Texture { - GLuint id; - int width; - int height; - - Texture() : id(0), width(0), height(0) {} - - Texture(GLuint id_i, int width_i, int height_i) - : id(id_i), width(width_i), height(height_i) - { - } -}; - -} // namespace yage - -#endif diff --git a/yage/core/vertex.h b/yage/core/vertex.h deleted file mode 100644 index 4cd095a9..00000000 --- a/yage/core/vertex.h +++ /dev/null @@ -1,84 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: vertex.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#ifndef VERTEX_H -#define VERTEX_H - -#include - -namespace yage -{ - -struct Position { - float x; - float y; - - Position() = default; - - Position(float x_, float y_) : x(x_), y(y_) {} -}; - -struct Colour { - GLubyte r; - GLubyte g; - GLubyte b; - GLubyte a; - - Colour() : r(0), g(0), b(0), a(0) {} - - Colour(GLubyte r_, GLubyte g_, GLubyte b_, GLubyte a_) - : r(r_), g(g_), b(b_), a(a_) - { - } -}; - -struct UV { - float u; - float v; - - UV() = default; - - UV(float u_, float v_) : u(u_), v(v_) {} -}; - -struct Vertex { - Position position; - Colour colour; - UV uv; - - Vertex() = default; - - Vertex(const Position &position_, const Colour &colour_, const UV &uv_) - : position(position_), colour(colour_), uv(uv_) - { - } - - void setPosition(float x, float y) - { - position.x = x; - position.y = y; - } - - void setColour(GLubyte r, GLubyte g, GLubyte b, GLubyte a) - { - colour.r = r; - colour.g = g; - colour.b = b; - colour.a = a; - } - - void setUv(float u, float v) - { - uv.u = u; - uv.v = v; - } -}; - -} // namespace yage - -#endif -- cgit