From 351286d16a4ae862b43245922477276408ac77f9 Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Sat, 16 Dec 2023 01:33:13 +0100 Subject: [PATCH] 07: Extend and play around with batching --- 07-boilerplate/.imgs/showcase.png | Bin 0 -> 5133 bytes 07-boilerplate/README.md | 9 +++- .../assets/shaders/default_vertex.glsl | 3 +- 07-boilerplate/src/base/graphics/graphics.hpp | 2 +- 07-boilerplate/src/base/graphics/renderer.cpp | 17 +++++- 07-boilerplate/src/base/graphics/renderer.hpp | 6 ++- 07-boilerplate/src/main.cpp | 50 +++++++++++++++--- 07-boilerplate/src/sprites/sprite.cpp | 14 +++-- 07-boilerplate/src/sprites/sprite.hpp | 6 ++- 9 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 07-boilerplate/.imgs/showcase.png diff --git a/07-boilerplate/.imgs/showcase.png b/07-boilerplate/.imgs/showcase.png new file mode 100644 index 0000000000000000000000000000000000000000..93690b22f87a36ee638f4070c93ba31001697233 GIT binary patch literal 5133 zcmeHKdpOkj{{M{W*Q8uFwyZQvn`A5L?iQwd%A$nal9iBaj2ZVC-7gy!i(JN(irrAT zq%k2`lwq0>&4ekpAu}Xq#u&!=V4dT4e(m#|{hi-=&iUi#dFJtXzTcV8`~G^p-dC+G z4#+QFu^0d#Z+39+Q2>x!0Ay?y$-pOD`;}M0f3g7w?SlYN_-*=w_<6m88UV@_W_x!X z3rXj}lI^E0Q4 z5{~CsS1YYEwAr(AGv!j9=e2WMM|Hn3w;EQuq}id={Edv94eG{5+_%@C?F` zfL9h(p_^XT5+)QA1EnS-gOLSt(5e5BKN6mq-3P!|jNd|OTylot#}e5D;8XPdYXktK zbwzjpu2^`Z0IBa^bNs)|f@g5#H3a|+CR?x9c9SP-k(mcm7EoS4{>~6uvAWm--@W$Y z!&o!^HOD`Q1#3R-7S^$!!xd3ITK(SYxGItGo&H%e0E=@l0$^`;G4dZiwwAzZVcYYg zrbgQeIUP|^&o!|@Z~w#v+y@&FagA(w+A=hPB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*|mQaM(ejC;FQF=}RP<)_Xu{#~(bW~LT z*2Q;1BxXt0SRjFKUG|Z`3axpYz=+o|%BH6Ap`%c9ddfQ(Vd?tBdZc)vO4$=bUnP)Q zKi5Jau(Y2`Q$ZuNm&}WM-bxC|gA%z?(vi08flEfDRj_avEj)-ukxtEOp1)yA^2EGk{$s~{f0!`JMLzD?rMAMM+`fcOvdoTIpQn-) zff%+|gX>H){xeIrGY77eaUE4b9dNyBWEp9i%V3q}LDRguA@7535#yQ(jPua!Mlr9~ n{)yusth@jJ_}`TQOzad&)oz4(&F-pX+6S|J7JKt|pFH;$O&!?z literal 0 HcmV?d00001 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();