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"
|
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>
|
2023-01-30 00:36:41 +01:00
|
|
|
#include <tuple>
|
2023-05-17 20:07:39 +02:00
|
|
|
#include <utility>
|
2023-01-30 00:36:41 +01:00
|
|
|
#include <vector>
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2023-05-17 20:23:53 +02:00
|
|
|
/* Notes on the coordinate system.
|
|
|
|
*
|
|
|
|
* glTF uses a right-handed coordinate system where +Z is the
|
|
|
|
* front-facing axis, and Irrlicht uses a left-handed coordinate
|
|
|
|
* system where -Z is the front-facing axis.
|
|
|
|
* We convert between them by reflecting the mesh across the X axis.
|
|
|
|
* Doing this correctly requires negating the Z coordinate on
|
|
|
|
* vertex positions and normals, and reversing the winding order
|
|
|
|
* of the vertex indices.
|
|
|
|
*/
|
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
// A helper function to disable tinygltf embedded image loading
|
|
|
|
static bool dummyImageLoader(tinygltf::Image *a,
|
|
|
|
const int b, std::string *c,
|
|
|
|
std::string *d,
|
|
|
|
int e,
|
|
|
|
int f,
|
|
|
|
const unsigned char * g,
|
|
|
|
int h,
|
2023-05-17 20:19:52 +02:00
|
|
|
void *userPointer)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-05-17 16:09:06 +02:00
|
|
|
return false;
|
2022-11-12 14:01:56 +01:00
|
|
|
};
|
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
namespace irr {
|
2022-11-07 01:00:42 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
namespace scene {
|
2022-11-06 14:55:09 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
CGLTFMeshFileLoader::BufferOffset::BufferOffset(
|
|
|
|
const std::vector<unsigned char>& buf,
|
|
|
|
const std::size_t offset)
|
|
|
|
: m_buf(buf)
|
|
|
|
, m_offset(offset)
|
|
|
|
{
|
|
|
|
}
|
2022-11-06 14:55:09 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
CGLTFMeshFileLoader::BufferOffset::BufferOffset(
|
|
|
|
const CGLTFMeshFileLoader::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
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
unsigned char CGLTFMeshFileLoader::BufferOffset::at(
|
|
|
|
const std::size_t fromOffset) const
|
2023-01-28 02:45:02 +01:00
|
|
|
{
|
2023-05-17 16:09:06 +02:00
|
|
|
return m_buf.at(m_offset + fromOffset);
|
|
|
|
}
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
CGLTFMeshFileLoader::CGLTFMeshFileLoader() noexcept
|
2022-11-10 20:58:28 +01:00
|
|
|
{
|
2023-05-17 16:09:06 +02:00
|
|
|
}
|
2022-11-10 20:58:28 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
bool CGLTFMeshFileLoader::isALoadableFileExtension(
|
|
|
|
const io::path& filename) const
|
2022-11-10 20:58:28 +01:00
|
|
|
{
|
2023-05-17 16:09:06 +02:00
|
|
|
return core::hasFileExtension(filename, "gltf");
|
|
|
|
}
|
2022-11-10 20:58:28 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
|
2022-11-10 20:58:28 +01:00
|
|
|
{
|
2023-05-17 23:47:02 +02:00
|
|
|
tinygltf::Model model {};
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
if (file->getSize() <= 0 || !tryParseGLTF(file, model)) {
|
2023-05-17 16:09:06 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 23:47:02 +02:00
|
|
|
ModelParser parser(std::move(model));
|
|
|
|
SMesh* baseMesh(new SMesh {});
|
2022-11-10 20:58:28 +01:00
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
for (std::size_t i = 0; i < parser.getMeshCount(); ++i) {
|
|
|
|
for (std::size_t j = 0; j < parser.getPrimitiveCount(i); ++j) {
|
|
|
|
auto indices = parser.getIndices(i, j);
|
|
|
|
auto vertices = parser.getVertices(i, j);
|
2023-05-17 23:47:02 +02:00
|
|
|
|
|
|
|
SMeshBuffer* meshbuf(new SMeshBuffer {});
|
2023-05-18 00:19:57 +02:00
|
|
|
meshbuf->append(vertices.data(), vertices.size(),
|
2023-05-17 23:47:02 +02:00
|
|
|
indices.data(), indices.size());
|
|
|
|
baseMesh->addMeshBuffer(meshbuf);
|
|
|
|
}
|
2023-01-30 00:36:41 +01:00
|
|
|
}
|
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
SAnimatedMesh* animatedMesh(new SAnimatedMesh {});
|
2023-05-17 23:47:02 +02:00
|
|
|
animatedMesh->addMesh(baseMesh);
|
2023-05-17 16:09:06 +02:00
|
|
|
|
|
|
|
return animatedMesh;
|
2022-11-10 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
CGLTFMeshFileLoader::ModelParser::ModelParser(
|
|
|
|
const tinygltf::Model& model)
|
|
|
|
: m_model(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-17 20:07:39 +02:00
|
|
|
CGLTFMeshFileLoader::ModelParser::ModelParser(
|
|
|
|
const tinygltf::Model&& model)
|
|
|
|
: m_model(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-17 23:47:02 +02:00
|
|
|
std::vector<u16> CGLTFMeshFileLoader::ModelParser::getIndices(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const
|
2023-05-17 18:30:58 +02:00
|
|
|
{
|
2023-05-17 23:47:02 +02:00
|
|
|
auto accessorIdx = getIndicesAccessorIdx(meshIdx, primitiveIdx);
|
|
|
|
auto buf = getBuffer(meshIdx, primitiveIdx, accessorIdx);
|
2023-05-17 18:30:58 +02:00
|
|
|
|
2023-05-17 23:47:02 +02:00
|
|
|
std::vector<u16> indices{};
|
2023-05-18 13:41:36 +02:00
|
|
|
const auto count = getElemCount(accessorIdx);
|
2023-05-17 23:47:02 +02:00
|
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
|
|
std::size_t elemIdx = count - i - 1;
|
|
|
|
indices.push_back(readPrimitive<u16>(
|
|
|
|
BufferOffset(buf, elemIdx * sizeof(u16))));
|
2023-05-17 18:30:58 +02:00
|
|
|
}
|
2023-05-17 23:47:02 +02:00
|
|
|
|
|
|
|
return indices;
|
2023-05-17 18:30:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
std::vector<video::S3DVertex> CGLTFMeshFileLoader::ModelParser::getVertices(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const
|
2023-05-17 18:30:58 +02:00
|
|
|
{
|
2023-05-18 00:19:57 +02:00
|
|
|
auto positionAccessorIdx = getPositionAccessorIdx(meshIdx, primitiveIdx);
|
|
|
|
auto vertexCount = getElemCount(positionAccessorIdx);
|
|
|
|
std::vector<video::S3DVertex> vertices{};
|
|
|
|
vertices.resize(vertexCount);
|
|
|
|
|
|
|
|
Span<video::S3DVertex> outVertices {vertices.data(), vertexCount};
|
2023-05-17 18:30:58 +02:00
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
copyPositions(outVertices, positionAccessorIdx);
|
2023-05-17 18:30:58 +02:00
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
const auto normalsField = m_model.meshes[meshIdx]
|
|
|
|
.primitives[primitiveIdx].attributes.find("NORMAL");
|
|
|
|
|
|
|
|
if (normalsField != m_model.meshes[meshIdx]
|
|
|
|
.primitives[primitiveIdx].attributes.end()) {
|
2023-05-17 18:30:58 +02:00
|
|
|
copyNormals(outVertices, normalsField->second);
|
|
|
|
}
|
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
const auto tCoordsField = m_model.meshes[meshIdx]
|
|
|
|
.primitives[primitiveIdx].attributes.find("TEXCOORD_0");
|
2023-05-17 18:30:58 +02:00
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
if (tCoordsField != m_model.meshes[meshIdx]
|
|
|
|
.primitives[primitiveIdx].attributes.end()) {
|
2023-05-17 18:30:58 +02:00
|
|
|
copyTCoords(outVertices, tCoordsField->second);
|
|
|
|
}
|
2023-05-18 00:19:57 +02:00
|
|
|
|
|
|
|
return vertices;
|
2023-05-17 18:30:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 23:47:02 +02:00
|
|
|
std::size_t CGLTFMeshFileLoader::ModelParser::getMeshCount() const
|
|
|
|
{
|
|
|
|
return m_model.meshes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t CGLTFMeshFileLoader::ModelParser::getPrimitiveCount(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx) const
|
2023-05-17 23:47:02 +02:00
|
|
|
{
|
|
|
|
return m_model.meshes[meshIdx].primitives.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
template <typename T>
|
2023-05-17 18:30:58 +02:00
|
|
|
T CGLTFMeshFileLoader::ModelParser::readPrimitive(
|
|
|
|
const BufferOffset& readFrom)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
core::vector2df CGLTFMeshFileLoader::ModelParser::readVec2DF(
|
2023-05-17 16:09:06 +02:00
|
|
|
const CGLTFMeshFileLoader::BufferOffset& readFrom)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-01-30 00:36:41 +01:00
|
|
|
return core::vector2df(readPrimitive<float>(readFrom),
|
2023-05-17 16:09:06 +02:00
|
|
|
readPrimitive<float>(BufferOffset(readFrom, sizeof(float))));
|
2022-11-12 14:01:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
core::vector3df CGLTFMeshFileLoader::ModelParser::readVec3DF(
|
|
|
|
const BufferOffset& readFrom,
|
2022-11-12 14:43:44 +01:00
|
|
|
const float scale = 1.0f)
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
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))),
|
2023-01-30 00:36:41 +01:00
|
|
|
-scale * readPrimitive<float>(BufferOffset(readFrom, 2 *
|
|
|
|
sizeof(float))));
|
2022-11-10 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
void CGLTFMeshFileLoader::ModelParser::copyPositions(
|
2023-05-17 16:09:06 +02:00
|
|
|
const Span<video::S3DVertex> vertices,
|
2023-05-17 18:30:58 +02:00
|
|
|
const std::size_t accessorId) const
|
2022-11-10 20:58:28 +01:00
|
|
|
{
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
const auto& view = m_model.bufferViews[
|
|
|
|
m_model.accessors[accessorId].bufferView];
|
|
|
|
const auto& buffer = m_model.buffers[view.buffer];
|
|
|
|
const auto count = m_model.accessors[accessorId].count;
|
2023-01-28 02:45:02 +01:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < count; i++) {
|
2023-01-30 00:36:41 +01:00
|
|
|
const auto v = readVec3DF(BufferOffset(
|
2023-05-17 18:30:58 +02:00
|
|
|
buffer.data,
|
|
|
|
view.byteOffset + (3 * sizeof(float) * i)),
|
|
|
|
getScale());
|
2022-11-12 14:01:56 +01:00
|
|
|
vertices.buffer[i].Pos = v;
|
|
|
|
}
|
|
|
|
}
|
2022-11-12 00:37:21 +01:00
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
void CGLTFMeshFileLoader::ModelParser::copyNormals(
|
2023-05-17 16:09:06 +02:00
|
|
|
const Span<video::S3DVertex> vertices,
|
2023-05-17 18:30:58 +02:00
|
|
|
const std::size_t accessorId) const
|
2022-11-12 14:43:44 +01:00
|
|
|
{
|
2023-05-17 18:30:58 +02:00
|
|
|
const auto& view = m_model.bufferViews[
|
|
|
|
m_model.accessors[accessorId].bufferView];
|
|
|
|
const auto& buffer = m_model.buffers[view.buffer];
|
|
|
|
const auto count = m_model.accessors[accessorId].count;
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-01-30 00:36:41 +01:00
|
|
|
for (std::size_t i = 0; i < count; i++) {
|
2023-05-17 23:47:02 +02:00
|
|
|
const auto n = readVec3DF(BufferOffset(buffer.data,
|
2023-01-30 00:36:41 +01:00
|
|
|
view.byteOffset + 3 * sizeof(float) * i ));
|
2022-11-12 14:43:44 +01:00
|
|
|
vertices.buffer[i].Normal = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
void CGLTFMeshFileLoader::ModelParser::copyTCoords(
|
2023-05-17 16:09:06 +02:00
|
|
|
const Span<video::S3DVertex> vertices,
|
2023-05-17 18:30:58 +02:00
|
|
|
const std::size_t accessorId) const
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-05-17 18:30:58 +02:00
|
|
|
const auto& view = m_model.bufferViews[
|
|
|
|
m_model.accessors[accessorId].bufferView];
|
|
|
|
const auto& buffer = m_model.buffers[view.buffer];
|
|
|
|
const auto count = m_model.accessors[accessorId].count;
|
2023-01-28 02:45:02 +01:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < count; ++i) {
|
2023-01-30 00:36:41 +01:00
|
|
|
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-05-17 18:30:58 +02:00
|
|
|
float CGLTFMeshFileLoader::ModelParser::getScale() const
|
2022-11-12 14:01:56 +01:00
|
|
|
{
|
2023-05-17 18:30:58 +02:00
|
|
|
if (m_model.nodes[0].scale.size() > 0) {
|
|
|
|
return static_cast<float>(m_model.nodes[0].scale[0]);
|
2022-11-12 00:37:21 +01:00
|
|
|
}
|
2022-11-10 20:58:28 +01:00
|
|
|
|
2023-05-17 18:30:58 +02:00
|
|
|
return 1.0f;
|
2022-11-07 01:00:42 +01:00
|
|
|
}
|
|
|
|
|
2023-05-17 23:47:02 +02:00
|
|
|
std::size_t CGLTFMeshFileLoader::ModelParser::getElemCount(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t accessorIdx) const
|
2023-05-17 23:47:02 +02:00
|
|
|
{
|
|
|
|
return m_model.accessors[accessorIdx].count;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGLTFMeshFileLoader::BufferOffset CGLTFMeshFileLoader::ModelParser::getBuffer(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx,
|
|
|
|
const std::size_t accessorIdx) const
|
2023-05-17 23:47:02 +02:00
|
|
|
{
|
|
|
|
const auto& accessor = m_model.accessors[accessorIdx];
|
|
|
|
const auto& view = m_model.bufferViews[accessor.bufferView];
|
|
|
|
const auto& buffer = m_model.buffers[view.buffer];
|
|
|
|
|
|
|
|
return BufferOffset(buffer.data, view.byteOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t CGLTFMeshFileLoader::ModelParser::getIndicesAccessorIdx(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const
|
2023-05-17 23:47:02 +02:00
|
|
|
{
|
|
|
|
return m_model.meshes[meshIdx].primitives[primitiveIdx].indices;
|
|
|
|
}
|
|
|
|
|
2023-05-18 00:19:57 +02:00
|
|
|
std::size_t CGLTFMeshFileLoader::ModelParser::getPositionAccessorIdx(
|
2023-05-18 13:41:36 +02:00
|
|
|
const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const
|
2023-05-18 00:19:57 +02:00
|
|
|
{
|
|
|
|
return m_model.meshes[meshIdx].primitives[primitiveIdx]
|
|
|
|
.attributes.find("POSITION")->second;
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file,
|
|
|
|
tinygltf::Model& model)
|
2022-10-22 23:36:29 +02:00
|
|
|
{
|
2023-05-17 16:09:06 +02:00
|
|
|
tinygltf::TinyGLTF loader {};
|
2023-01-30 00:36:41 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
// Stop embedded textures from making model fail to load
|
|
|
|
loader.SetImageLoader(dummyImageLoader, nullptr);
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
std::string err {};
|
|
|
|
std::string warn {};
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
auto buf = std::make_unique<char[]>(file->getSize());
|
|
|
|
file->read(buf.get(), file->getSize());
|
2023-01-28 02:45:02 +01:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
if (err != "") {
|
|
|
|
return false;
|
2023-01-28 02:45:02 +01:00
|
|
|
}
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2023-05-17 16:09:06 +02:00
|
|
|
return loader.LoadASCIIFromString(&model, &err, &warn, buf.get(),
|
|
|
|
file->getSize(), "", 1);
|
|
|
|
}
|
2022-10-22 23:36:29 +02:00
|
|
|
|
2022-11-10 20:58:28 +01:00
|
|
|
} // namespace scene
|
2022-10-22 23:36:29 +02:00
|
|
|
|
|
|
|
} // namespace irr
|
|
|
|
|