Test for correct vertex indices

This commit is contained in:
Josiah VanderZee 2022-11-10 13:58:28 -06:00 committed by JosiahWI
parent f2a3e990f3
commit 1b9d3ca502
No known key found for this signature in database
GPG Key ID: C7BB8573A4ABC4B9
3 changed files with 87 additions and 52 deletions

View File

@ -18,12 +18,6 @@
#include <memory>
#include <string>
namespace irr
{
namespace scene
{
class BufferOffset
{
public:
@ -50,20 +44,77 @@ private:
std::size_t m_offset;
};
static float readFloat(const BufferOffset& readFrom) {
unsigned char d[4]{};
for (int i = 0; i < 4; ++i) {
namespace irr
{
namespace scene
{
static bool tryParseGLTF(io::IReadFile* file, tinygltf::Model& model)
{
tinygltf::TinyGLTF loader {};
std::string err {};
std::string warn {};
auto buf = std::make_unique<char[]>(file->getSize());
file->read(buf.get(), file->getSize());
return loader.LoadASCIIFromString(
&model, &err, &warn, buf.get(), file->getSize(), "", 1);
}
template <class T>
static T readPrimative(const BufferOffset& readFrom) {
unsigned char d[sizeof(T)]{};
for (std::size_t i = 0; i < sizeof(T); ++i) {
d[i] = readFrom.at(i);
}
return *reinterpret_cast<float*>(d);
return *reinterpret_cast<T*>(d);
}
static core::vector3df readVector(const BufferOffset& readFrom) {
// glTF coordinates are right-handed, minetest ones are left-handed
// 1 glTF coordinate is equivalent to 10 Irrlicht coordinates
return core::vector3df(
-readFloat(readFrom),
readFloat(BufferOffset(readFrom, sizeof(float))),
readFloat(BufferOffset(readFrom, 2 * sizeof(float))));
-1.0 * readPrimative<float>(readFrom),
1.0 * readPrimative<float>(BufferOffset(readFrom, sizeof(float))),
1.0 * readPrimative<float>(BufferOffset(readFrom, 2 * sizeof(float))));
}
static u16* readIndices(const BufferOffset& readFrom, const std::size_t count)
{
auto* indices = new u16[count]{};
for (std::size_t i = 0; i < count; ++i) {
indices[i] = readPrimative<u16>(BufferOffset(readFrom, i * sizeof(u16)));
}
return indices;
}
video::S3DVertex* getVertices(const tinygltf::Model& model, const std::size_t accessorId)
{
const auto& view = model.bufferViews[
model.accessors[accessorId].bufferView];
const auto& buffer = model.buffers[view.buffer];
const auto v1 = readVector(BufferOffset(
buffer.data, view.byteOffset));
const auto v2 = readVector(BufferOffset(
buffer.data, view.byteOffset + (3 * sizeof(float))));
const auto v3 = readVector(BufferOffset(
buffer.data, view.byteOffset + (6 * sizeof(float))));
return (new video::S3DVertex[3] {
{v1, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 0.0f}},
{v2, {0.0f, 0.0f, 1.0f}, {}, {1.0f, 0.0f}},
{v3, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 1.0f}} } );
}
u16* getIndices(const tinygltf::Model& model, const std::size_t accessorId)
{
const auto& indicesView = model.bufferViews[
model.accessors[accessorId].bufferView];
const auto& indicesBuffer = model.buffers[indicesView.buffer];
return readIndices(
BufferOffset(indicesBuffer.data, indicesView.byteOffset),
model.accessors[0].count);
}
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
@ -84,24 +135,13 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
return nullptr;
}
const auto& view = model.bufferViews[model.accessors[1].bufferView];
const auto& buffer = model.buffers[view.buffer];
const auto v1 = readVector(BufferOffset(
buffer.data, view.byteOffset));
const auto v2 = readVector(BufferOffset(
buffer.data, view.byteOffset + (3 * sizeof(float))));
const auto v3 = readVector(BufferOffset(
buffer.data, view.byteOffset + (6 * sizeof(float))));
auto* indices = getIndices(model, 0);
auto* vertices = getVertices(model, 1);
// sorry Bjarne
SMeshBuffer* meshbuf { new SMeshBuffer {} };
const video::S3DVertex* vertices { new video::S3DVertex[3] {
{v1, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 0.0f}},
{v2, {0.0f, 0.0f, 1.0f}, {}, {1.0f, 0.0f}},
{v3, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 1.0f}} } };
const u16* indices { new u16[3] {0, 2, 1} };
meshbuf->append(vertices, 3, indices, 3);
meshbuf->append(vertices, model.accessors[1].count,
indices, model.accessors[0].count);
SMesh* mesh { new SMesh {} };
mesh->addMeshBuffer(meshbuf);
@ -112,20 +152,7 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
return animatedMesh;
}
bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file,
tinygltf::Model& model) const
{
tinygltf::TinyGLTF loader {};
std::string err {};
std::string warn {};
auto buf = std::make_unique<char[]>(file->getSize());
file->read(buf.get(), file->getSize());
return loader.LoadASCIIFromString(
&model, &err, &warn, buf.get(), file->getSize(), "", 1);
}
} // namespace scene
} // namespace irr
} // namespace scene

View File

@ -22,9 +22,6 @@ public:
bool isALoadableFileExtension(const io::path& filename) const override;
IAnimatedMesh* createMesh(io::IReadFile* file) override;
private:
bool tryParseGLTF(io::IReadFile* file, tinygltf::Model& model) const;
};
} // namespace scene

View File

@ -45,19 +45,30 @@ TEST_CASE("load empty gltf file") {
CHECK(sm.getMesh() == nullptr);
}
TEST_CASE("minimal triangle has correct vertices") {
TEST_CASE("minimal triangle") {
ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
auto* mesh = sm.getMesh();
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
auto* meshbuf = mesh->getMeshBuffer(0);
REQUIRE(meshbuf->getVertexCount() == 3);
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices());
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
SECTION("vertex coordinates are correct") {
REQUIRE(meshbuf->getVertexCount() == 3);
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices());
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
}
SECTION("vertex indices are correct") {
REQUIRE(meshbuf->getIndexCount() == 3);
auto* indices = reinterpret_cast<irr::u16*>(meshbuf->getIndices());
CHECK(indices[0] == 0);
CHECK(indices[1] == 1);
CHECK(indices[2] == 2);
}
}
TEST_CASE("mesh loader returns nullptr when given null file pointer") {