Scale cube vertices correctly

This commit is contained in:
Josiah VanderZee 2022-11-11 21:00:34 -06:00 committed by JosiahWI
parent e784335115
commit b1260a8927
No known key found for this signature in database
GPG Key ID: C7BB8573A4ABC4B9
2 changed files with 22 additions and 13 deletions

View File

@ -71,13 +71,13 @@ static T readPrimitive(const BufferOffset& readFrom) {
return *reinterpret_cast<T*>(d);
}
static core::vector3df readVector(const BufferOffset& readFrom) {
static core::vector3df readVector(const BufferOffset& readFrom, const float scale) {
// glTF coordinates are right-handed, minetest ones are left-handed
// 1 glTF coordinate is equivalent to 10 Irrlicht coordinates
return core::vector3df(
-1.0f * readPrimitive<float>(readFrom),
1.0f * readPrimitive<float>(BufferOffset(readFrom, sizeof(float))),
1.0 * readPrimitive<float>(BufferOffset(readFrom, 2 * sizeof(float))));
-scale * readPrimitive<float>(readFrom),
scale * readPrimitive<float>(BufferOffset(readFrom, sizeof(float))),
scale * readPrimitive<float>(BufferOffset(readFrom, 2 * sizeof(float))));
}
static u16* readIndices(const BufferOffset& readFrom, const std::size_t count)
@ -89,6 +89,15 @@ static u16* readIndices(const BufferOffset& readFrom, const std::size_t count)
return indices;
}
float getScale(const tinygltf::Model& model)
{
if (model.nodes[0].scale.size() > 0) {
return static_cast<float>(model.nodes[0].scale[0]);
}
return 1.0f;
}
video::S3DVertex* getVertices(const tinygltf::Model& model, const std::size_t accessorId)
{
const auto& view = model.bufferViews[
@ -99,7 +108,7 @@ video::S3DVertex* getVertices(const tinygltf::Model& model, const std::size_t ac
for (std::size_t i = 0; i < model.accessors[accessorId].count; ++i) {
const auto v = readVector(BufferOffset(
buffer.data, view.byteOffset + 3 * sizeof(float) * i));
buffer.data, view.byteOffset + 3 * sizeof(float) * i), getScale(model));
vertices[i] = {v, {0.0f, 0.0f, 0.0f}, {}, {0.0f, 0.0f}};
}

View File

@ -74,14 +74,14 @@ TEST_CASE("blender cube") {
REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24);
const auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
sm.getMesh()->getMeshBuffer(0)->getVertices());
CHECK(vertices[0].Pos == irr::core::vector3df{1.0f, -1.0f, 1.0f});
CHECK(vertices[3].Pos == irr::core::vector3df{1.0f, 1.0f, 1.0f});
CHECK(vertices[6].Pos == irr::core::vector3df{1.0f, -1.0f, -1.0f});
CHECK(vertices[9].Pos == irr::core::vector3df{1.0f, 1.0f, -1.0f});
CHECK(vertices[12].Pos == irr::core::vector3df{-1.0f, -1.0f, 1.0f});
CHECK(vertices[15].Pos == irr::core::vector3df{-1.0f, 1.0f, 1.0f});
CHECK(vertices[18].Pos == irr::core::vector3df{-1.0f, -1.0f, -1.0f});
CHECK(vertices[21].Pos == irr::core::vector3df{-1.0f, 1.0f, -1.0f});
CHECK(vertices[0].Pos == irr::core::vector3df{10.0f, -10.0f, 10.0f});
CHECK(vertices[3].Pos == irr::core::vector3df{10.0f, 10.0f, 10.0f});
CHECK(vertices[6].Pos == irr::core::vector3df{10.0f, -10.0f, -10.0f});
CHECK(vertices[9].Pos == irr::core::vector3df{10.0f, 10.0f, -10.0f});
CHECK(vertices[12].Pos == irr::core::vector3df{-10.0f, -10.0f, 10.0f});
CHECK(vertices[15].Pos == irr::core::vector3df{-10.0f, 10.0f, 10.0f});
CHECK(vertices[18].Pos == irr::core::vector3df{-10.0f, -10.0f, -10.0f});
CHECK(vertices[21].Pos == irr::core::vector3df{-10.0f, 10.0f, -10.0f});
}
SECTION("vertex indices are correct")