update ModelBatch semantics

This commit is contained in:
MihailRis
2024-06-23 01:57:28 +03:00
parent ea0add3017
commit 94fe5eeb5b
7 changed files with 97 additions and 35 deletions
+49 -23
View File
@@ -50,8 +50,8 @@ ModelBatch::ModelBatch(size_t capacity, Assets* assets, Chunks* chunks)
ModelBatch::~ModelBatch() {
}
void ModelBatch::draw(const model::Model* model) {
glm::vec3 gpos = combined * glm::vec4(glm::vec3(), 1.0f);
void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const glm::mat3& rotation) {
glm::vec3 gpos = matrix * glm::vec4(glm::vec3(), 1.0f);
light_t light = chunks->getLight(gpos.x, gpos.y, gpos.z);
glm::vec4 lights (
Lightmap::extract(light, 0) / 15.0f,
@@ -59,31 +59,44 @@ void ModelBatch::draw(const model::Model* model) {
Lightmap::extract(light, 2) / 15.0f,
Lightmap::extract(light, 3) / 15.0f
);
for (const auto& mesh : model->meshes) {
auto texture = assets->get<Texture>(mesh.texture);
if (texture) {
texture->bind();
} else {
blank->bind();
setTexture(assets->get<Texture>(mesh.texture));
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();
}
for (size_t i = 0; i < mesh.vertices.size() / 3; i++) {
if (index + VERTEX_SIZE * 3 > capacity) {
flush();
}
for (size_t j = 0; j < 3; j++) {
const auto& vert = mesh.vertices[i * 3 + j];
auto norm = rotation * vert.normal;
float d = glm::dot(norm, SUN_VECTOR);
d = 0.8f + d * 0.2f;
auto color = lights * d;
vertex(vert.coord, vert.uv, color);
}
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;
auto color = lights * d;
vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, color);
}
flush();
}
}
void ModelBatch::draw(const model::Model* model) {
for (const auto& mesh : model->meshes) {
entries.push_back({combined, rotation, &mesh});
}
}
void ModelBatch::render() {
std::sort(entries.begin(), entries.end(),
[](const DrawEntry& a, const DrawEntry& b) {
return a.mesh->texture < b.mesh->texture;
}
);
for (auto& entry : entries) {
draw(*entry.mesh, entry.matrix, entry.rotation);
}
flush();
entries.clear();
}
void ModelBatch::box(glm::vec3 pos, glm::vec3 size, glm::vec4 lights) {
if (index + 36 < capacity*VERTEX_SIZE) {
flush();
@@ -98,11 +111,24 @@ void ModelBatch::box(glm::vec3 pos, glm::vec3 size, glm::vec4 lights) {
plane(pos-X*size, Z*size, Y*size, -X, lights);
}
void ModelBatch::setTexture(Texture* texture) {
if (texture == nullptr) {
texture = blank.get();
}
if (texture != this->texture) {
flush();
}
this->texture = texture;
}
void ModelBatch::flush() {
if (index == 0) {
return;
}
// blank->bind();
if (texture == nullptr) {
texture = blank.get();
}
texture->bind();
mesh->reload(buffer.get(), index / VERTEX_SIZE);
mesh->draw();
index = 0;