optimize ModelBatch

This commit is contained in:
MihailRis 2024-07-20 17:59:15 +03:00
parent 4197746c69
commit fba0bca0dc
3 changed files with 21 additions and 84 deletions

View File

@ -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<float[]>(capacity * VERTEX_SIZE)),
capacity(capacity),
index(0),
mesh(std::make_unique<Mesh>(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);
}

View File

@ -29,10 +29,6 @@ class ModelBatch {
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank;
glm::mat4 combined;
std::vector<glm::mat4> 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();

View File

@ -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);
}
}
}