07: Extend and play around with batching
This commit is contained in:
parent
6fdbcc8825
commit
351286d16a
BIN
07-boilerplate/.imgs/showcase.png
Normal file
BIN
07-boilerplate/.imgs/showcase.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.0 KiB |
|
@ -1,3 +1,8 @@
|
||||||
# Boilerplate API
|
<p align="center">
|
||||||
|
<img src=".imgs/showcase.png" width="300" />
|
||||||
|
</p>
|
||||||
|
|
||||||
Creating a boilerplate API to abstract away OpenGL
|
# Boilerplate
|
||||||
|
|
||||||
|
Creating a boilerplate API to abstract away OpenGL.
|
||||||
|
- Also added my first ever batch renderer
|
||||||
|
|
|
@ -15,8 +15,9 @@ out float _ctt;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec3 origined_position = origin.xyz + position.xyz;
|
vec3 origined_position = origin.xyz + position.xyz;
|
||||||
|
origined_position *= 0.2;
|
||||||
//gl_Position = projection_matrix * view_matrix * position;
|
//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;
|
_color = color;
|
||||||
_texture_position = texture_position;
|
_texture_position = texture_position;
|
||||||
_ctt = ctt;
|
_ctt = ctt;
|
||||||
|
|
|
@ -7,4 +7,4 @@
|
||||||
#define DEFAULT_CTT_ATTRIBUTE 4
|
#define DEFAULT_CTT_ATTRIBUTE 4
|
||||||
|
|
||||||
#define QUAD 6
|
#define QUAD 6
|
||||||
#define DEFAULT_VBO_SIZE QUAD * 1000 // unit: rows of RenderData
|
#define DEFAULT_VBO_SIZE QUAD * 1000000 // unit: rows of RenderData
|
||||||
|
|
|
@ -92,7 +92,7 @@ int Renderer::setup(std::optional<Program> program) {
|
||||||
glEnableVertexAttribArray(DEFAULT_TEXTURE_ATTRIBUTE);
|
glEnableVertexAttribArray(DEFAULT_TEXTURE_ATTRIBUTE);
|
||||||
|
|
||||||
glVertexAttribPointer(
|
glVertexAttribPointer(
|
||||||
DEFAULT_COLOR_ATTRIBUTE,
|
DEFAULT_CTT_ATTRIBUTE,
|
||||||
1,
|
1,
|
||||||
GL_FLOAT,
|
GL_FLOAT,
|
||||||
GL_FALSE,
|
GL_FALSE,
|
||||||
|
@ -107,16 +107,29 @@ int Renderer::setup(std::optional<Program> program) {
|
||||||
return RET_OK;
|
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);
|
glBindVertexArray(this->array_buffer_id);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertex_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);
|
glUseProgram(this->program.id);
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(this->program.id, "view_matrix"), 1, GL_FALSE, &this->view_matrix[0][0]);
|
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]);
|
glUniformMatrix4fv(glGetUniformLocation(this->program.id, "projection_matrix"), 1, GL_FALSE, &this->projection_matrix[0][0]);
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, DEFAULT_VBO_SIZE);
|
glDrawArrays(GL_TRIANGLES, 0, DEFAULT_VBO_SIZE);
|
||||||
|
|
||||||
|
batch_buffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::logic() {
|
void Renderer::logic() {
|
||||||
|
|
|
@ -24,5 +24,9 @@ public:
|
||||||
|
|
||||||
int setup(std::optional<Program> program = std::nullopt);
|
int setup(std::optional<Program> program = std::nullopt);
|
||||||
void logic();
|
void logic();
|
||||||
void render();
|
|
||||||
|
std::vector<struct RendererData> batch_buffer;
|
||||||
|
|
||||||
|
void draw(struct RendererData data);
|
||||||
|
void batch();
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <cstdlib>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <stb/stb_image.h>
|
#include <stb/stb_image.h>
|
||||||
|
@ -22,19 +23,56 @@ int main() {
|
||||||
return RET_ERR;
|
return RET_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprite new_sprite(main_renderer.vertex_buffer_id);
|
std::vector<Sprite> sprites;
|
||||||
|
|
||||||
|
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();
|
new_sprite.bake();
|
||||||
|
|
||||||
new_sprite.origin = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
float r = (float) rand() / (float) RAND_MAX;
|
||||||
new_sprite.color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
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)) {
|
while (!glfwWindowShouldClose(window.window)) {
|
||||||
glClearColor(0, 0, 0, 1);
|
glClearColor(0, 0, 0, 1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
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.logic();
|
||||||
main_renderer.render();
|
main_renderer.batch();
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
glfwSwapBuffers(window.window);
|
glfwSwapBuffers(window.window);
|
||||||
|
|
|
@ -3,18 +3,18 @@
|
||||||
#include <glm/vec4.hpp>
|
#include <glm/vec4.hpp>
|
||||||
#include <glm/vec2.hpp>
|
#include <glm/vec2.hpp>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include "../base/graphics/renderer_pack.hpp"
|
#include "../base/graphics/renderer.hpp"
|
||||||
#include "sprite.hpp"
|
#include "sprite.hpp"
|
||||||
|
|
||||||
Sprite::Sprite(GLuint vertex_buffer_id) {
|
Sprite::Sprite(Renderer &renderer) : renderer(renderer) {
|
||||||
this->vertices = std::vector<glm::vec4>();
|
this->vertices = std::vector<glm::vec4>();
|
||||||
this->origin = glm::vec4(1.0f);
|
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->texture_position = glm::vec2(0.0f);
|
||||||
this->ctt = 1.0f;
|
this->ctt = 1.0f;
|
||||||
this->vertex_buffer_id = vertex_buffer_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepare data
|
||||||
void Sprite::bake() {
|
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));
|
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
|
.ctt_ratio = this->ctt
|
||||||
};
|
};
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertex_buffer_id);
|
this->renderer.draw(data);
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, i*sizeof(struct RendererData), sizeof(struct RendererData), &data);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <glm/vec4.hpp>
|
#include <glm/vec4.hpp>
|
||||||
#include <glm/vec2.hpp>
|
#include <glm/vec2.hpp>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
#include "../base/graphics/renderer.hpp"
|
||||||
|
|
||||||
class Sprite {
|
class Sprite {
|
||||||
public:
|
public:
|
||||||
|
@ -14,9 +15,10 @@ public:
|
||||||
glm::vec2 texture_position;
|
glm::vec2 texture_position;
|
||||||
float ctt;
|
float ctt;
|
||||||
|
|
||||||
GLuint vertex_buffer_id;
|
// GLuint vertex_buffer_id;
|
||||||
|
Renderer &renderer;
|
||||||
|
|
||||||
Sprite(GLuint vertex_buffer_id);
|
Sprite(Renderer &renderer);
|
||||||
~Sprite();
|
~Sprite();
|
||||||
|
|
||||||
void bake();
|
void bake();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user