add modeltree:set_texture

This commit is contained in:
MihailRis
2024-07-03 03:33:39 +03:00
parent 8add39e506
commit 39d850a48c
7 changed files with 68 additions and 14 deletions
+37 -6
View File
@@ -2,6 +2,7 @@
#include "../core/Mesh.hpp"
#include "../core/Model.hpp"
#include "../core/Atlas.hpp"
#include "../core/Texture.hpp"
#include "../../assets/Assets.hpp"
#include "../../window/Window.hpp"
@@ -53,7 +54,9 @@ ModelBatch::ModelBatch(size_t capacity, Assets* assets, Chunks* chunks)
ModelBatch::~ModelBatch() {
}
void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const glm::mat3& rotation) {
void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix,
const glm::mat3& rotation,
const texture_names_map* varTextures) {
glm::vec3 gpos = matrix * glm::vec4(glm::vec3(), 1.0f);
light_t light = chunks->getLight(floor(gpos.x), floor(gpos.y), floor(gpos.z));
glm::vec4 lights (
@@ -62,7 +65,7 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const gl
Lightmap::extract(light, 2) / 15.0f,
Lightmap::extract(light, 3) / 15.0f
);
setTexture(assets->get<Texture>(mesh.texture));
setTexture(mesh.texture, varTextures);
size_t vcount = mesh.vertices.size();
const auto& vertexData = mesh.vertices.data();
for (size_t i = 0; i < vcount / 3; i++) {
@@ -70,7 +73,7 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const gl
flush();
}
for (size_t j = 0; j < 3; j++) {
const auto& vert = vertexData[i * 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;
@@ -81,9 +84,10 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const gl
}
}
void ModelBatch::draw(const model::Model* model) {
void ModelBatch::draw(const model::Model* model,
const texture_names_map* varTextures) {
for (const auto& mesh : model->meshes) {
entries.push_back({combined, rotation, &mesh});
entries.push_back({combined, rotation, &mesh, varTextures});
}
}
@@ -94,7 +98,7 @@ void ModelBatch::render() {
}
);
for (auto& entry : entries) {
draw(*entry.mesh, entry.matrix, entry.rotation);
draw(*entry.mesh, entry.matrix, entry.rotation, entry.varTextures);
}
flush();
entries.clear();
@@ -114,6 +118,32 @@ 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(const std::string& name,
const texture_names_map* varTextures) {
if (name.at(0) == '$') {
const auto& found = varTextures->find(name);
if (found == varTextures->end()) {
return setTexture(nullptr);
} else {
return setTexture(found->second, varTextures);
}
}
size_t sep = name.find(':');
if (sep == std::string::npos) {
setTexture(assets->get<Texture>(name));
} else {
auto atlas = assets->get<Atlas>(name.substr(0, sep));
if (atlas == nullptr) {
setTexture(nullptr);
} else {
setTexture(atlas->getTexture());
if (auto reg = atlas->getIf(name.substr(sep+1))) {
region = *reg;
}
}
}
}
void ModelBatch::setTexture(Texture* texture) {
if (texture == nullptr) {
texture = blank.get();
@@ -122,6 +152,7 @@ void ModelBatch::setTexture(Texture* texture) {
flush();
}
this->texture = texture;
region = UVRegion {0.0f, 0.0f, 1.0f, 1.0f};
}
void ModelBatch::flush() {