Test vertex normals on cube

This commit is contained in:
Josiah VanderZee 2022-11-12 07:43:44 -06:00 committed by JosiahWI
parent d90b906d26
commit 93b538cb58
No known key found for this signature in database
GPG Key ID: C7BB8573A4ABC4B9
2 changed files with 35 additions and 1 deletions

View File

@ -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 // glTF coordinates are right-handed, minetest ones are left-handed
// 1 glTF coordinate is equivalent to 10 Irrlicht coordinates // 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<video::S3DVertex> 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, static void copyTCoords(const tinygltf::Model& model,
const Span<video::S3DVertex> vertices, const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const std::size_t accessorId)
@ -153,6 +168,12 @@ static video::S3DVertex* getVertices(const tinygltf::Model& model,
vertexBuffer, model.accessors[accessorId].count}; vertexBuffer, model.accessors[accessorId].count};
copyPositions(model, vertices, accessorId); 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 const auto tCoordsField
= model.meshes[0].primitives[0].attributes.find("TEXCOORD_0"); = model.meshes[0].primitives[0].attributes.find("TEXCOORD_0");
if (tCoordsField != model.meshes[0].primitives[0].attributes.end()) { if (tCoordsField != model.meshes[0].primitives[0].attributes.end()) {

View File

@ -93,6 +93,19 @@ TEST_CASE("blender cube") {
CHECK(indices[2] == 9); CHECK(indices[2] == 9);
} }
SECTION("vertex normals are correct") {
REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24);
const auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
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") { SECTION("texture coords are correct") {
REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24);
const auto* vertices = reinterpret_cast<irr::video::S3DVertex*>( const auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(