Merge branch 'main' into volume-renderer

This commit is contained in:
MihailRis 2026-01-18 22:53:07 +03:00
commit b2df0a7e91
28 changed files with 642 additions and 184 deletions

View file

@ -1,6 +1,10 @@
#define GLM_ENABLE_EXPERIMENTAL
#include "Model.hpp"
#include <algorithm>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/matrix_decompose.hpp>
using namespace model;
@ -71,6 +75,39 @@ void Mesh::addRect(
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
}
static glm::mat4 extract_rotation(const glm::mat4& matrix) {
glm::vec3 scale;
glm::quat rotation;
glm::vec3 translation;
glm::vec3 skew;
glm::vec4 perspective;
glm::decompose(matrix, scale, rotation, translation, skew, perspective);
return glm::mat4_cast(rotation);
}
void Mesh::addRect(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm,
const UVRegion& uv,
const glm::mat4& transform
) {
auto a = transform * glm::vec4(pos - right - up, 1.0f);
auto b = transform * glm::vec4(pos + right - up, 1.0f);
auto c = transform * glm::vec4(pos + right + up, 1.0f);
auto d = transform * glm::vec4(pos - right + up, 1.0f);
auto tsfnorm = extract_rotation(transform) * glm::vec4(norm, 1.0f);
vertices.push_back({a, {uv.u1, uv.v1}, tsfnorm});
vertices.push_back({b, {uv.u2, uv.v1}, tsfnorm});
vertices.push_back({c, {uv.u2, uv.v2}, tsfnorm});
vertices.push_back({a, {uv.u1, uv.v1}, tsfnorm});
vertices.push_back({c, {uv.u2, uv.v2}, tsfnorm});
vertices.push_back({d, {uv.u1, uv.v2}, tsfnorm});
}
void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
UVRegion fullRegion (0, 0, 1, 1);
addPlane(pos+Z*size, X*size, Y*size, Z, fullRegion);
@ -119,6 +156,18 @@ void Mesh::scale(const glm::vec3& size) {
}
}
void Mesh::transform(const glm::mat4& matrix) {
for (auto& vertex : vertices) {
vertex.coord = matrix * glm::vec4(vertex.coord, 1.0f);
}
}
void Mesh::translate(const glm::vec3& offset) {
for (auto& vertex : vertices) {
vertex.coord += offset;
}
}
void Model::clean() {
meshes.erase(
std::remove_if(meshes.begin(), meshes.end(),
@ -128,3 +177,41 @@ void Model::clean() {
meshes.end()
);
}
void Model::transform(const glm::mat4& matrix) {
for (auto& mesh : meshes) {
mesh.transform(matrix);
}
}
void Model::translate(const glm::vec3& offset) {
for (auto& mesh : meshes) {
mesh.translate(offset);
}
}
void Model::merge(const Model& source) {
for (const auto& srcMesh : source.meshes) {
auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading);
dstMesh.vertices.insert(
dstMesh.vertices.end(),
srcMesh.vertices.begin(),
srcMesh.vertices.end()
);
}
}
void Model::merge(Model&& source) {
for (auto& srcMesh : source.meshes) {
auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading);
if (dstMesh.vertices.empty()) {
dstMesh = std::move(srcMesh);
} else {
dstMesh.vertices.insert(
dstMesh.vertices.end(),
srcMesh.vertices.begin(),
srcMesh.vertices.end()
);
}
}
}

View file

@ -40,6 +40,14 @@ namespace model {
const glm::vec3& norm,
const UVRegion& region
);
void addRect(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm,
const UVRegion& uv,
const glm::mat4& transform
);
void addTriangle(
const glm::vec3& a,
const glm::vec3& b,
@ -64,6 +72,12 @@ namespace model {
const glm::mat4& transform
);
void scale(const glm::vec3& size);
/// @brief Apply transformation matrix to vertices coordinates
void transform(const glm::mat4& matrix);
/// @brief Move vertices coordinates by offset
void translate(const glm::vec3& offset);
};
struct Model {
@ -81,7 +95,22 @@ namespace model {
meshes.push_back({texture, {}, shading});
return meshes[meshes.size() - 1];
}
/// @brief Remove all empty meshes
void clean();
/// @brief Apply transformation matrix to vertices coordinates
void transform(const glm::mat4& matrix);
/// @brief Move vertices coordinates by offset
void translate(const glm::vec3& offset);
/// @brief Copy or merge meshes from source model to this.
/// Does not remove duplicates
void merge(const Model& source);
/// @brief Move or merge meshes from source model to this.
/// Does not remove duplicates
void merge(Model&& source);
};
}

View file

@ -23,6 +23,7 @@
#include "maths/voxmaths.hpp"
#include "objects/Entities.hpp"
#include "objects/Player.hpp"
#include "objects/rigging.hpp"
#include "settings.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
@ -110,7 +111,7 @@ WorldRenderer::WorldRenderer(
const auto& content = level.content;
skeletons = std::make_unique<NamedSkeletons>();
const auto& skeletonConfig = content.requireSkeleton(
const auto& skeletonConfig = assets->require<rigging::SkeletonConfig>(
content.getDefaults()["hand-skeleton"].asString()
);
hands = std::make_unique<HandsRenderer>(