2022-10-22 23:36:29 +02:00
|
|
|
#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"
|
2022-11-06 14:55:09 +01:00
|
|
|
#include "vector3d.h"
|
2023-01-28 02:45:02 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <tuple>
|
2022-10-22 23:36:29 +02:00
|
|
|
|
|
|
|
#define TINYGLTF_IMPLEMENTATION
|
|
|
|
#include <tiny_gltf.h>
|
|
|
|
|
2022-11-06 14:55:09 +01:00
|
|
|
#include <cstddef>
|
2022-11-13 14:55:30 +01:00
|
|
|
#include <cstring>
|
2022-10-22 23:36:29 +02:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2022-11-06 14:55:09 +01:00
|
|
|
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)
|
2022-11-06 14:55:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// A helper function to disable tinygltf embedded image loading
|
|
|
|
bool turn_off_textures_hack(tinygltf::Image *a, const int b, std::string *c,
|
|
|
|
std::string *d, int e, int f, const unsigned char * g,int h, void *user_pointer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2022-11-10 20:58:28 +01:00
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
static bool tryParseGLTF(io::IReadFile* file, tinygltf::Model& model)
|
|
|
|
{
|
|
|
|
tinygltf::TinyGLTF loader {};
|
2023-01-28 02:45:02 +01:00
|
|
|
|
|
|
|
// Stop embedded textures from making model fail to load
|
|
|
|
void *the_void = 0;
|
|
|
|
loader.SetImageLoader(turn_off_textures_hack, the_void);
|
|
|
|
|
2022-11-10 20:58:28 +01:00
|
|
|
std::string err {};
|
|
|
|
std::string warn {};
|
|
|
|
|
|
|
|
auto buf = std::make_unique<char[]>(file->getSize());
|
|
|
|
file->read(buf.get(), file->getSize());
|
2023-01-28 02:45:02 +01:00
|
|
|
|
|
|
|
return loader.LoadASCIIFromString(&model, &err, &warn, buf.get(), file->getSize(), "", 1);
|
2022-11-10 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-11-06 14:55:09 +01:00
|
|
|
d[i] = readFrom.at(i);
|
|
|
|
}
|
2022-11-13 14:55:30 +01:00
|
|
|
T dest;
|
|
|
|
std::memcpy(&dest, d, sizeof(dest));
|
|
|
|
return dest;
|
2022-11-06 14:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 14:01:56 +01:00
|
|
|
static core::vector2df readVec2DF(const BufferOffset& readFrom)
|
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
return core::vector2df( readPrimitive<float>(readFrom), readPrimitive<float>( BufferOffset( readFrom, sizeof(float) ) ) );
|
2022-11-12 14:01:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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-14 22:47:30 +01:00
|
|
|
// glTF's coordinate system is right-handed, Irrlicht's is left-handed
|
|
|
|
// glTF's +Z axis corresponds to Irrlicht's -Z axis
|
2022-11-07 01:00:42 +01:00
|
|
|
return core::vector3df(
|
2022-11-14 22:47:30 +01:00
|
|
|
scale * readPrimitive<float>(readFrom),
|
2022-11-12 04:00:34 +01:00
|
|
|
scale * readPrimitive<float>(BufferOffset(readFrom, sizeof(float))),
|
2022-11-14 22:47:30 +01:00
|
|
|
-scale * readPrimitive<float>(BufferOffset(readFrom, 2 * sizeof(float))));
|
2022-11-10 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:45:02 +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
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
|
|
|
|
const auto& view = model.bufferViews[ model.accessors[accessorId].bufferView];
|
2022-11-10 20:58:28 +01:00
|
|
|
const auto& buffer = model.buffers[view.buffer];
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto count = model.accessors[accessorId].count;
|
|
|
|
|
|
|
|
float scale = getScale(model);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < count; i++) {
|
|
|
|
const auto v = readVec3DF(BufferOffset( buffer.data, view.byteOffset + (3 * sizeof(float) * i)), scale);
|
2022-11-12 14:01:56 +01:00
|
|
|
vertices.buffer[i].Pos = v;
|
|
|
|
}
|
|
|
|
}
|
2022-11-12 00:37:21 +01:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
static void copyNormals(const tinygltf::Model& model, const Span<video::S3DVertex> vertices, const std::size_t accessorId)
|
2022-11-12 14:43:44 +01:00
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto& view = model.bufferViews[model.accessors[accessorId].bufferView];
|
2022-11-12 14:43:44 +01:00
|
|
|
const auto& buffer = model.buffers[view.buffer];
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto count = model.accessors[accessorId].count;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
|
|
const auto n = readVec3DF(BufferOffset( buffer.data, view.byteOffset + 3 * sizeof(float) * i ));
|
2022-11-12 14:43:44 +01:00
|
|
|
vertices.buffer[i].Normal = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
static void copyTCoords(const tinygltf::Model& model, const Span<video::S3DVertex> vertices, const std::size_t accessorId)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto& view = model.bufferViews[ model.accessors[accessorId].bufferView ];
|
2022-11-12 14:01:56 +01:00
|
|
|
const auto& buffer = model.buffers[view.buffer];
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto count = model.accessors[accessorId].count;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
|
|
const auto t = readVec2DF(BufferOffset(buffer.data, view.byteOffset + 2 * sizeof(float) * i));
|
2022-11-12 14:01:56 +01:00
|
|
|
vertices.buffer[i].TCoords = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
static void getIndices(const tinygltf::Model& model, const std::size_t accessorId, std::vector<u16> *indicesBuffer)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto& view = model.bufferViews[model.accessors[accessorId].bufferView];
|
|
|
|
const auto& modelIndices = model.buffers[view.buffer];
|
2022-11-12 14:01:56 +01:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
auto bufferobject = BufferOffset(modelIndices.data, view.byteOffset);
|
|
|
|
auto count = model.accessors[accessorId].count;
|
2022-11-12 14:43:44 +01:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
for (std::size_t i = 0; i < count; i++) {
|
|
|
|
auto current = readPrimitive<u16>(BufferOffset(bufferobject, i * sizeof(u16)));
|
|
|
|
// Inverse the order of indices
|
|
|
|
indicesBuffer->insert(indicesBuffer->begin(), current);
|
2022-11-12 00:37:21 +01:00
|
|
|
}
|
2022-11-10 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// Returns a tuple of the current counts (current_vertex_index, current_normals_index, current_tcoords_index)
|
|
|
|
static void getVertices
|
|
|
|
( const tinygltf::Model& model, const std::size_t accessorId, Span<video::S3DVertex> *verticesBuffer,
|
|
|
|
std::size_t mesh_index, std::size_t primitive_index )
|
2022-11-10 20:58:28 +01:00
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
copyPositions(model, *verticesBuffer, accessorId);
|
|
|
|
|
|
|
|
const auto normalsField = model.meshes[mesh_index].primitives[primitive_index].attributes.find("NORMAL");
|
|
|
|
|
|
|
|
if (normalsField != model.meshes[mesh_index].primitives[primitive_index].attributes.end()) {
|
|
|
|
copyNormals(model, *verticesBuffer, normalsField->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto tCoordsField = model.meshes[mesh_index].primitives[primitive_index].attributes.find("TEXCOORD_0");
|
|
|
|
|
|
|
|
if (tCoordsField != model.meshes[mesh_index].primitives[primitive_index].attributes.end()) {
|
|
|
|
copyTCoords(model, *verticesBuffer, tCoordsField->second);
|
|
|
|
}
|
2022-11-07 01:00:42 +01:00
|
|
|
}
|
|
|
|
|
2022-10-22 23:36:29 +02:00
|
|
|
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGLTFMeshFileLoader::isALoadableFileExtension(
|
|
|
|
const io::path& filename) const
|
|
|
|
{
|
|
|
|
return core::hasFileExtension(filename, "gltf");
|
|
|
|
}
|
|
|
|
|
|
|
|
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
|
|
|
|
{
|
2022-11-06 14:55:09 +01:00
|
|
|
tinygltf::Model model{};
|
|
|
|
|
|
|
|
if (file->getSize() == 0 || !tryParseGLTF(file, model)) {
|
2022-10-22 23:36:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// Create the base mesh
|
|
|
|
SMesh* mesh { new SMesh {} };
|
2022-11-12 00:37:21 +01:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// Iterate models
|
|
|
|
for (std::size_t mesh_index = 0; mesh_index < model.meshes.size(); mesh_index++) {
|
|
|
|
// Iterate primitives
|
|
|
|
for (std::size_t primitive_index = 0; primitive_index < model.meshes[mesh_index].primitives.size(); primitive_index++) {
|
2022-11-06 14:55:09 +01:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
const auto positionAccessorId = model.meshes[mesh_index].primitives[primitive_index].attributes["POSITION"];
|
|
|
|
const auto indicesAccessorId = model.meshes[mesh_index].primitives[primitive_index].indices;
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// Creates counts for preallocation
|
|
|
|
std::size_t vertices_count = model.accessors[positionAccessorId].count;
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// We must count to create containers for the data
|
|
|
|
// Create new buffer for vertices, positions, and normals. Will loop through this soon
|
|
|
|
auto* vertexBuffer = new video::S3DVertex[vertices_count]{};
|
|
|
|
// This is used to copy data into the vertexBuffer
|
|
|
|
Span<video::S3DVertex> verticesBuffer{ vertexBuffer, vertices_count };
|
|
|
|
// Create dynamic indices buffer so it's easier to work with
|
|
|
|
std::vector<u16> indicesBuffer;
|
|
|
|
|
|
|
|
getIndices(model, indicesAccessorId, &indicesBuffer);
|
|
|
|
getVertices(model, positionAccessorId, &verticesBuffer, mesh_index, primitive_index);
|
|
|
|
|
|
|
|
// Create the mesh buffer
|
|
|
|
SMeshBuffer* meshbuf { new SMeshBuffer {} };
|
|
|
|
meshbuf->append(vertexBuffer, vertices_count, indicesBuffer.data(), indicesBuffer.size());
|
|
|
|
|
|
|
|
mesh->addMeshBuffer(meshbuf);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2023-01-28 02:45:02 +01:00
|
|
|
// Create the mesh animations
|
2022-10-22 23:36:29 +02:00
|
|
|
SAnimatedMesh* animatedMesh { new SAnimatedMesh {} };
|
2023-01-28 02:45:02 +01:00
|
|
|
animatedMesh->addMesh(mesh);
|
2022-10-22 23:36:29 +02:00
|
|
|
|
|
|
|
return animatedMesh;
|
|
|
|
}
|
|
|
|
|
2022-11-10 20:58:28 +01:00
|
|
|
} // namespace scene
|
2022-10-22 23:36:29 +02:00
|
|
|
|
|
|
|
} // namespace irr
|
|
|
|
|