From fba0bca0dc02f8c06b9693cc41fbd8306116b39b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 20 Jul 2024 17:59:15 +0300 Subject: [PATCH] optimize ModelBatch --- src/graphics/render/ModelBatch.cpp | 72 +++++++----------------------- src/graphics/render/ModelBatch.hpp | 28 +----------- src/objects/rigging.cpp | 5 +-- 3 files changed, 21 insertions(+), 84 deletions(-) diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 10e63d48..4c5509b4 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -35,12 +35,25 @@ struct DecomposedMat4 { glm::vec4 perspective; }; +static glm::mat4 extract_rotation(glm::mat4 matrix) { + DecomposedMat4 decomposed = {}; + glm::quat rotation; + glm::decompose( + matrix, + decomposed.scale, + rotation, + decomposed.translation, + decomposed.skew, + decomposed.perspective + ); + return glm::toMat3(rotation); +} + ModelBatch::ModelBatch(size_t capacity, Assets* assets, Chunks* chunks) : buffer(std::make_unique(capacity * VERTEX_SIZE)), capacity(capacity), index(0), mesh(std::make_unique(buffer.get(), 0, attrs)), - combined(1.0f), assets(assets), chunks(chunks) { @@ -84,10 +97,11 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, } } -void ModelBatch::draw(const model::Model* model, +void ModelBatch::draw(glm::mat4 matrix, + const model::Model* model, const texture_names_map* varTextures) { for (const auto& mesh : model->meshes) { - entries.push_back({combined, rotation, &mesh, varTextures}); + entries.push_back({matrix, extract_rotation(matrix), &mesh, varTextures}); } } @@ -104,20 +118,6 @@ void ModelBatch::render() { entries.clear(); } -void ModelBatch::box(glm::vec3 pos, glm::vec3 size, glm::vec4 lights) { - if (index + 36 < capacity*VERTEX_SIZE) { - flush(); - } - plane(pos+Z*size, X*size, Y*size, Z, lights); - plane(pos-Z*size, -X*size, Y*size, -Z, lights); - - plane(pos+Y*size, X*size, -Z*size, Y, lights); - plane(pos-Y*size, X*size, Z*size, -Y, lights); - - plane(pos+X*size, -Z*size, Y*size, X, lights); - plane(pos-X*size, Z*size, Y*size, -X, lights); -} - void ModelBatch::setTexture(const std::string& name, const texture_names_map* varTextures) { if (name.at(0) == '$') { @@ -169,41 +169,3 @@ void ModelBatch::flush() { mesh->draw(); index = 0; } - -static glm::mat4 extract_rotation(glm::mat4 matrix) { - DecomposedMat4 decomposed = {}; - glm::quat rotation; - glm::decompose( - matrix, - decomposed.scale, - rotation, - decomposed.translation, - decomposed.skew, - decomposed.perspective - ); - return glm::toMat3(rotation); -} - -void ModelBatch::translate(glm::vec3 vec) { - pushMatrix(glm::translate(glm::mat4(1.0f), vec)); -} - -void ModelBatch::rotate(glm::vec3 axis, float angle) { - pushMatrix(glm::rotate(glm::mat4(1.0f), angle, axis)); -} - -void ModelBatch::scale(glm::vec3 vec) { - pushMatrix(glm::scale(glm::mat4(1.0f), vec)); -} - -void ModelBatch::pushMatrix(glm::mat4 matrix) { - matrices.push_back(combined); - combined = combined * matrix; - rotation = extract_rotation(combined); -} - -void ModelBatch::popMatrix() { - combined = matrices[matrices.size()-1]; - matrices.erase(matrices.end()-1); - rotation = extract_rotation(combined); -} diff --git a/src/graphics/render/ModelBatch.hpp b/src/graphics/render/ModelBatch.hpp index 576ed878..cd6b4f43 100644 --- a/src/graphics/render/ModelBatch.hpp +++ b/src/graphics/render/ModelBatch.hpp @@ -29,10 +29,6 @@ class ModelBatch { std::unique_ptr mesh; std::unique_ptr blank; - glm::mat4 combined; - std::vector matrices; - glm::mat3 rotation; - Assets* assets; Chunks* chunks; Texture* texture = nullptr; @@ -63,25 +59,8 @@ class ModelBatch { buffer[index++] = compressed.floating; } - inline void plane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm, glm::vec4 lights) { - norm = rotation * norm; - float d = glm::dot(norm, SUN_VECTOR); - d = 0.8f + d * 0.2f; - - auto color = lights * d; - - vertex(pos-right-up, {0,0}, color); - vertex(pos+right-up, {1,0}, color); - vertex(pos+right+up, {1,1}, color); - - vertex(pos-right-up, {0,0}, color); - vertex(pos+right+up, {1,1}, color); - vertex(pos-right+up, {0,1}, color); - } - void draw(const model::Mesh& mesh, const glm::mat4& matrix, const glm::mat3& rotation, const texture_names_map* varTextures); - void box(glm::vec3 pos, glm::vec3 size, glm::vec4 lights); void setTexture(const std::string& name, const texture_names_map* varTextures); void setTexture(Texture* texture); @@ -98,13 +77,10 @@ public: ModelBatch(size_t capacity, Assets* assets, Chunks* chunks); ~ModelBatch(); - void translate(glm::vec3 vec); - void rotate(glm::vec3 axis, float angle); - void scale(glm::vec3 vec); - void pushMatrix(glm::mat4 matrix); void popMatrix(); - void draw(const model::Model* model, + void draw(glm::mat4 matrix, + const model::Model* model, const texture_names_map* varTextures); void render(); diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index c4fed1e1..afb3583b 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -99,9 +99,8 @@ void SkeletonConfig::render( } model = modelOverride.model ? modelOverride.model : model; if (model) { - batch.pushMatrix(skeleton.calculated.matrices[i]); - batch.draw(model, &skeleton.textures); - batch.popMatrix(); + batch.draw( + skeleton.calculated.matrices[i], model, &skeleton.textures); } } }