irrlicht/source/Irrlicht/CGLTFMeshFileLoader.cpp

241 lines
6.3 KiB
C++
Raw Normal View History

#include "CGLTFMeshFileLoader.h"
#include "CMeshBuffer.h"
#include "coreutil.h"
#include "IAnimatedMesh.h"
#include "IReadFile.h"
#include "irrTypes.h"
#include "path.h"
#include "S3DVertex.h"
#include "SAnimatedMesh.h"
#include "SColor.h"
#include "SMesh.h"
#include "vector3d.h"
#define TINYGLTF_IMPLEMENTATION
#include <tiny_gltf.h>
#include <cstddef>
#include <memory>
#include <string>
2022-11-12 14:01:56 +01:00
template <class T>
struct Span
{
T* buffer = nullptr;
std::size_t size = 0;
};
class BufferOffset
{
public:
BufferOffset(const std::vector<unsigned char>& buf,
2022-11-07 01:00:42 +01:00
const std::size_t offset)
: m_buf(buf)
, m_offset(offset)
{
}
BufferOffset(const BufferOffset& other, const std::size_t fromOffset)
: m_buf(other.m_buf)
, m_offset(other.m_offset + fromOffset)
{
}
unsigned char at(const std::size_t fromOffset) const
{
return m_buf.at(m_offset + fromOffset);
}
private:
const std::vector<unsigned char>& m_buf;
std::size_t m_offset;
};
2022-11-10 20:58:28 +01:00
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>
2022-11-12 14:01:56 +01:00
static T readPrimitive(const BufferOffset& readFrom)
{
2022-11-10 20:58:28 +01:00
unsigned char d[sizeof(T)]{};
for (std::size_t i = 0; i < sizeof(T); ++i) {
d[i] = readFrom.at(i);
}
2022-11-10 20:58:28 +01:00
return *reinterpret_cast<T*>(d);
}
2022-11-12 14:01:56 +01:00
static core::vector2df readVec2DF(const BufferOffset& readFrom)
{
return core::vector2df(
readPrimitive<float>(readFrom),
readPrimitive<float>(BufferOffset(readFrom, sizeof(float))));
}
2022-11-12 14:43:44 +01:00
static core::vector3df readVec3DF(const BufferOffset& readFrom,
const float scale = 1.0f)
2022-11-12 14:01:56 +01:00
{
2022-11-07 01:00:42 +01:00
// glTF coordinates are right-handed, minetest ones are left-handed
2022-11-10 20:58:28 +01:00
// 1 glTF coordinate is equivalent to 10 Irrlicht coordinates
2022-11-07 01:00:42 +01:00
return core::vector3df(
2022-11-12 04:00:34 +01:00
-scale * readPrimitive<float>(readFrom),
scale * readPrimitive<float>(BufferOffset(readFrom, sizeof(float))),
scale * readPrimitive<float>(BufferOffset(readFrom, 2 * sizeof(float))));
2022-11-10 20:58:28 +01:00
}
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) {
2022-11-12 00:37:21 +01:00
indices[i] = readPrimitive<u16>(BufferOffset(readFrom, i * sizeof(u16)));
2022-11-10 20:58:28 +01:00
}
return indices;
}
2022-11-12 04:00:34 +01:00
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;
}
2022-11-12 14:01:56 +01:00
static void copyPositions(const tinygltf::Model& model,
const Span<video::S3DVertex> vertices,
const std::size_t accessorId)
2022-11-10 20:58:28 +01:00
{
const auto& view = model.bufferViews[
model.accessors[accessorId].bufferView];
const auto& buffer = model.buffers[view.buffer];
2022-11-12 14:01:56 +01:00
for (std::size_t i = 0; i < model.accessors[accessorId].count; ++i) {
const auto v = readVec3DF(BufferOffset(
buffer.data, view.byteOffset + 3 * sizeof(float) * i),
getScale(model));
vertices.buffer[i].Pos = v;
}
}
2022-11-12 00:37:21 +01:00
2022-11-12 14:43:44 +01:00
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;
}
}
2022-11-12 14:01:56 +01:00
static void copyTCoords(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];
2022-11-12 00:37:21 +01:00
for (std::size_t i = 0; i < model.accessors[accessorId].count; ++i) {
2022-11-12 14:01:56 +01:00
const auto t = readVec2DF(BufferOffset(
buffer.data, view.byteOffset + 2 * sizeof(float) * i));
vertices.buffer[i].TCoords = t;
}
}
static video::S3DVertex* getVertices(const tinygltf::Model& model,
const std::size_t accessorId)
{
auto* vertexBuffer = new video::S3DVertex[
model.accessors[accessorId].count]{};
Span<video::S3DVertex> vertices{
vertexBuffer, model.accessors[accessorId].count};
copyPositions(model, vertices, accessorId);
2022-11-12 14:43:44 +01:00
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);
}
2022-11-12 14:01:56 +01:00
const auto tCoordsField
= model.meshes[0].primitives[0].attributes.find("TEXCOORD_0");
if (tCoordsField != model.meshes[0].primitives[0].attributes.end()) {
copyTCoords(model, vertices, tCoordsField->second);
2022-11-12 00:37:21 +01:00
}
2022-11-12 14:01:56 +01:00
return vertexBuffer;
2022-11-10 20:58:28 +01:00
}
2022-11-12 14:01:56 +01:00
static u16* getIndices(const tinygltf::Model& model,
const std::size_t accessorId)
2022-11-10 20:58:28 +01:00
{
2022-11-12 00:37:21 +01:00
const auto& view = model.bufferViews[
2022-11-10 20:58:28 +01:00
model.accessors[accessorId].bufferView];
2022-11-12 00:37:21 +01:00
const auto& indicesBuffer = model.buffers[view.buffer];
2022-11-10 20:58:28 +01:00
return readIndices(
2022-11-12 00:37:21 +01:00
BufferOffset(indicesBuffer.data, view.byteOffset),
2022-11-10 20:58:28 +01:00
model.accessors[0].count);
2022-11-07 01:00:42 +01:00
}
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
{
}
bool CGLTFMeshFileLoader::isALoadableFileExtension(
const io::path& filename) const
{
return core::hasFileExtension(filename, "gltf");
}
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
{
tinygltf::Model model{};
if (file->getSize() == 0 || !tryParseGLTF(file, model)) {
return nullptr;
}
2022-11-12 00:37:21 +01:00
const auto indicesAccessorId =
model.meshes[0].primitives[0].indices;
const auto positionAccessorId =
model.meshes[0].primitives[0].attributes["POSITION"];
auto* indices = getIndices(model, indicesAccessorId);
auto* vertices = getVertices(model, positionAccessorId);
SMeshBuffer* meshbuf { new SMeshBuffer {} };
2022-11-12 00:37:21 +01:00
meshbuf->append(vertices, model.accessors[positionAccessorId].count,
indices, model.accessors[indicesAccessorId].count);
SMesh* mesh { new SMesh {} };
mesh->addMeshBuffer(meshbuf);
SAnimatedMesh* animatedMesh { new SAnimatedMesh {} };
animatedMesh->addMesh(mesh);
return animatedMesh;
}
2022-11-10 20:58:28 +01:00
} // namespace scene
} // namespace irr