diff --git a/07-boilerplate/.imgs/showcase.png b/07-boilerplate/.imgs/showcase.png new file mode 100644 index 0000000..93690b2 Binary files /dev/null and b/07-boilerplate/.imgs/showcase.png differ diff --git a/07-boilerplate/README.md b/07-boilerplate/README.md index f7376fb..a00b1a0 100644 --- a/07-boilerplate/README.md +++ b/07-boilerplate/README.md @@ -1,3 +1,8 @@ -# Boilerplate API +

+ +

-Creating a boilerplate API to abstract away OpenGL +# Boilerplate + +Creating a boilerplate API to abstract away OpenGL. + - Also added my first ever batch renderer diff --git a/07-boilerplate/assets/shaders/default_vertex.glsl b/07-boilerplate/assets/shaders/default_vertex.glsl index fb64759..66f757a 100644 --- a/07-boilerplate/assets/shaders/default_vertex.glsl +++ b/07-boilerplate/assets/shaders/default_vertex.glsl @@ -15,8 +15,9 @@ out float _ctt; void main() { vec3 origined_position = origin.xyz + position.xyz; + origined_position *= 0.2; //gl_Position = projection_matrix * view_matrix * position; - gl_Position = projection_matrix * view_matrix * vec4(position.xyz, 1.0); + gl_Position = projection_matrix * view_matrix * vec4(origined_position, 1.0); _color = color; _texture_position = texture_position; _ctt = ctt; diff --git a/07-boilerplate/src/base/graphics/graphics.hpp b/07-boilerplate/src/base/graphics/graphics.hpp index f682b2c..0160363 100644 --- a/07-boilerplate/src/base/graphics/graphics.hpp +++ b/07-boilerplate/src/base/graphics/graphics.hpp @@ -7,4 +7,4 @@ #define DEFAULT_CTT_ATTRIBUTE 4 #define QUAD 6 -#define DEFAULT_VBO_SIZE QUAD * 1000 // unit: rows of RenderData +#define DEFAULT_VBO_SIZE QUAD * 1000000 // unit: rows of RenderData diff --git a/07-boilerplate/src/base/graphics/renderer.cpp b/07-boilerplate/src/base/graphics/renderer.cpp index 944f021..4b2b036 100644 --- a/07-boilerplate/src/base/graphics/renderer.cpp +++ b/07-boilerplate/src/base/graphics/renderer.cpp @@ -92,7 +92,7 @@ int Renderer::setup(std::optional program) { glEnableVertexAttribArray(DEFAULT_TEXTURE_ATTRIBUTE); glVertexAttribPointer( - DEFAULT_COLOR_ATTRIBUTE, + DEFAULT_CTT_ATTRIBUTE, 1, GL_FLOAT, GL_FALSE, @@ -107,16 +107,29 @@ int Renderer::setup(std::optional program) { return RET_OK; } -void Renderer::render() { +void Renderer::draw(struct RendererData data) { + if (batch_buffer.size() >= DEFAULT_VBO_SIZE) { + LOG(LOG_WARN, "Batch buffer limit reached!"); + return; + } + + batch_buffer.push_back(data); +} + +void Renderer::batch() { glBindVertexArray(this->array_buffer_id); glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer_id); + glBufferSubData(GL_ARRAY_BUFFER, 0, batch_buffer.size() * sizeof(struct RendererData), batch_buffer.data()); + glUseProgram(this->program.id); glUniformMatrix4fv(glGetUniformLocation(this->program.id, "view_matrix"), 1, GL_FALSE, &this->view_matrix[0][0]); glUniformMatrix4fv(glGetUniformLocation(this->program.id, "projection_matrix"), 1, GL_FALSE, &this->projection_matrix[0][0]); glDrawArrays(GL_TRIANGLES, 0, DEFAULT_VBO_SIZE); + + batch_buffer.clear(); } void Renderer::logic() { diff --git a/07-boilerplate/src/base/graphics/renderer.hpp b/07-boilerplate/src/base/graphics/renderer.hpp index d1d1e34..f20f560 100644 --- a/07-boilerplate/src/base/graphics/renderer.hpp +++ b/07-boilerplate/src/base/graphics/renderer.hpp @@ -24,5 +24,9 @@ public: int setup(std::optional program = std::nullopt); void logic(); - void render(); + + std::vector batch_buffer; + + void draw(struct RendererData data); + void batch(); }; diff --git a/07-boilerplate/src/main.cpp b/07-boilerplate/src/main.cpp index 8b629e6..9a363da 100644 --- a/07-boilerplate/src/main.cpp +++ b/07-boilerplate/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -22,19 +23,56 @@ int main() { return RET_ERR; } - Sprite new_sprite(main_renderer.vertex_buffer_id); - new_sprite.bake(); + std::vector sprites; - new_sprite.origin = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); - new_sprite.color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f); + int limit_x = 10; + int limit_y = 10; + float scale = 0.2; + for (int i = -limit_x/2; i < limit_x/2; i++) { + for (int j = -limit_y/2; j < limit_y/2; j++) { + Sprite new_sprite(main_renderer); + new_sprite.bake(); + + float r = (float) rand() / (float) RAND_MAX; + float g = (float) rand() / (float) RAND_MAX; + float b = (float) rand() / (float) RAND_MAX; + + new_sprite.color = glm::vec4(r, g, b, 1.0f); + new_sprite.origin = glm::vec4(i*scale, j*scale, 0.0f, 0.0f); + + sprites.push_back(new_sprite); + } + } + while (!glfwWindowShouldClose(window.window)) { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - new_sprite.update(); + for (int i = 0; i < sprites.size(); i++) { + // randomly update their position by little + float x = ((float) rand() / (float) RAND_MAX) * 0.5; + float y = ((float) rand() / (float) RAND_MAX) * 0.5; + + int x_side = rand() % 2; + int y_side = rand() % 2; + + if (x_side == 0) + sprites[i].origin.x += x; + else + sprites[i].origin.x -= x; + + if (y_side == 0) + sprites[i].origin.y += y; + else + sprites[i].origin.y -= y; + + + sprites[i].update(); + } + main_renderer.logic(); - main_renderer.render(); + main_renderer.batch(); glfwPollEvents(); glfwSwapBuffers(window.window); diff --git a/07-boilerplate/src/sprites/sprite.cpp b/07-boilerplate/src/sprites/sprite.cpp index b738ec9..e26f032 100644 --- a/07-boilerplate/src/sprites/sprite.cpp +++ b/07-boilerplate/src/sprites/sprite.cpp @@ -3,19 +3,19 @@ #include #include #include -#include "../base/graphics/renderer_pack.hpp" +#include "../base/graphics/renderer.hpp" #include "sprite.hpp" -Sprite::Sprite(GLuint vertex_buffer_id) { +Sprite::Sprite(Renderer &renderer) : renderer(renderer) { this->vertices = std::vector(); this->origin = glm::vec4(1.0f); - this->color = glm::vec4(1.0f); + this->color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); this->texture_position = glm::vec2(0.0f); this->ctt = 1.0f; - this->vertex_buffer_id = vertex_buffer_id; } -void Sprite::bake() { +// prepare data +void Sprite::bake() { this->vertices.push_back(glm::vec4(0.5f, 0.5f, 0.0f, 0.0f)); this->vertices.push_back(glm::vec4(0.5f, -0.5f, 0.0f, 0.0f)); this->vertices.push_back(glm::vec4(-0.5f, -0.5f, 0.0f, 0.0f)); @@ -35,9 +35,7 @@ void Sprite::update() { .ctt_ratio = this->ctt }; - glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer_id); - glBufferSubData(GL_ARRAY_BUFFER, i*sizeof(struct RendererData), sizeof(struct RendererData), &data); - glBindBuffer(GL_ARRAY_BUFFER, 0); + this->renderer.draw(data); } } diff --git a/07-boilerplate/src/sprites/sprite.hpp b/07-boilerplate/src/sprites/sprite.hpp index bb2c92c..f657638 100644 --- a/07-boilerplate/src/sprites/sprite.hpp +++ b/07-boilerplate/src/sprites/sprite.hpp @@ -5,6 +5,7 @@ #include #include #include +#include "../base/graphics/renderer.hpp" class Sprite { public: @@ -14,9 +15,10 @@ public: glm::vec2 texture_position; float ctt; - GLuint vertex_buffer_id; + // GLuint vertex_buffer_id; + Renderer &renderer; - Sprite(GLuint vertex_buffer_id); + Sprite(Renderer &renderer); ~Sprite(); void bake();