1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Add static glTF support (#14557)

Co-authored-by: Lars Mueller <appgurulars@gmx.de>
Co-authored-by: jordan4ibanez <jordan4ibanez@users.noreply.github.com>
Co-authored-by: sfan5 <sfan5@live.de>
Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
This commit is contained in:
JosiahWI 2024-09-02 07:50:30 -05:00 committed by GitHub
parent 8972c80d7d
commit ac11a14509
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 2863 additions and 28 deletions

View file

@ -826,7 +826,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename,
}
const char *model_ext[] = {
".x", ".b3d", ".obj",
".x", ".b3d", ".obj", ".gltf",
NULL
};
name = removeStringEnd(filename, model_ext);

View file

@ -844,14 +844,19 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
if (m_animated_meshnode) {
u32 mat_count = m_animated_meshnode->getMaterialCount();
assert(mat_count == m_animated_meshnode->getMesh()->getMeshBufferCount());
u32 max_tex_idx = 0;
for (u32 i = 0; i < mat_count; ++i) {
max_tex_idx = std::max(max_tex_idx,
m_animated_meshnode->getMesh()->getTextureSlot(i));
}
if (mat_count == 0 || m_prop.textures.empty()) {
// nothing
} else if (mat_count > m_prop.textures.size()) {
} else if (max_tex_idx >= m_prop.textures.size()) {
std::ostringstream oss;
oss << "GenericCAO::addToScene(): Model "
<< m_prop.mesh << " loaded with " << mat_count
<< " mesh buffers but only " << m_prop.textures.size()
<< " texture(s) specified, this is deprecated.";
<< m_prop.mesh << " is missing " << (max_tex_idx + 1 - m_prop.textures.size())
<< " more texture(s), this is deprecated.";
logOnce(oss, warningstream);
video::ITexture *last = m_animated_meshnode->getMaterial(0).TextureLayers[0].Texture;
@ -1370,9 +1375,11 @@ void GenericCAO::updateTextures(std::string mod)
else if (m_animated_meshnode) {
if (m_prop.visual == "mesh") {
for (u32 i = 0; i < m_prop.textures.size() &&
i < m_animated_meshnode->getMaterialCount(); ++i) {
std::string texturestring = m_prop.textures[i];
for (u32 i = 0; i < m_animated_meshnode->getMaterialCount(); ++i) {
const auto texture_idx = m_animated_meshnode->getMesh()->getTextureSlot(i);
if (texture_idx >= m_prop.textures.size())
continue;
std::string texturestring = m_prop.textures[texture_idx];
if (texturestring.empty())
continue; // Empty texture string means don't modify that material
texturestring += mod;

View file

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cmath>
#include "content_mapblock.h"
#include "util/basic_macros.h"
#include "util/numeric.h"
#include "util/directiontables.h"
#include "mapblock_mesh.h"
@ -1676,7 +1677,9 @@ void MapblockMeshGenerator::drawMeshNode()
int mesh_buffer_count = mesh->getMeshBufferCount();
for (int j = 0; j < mesh_buffer_count; j++) {
useTile(j);
// Only up to 6 tiles are supported
const auto tile = mesh->getTextureSlot(j);
useTile(MYMIN(tile, 5));
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();
int vertex_count = buf->getVertexCount();

View file

@ -397,8 +397,8 @@ scene::SMesh* cloneMesh(scene::IMesh *src_mesh)
scene::IMeshBuffer *temp_buf = cloneMeshBuffer(
src_mesh->getMeshBuffer(j));
dst_mesh->addMeshBuffer(temp_buf);
dst_mesh->setTextureSlot(j, src_mesh->getTextureSlot(j));
temp_buf->drop();
}
return dst_mesh;
}

View file

@ -2807,8 +2807,13 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
auto meshnode = e->setMesh(mesh);
for (u32 i = 0; i < textures.size() && i < meshnode->getMaterialCount(); ++i)
e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[i])));
for (u32 i = 0; i < meshnode->getMaterialCount(); ++i) {
const auto texture_idx = mesh->getTextureSlot(i);
if (texture_idx >= textures.size())
warningstream << "Invalid model element: Not enough textures" << std::endl;
else
e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[texture_idx])));
}
if (vec_rot.size() >= 2)
e->setRotation(v2f(stof(vec_rot[0]), stof(vec_rot[1])));

View file

@ -2465,7 +2465,7 @@ bool Server::addMediaFile(const std::string &filename,
const char *supported_ext[] = {
".png", ".jpg", ".bmp", ".tga",
".ogg",
".x", ".b3d", ".obj",
".x", ".b3d", ".obj", ".gltf",
// Custom translation file format
".tr",
NULL

View file

@ -49,7 +49,7 @@ set (UNITTEST_CLIENT_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_content_mapblock.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_eventmanager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_gameui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_irr_gltf_mesh_loader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mesh_compare.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
PARENT_SCOPE)

View file

@ -0,0 +1,366 @@
// Minetest
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "CSceneManager.h"
#include "content/subgames.h"
#include "filesys.h"
#include "CReadFile.h"
#include "irr_v3d.h"
#include "irr_v2d.h"
#include <irrlicht.h>
#include "catch.h"
TEST_CASE("gltf") {
const auto gamespec = findSubgame("devtest");
if (!gamespec.isValid())
SKIP();
irr::scene::CSceneManager smgr(nullptr, nullptr, nullptr);
const auto loadMesh = [&smgr](const irr::io::path& filepath) {
irr::io::CReadFile file(filepath);
return smgr.getMesh(&file);
};
const static auto model_stem = gamespec.gamemods_path +
DIR_DELIM + "gltf" + DIR_DELIM + "models" + DIR_DELIM + "gltf_";
SECTION("error cases") {
const static auto invalid_model_path = gamespec.gamemods_path + DIR_DELIM + "gltf" + DIR_DELIM + "invalid" + DIR_DELIM;
SECTION("empty gltf file") {
CHECK(loadMesh(invalid_model_path + "empty.gltf") == nullptr);
}
SECTION("null file pointer") {
CHECK(smgr.getMesh(nullptr) == nullptr);
}
SECTION("invalid JSON") {
CHECK(loadMesh(invalid_model_path + "json_missing_brace.gltf") == nullptr);
}
// This is an example of something that should be validated by tiniergltf.
SECTION("invalid bufferview bounds")
{
CHECK(loadMesh(invalid_model_path + "invalid_bufferview_bounds.gltf") == nullptr);
}
}
SECTION("minimal triangle") {
const auto path = GENERATE(
model_stem + "minimal_triangle.gltf",
model_stem + "triangle_with_vertex_stride.gltf",
// Test non-indexed geometry.
model_stem + "triangle_without_indices.gltf");
INFO(path);
const auto mesh = loadMesh(path);
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
SECTION("vertex coordinates are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 3);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Pos == v3f {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == v3f {1.0f, 0.0f, 0.0f});
CHECK(vertices[2].Pos == v3f {0.0f, 1.0f, 0.0f});
}
SECTION("vertex indices are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getIndexCount() == 3);
auto indices = static_cast<const irr::u16 *>(
mesh->getMeshBuffer(0)->getIndices());
CHECK(indices[0] == 2);
CHECK(indices[1] == 1);
CHECK(indices[2] == 0);
}
}
SECTION("blender cube") {
const auto mesh = loadMesh(model_stem + "blender_cube.gltf");
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
SECTION("vertex coordinates are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Pos == v3f{-10.0f, -10.0f, -10.0f});
CHECK(vertices[3].Pos == v3f{-10.0f, 10.0f, -10.0f});
CHECK(vertices[6].Pos == v3f{-10.0f, -10.0f, 10.0f});
CHECK(vertices[9].Pos == v3f{-10.0f, 10.0f, 10.0f});
CHECK(vertices[12].Pos == v3f{10.0f, -10.0f, -10.0f});
CHECK(vertices[15].Pos == v3f{10.0f, 10.0f, -10.0f});
CHECK(vertices[18].Pos == v3f{10.0f, -10.0f, 10.0f});
CHECK(vertices[21].Pos == v3f{10.0f, 10.0f, 10.0f});
}
SECTION("vertex indices are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getIndexCount() == 36);
auto indices = static_cast<const irr::u16 *>(
mesh->getMeshBuffer(0)->getIndices());
CHECK(indices[0] == 16);
CHECK(indices[1] == 5);
CHECK(indices[2] == 22);
CHECK(indices[35] == 0);
}
SECTION("vertex normals are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Normal == v3f{-1.0f, 0.0f, 0.0f});
CHECK(vertices[1].Normal == v3f{0.0f, -1.0f, 0.0f});
CHECK(vertices[2].Normal == v3f{0.0f, 0.0f, -1.0f});
CHECK(vertices[3].Normal == v3f{-1.0f, 0.0f, 0.0f});
CHECK(vertices[6].Normal == v3f{-1.0f, 0.0f, 0.0f});
CHECK(vertices[23].Normal == v3f{1.0f, 0.0f, 0.0f});
}
SECTION("texture coords are correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].TCoords == v2f{0.375f, 1.0f});
CHECK(vertices[1].TCoords == v2f{0.125f, 0.25f});
CHECK(vertices[2].TCoords == v2f{0.375f, 0.0f});
CHECK(vertices[3].TCoords == v2f{0.6250f, 1.0f});
CHECK(vertices[6].TCoords == v2f{0.375f, 0.75f});
}
}
SECTION("blender cube scaled") {
const auto mesh = loadMesh(model_stem + "blender_cube_scaled.gltf");
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
SECTION("Scaling is correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Pos == v3f{-150.0f, -1.0f, -21.5f});
CHECK(vertices[3].Pos == v3f{-150.0f, 1.0f, -21.5f});
CHECK(vertices[6].Pos == v3f{-150.0f, -1.0f, 21.5f});
CHECK(vertices[9].Pos == v3f{-150.0f, 1.0f, 21.5f});
CHECK(vertices[12].Pos == v3f{150.0f, -1.0f, -21.5f});
CHECK(vertices[15].Pos == v3f{150.0f, 1.0f, -21.5f});
CHECK(vertices[18].Pos == v3f{150.0f, -1.0f, 21.5f});
CHECK(vertices[21].Pos == v3f{150.0f, 1.0f, 21.5f});
}
}
SECTION("blender cube matrix transform") {
const auto mesh = loadMesh(model_stem + "blender_cube_matrix_transform.gltf");
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
SECTION("Transformation is correct") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
const auto checkVertex = [&](const std::size_t i, v3f vec) {
// The transform scales by (1, 2, 3) and translates by (4, 5, 6).
CHECK(vertices[i].Pos == vec * v3f{1, 2, 3}
// The -6 is due to the coordinate system conversion.
+ v3f{4, 5, -6});
};
checkVertex(0, v3f{-1, -1, -1});
checkVertex(3, v3f{-1, 1, -1});
checkVertex(6, v3f{-1, -1, 1});
checkVertex(9, v3f{-1, 1, 1});
checkVertex(12, v3f{1, -1, -1});
checkVertex(15, v3f{1, 1, -1});
checkVertex(18, v3f{1, -1, 1});
checkVertex(21, v3f{1, 1, 1});
}
}
SECTION("snow man") {
const auto mesh = loadMesh(model_stem + "snow_man.gltf");
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 3);
SECTION("vertex coordinates are correct for all buffers") {
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
{
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Pos == v3f{3.0f, 24.0f, -3.0f});
CHECK(vertices[3].Pos == v3f{3.0f, 18.0f, 3.0f});
CHECK(vertices[6].Pos == v3f{-3.0f, 18.0f, -3.0f});
CHECK(vertices[9].Pos == v3f{3.0f, 24.0f, 3.0f});
CHECK(vertices[12].Pos == v3f{3.0f, 18.0f, -3.0f});
CHECK(vertices[15].Pos == v3f{-3.0f, 18.0f, 3.0f});
CHECK(vertices[18].Pos == v3f{3.0f, 18.0f, -3.0f});
CHECK(vertices[21].Pos == v3f{3.0f, 18.0f, 3.0f});
}
{
REQUIRE(mesh->getMeshBuffer(1)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(1)->getVertices());
CHECK(vertices[2].Pos == v3f{5.0f, 10.0f, 5.0f});
CHECK(vertices[3].Pos == v3f{5.0f, 0.0f, 5.0f});
CHECK(vertices[7].Pos == v3f{-5.0f, 0.0f, 5.0f});
CHECK(vertices[8].Pos == v3f{5.0f, 10.0f, -5.0f});
CHECK(vertices[14].Pos == v3f{5.0f, 0.0f, 5.0f});
CHECK(vertices[16].Pos == v3f{5.0f, 10.0f, -5.0f});
CHECK(vertices[22].Pos == v3f{-5.0f, 10.0f, 5.0f});
CHECK(vertices[23].Pos == v3f{-5.0f, 0.0f, 5.0f});
}
{
REQUIRE(mesh->getMeshBuffer(2)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(2)->getVertices());
CHECK(vertices[1].Pos == v3f{4.0f, 10.0f, -4.0f});
CHECK(vertices[2].Pos == v3f{4.0f, 18.0f, 4.0f});
CHECK(vertices[3].Pos == v3f{4.0f, 10.0f, 4.0f});
CHECK(vertices[10].Pos == v3f{-4.0f, 18.0f, -4.0f});
CHECK(vertices[11].Pos == v3f{-4.0f, 18.0f, 4.0f});
CHECK(vertices[12].Pos == v3f{4.0f, 10.0f, -4.0f});
CHECK(vertices[17].Pos == v3f{-4.0f, 18.0f, -4.0f});
CHECK(vertices[18].Pos == v3f{4.0f, 10.0f, -4.0f});
}
}
SECTION("vertex indices are correct for all buffers") {
{
REQUIRE(mesh->getMeshBuffer(0)->getIndexCount() == 36);
auto indices = static_cast<const irr::u16 *>(
mesh->getMeshBuffer(0)->getIndices());
CHECK(indices[0] == 23);
CHECK(indices[1] == 21);
CHECK(indices[2] == 22);
CHECK(indices[35] == 2);
}
{
REQUIRE(mesh->getMeshBuffer(1)->getIndexCount() == 36);
auto indices = static_cast<const irr::u16 *>(
mesh->getMeshBuffer(1)->getIndices());
CHECK(indices[10] == 16);
CHECK(indices[11] == 18);
CHECK(indices[15] == 13);
CHECK(indices[27] == 5);
}
{
REQUIRE(mesh->getMeshBuffer(2)->getIndexCount() == 36);
auto indices = static_cast<const irr::u16 *>(
mesh->getMeshBuffer(2)->getIndices());
CHECK(indices[26] == 6);
CHECK(indices[27] == 5);
CHECK(indices[29] == 6);
CHECK(indices[32] == 2);
}
}
SECTION("vertex normals are correct for all buffers") {
{
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[1].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[2].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[3].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[6].Normal == v3f{-1.0f, 0.0f, -0.0f});
CHECK(vertices[23].Normal == v3f{0.0f, 0.0f, 1.0f});
}
{
REQUIRE(mesh->getMeshBuffer(1)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(1)->getVertices());
CHECK(vertices[0].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[1].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[3].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[6].Normal == v3f{-1.0f, 0.0f, -0.0f});
CHECK(vertices[7].Normal == v3f{-1.0f, 0.0f, -0.0f});
CHECK(vertices[22].Normal == v3f{0.0f, 0.0f, 1.0f});
}
{
REQUIRE(mesh->getMeshBuffer(2)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(2)->getVertices());
CHECK(vertices[3].Normal == v3f{1.0f, 0.0f, -0.0f});
CHECK(vertices[4].Normal == v3f{-1.0f, 0.0f, -0.0f});
CHECK(vertices[5].Normal == v3f{-1.0f, 0.0f, -0.0f});
CHECK(vertices[10].Normal == v3f{0.0f, 1.0f, -0.0f});
CHECK(vertices[11].Normal == v3f{0.0f, 1.0f, -0.0f});
CHECK(vertices[19].Normal == v3f{0.0f, 0.0f, -1.0f});
}
}
SECTION("texture coords are correct for all buffers") {
{
REQUIRE(mesh->getMeshBuffer(0)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].TCoords == v2f{0.583333313f, 0.791666686f});
CHECK(vertices[1].TCoords == v2f{0.583333313f, 0.666666686f});
CHECK(vertices[2].TCoords == v2f{0.708333313f, 0.791666686f});
CHECK(vertices[5].TCoords == v2f{0.375f, 0.416666657f});
CHECK(vertices[6].TCoords == v2f{0.5f, 0.291666657f});
CHECK(vertices[19].TCoords == v2f{0.708333313f, 0.75f});
}
{
REQUIRE(mesh->getMeshBuffer(1)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(1)->getVertices());
CHECK(vertices[1].TCoords == v2f{0.0f, 0.791666686f});
CHECK(vertices[4].TCoords == v2f{0.208333328f, 0.791666686f});
CHECK(vertices[5].TCoords == v2f{0.0f, 0.791666686f});
CHECK(vertices[6].TCoords == v2f{0.208333328f, 0.583333313f});
CHECK(vertices[12].TCoords == v2f{0.416666657f, 0.791666686f});
CHECK(vertices[15].TCoords == v2f{0.208333328f, 0.583333313f});
}
{
REQUIRE(mesh->getMeshBuffer(2)->getVertexCount() == 24);
auto vertices = static_cast<const irr::video::S3DVertex *>(
mesh->getMeshBuffer(2)->getVertices());
CHECK(vertices[10].TCoords == v2f{0.375f, 0.416666657f});
CHECK(vertices[11].TCoords == v2f{0.375f, 0.583333313f});
CHECK(vertices[12].TCoords == v2f{0.708333313f, 0.625f});
CHECK(vertices[17].TCoords == v2f{0.541666687f, 0.458333343f});
CHECK(vertices[20].TCoords == v2f{0.208333328f, 0.416666657f});
CHECK(vertices[22].TCoords == v2f{0.375f, 0.416666657f});
}
}
}
// https://github.com/KhronosGroup/glTF-Sample-Models/tree/main/2.0/SimpleSparseAccessor
SECTION("simple sparse accessor")
{
const auto mesh = loadMesh(model_stem + "simple_sparse_accessor.gltf");
REQUIRE(mesh != nullptr);
const auto *vertices = reinterpret_cast<irr::video::S3DVertex *>(
mesh->getMeshBuffer(0)->getVertices());
const std::array<v3f, 14> expectedPositions = {
// Lower
v3f(0, 0, 0),
v3f(1, 0, 0),
v3f(2, 0, 0),
v3f(3, 0, 0),
v3f(4, 0, 0),
v3f(5, 0, 0),
v3f(6, 0, 0),
// Upper
v3f(0, 1, 0),
v3f(1, 2, 0), // overridden
v3f(2, 1, 0),
v3f(3, 3, 0), // overridden
v3f(4, 1, 0),
v3f(5, 4, 0), // overridden
v3f(6, 1, 0),
};
for (std::size_t i = 0; i < expectedPositions.size(); ++i)
CHECK(vertices[i].Pos == expectedPositions[i]);
}
}

View file

@ -122,7 +122,7 @@ void TestServerModManager::testGetMods()
ServerModManager sm(m_worlddir);
const auto &mods = sm.getMods();
// `ls ./games/devtest/mods | wc -l` + 1 (test mod)
UASSERTEQ(std::size_t, mods.size(), 31 + 1);
UASSERTEQ(std::size_t, mods.size(), 32 + 1);
// Ensure we found basenodes mod (part of devtest)
// and test_mod (for testing MINETEST_MOD_PATH).