mirror of
https://github.com/minetest/irrlicht.git
synced 2024-11-18 08:10:32 +01:00
Test for correct vertex indices
This commit is contained in:
parent
f2a3e990f3
commit
1b9d3ca502
@ -18,12 +18,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace irr
|
|
||||||
{
|
|
||||||
|
|
||||||
namespace scene
|
|
||||||
{
|
|
||||||
|
|
||||||
class BufferOffset
|
class BufferOffset
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -50,20 +44,77 @@ private:
|
|||||||
std::size_t m_offset;
|
std::size_t m_offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
static float readFloat(const BufferOffset& readFrom) {
|
namespace irr
|
||||||
unsigned char d[4]{};
|
{
|
||||||
for (int i = 0; i < 4; ++i) {
|
|
||||||
|
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);
|
d[i] = readFrom.at(i);
|
||||||
}
|
}
|
||||||
return *reinterpret_cast<float*>(d);
|
return *reinterpret_cast<T*>(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
static core::vector3df readVector(const BufferOffset& readFrom) {
|
static core::vector3df readVector(const BufferOffset& readFrom) {
|
||||||
// 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
|
||||||
return core::vector3df(
|
return core::vector3df(
|
||||||
-readFloat(readFrom),
|
-1.0 * readPrimative<float>(readFrom),
|
||||||
readFloat(BufferOffset(readFrom, sizeof(float))),
|
1.0 * readPrimative<float>(BufferOffset(readFrom, sizeof(float))),
|
||||||
readFloat(BufferOffset(readFrom, 2 * 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()
|
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
|
||||||
@ -84,24 +135,13 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& view = model.bufferViews[model.accessors[1].bufferView];
|
auto* indices = getIndices(model, 0);
|
||||||
const auto& buffer = model.buffers[view.buffer];
|
auto* vertices = getVertices(model, 1);
|
||||||
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))));
|
|
||||||
|
|
||||||
// sorry Bjarne
|
|
||||||
SMeshBuffer* meshbuf { new SMeshBuffer {} };
|
SMeshBuffer* meshbuf { new SMeshBuffer {} };
|
||||||
|
|
||||||
const video::S3DVertex* vertices { new video::S3DVertex[3] {
|
meshbuf->append(vertices, model.accessors[1].count,
|
||||||
{v1, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 0.0f}},
|
indices, model.accessors[0].count);
|
||||||
{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);
|
|
||||||
|
|
||||||
SMesh* mesh { new SMesh {} };
|
SMesh* mesh { new SMesh {} };
|
||||||
mesh->addMeshBuffer(meshbuf);
|
mesh->addMeshBuffer(meshbuf);
|
||||||
@ -112,20 +152,7 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
|
|||||||
return animatedMesh;
|
return animatedMesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file,
|
} // namespace scene
|
||||||
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 irr
|
} // namespace irr
|
||||||
|
|
||||||
} // namespace scene
|
|
||||||
|
|
||||||
|
@ -22,9 +22,6 @@ public:
|
|||||||
bool isALoadableFileExtension(const io::path& filename) const override;
|
bool isALoadableFileExtension(const io::path& filename) const override;
|
||||||
|
|
||||||
IAnimatedMesh* createMesh(io::IReadFile* file) override;
|
IAnimatedMesh* createMesh(io::IReadFile* file) override;
|
||||||
|
|
||||||
private:
|
|
||||||
bool tryParseGLTF(io::IReadFile* file, tinygltf::Model& model) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace scene
|
} // namespace scene
|
||||||
|
@ -45,19 +45,30 @@ TEST_CASE("load empty gltf file") {
|
|||||||
CHECK(sm.getMesh() == nullptr);
|
CHECK(sm.getMesh() == nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("minimal triangle has correct vertices") {
|
TEST_CASE("minimal triangle") {
|
||||||
ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
|
ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
|
||||||
|
|
||||||
auto* mesh = sm.getMesh();
|
auto* mesh = sm.getMesh();
|
||||||
REQUIRE(mesh != nullptr);
|
REQUIRE(mesh != nullptr);
|
||||||
REQUIRE(mesh->getMeshBufferCount() == 1);
|
REQUIRE(mesh->getMeshBufferCount() == 1);
|
||||||
auto* meshbuf = mesh->getMeshBuffer(0);
|
auto* meshbuf = mesh->getMeshBuffer(0);
|
||||||
|
|
||||||
|
SECTION("vertex coordinates are correct") {
|
||||||
REQUIRE(meshbuf->getVertexCount() == 3);
|
REQUIRE(meshbuf->getVertexCount() == 3);
|
||||||
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
|
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
|
||||||
meshbuf->getVertices());
|
meshbuf->getVertices());
|
||||||
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
|
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[1].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
|
||||||
CHECK(vertices[2].Pos == irr::core::vector3df {0.0f, 1.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") {
|
TEST_CASE("mesh loader returns nullptr when given null file pointer") {
|
||||||
|
Loading…
Reference in New Issue
Block a user