Skip to content

Commit

Permalink
refactor: Batch2D and Batch3D
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Jun 6, 2024
1 parent 18a3dca commit 7db76a0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 43 deletions.
21 changes: 11 additions & 10 deletions src/graphics/core/Batch2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
{2}, {2}, {4}, {0}
};

buffer = new float[capacity * B2D_VERTEX_SIZE];
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
buffer = std::make_unique<float[]>(capacity * B2D_VERTEX_SIZE);
mesh = std::make_unique<Mesh>(buffer.get(), 0, attrs);
index = 0;

ubyte pixels[] = {
0xFF, 0xFF, 0xFF, 0xFF
};
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr;
currentTexture = nullptr;
}

Batch2D::~Batch2D(){
delete[] buffer;
}

void Batch2D::setPrimitive(DrawPrimitive primitive) {
Expand All @@ -37,7 +36,7 @@ void Batch2D::setPrimitive(DrawPrimitive primitive) {
}

void Batch2D::begin(){
_texture = nullptr;
currentTexture = nullptr;
blank->bind();
color = glm::vec4(1.0f);
primitive = DrawPrimitive::triangle;
Expand Down Expand Up @@ -73,14 +72,16 @@ void Batch2D::vertex(
}

void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture)
if (currentTexture == new_texture) {
return;
}
flush();
_texture = new_texture;
if (new_texture == nullptr)
currentTexture = new_texture;
if (new_texture == nullptr) {
blank->bind();
else
} else {
new_texture->bind();
}
}

void Batch2D::untexture() {
Expand Down Expand Up @@ -327,7 +328,7 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
void Batch2D::flush() {
if (index == 0)
return;
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
mesh->reload(buffer.get(), index / B2D_VERTEX_SIZE);
mesh->draw(gl::to_glenum(primitive));
index = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/core/Batch2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Texture;
struct UVRegion;

class Batch2D {
float* buffer;
std::unique_ptr<float[]> buffer;
size_t capacity;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank;
size_t index;
glm::vec4 color;
Texture* _texture;
Texture* currentTexture;
DrawPrimitive primitive = DrawPrimitive::triangle;

void setPrimitive(DrawPrimitive primitive);
Expand Down
41 changes: 24 additions & 17 deletions src/graphics/core/Batch3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@ Batch3D::Batch3D(size_t capacity)
{3}, {2}, {4}, {0}
};

buffer = new float[capacity * B3D_VERTEX_SIZE];
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
buffer = std::make_unique<float[]>(capacity * B3D_VERTEX_SIZE);
mesh = std::make_unique<Mesh>(buffer.get(), 0, attrs);
index = 0;

ubyte pixels[] = {
255, 255, 255, 255,
};
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr;
currentTexture = nullptr;
}

Batch3D::~Batch3D(){
delete[] buffer;
}

void Batch3D::begin(){
_texture = nullptr;
currentTexture = nullptr;
blank->bind();
}

void Batch3D::vertex(float x, float y, float z, float u, float v,
float r, float g, float b, float a) {
void Batch3D::vertex(
float x, float y, float z, float u, float v,
float r, float g, float b, float a
) {
buffer[index++] = x;
buffer[index++] = y;
buffer[index++] = z;
Expand All @@ -46,8 +47,10 @@ void Batch3D::vertex(float x, float y, float z, float u, float v,
buffer[index++] = b;
buffer[index++] = a;
}
void Batch3D::vertex(glm::vec3 coord, float u, float v,
float r, float g, float b, float a) {
void Batch3D::vertex(
glm::vec3 coord, float u, float v,
float r, float g, float b, float a
) {
buffer[index++] = coord.x;
buffer[index++] = coord.y;
buffer[index++] = coord.z;
Expand All @@ -58,9 +61,11 @@ void Batch3D::vertex(glm::vec3 coord, float u, float v,
buffer[index++] = b;
buffer[index++] = a;
}
void Batch3D::vertex(glm::vec3 point,
glm::vec2 uvpoint,
float r, float g, float b, float a) {
void Batch3D::vertex(
glm::vec3 point,
glm::vec2 uvpoint,
float r, float g, float b, float a
) {
buffer[index++] = point.x;
buffer[index++] = point.y;
buffer[index++] = point.z;
Expand Down Expand Up @@ -99,10 +104,10 @@ void Batch3D::face(
}

void Batch3D::texture(Texture* new_texture){
if (_texture == new_texture)
if (currentTexture == new_texture)
return;
flush();
_texture = new_texture;
currentTexture = new_texture;
if (new_texture == nullptr)
blank->bind();
else
Expand Down Expand Up @@ -166,7 +171,9 @@ inline glm::vec4 do_tint(float value) {
return glm::vec4(value, value, value, 1.0f);
}

void Batch3D::xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading) {
void Batch3D::xSprite(
float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading
) {
face(
glm::vec3(-w * 0.25f, 0.0f, -w * 0.25f),
w, h,
Expand Down Expand Up @@ -244,13 +251,13 @@ void Batch3D::point(glm::vec3 coord, glm::vec4 tint) {
}

void Batch3D::flush() {
mesh->reload(buffer, index / B3D_VERTEX_SIZE);
mesh->reload(buffer.get(), index / B3D_VERTEX_SIZE);
mesh->draw();
index = 0;
}

void Batch3D::flushPoints() {
mesh->reload(buffer, index / B3D_VERTEX_SIZE);
mesh->reload(buffer.get(), index / B3D_VERTEX_SIZE);
mesh->draw(GL_POINTS);
index = 0;
}
35 changes: 21 additions & 14 deletions src/graphics/core/Batch3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@ class Mesh;
class Texture;

class Batch3D {
float* buffer;
std::unique_ptr<float[]> buffer;
size_t capacity;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank;
size_t index;

Texture* _texture;

void vertex(float x, float y, float z,
float u, float v,
float r, float g, float b, float a);
void vertex(glm::vec3 coord,
float u, float v,
float r, float g, float b, float a);
void vertex(glm::vec3 point, glm::vec2 uvpoint,
float r, float g, float b, float a);

void face(const glm::vec3& coord, float w, float h,
Texture* currentTexture;

void vertex(
float x, float y, float z,
float u, float v,
float r, float g, float b, float a
);
void vertex(
glm::vec3 coord,
float u, float v,
float r, float g, float b, float a
);
void vertex(
glm::vec3 point, glm::vec2 uvpoint,
float r, float g, float b, float a
);
void face(
const glm::vec3& coord, float w, float h,
const glm::vec3& axisX,
const glm::vec3& axisY,
const UVRegion& region,
const glm::vec4& tint);
const glm::vec4& tint
);

public:
Batch3D(size_t capacity);
Expand Down

0 comments on commit 7db76a0

Please sign in to comment.