Fix gltf static mesh loading issues (#14)

* Support u8 / u32 indices

* Skip primitives without vertices

* Add support for non-indexed geometry & skipping primitives

* Fix possible memory leak on error

* Use SSkinnedMesh

* Check indices

* Properly mirror node hierarchy

* Update .gitignore

* Reorder includes

* Add some throws for logic errors

* Fix non-indexed geometry winding order, add unit test

* Address code review comments

* Add matrix transform unit test
This commit is contained in:
Lars Müller
2024-04-15 15:11:37 +02:00
committed by Josiah VanderZee
parent 0faf1320c5
commit 036b40a9d8
7 changed files with 466 additions and 113 deletions

View File

@ -0,0 +1,106 @@
{
"asset" : {
"generator" : "Khronos glTF Blender I/O v1.7.33",
"version" : "2.0"
},
"scene" : 0,
"scenes" : [
{
"name" : "Scene",
"nodes" : [
0
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "Cube",
"matrix" : [
1, 0, 0, 0,
0, 2, 0, 0,
0, 0, 3, 0,
4, 5, 6, 1
]
}
],
"meshes" : [
{
"name" : "Cube.004",
"primitives" : [
{
"attributes" : {
"POSITION" : 0,
"NORMAL" : 1,
"TEXCOORD_0" : 2
},
"indices" : 3
}
]
}
],
"accessors" : [
{
"bufferView" : 0,
"componentType" : 5126,
"count" : 24,
"max" : [
1,
1,
1
],
"min" : [
-1,
-1,
-1
],
"type" : "VEC3"
},
{
"bufferView" : 1,
"componentType" : 5126,
"count" : 24,
"type" : "VEC3"
},
{
"bufferView" : 2,
"componentType" : 5126,
"count" : 24,
"type" : "VEC2"
},
{
"bufferView" : 3,
"componentType" : 5123,
"count" : 36,
"type" : "SCALAR"
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 288,
"byteOffset" : 0
},
{
"buffer" : 0,
"byteLength" : 288,
"byteOffset" : 288
},
{
"buffer" : 0,
"byteLength" : 192,
"byteOffset" : 576
},
{
"buffer" : 0,
"byteLength" : 72,
"byteOffset" : 768
}
],
"buffers" : [
{
"byteLength" : 840,
"uri" : "data:application/octet-stream;base64,AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvw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0,0 +1,54 @@
{
"scene" : 0,
"scenes" : [
{
"nodes" : [ 0 ]
}
],
"nodes" : [
{
"mesh" : 0
}
],
"meshes" : [
{
"primitives" : [ {
"attributes" : {
"POSITION" : 0
}
} ]
}
],
"buffers" : [
{
"uri" : "data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAA",
"byteLength" : 36
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteOffset" : 0,
"byteLength" : 36,
"target" : 34962
}
],
"accessors" : [
{
"bufferView" : 0,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 3,
"type" : "VEC3",
"max" : [ 1.0, 1.0, 0.0 ],
"min" : [ 0.0, 0.0, 0.0 ]
}
],
"asset" : {
"version" : "2.0"
}
}

View File

@ -1,4 +1,5 @@
#include "CReadFile.h"
#include "vector3d.h"
#include <irrlicht.h>
@ -54,8 +55,10 @@ TEST_CASE("load empty gltf file") {
TEST_CASE("minimal triangle") {
auto path = GENERATE(
"source/Irrlicht/tests/assets/minimal_triangle.gltf",
"source/Irrlicht/tests/assets/triangle_with_vertex_stride.gltf");
"source/Irrlicht/tests/assets/minimal_triangle.gltf",
"source/Irrlicht/tests/assets/triangle_with_vertex_stride.gltf",
// Test non-indexed geometry.
"source/Irrlicht/tests/assets/triangle_without_indices.gltf");
INFO(path);
ScopedMesh sm(path);
REQUIRE(sm.getMesh() != nullptr);
@ -164,6 +167,32 @@ TEST_CASE("blender cube scaled") {
}
}
TEST_CASE("blender cube matrix transform") {
ScopedMesh sm("source/Irrlicht/tests/assets/blender_cube_matrix_transform.gltf");
REQUIRE(sm.getMesh() != nullptr);
REQUIRE(sm.getMesh()->getMeshBufferCount() == 1);
SECTION("Transformation is correct") {
REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24);
const auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
sm.getMesh()->getMeshBuffer(0)->getVertices());
const auto checkVertex = [&](const std::size_t i, irr::core::vector3df vec) {
// The transform scales by (1, 2, 3) and translates by (4, 5, 6).
CHECK(vertices[i].Pos == vec * irr::core::vector3df{1, 2, 3}
// The -6 is due to the coordinate system conversion.
+ irr::core::vector3df{4, 5, -6});
};
checkVertex(0, irr::core::vector3df{-1, -1, -1});
checkVertex(3, irr::core::vector3df{-1, 1, -1});
checkVertex(6, irr::core::vector3df{-1, -1, 1});
checkVertex(9, irr::core::vector3df{-1, 1, 1});
checkVertex(12, irr::core::vector3df{1, -1, -1});
checkVertex(15, irr::core::vector3df{1, 1, -1});
checkVertex(18, irr::core::vector3df{1, -1, 1});
checkVertex(21, irr::core::vector3df{1, 1, 1});
}
}
TEST_CASE("snow man") {
ScopedMesh sm("source/Irrlicht/tests/assets/snow_man.gltf");
REQUIRE(sm.getMesh() != nullptr);