From 7220104c37a40810ba725875e0fcc83e4d05cceb Mon Sep 17 00:00:00 2001 From: Josiah VanderZee Date: Sat, 12 Nov 2022 07:43:44 -0600 Subject: [PATCH] Test vertex normals on cube --- source/Irrlicht/CGLTFMeshFileLoader.cpp | 23 ++++++++++++++++++- .../tests/testCGLTFMeshFileLoader.cpp | 13 +++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/source/Irrlicht/CGLTFMeshFileLoader.cpp b/source/Irrlicht/CGLTFMeshFileLoader.cpp index 3b533363..a1ef7b3f 100644 --- a/source/Irrlicht/CGLTFMeshFileLoader.cpp +++ b/source/Irrlicht/CGLTFMeshFileLoader.cpp @@ -87,7 +87,8 @@ static core::vector2df readVec2DF(const BufferOffset& readFrom) } -static core::vector3df readVec3DF(const BufferOffset& readFrom, const float scale) +static core::vector3df readVec3DF(const BufferOffset& readFrom, + const float scale = 1.0f) { // glTF coordinates are right-handed, minetest ones are left-handed // 1 glTF coordinate is equivalent to 10 Irrlicht coordinates @@ -130,6 +131,20 @@ static void copyPositions(const tinygltf::Model& model, } } +static void copyNormals(const tinygltf::Model& model, + const Span vertices, + const std::size_t accessorId) +{ + const auto& view = model.bufferViews[ + model.accessors[accessorId].bufferView]; + const auto& buffer = model.buffers[view.buffer]; + for (std::size_t i = 0; i < model.accessors[accessorId].count; ++i) { + const auto n = readVec3DF(BufferOffset( + buffer.data, view.byteOffset + 3 * sizeof(float) * i)); + vertices.buffer[i].Normal = n; + } +} + static void copyTCoords(const tinygltf::Model& model, const Span vertices, const std::size_t accessorId) @@ -153,6 +168,12 @@ static video::S3DVertex* getVertices(const tinygltf::Model& model, vertexBuffer, model.accessors[accessorId].count}; copyPositions(model, vertices, accessorId); + const auto normalsField + = model.meshes[0].primitives[0].attributes.find("NORMAL"); + if (normalsField != model.meshes[0].primitives[0].attributes.end()) { + copyNormals(model, vertices, normalsField->second); + } + const auto tCoordsField = model.meshes[0].primitives[0].attributes.find("TEXCOORD_0"); if (tCoordsField != model.meshes[0].primitives[0].attributes.end()) { diff --git a/source/Irrlicht/tests/testCGLTFMeshFileLoader.cpp b/source/Irrlicht/tests/testCGLTFMeshFileLoader.cpp index a0a14052..74d10f0f 100644 --- a/source/Irrlicht/tests/testCGLTFMeshFileLoader.cpp +++ b/source/Irrlicht/tests/testCGLTFMeshFileLoader.cpp @@ -93,6 +93,19 @@ TEST_CASE("blender cube") { CHECK(indices[2] == 9); } + SECTION("vertex normals are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + CHECK(vertices[0].Normal == irr::core::vector3df{1.0f, 0.0f, 0.0f}); + CHECK(vertices[1].Normal == irr::core::vector3df{0.0f, -1.0f, 0.0f}); + CHECK(vertices[2].Normal == irr::core::vector3df{0.0f, 0.0f, 1.0f}); + CHECK(vertices[3].Normal == irr::core::vector3df{1.0f, 0.0f, 0.0f}); + CHECK(vertices[6].Normal == irr::core::vector3df{1.0f, 0.0f, 0.0f}); + CHECK(vertices[23].Normal == irr::core::vector3df{-1.0f, 0.0f, 0.0f}); + + } + SECTION("texture coords are correct") { REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); const auto* vertices = reinterpret_cast(