add MainBatch & refactor ModelBatch, Batch3D

This commit is contained in:
MihailRis
2024-11-02 19:37:39 +03:00
parent 316de42cca
commit bbd13f1825
6 changed files with 159 additions and 110 deletions
+8 -50
View File
@@ -10,6 +10,7 @@
#include "voxels/Chunks.hpp"
#include "lighting/Lightmap.hpp"
#include "settings.hpp"
#include "MainBatch.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
@@ -18,13 +19,6 @@
#include <algorithm>
/// xyz, uv, color, compressed lights
inline constexpr uint VERTEX_SIZE = 9;
static const vattr attrs[] = {
{3}, {2}, {3}, {1}, {0}
};
inline constexpr glm::vec3 X(1, 0, 0);
inline constexpr glm::vec3 Y(0, 1, 0);
inline constexpr glm::vec3 Z(0, 0, 1);
@@ -57,18 +51,10 @@ ModelBatch::ModelBatch(
Chunks* chunks,
const EngineSettings* settings
)
: buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
capacity(capacity),
index(0),
mesh(std::make_unique<Mesh>(buffer.get(), 0, attrs)),
: batch(std::make_unique<MainBatch>(capacity)),
assets(assets),
chunks(chunks),
settings(settings) {
const ubyte pixels[] = {
255, 255, 255, 255,
};
ImageData image(ImageFormat::rgba8888, 1, 1, pixels);
blank = Texture::from(&image);
}
ModelBatch::~ModelBatch() = default;
@@ -94,15 +80,13 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix,
size_t vcount = mesh.vertices.size();
const auto& vertexData = mesh.vertices.data();
for (size_t i = 0; i < vcount / 3; i++) {
if (index + VERTEX_SIZE * 3 > capacity * VERTEX_SIZE) {
flush();
}
batch->prepare(3);
for (size_t j = 0; j < 3; j++) {
const auto vert = vertexData[i * 3 + j];
auto norm = rotation * vert.normal;
float d = glm::dot(norm, SUN_VECTOR);
d = 0.8f + d * 0.2f;
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint);
batch->vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint);
}
}
}
@@ -135,7 +119,7 @@ void ModelBatch::render() {
backlight
);
}
flush();
batch->flush();
entries.clear();
}
@@ -148,37 +132,11 @@ void ModelBatch::setTexture(const std::string& name,
if (varTextures && name.at(0) == '$') {
const auto& found = varTextures->find(name);
if (found == varTextures->end()) {
return setTexture(nullptr);
return batch->setTexture(nullptr);
} else {
return setTexture(found->second, varTextures);
}
}
auto textureRegion = util::get_texture_region(*assets, name, "blocks:notfound");
setTexture(textureRegion.texture);
region = textureRegion.region;
}
void ModelBatch::setTexture(const Texture* texture) {
if (texture == nullptr) {
texture = blank.get();
}
if (texture != this->texture) {
flush();
}
this->texture = texture;
region = UVRegion {0.0f, 0.0f, 1.0f, 1.0f};
}
void ModelBatch::flush() {
if (index == 0) {
return;
}
if (texture == nullptr) {
texture = blank.get();
}
texture->bind();
mesh->reload(buffer.get(), index / VERTEX_SIZE);
mesh->draw();
index = 0;
auto region = util::get_texture_region(*assets, name, "blocks:notfound");
batch->setTexture(region.texture, region.region);
}