diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cd2e7336..0397c3be5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -283,6 +283,8 @@ if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) add_subdirectory(lib/catch2) endif() +add_subdirectory(lib/tiniergltf) + # Subdirectories # Be sure to add all relevant definitions above this add_subdirectory(src) diff --git a/doc/lua_api.md b/doc/lua_api.md index b1412ca85..eece7ebab 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -273,7 +273,7 @@ Accepted formats are: images: .png, .jpg, .tga, (deprecated:) .bmp sounds: .ogg vorbis - models: .x, .b3d, .obj + models: .x, .b3d, .obj, (5.9+:) .gltf Other formats won't be sent to the client (e.g. you can store .blend files in a folder for convenience, without the risk that such files are transferred) diff --git a/irr/include/IMesh.h b/irr/include/IMesh.h index 6d06eb762..0b458a7f2 100644 --- a/irr/include/IMesh.h +++ b/irr/include/IMesh.h @@ -52,6 +52,9 @@ enum E_ANIMATED_MESH_TYPE //! Halflife MDL model file EAMT_MDL_HALFLIFE, + //! Graphics Language Transmission Format 2.0 (.gltf) mesh + EAMT_GLTF2, + //! generic skinned mesh EAMT_SKINNED, diff --git a/irr/include/SSkinMeshBuffer.h b/irr/include/SSkinMeshBuffer.h index 5ced6057d..47ec4b2b6 100644 --- a/irr/include/SSkinMeshBuffer.h +++ b/irr/include/SSkinMeshBuffer.h @@ -301,7 +301,26 @@ struct SSkinMeshBuffer : public IMeshBuffer } //! append the vertices and indices to the current buffer - void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override {} + void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) override { + if (vertices == getVertices()) + throw std::logic_error("can't append own vertices"); + + if (VertexType != video::EVT_STANDARD) + throw std::logic_error("invalid vertex type"); + + const u32 prevVertexCount = getVertexCount(); + + Vertices_Standard.reallocate(prevVertexCount + numVertices); + for (u32 i=0; i < numVertices; ++i) { + Vertices_Standard.push_back(static_cast(vertices)[i]); + BoundingBox.addInternalPoint(static_cast(vertices)[i].Pos); + } + + Indices.reallocate(getIndexCount() + numIndices); + for (u32 i=0; i < numIndices; ++i) { + Indices.push_back(indices[i] + prevVertexCount); + } + } //! get the current hardware mapping hint for vertex buffers E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override diff --git a/irr/src/CGLTFMeshFileLoader.cpp b/irr/src/CGLTFMeshFileLoader.cpp new file mode 100644 index 000000000..8747cb8bb --- /dev/null +++ b/irr/src/CGLTFMeshFileLoader.cpp @@ -0,0 +1,610 @@ +#include "CGLTFMeshFileLoader.h" + +#include "coreutil.h" +#include "CSkinnedMesh.h" +#include "ISkinnedMesh.h" +#include "irrTypes.h" +#include "IReadFile.h" +#include "matrix4.h" +#include "path.h" +#include "S3DVertex.h" +#include "quaternion.h" +#include "vector3d.h" + +#include "tiniergltf.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* 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. + */ + +namespace irr { + +namespace scene { + +CGLTFMeshFileLoader::BufferOffset::BufferOffset( + const std::vector& buf, + const std::size_t offset) + : m_buf(buf) + , m_offset(offset) +{ +} + +CGLTFMeshFileLoader::BufferOffset::BufferOffset( + const CGLTFMeshFileLoader::BufferOffset& other, + const std::size_t fromOffset) + : m_buf(other.m_buf) + , m_offset(other.m_offset + fromOffset) +{ +} + +/** + * Get a raw unsigned char (ubyte) from a buffer offset. +*/ +unsigned char CGLTFMeshFileLoader::BufferOffset::at( + const std::size_t fromOffset) const +{ + return m_buf.at(m_offset + fromOffset); +} + +CGLTFMeshFileLoader::CGLTFMeshFileLoader() noexcept +{ +} + +/** + * The most basic portion of the code base. This tells irllicht if this file has a .gltf extension. +*/ +bool CGLTFMeshFileLoader::isALoadableFileExtension( + const io::path& filename) const +{ + return core::hasFileExtension(filename, "gltf"); +} + +/** + * Entry point into loading a GLTF model. +*/ +IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file) +{ + if (file->getSize() <= 0) { + return nullptr; + } + std::optional model = tryParseGLTF(file); + if (!model.has_value()) { + return nullptr; + } + + if (!(model->buffers.has_value() + && model->bufferViews.has_value() + && model->accessors.has_value() + && model->meshes.has_value() + && model->nodes.has_value())) { + return nullptr; + } + + ISkinnedMesh *mesh = new CSkinnedMesh(); + MeshExtractor parser(std::move(model.value()), mesh); + try { + parser.loadNodes(); + } catch (std::runtime_error &e) { + mesh->drop(); + return nullptr; + } + return mesh; +} + +static void transformVertices(std::vector &vertices, const core::matrix4 &transform) +{ + for (auto &vertex : vertices) { + // Apply scaling, rotation and rotation (in that order) to the position. + transform.transformVect(vertex.Pos); + // For the normal, we do not want to apply the translation. + // TODO note that this also applies scaling; the Irrlicht method is misnamed. + transform.rotateVect(vertex.Normal); + // Renormalize (length might have been affected by scaling). + vertex.Normal.normalize(); + } +} + +static void checkIndices(const std::vector &indices, const std::size_t nVerts) +{ + for (u16 index : indices) { + if (index >= nVerts) + throw std::runtime_error("index out of bounds"); + } +} + +static std::vector generateIndices(const std::size_t nVerts) +{ + std::vector indices(nVerts); + for (std::size_t i = 0; i < nVerts; i += 3) { + // Reverse winding order per triangle + indices[i] = i + 2; + indices[i + 1] = i + 1; + indices[i + 2] = i; + } + return indices; +} + +/** + * Load up the rawest form of the model. The vertex positions and indices. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes + * If material is undefined, then a default material MUST be used. +*/ +void CGLTFMeshFileLoader::MeshExtractor::loadMesh( + const std::size_t meshIdx, + ISkinnedMesh::SJoint *parent) const +{ + for (std::size_t j = 0; j < getPrimitiveCount(meshIdx); ++j) { + auto vertices = getVertices(meshIdx, j); + if (!vertices.has_value()) + continue; // "When positions are not specified, client implementations SHOULD skip primitive’s rendering" + + // Excludes the max value for consistency. + if (vertices->size() >= std::numeric_limits::max()) + throw std::runtime_error("too many vertices"); + + // Apply the global transform along the parent chain. + transformVertices(*vertices, parent->GlobalMatrix); + + auto maybeIndices = getIndices(meshIdx, j); + std::vector indices; + if (maybeIndices.has_value()) { + indices = std::move(*maybeIndices); + checkIndices(indices, vertices->size()); + } else { + // Non-indexed geometry + indices = generateIndices(vertices->size()); + } + + auto *meshbuf = m_irr_model->addMeshBuffer(); + meshbuf->append(vertices->data(), vertices->size(), + indices.data(), indices.size()); + } +} + +// Base transformation between left & right handed coordinate systems. +// This just inverts the Z axis. +static core::matrix4 leftToRight = core::matrix4( + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, -1, 0, + 0, 0, 0, 1 +); +static core::matrix4 rightToLeft = leftToRight; + +static core::matrix4 loadTransform(const tiniergltf::Node::Matrix &m) +{ + // Note: Under the hood, this casts these doubles to floats. + return core::matrix4( + m[0], m[1], m[2], m[3], + m[4], m[5], m[6], m[7], + m[8], m[9], m[10], m[11], + m[12], m[13], m[14], m[15]); +} + +static core::matrix4 loadTransform(const tiniergltf::Node::TRS &trs) +{ + const auto &trans = trs.translation; + const auto &rot = trs.rotation; + const auto &scale = trs.scale; + core::matrix4 transMat; + transMat.setTranslation(core::vector3df(trans[0], trans[1], trans[2])); + core::matrix4 rotMat = core::quaternion(rot[0], rot[1], rot[2], rot[3]).getMatrix(); + core::matrix4 scaleMat; + scaleMat.setScale(core::vector3df(scale[0], scale[1], scale[2])); + return transMat * rotMat * scaleMat; +} + +static core::matrix4 loadTransform(std::optional> transform) { + if (!transform.has_value()) { + return core::matrix4(); + } + core::matrix4 mat = std::visit([](const auto &t) { return loadTransform(t); }, *transform); + return rightToLeft * mat * leftToRight; +} + +void CGLTFMeshFileLoader::MeshExtractor::loadNode( + const std::size_t nodeIdx, + ISkinnedMesh::SJoint *parent) const +{ + const auto &node = m_gltf_model.nodes->at(nodeIdx); + auto *joint = m_irr_model->addJoint(parent); + const core::matrix4 transform = loadTransform(node.transform); + joint->LocalMatrix = transform; + joint->GlobalMatrix = parent ? parent->GlobalMatrix * joint->LocalMatrix : joint->LocalMatrix; + if (node.name.has_value()) { + joint->Name = node.name->c_str(); + } + if (node.mesh.has_value()) { + loadMesh(*node.mesh, joint); + } + if (node.children.has_value()) { + for (const auto &child : *node.children) { + loadNode(child, joint); + } + } +} + +void CGLTFMeshFileLoader::MeshExtractor::loadNodes() const +{ + std::vector isChild(m_gltf_model.nodes->size()); + for (const auto &node : *m_gltf_model.nodes) { + if (!node.children.has_value()) + continue; + for (const auto &child : *node.children) { + isChild[child] = true; + } + } + // Load all nodes that aren't children. + // Children will be loaded by their parent nodes. + for (std::size_t i = 0; i < m_gltf_model.nodes->size(); ++i) { + if (!isChild[i]) { + loadNode(i, nullptr); + } + } +} + +/** + * Extracts GLTF mesh indices into the irrlicht model. +*/ +std::optional> CGLTFMeshFileLoader::MeshExtractor::getIndices( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + const auto accessorIdx = getIndicesAccessorIdx(meshIdx, primitiveIdx); + if (!accessorIdx.has_value()) + return std::nullopt; // non-indexed geometry + const auto &accessor = m_gltf_model.accessors->at(accessorIdx.value()); + + const auto& buf = getBuffer(accessorIdx.value()); + + std::vector indices{}; + const auto count = getElemCount(accessorIdx.value()); + for (std::size_t i = 0; i < count; ++i) { + std::size_t elemIdx = count - i - 1; // reverse index order + u16 index; + // Note: glTF forbids the max value for each component type. + switch (accessor.componentType) { + case tiniergltf::Accessor::ComponentType::UNSIGNED_BYTE: { + index = readPrimitive(BufferOffset(buf, elemIdx * sizeof(u8))); + if (index == std::numeric_limits::max()) + throw std::runtime_error("invalid index"); + break; + } + case tiniergltf::Accessor::ComponentType::UNSIGNED_SHORT: { + index = readPrimitive(BufferOffset(buf, elemIdx * sizeof(u16))); + if (index == std::numeric_limits::max()) + throw std::runtime_error("invalid index"); + break; + } + case tiniergltf::Accessor::ComponentType::UNSIGNED_INT: { + u32 indexWide = readPrimitive(BufferOffset(buf, elemIdx * sizeof(u32))); + // Use >= here for consistency. + if (indexWide >= std::numeric_limits::max()) + throw std::runtime_error("index too large (>= 65536)"); + index = static_cast(indexWide); + break; + } + default: + throw std::runtime_error("invalid index component type"); + } + indices.push_back(index); + } + + return indices; +} + +/** + * Create a vector of video::S3DVertex (model data) from a mesh & primitive index. +*/ +std::optional> CGLTFMeshFileLoader::MeshExtractor::getVertices( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + const auto positionAccessorIdx = getPositionAccessorIdx( + meshIdx, primitiveIdx); + if (!positionAccessorIdx.has_value()) { + // "When positions are not specified, client implementations SHOULD skip primitive's rendering" + return std::nullopt; + } + + std::vector vertices{}; + vertices.resize(getElemCount(*positionAccessorIdx)); + copyPositions(*positionAccessorIdx, vertices); + + const auto normalAccessorIdx = getNormalAccessorIdx( + meshIdx, primitiveIdx); + if (normalAccessorIdx.has_value()) { + copyNormals(normalAccessorIdx.value(), vertices); + } + + const auto tCoordAccessorIdx = getTCoordAccessorIdx( + meshIdx, primitiveIdx); + if (tCoordAccessorIdx.has_value()) { + copyTCoords(tCoordAccessorIdx.value(), vertices); + } + + return vertices; +} + +/** + * Get the amount of meshes that a model contains. +*/ +std::size_t CGLTFMeshFileLoader::MeshExtractor::getMeshCount() const +{ + return m_gltf_model.meshes->size(); +} + +/** + * Get the amount of primitives that a mesh in a model contains. +*/ +std::size_t CGLTFMeshFileLoader::MeshExtractor::getPrimitiveCount( + const std::size_t meshIdx) const +{ + return m_gltf_model.meshes->at(meshIdx).primitives.size(); +} + +/** + * Templated buffer reader. Based on type width. + * This is specifically used to build upon to read more complex data types. + * It is also used raw to read arrays directly. + * Basically we're using the width of the type to infer + * how big of a gap we have from the beginning of the buffer. +*/ +template +T CGLTFMeshFileLoader::MeshExtractor::readPrimitive( + const BufferOffset& readFrom) +{ + unsigned char d[sizeof(T)]{}; + for (std::size_t i = 0; i < sizeof(T); ++i) { + d[i] = readFrom.at(i); + } + T dest; + std::memcpy(&dest, d, sizeof(dest)); + return dest; +} + +/** + * Read a vector2df from a buffer at an offset. + * @return vec2 core::Vector2df +*/ +core::vector2df CGLTFMeshFileLoader::MeshExtractor::readVec2DF( + const CGLTFMeshFileLoader::BufferOffset& readFrom) +{ + return core::vector2df(readPrimitive(readFrom), + readPrimitive(BufferOffset(readFrom, sizeof(float)))); + +} + +/** + * Read a vector3df from a buffer at an offset. + * Also does right-to-left-handed coordinate system conversion (inverts Z axis). + * @return vec3 core::Vector3df +*/ +core::vector3df CGLTFMeshFileLoader::MeshExtractor::readVec3DF( + const BufferOffset& readFrom, + const core::vector3df scale = {1.0f,1.0f,1.0f}) +{ + return core::vector3df( + readPrimitive(readFrom), + readPrimitive(BufferOffset(readFrom, sizeof(float))), + -readPrimitive(BufferOffset(readFrom, 2 * + sizeof(float)))); +} + +/** + * Streams vertex positions raw data into usable buffer via reference. + * Buffer: ref Vector +*/ +void CGLTFMeshFileLoader::MeshExtractor::copyPositions( + const std::size_t accessorIdx, + std::vector& vertices) const +{ + + const auto& buffer = getBuffer(accessorIdx); + const auto count = getElemCount(accessorIdx); + const auto byteStride = getByteStride(accessorIdx); + + for (std::size_t i = 0; i < count; i++) { + const auto v = readVec3DF(BufferOffset(buffer, byteStride * i)); + vertices[i].Pos = v; + } +} + +/** + * Streams normals raw data into usable buffer via reference. + * Buffer: ref Vector +*/ +void CGLTFMeshFileLoader::MeshExtractor::copyNormals( + const std::size_t accessorIdx, + std::vector& vertices) const +{ + const auto& buffer = getBuffer(accessorIdx); + const auto count = getElemCount(accessorIdx); + + for (std::size_t i = 0; i < count; i++) { + const auto n = readVec3DF(BufferOffset(buffer, + 3 * sizeof(float) * i)); + vertices[i].Normal = n; + } +} + +/** + * Streams texture coordinate raw data into usable buffer via reference. + * Buffer: ref Vector +*/ +void CGLTFMeshFileLoader::MeshExtractor::copyTCoords( + const std::size_t accessorIdx, + std::vector& vertices) const +{ + + const auto& buffer = getBuffer(accessorIdx); + const auto count = getElemCount(accessorIdx); + + for (std::size_t i = 0; i < count; ++i) { + const auto t = readVec2DF(BufferOffset(buffer, + 2 * sizeof(float) * i)); + vertices[i].TCoords = t; + } +} + +/** + * The number of elements referenced by this accessor, not to be confused with the number of bytes or number of components. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_accessor_count + * Type: Integer + * Required: YES +*/ +std::size_t CGLTFMeshFileLoader::MeshExtractor::getElemCount( + const std::size_t accessorIdx) const +{ + return m_gltf_model.accessors->at(accessorIdx).count; +} + +/** + * The stride, in bytes, between vertex attributes. + * When this is not defined, data is tightly packed. + * When two or more accessors use the same buffer view, this field MUST be defined. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_bufferview_bytestride + * Required: NO +*/ +std::size_t CGLTFMeshFileLoader::MeshExtractor::getByteStride( + const std::size_t accessorIdx) const +{ + const auto& accessor = m_gltf_model.accessors->at(accessorIdx); + // FIXME this does not work with sparse / zero-initialized accessors + const auto& view = m_gltf_model.bufferViews->at(accessor.bufferView.value()); + return view.byteStride.value_or(accessor.elementSize()); +} + +/** + * Specifies whether integer data values are normalized (true) to [0, 1] (for unsigned types) + * or to [-1, 1] (for signed types) when they are accessed. This property MUST NOT be set to + * true for accessors with FLOAT or UNSIGNED_INT component type. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_accessor_normalized + * Required: NO +*/ +bool CGLTFMeshFileLoader::MeshExtractor::isAccessorNormalized( + const std::size_t accessorIdx) const +{ + const auto& accessor = m_gltf_model.accessors->at(accessorIdx); + return accessor.normalized; +} + +/** + * Walk through the complex chain of the model to extract the required buffer. + * Accessor -> BufferView -> Buffer +*/ +CGLTFMeshFileLoader::BufferOffset CGLTFMeshFileLoader::MeshExtractor::getBuffer( + const std::size_t accessorIdx) const +{ + const auto& accessor = m_gltf_model.accessors->at(accessorIdx); + // FIXME this does not work with sparse / zero-initialized accessors + const auto& view = m_gltf_model.bufferViews->at(accessor.bufferView.value()); + const auto& buffer = m_gltf_model.buffers->at(view.buffer); + + return BufferOffset(buffer.data, view.byteOffset); +} + +/** + * The index of the accessor that contains the vertex indices. + * When this is undefined, the primitive defines non-indexed geometry. + * When defined, the accessor MUST have SCALAR type and an unsigned integer component type. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_mesh_primitive_indices + * Type: Integer + * Required: NO +*/ +std::optional CGLTFMeshFileLoader::MeshExtractor::getIndicesAccessorIdx( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + return m_gltf_model.meshes->at(meshIdx).primitives[primitiveIdx].indices; +} + +/** + * The index of the accessor that contains the POSITIONs. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview + * Type: VEC3 (Float) +*/ +std::optional CGLTFMeshFileLoader::MeshExtractor::getPositionAccessorIdx( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + return m_gltf_model.meshes->at(meshIdx).primitives[primitiveIdx].attributes.position; +} + +/** + * The index of the accessor that contains the NORMALs. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview + * Type: VEC3 (Float) + * ! Required: NO (Appears to not be, needs another pair of eyes to research.) +*/ +std::optional CGLTFMeshFileLoader::MeshExtractor::getNormalAccessorIdx( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + return m_gltf_model.meshes->at(meshIdx).primitives[primitiveIdx].attributes.normal; +} + +/** + * The index of the accessor that contains the TEXCOORDs. + * Documentation: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview + * Type: VEC3 (Float) + * ! Required: YES (Appears so, needs another pair of eyes to research.) +*/ +std::optional CGLTFMeshFileLoader::MeshExtractor::getTCoordAccessorIdx( + const std::size_t meshIdx, + const std::size_t primitiveIdx) const +{ + const auto& texcoords = m_gltf_model.meshes->at(meshIdx).primitives[primitiveIdx].attributes.texcoord; + if (!texcoords.has_value()) + return std::nullopt; + return texcoords->at(0); +} + +/** + * This is where the actual model's GLTF file is loaded and parsed by tiniergltf. +*/ +std::optional CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file) +{ + auto size = file->getSize(); + auto buf = std::make_unique(size + 1); + file->read(buf.get(), size); + // We probably don't need this, but add it just to be sure. + buf[size] = '\0'; + Json::CharReaderBuilder builder; + const std::unique_ptr reader(builder.newCharReader()); + Json::Value json; + JSONCPP_STRING err; + if (!reader->parse(buf.get(), buf.get() + size, &json, &err)) { + return std::nullopt; + } + try { + return tiniergltf::GlTF(json); + } catch (const std::runtime_error &e) { + return std::nullopt; + } catch (const std::out_of_range &e) { + return std::nullopt; + } +} + +} // namespace scene + +} // namespace irr + diff --git a/irr/src/CGLTFMeshFileLoader.h b/irr/src/CGLTFMeshFileLoader.h new file mode 100644 index 000000000..a4798c490 --- /dev/null +++ b/irr/src/CGLTFMeshFileLoader.h @@ -0,0 +1,145 @@ +#ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__ +#define __C_GLTF_MESH_FILE_LOADER_INCLUDED__ + +#include "ISkinnedMesh.h" +#include "IMeshLoader.h" +#include "IReadFile.h" +#include "irrTypes.h" +#include "path.h" +#include "S3DVertex.h" +#include "vector2d.h" +#include "vector3d.h" + +#include + +#include +#include + +namespace irr +{ + +namespace scene +{ + +class CGLTFMeshFileLoader : public IMeshLoader +{ +public: + CGLTFMeshFileLoader() noexcept; + + bool isALoadableFileExtension(const io::path& filename) const override; + + IAnimatedMesh* createMesh(io::IReadFile* file) override; + +private: + class BufferOffset + { + public: + BufferOffset(const std::vector& buf, + const std::size_t offset); + + BufferOffset(const BufferOffset& other, + const std::size_t fromOffset); + + unsigned char at(const std::size_t fromOffset) const; + private: + const std::vector& m_buf; + std::size_t m_offset; + }; + + class MeshExtractor { + public: + using vertex_t = video::S3DVertex; + + MeshExtractor(const tiniergltf::GlTF &model, + ISkinnedMesh *mesh) noexcept + : m_gltf_model(model), m_irr_model(mesh) {}; + + MeshExtractor(const tiniergltf::GlTF &&model, + ISkinnedMesh *mesh) noexcept + : m_gltf_model(model), m_irr_model(mesh) {}; + + /* Gets indices for the given mesh/primitive. + * + * Values are return in Irrlicht winding order. + */ + std::optional> getIndices(const std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + std::optional> getVertices(std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + std::size_t getMeshCount() const; + + std::size_t getPrimitiveCount(const std::size_t meshIdx) const; + + void loadNodes() const; + + private: + const tiniergltf::GlTF m_gltf_model; + ISkinnedMesh *m_irr_model; + + template + static T readPrimitive(const BufferOffset& readFrom); + + static core::vector2df readVec2DF( + const BufferOffset& readFrom); + + /* Read a vec3df from a buffer with transformations applied. + * + * Values are returned in Irrlicht coordinates. + */ + static core::vector3df readVec3DF( + const BufferOffset& readFrom, + const core::vector3df scale); + + void copyPositions(const std::size_t accessorIdx, + std::vector& vertices) const; + + void copyNormals(const std::size_t accessorIdx, + std::vector& vertices) const; + + void copyTCoords(const std::size_t accessorIdx, + std::vector& vertices) const; + + std::size_t getElemCount(const std::size_t accessorIdx) const; + + std::size_t getByteStride(const std::size_t accessorIdx) const; + + bool isAccessorNormalized(const std::size_t accessorIdx) const; + + BufferOffset getBuffer(const std::size_t accessorIdx) const; + + std::optional getIndicesAccessorIdx(const std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + std::optional getPositionAccessorIdx(const std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + /* Get the accessor id of the normals of a primitive. + */ + std::optional getNormalAccessorIdx(const std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + /* Get the accessor id for the tcoords of a primitive. + */ + std::optional getTCoordAccessorIdx(const std::size_t meshIdx, + const std::size_t primitiveIdx) const; + + void loadMesh( + std::size_t meshIdx, + ISkinnedMesh::SJoint *parentJoint) const; + + void loadNode( + const std::size_t nodeIdx, + ISkinnedMesh::SJoint *parentJoint) const; + }; + + std::optional tryParseGLTF(io::IReadFile* file); +}; + +} // namespace scene + +} // namespace irr + +#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__ + diff --git a/irr/src/CMakeLists.txt b/irr/src/CMakeLists.txt index daff63bc2..86ac3a8fc 100644 --- a/irr/src/CMakeLists.txt +++ b/irr/src/CMakeLists.txt @@ -15,7 +15,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$") set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_DEBUG "-g") - add_compile_options(-Wall -pipe -fno-exceptions) + add_compile_options(-Wall -pipe -fno-rtti) # Enable SSE for floating point math on 32-bit x86 by default # reasoning see minetest issue #11810 and https://gcc.gnu.org/wiki/FloatingPointMath @@ -314,6 +314,7 @@ set(link_includes set(IRRMESHLOADER CB3DMeshFileLoader.cpp + CGLTFMeshFileLoader.cpp COBJMeshFileLoader.cpp CXMeshFileLoader.cpp ) @@ -326,6 +327,8 @@ add_library(IRRMESHOBJ OBJECT ${IRRMESHLOADER} ) +target_link_libraries(IRRMESHOBJ PUBLIC tiniergltf::tiniergltf) + add_library(IRROBJ OBJECT CBillboardSceneNode.cpp CCameraSceneNode.cpp @@ -337,6 +340,8 @@ add_library(IRROBJ OBJECT CMeshCache.cpp ) +target_link_libraries(IRROBJ PRIVATE IRRMESHOBJ) + set(IRRDRVROBJ CNullDriver.cpp CGLXManager.cpp @@ -501,6 +506,7 @@ target_include_directories(IrrlichtMt # this needs to be here and not in a variable (like link_includes) due to issues # with the generator expressions on at least CMake 3.22, but not 3.28 or later target_link_libraries(IrrlichtMt PRIVATE + tiniergltf::tiniergltf ${ZLIB_LIBRARY} ${JPEG_LIBRARY} ${PNG_LIBRARY} diff --git a/irr/src/CSceneManager.cpp b/irr/src/CSceneManager.cpp index b20f6010a..568de040a 100644 --- a/irr/src/CSceneManager.cpp +++ b/irr/src/CSceneManager.cpp @@ -18,6 +18,7 @@ #include "CXMeshFileLoader.h" #include "COBJMeshFileLoader.h" #include "CB3DMeshFileLoader.h" +#include "CGLTFMeshFileLoader.h" #include "CBillboardSceneNode.h" #include "CAnimatedMeshSceneNode.h" #include "CCameraSceneNode.h" @@ -76,6 +77,7 @@ CSceneManager::CSceneManager(video::IVideoDriver *driver, MeshLoaderList.push_back(new CXMeshFileLoader(this)); MeshLoaderList.push_back(new COBJMeshFileLoader(this)); MeshLoaderList.push_back(new CB3DMeshFileLoader(this)); + MeshLoaderList.push_back(new CGLTFMeshFileLoader()); } //! destructor diff --git a/irr/src/tests/CMakeLists.txt b/irr/src/tests/CMakeLists.txt new file mode 100644 index 000000000..811b0c122 --- /dev/null +++ b/irr/src/tests/CMakeLists.txt @@ -0,0 +1,33 @@ +add_executable(tests + testCGLTFMeshFileLoader.cpp + "${PROJECT_SOURCE_DIR}/source/Irrlicht/CReadFile.cpp" +) + +set_target_properties(tests PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +target_compile_options(tests + PRIVATE + "$<$:-Wall>" +) + +target_include_directories(tests + PRIVATE + # For CReadFile + "${PROJECT_SOURCE_DIR}/source/Irrlicht" +) + +target_link_libraries(tests + PRIVATE + Catch2::Catch + IrrlichtMt::IrrlichtMt +) + +add_test( + NAME tests + COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" +) diff --git a/irr/src/tests/assets/blender_cube.gltf b/irr/src/tests/assets/blender_cube.gltf new file mode 100644 index 000000000..d3c4cc32d --- /dev/null +++ b/irr/src/tests/assets/blender_cube.gltf @@ -0,0 +1,105 @@ +{ + "asset" : { + "generator" : "Khronos glTF Blender I/O v1.7.33", + "version" : "2.0" + }, + "scene" : 0, + "scenes" : [ + { + "name" : "Scene", + "nodes" : [ + 0 + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "Cube", + "scale" : [ + 10, + 10, + 10 + ] + } + ], + "meshes" : [ + { + "name" : "Cube.004", + "primitives" : [ + { + "attributes" : { + "POSITION" : 0, + "NORMAL" : 1, + "TEXCOORD_0" : 2 + }, + "indices" : 3 + } + ] + } + ], + "accessors" : [ + { + "bufferView" : 0, + "componentType" : 5126, + "count" : 24, + "max" : [ + 1, + 1, + 1 + ], + "min" : [ + -1, + -1, + -1 + ], + "type" : "VEC3" + }, + { + "bufferView" : 1, + "componentType" : 5126, + "count" : 24, + "type" : "VEC3" + }, + { + "bufferView" : 2, + "componentType" : 5126, + "count" : 24, + "type" : "VEC2" + }, + { + "bufferView" : 3, + "componentType" : 5123, + "count" : 36, + "type" : "SCALAR" + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 0 + }, + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 288 + }, + { + "buffer" : 0, + "byteLength" : 192, + "byteOffset" : 576 + }, + { + "buffer" : 0, + "byteLength" : 72, + "byteOffset" : 768 + } + ], + "buffers" : [ + { + "byteLength" : 840, + "uri" : "data:application/octet-stream;base64,AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAvwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAvwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAPwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAPwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAADAPgAAgD8AAAA+AACAPgAAwD4AAAAAAAAgPwAAgD8AACA/AAAAAAAAYD8AAIA+AADAPgAAQD8AAAA+AAAAPwAAwD4AAEA/AAAgPwAAQD8AACA/AABAPwAAYD8AAAA/AADAPgAAgD4AAMA+AACAPgAAwD4AAIA+AAAgPwAAgD4AACA/AACAPgAAID8AAIA+AADAPgAAAD8AAMA+AAAAPwAAwD4AAAA/AAAgPwAAAD8AACA/AAAAPwAAID8AAAA/AAADAAkAAAAJAAYACAAKABUACAAVABMAFAAXABEAFAARAA4ADQAPAAQADQAEAAIABwASAAwABwAMAAEAFgALAAUAFgAFABAA" + } + ] +} diff --git a/irr/src/tests/assets/blender_cube_matrix_transform.gltf b/irr/src/tests/assets/blender_cube_matrix_transform.gltf new file mode 100644 index 000000000..aefd46fa4 --- /dev/null +++ b/irr/src/tests/assets/blender_cube_matrix_transform.gltf @@ -0,0 +1,106 @@ +{ + "asset" : { + "generator" : "Khronos glTF Blender I/O v1.7.33", + "version" : "2.0" + }, + "scene" : 0, + "scenes" : [ + { + "name" : "Scene", + "nodes" : [ + 0 + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "Cube", + "matrix" : [ + 1, 0, 0, 0, + 0, 2, 0, 0, + 0, 0, 3, 0, + 4, 5, 6, 1 + ] + } + ], + "meshes" : [ + { + "name" : "Cube.004", + "primitives" : [ + { + "attributes" : { + "POSITION" : 0, + "NORMAL" : 1, + "TEXCOORD_0" : 2 + }, + "indices" : 3 + } + ] + } + ], + "accessors" : [ + { + "bufferView" : 0, + "componentType" : 5126, + "count" : 24, + "max" : [ + 1, + 1, + 1 + ], + "min" : [ + -1, + -1, + -1 + ], + "type" : "VEC3" + }, + { + "bufferView" : 1, + "componentType" : 5126, + "count" : 24, + "type" : "VEC3" + }, + { + "bufferView" : 2, + "componentType" : 5126, + "count" : 24, + "type" : "VEC2" + }, + { + "bufferView" : 3, + "componentType" : 5123, + "count" : 36, + "type" : "SCALAR" + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 0 + }, + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 288 + }, + { + "buffer" : 0, + "byteLength" : 192, + "byteOffset" : 576 + }, + { + "buffer" : 0, + "byteLength" : 72, + "byteOffset" : 768 + } + ], + "buffers" : [ + { + "byteLength" : 840, + "uri" : "data:application/octet-stream;base64,AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAvwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAvwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAPwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAPwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAADAPgAAgD8AAAA+AACAPgAAwD4AAAAAAAAgPwAAgD8AACA/AAAAAAAAYD8AAIA+AADAPgAAQD8AAAA+AAAAPwAAwD4AAEA/AAAgPwAAQD8AACA/AABAPwAAYD8AAAA/AADAPgAAgD4AAMA+AACAPgAAwD4AAIA+AAAgPwAAgD4AACA/AACAPgAAID8AAIA+AADAPgAAAD8AAMA+AAAAPwAAwD4AAAA/AAAgPwAAAD8AACA/AAAAPwAAID8AAAA/AAADAAkAAAAJAAYACAAKABUACAAVABMAFAAXABEAFAARAA4ADQAPAAQADQAEAAIABwASAAwABwAMAAEAFgALAAUAFgAFABAA" + } + ] +} diff --git a/irr/src/tests/assets/blender_cube_scaled.gltf b/irr/src/tests/assets/blender_cube_scaled.gltf new file mode 100644 index 000000000..151cb5b76 --- /dev/null +++ b/irr/src/tests/assets/blender_cube_scaled.gltf @@ -0,0 +1,105 @@ +{ + "asset" : { + "generator" : "Khronos glTF Blender I/O v1.7.33", + "version" : "2.0" + }, + "scene" : 0, + "scenes" : [ + { + "name" : "Scene", + "nodes" : [ + 0 + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "Cube", + "scale" : [ + 150, + 1, + 21.5 + ] + } + ], + "meshes" : [ + { + "name" : "Cube.004", + "primitives" : [ + { + "attributes" : { + "POSITION" : 0, + "NORMAL" : 1, + "TEXCOORD_0" : 2 + }, + "indices" : 3 + } + ] + } + ], + "accessors" : [ + { + "bufferView" : 0, + "componentType" : 5126, + "count" : 24, + "max" : [ + 1, + 1, + 1 + ], + "min" : [ + -1, + -1, + -1 + ], + "type" : "VEC3" + }, + { + "bufferView" : 1, + "componentType" : 5126, + "count" : 24, + "type" : "VEC3" + }, + { + "bufferView" : 2, + "componentType" : 5126, + "count" : 24, + "type" : "VEC2" + }, + { + "bufferView" : 3, + "componentType" : 5123, + "count" : 36, + "type" : "SCALAR" + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 0 + }, + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 288 + }, + { + "buffer" : 0, + "byteLength" : 192, + "byteOffset" : 576 + }, + { + "buffer" : 0, + "byteLength" : 72, + "byteOffset" : 768 + } + ], + "buffers" : [ + { + "byteLength" : 840, + "uri" : "data:application/octet-stream;base64,AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgL8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIC/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAvwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAvwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAvwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIA/AACAPwAAAAAAAACAAAAAAAAAAAAAAIA/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAAAAAAAAgL8AAACAAAAAAAAAAAAAAIC/AACAPwAAAAAAAACAAAAAAAAAAAAAAIC/AAAAAAAAgD8AAACAAACAPwAAAAAAAACAAADAPgAAgD8AAAA+AACAPgAAwD4AAAAAAAAgPwAAgD8AACA/AAAAAAAAYD8AAIA+AADAPgAAQD8AAAA+AAAAPwAAwD4AAEA/AAAgPwAAQD8AACA/AABAPwAAYD8AAAA/AADAPgAAgD4AAMA+AACAPgAAwD4AAIA+AAAgPwAAgD4AACA/AACAPgAAID8AAIA+AADAPgAAAD8AAMA+AAAAPwAAwD4AAAA/AAAgPwAAAD8AACA/AAAAPwAAID8AAAA/AAADAAkAAAAJAAYACAAKABUACAAVABMAFAAXABEAFAARAA4ADQAPAAQADQAEAAIABwASAAwABwAMAAEAFgALAAUAFgAFABAA" + } + ] +} diff --git a/irr/src/tests/assets/empty.gltf b/irr/src/tests/assets/empty.gltf new file mode 100644 index 000000000..e69de29bb diff --git a/irr/src/tests/assets/json_missing_brace.gltf b/irr/src/tests/assets/json_missing_brace.gltf new file mode 100644 index 000000000..98232c64f --- /dev/null +++ b/irr/src/tests/assets/json_missing_brace.gltf @@ -0,0 +1 @@ +{ diff --git a/irr/src/tests/assets/minimal_triangle.gltf b/irr/src/tests/assets/minimal_triangle.gltf new file mode 100644 index 000000000..412dbac58 --- /dev/null +++ b/irr/src/tests/assets/minimal_triangle.gltf @@ -0,0 +1,70 @@ +{ + "scene": 0, + "scenes" : [ + { + "nodes" : [ 0 ] + } + ], + + "nodes" : [ + { + "mesh" : 0 + } + ], + + "meshes" : [ + { + "primitives" : [ { + "attributes" : { + "POSITION" : 1 + }, + "indices" : 0 + } ] + } + ], + + "buffers" : [ + { + "uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=", + "byteLength" : 44 + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteOffset" : 0, + "byteLength" : 6, + "target" : 34963 + }, + { + "buffer" : 0, + "byteOffset" : 8, + "byteLength" : 36, + "target" : 34962 + } + ], + "accessors" : [ + { + "bufferView" : 0, + "byteOffset" : 0, + "componentType" : 5123, + "count" : 3, + "type" : "SCALAR", + "max" : [ 2 ], + "min" : [ 0 ] + }, + { + "bufferView" : 1, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 3, + "type" : "VEC3", + "max" : [ 1.0, 1.0, 0.0 ], + "min" : [ 0.0, 0.0, 0.0 ] + } + ], + + "asset" : { + "version" : "2.0" + } +} diff --git a/irr/src/tests/assets/snow_man.gltf b/irr/src/tests/assets/snow_man.gltf new file mode 100644 index 000000000..d384867c3 --- /dev/null +++ b/irr/src/tests/assets/snow_man.gltf @@ -0,0 +1 @@ +{"asset":{"version":"2.0","generator":"Blockbench 4.6.0 glTF exporter"},"scenes":[{"nodes":[3],"name":"blockbench_export"}],"scene":0,"nodes":[{"name":"cube","mesh":0},{"name":"cube","mesh":1},{"name":"cube","mesh":2},{"children":[0,1,2]}],"bufferViews":[{"buffer":0,"byteOffset":0,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":288,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":576,"byteLength":192,"target":34962,"byteStride":8},{"buffer":0,"byteOffset":768,"byteLength":72,"target":34963},{"buffer":0,"byteOffset":840,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":1128,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":1416,"byteLength":192,"target":34962,"byteStride":8},{"buffer":0,"byteOffset":1608,"byteLength":72,"target":34963},{"buffer":0,"byteOffset":1680,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":1968,"byteLength":288,"target":34962,"byteStride":12},{"buffer":0,"byteOffset":2256,"byteLength":192,"target":34962,"byteStride":8},{"buffer":0,"byteOffset":2448,"byteLength":72,"target":34963}],"buffers":[{"byteLength":2520,"uri":"data:application/octet-stream;base64,AABAQAAAwEEAAEBAAABAQAAAkEEAAEBAAABAQAAAwEEAAEDAAABAQAAAkEEAAEDAAABAwAAAwEEAAEBAAABAwAAAwEEAAEDAAABAwAAAkEEAAEBAAABAwAAAkEEAAEDAAABAQAAAwEEAAEBAAABAQAAAwEEAAEDAAABAwAAAwEEAAEBAAABAwAAAwEEAAEDAAABAQAAAkEEAAEBAAABAwAAAkEEAAEBAAABAQAAAkEEAAEDAAABAwAAAkEEAAEDAAABAQAAAwEEAAEBAAABAwAAAwEEAAEBAAABAQAAAkEEAAEBAAABAwAAAkEEAAEBAAABAQAAAwEEAAEDAAABAQAAAkEEAAEDAAABAwAAAwEEAAEDAAABAwAAAkEEAAEDAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/VVUVP6uqSj9VVRU/q6oqP1VVNT+rqko/VVU1P6uqKj8AAAA/VVXVPgAAwD5VVdU+AAAAP1VVlT4AAMA+VVWVPgAAAD4AAIA+AAAAPgAAwD4AAAAAAACAPgAAAAAAAMA+AABAPwAAgD8AACA/AACAPwAAQD8AAGA/AAAgPwAAYD9VVVU/AABgP1VVNT8AAGA/VVVVPwAAQD9VVTU/AABAP1VVNT8AAEA/VVU1PwAAID9VVVU/AABAP1VVVT8AACA/AgAAAAEAAgABAAMABgAEAAUABgAFAAcACgAIAAkACgAJAAsADgAMAA0ADgANAA8AEgAQABEAEgARABMAFgAUABUAFgAVABcAAACgQAAAIEEAAKBAAACgQAAAAAAAAKBAAACgQAAAIEEAAKDAAACgQAAAAAAAAKDAAACgwAAAIEEAAKBAAACgwAAAIEEAAKDAAACgwAAAAAAAAKBAAACgwAAAAAAAAKDAAACgQAAAIEEAAKBAAACgQAAAIEEAAKDAAACgwAAAIEEAAKBAAACgwAAAIEEAAKDAAACgQAAAAAAAAKBAAACgwAAAAAAAAKBAAACgQAAAAAAAAKDAAACgwAAAAAAAAKDAAACgQAAAIEEAAKBAAACgwAAAIEEAAKBAAACgQAAAAAAAAKBAAACgwAAAAAAAAKBAAACgQAAAIEEAAKDAAACgQAAAAAAAAKDAAACgwAAAIEEAAKDAAACgwAAAAAAAAKDAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAgD8AAAAAq6pKP1VVVT4AAIA/VVVVPquqSj9VVVU+q6pKPwAAAACrqko/VVVVPlVVFT8AAAAAVVUVP1VV1T6rqko/VVXVPgAAgD9VVVU+q6pKP1VVVT4AAIA/VVXVPquqSj9VVVU+q6pKP1VV1T5VVRU/VVVVPlVVFT9VVVU+VVUVPwAAAABVVRU/VVVVPgAAwD4AAAAAAADAPlVV1T4AAIA/VVXVPquqSj8AACA/AACAPwAAID+rqko/AgAAAAEAAgABAAMABgAEAAUABgAFAAcACgAIAAkACgAJAAsADgAMAA0ADgANAA8AEgAQABEAEgARABMAFgAUABUAFgAVABcAAACAQAAAkEEAAIBAAACAQAAAIEEAAIBAAACAQAAAkEEAAIDAAACAQAAAIEEAAIDAAACAwAAAkEEAAIBAAACAwAAAkEEAAIDAAACAwAAAIEEAAIBAAACAwAAAIEEAAIDAAACAQAAAkEEAAIBAAACAQAAAkEEAAIDAAACAwAAAkEEAAIBAAACAwAAAkEEAAIDAAACAQAAAIEEAAIBAAACAwAAAIEEAAIBAAACAQAAAIEEAAIDAAACAwAAAIEEAAIDAAACAQAAAkEEAAIBAAACAwAAAkEEAAIBAAACAQAAAIEEAAIBAAACAwAAAIEEAAIBAAACAQAAAkEEAAIDAAACAQAAAIEEAAIDAAACAwAAAkEEAAIDAAACAwAAAIEEAAIDAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAPwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAACAvwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAgL8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/AAAAAAAAAAAAAIC/VVVVPlVVFT9VVVU+VVXVPgAAwD5VVRU/AADAPlVV1T5VVRU/q6pKP1VV1T6rqko/VVUVPwAAID9VVdU+AAAgP6uqCj9VVdU+q6oKP1VVFT8AAMA+VVXVPgAAwD5VVRU/VVU1PwAAID+rqgo/AAAgP1VVNT+rquo+q6oKP6uq6j5VVTU/q6rqPquqCj+rquo+VVU1P1VVlT6rqgo/VVWVPlVVVT5VVdU+VVVVPgAAgD4AAMA+VVXVPgAAwD4AAIA+AgAAAAEAAgABAAMABgAEAAUABgAFAAcACgAIAAkACgAJAAsADgAMAA0ADgANAA8AEgAQABEAEgARABMAFgAUABUAFgAVABcA"}],"accessors":[{"bufferView":0,"componentType":5126,"count":24,"max":[3,24,3],"min":[-3,18,-3],"type":"VEC3"},{"bufferView":1,"componentType":5126,"count":24,"max":[1,1,1],"min":[-1,-1,-1],"type":"VEC3"},{"bufferView":2,"componentType":5126,"count":24,"max":[0.8333333134651184,1],"min":[0,0.25],"type":"VEC2"},{"bufferView":3,"componentType":5123,"count":36,"max":[23],"min":[0],"type":"SCALAR"},{"bufferView":4,"componentType":5126,"count":24,"max":[5,10,5],"min":[-5,0,-5],"type":"VEC3"},{"bufferView":5,"componentType":5126,"count":24,"max":[1,1,1],"min":[-1,-1,-1],"type":"VEC3"},{"bufferView":6,"componentType":5126,"count":24,"max":[0.625,1],"min":[0,0.375],"type":"VEC2"},{"bufferView":7,"componentType":5123,"count":36,"max":[23],"min":[0],"type":"SCALAR"},{"bufferView":8,"componentType":5126,"count":24,"max":[4,18,4],"min":[-4,10,-4],"type":"VEC3"},{"bufferView":9,"componentType":5126,"count":24,"max":[1,1,1],"min":[-1,-1,-1],"type":"VEC3"},{"bufferView":10,"componentType":5126,"count":24,"max":[0.7083333134651184,0.7916666865348816],"min":[0.2083333283662796,0.25],"type":"VEC2"},{"bufferView":11,"componentType":5123,"count":36,"max":[23],"min":[0],"type":"SCALAR"}],"materials":[{"pbrMetallicRoughness":{"metallicFactor":0,"roughnessFactor":1},"alphaMode":"MASK","alphaCutoff":0.05,"doubleSided":true}],"meshes":[{"primitives":[{"mode":4,"attributes":{"POSITION":0,"NORMAL":1,"TEXCOORD_0":2},"indices":3,"material":0}]},{"primitives":[{"mode":4,"attributes":{"POSITION":4,"NORMAL":5,"TEXCOORD_0":6},"indices":7,"material":0}]},{"primitives":[{"mode":4,"attributes":{"POSITION":8,"NORMAL":9,"TEXCOORD_0":10},"indices":11,"material":0}]}]} \ No newline at end of file diff --git a/irr/src/tests/assets/triangle_with_vertex_stride.gltf b/irr/src/tests/assets/triangle_with_vertex_stride.gltf new file mode 100644 index 000000000..fce122c8a --- /dev/null +++ b/irr/src/tests/assets/triangle_with_vertex_stride.gltf @@ -0,0 +1,71 @@ +{ + "scene": 0, + "scenes" : [ + { + "nodes" : [ 0 ] + } + ], + + "nodes" : [ + { + "mesh" : 0 + } + ], + + "meshes" : [ + { + "primitives" : [ { + "attributes" : { + "POSITION" : 1 + }, + "indices" : 0 + } ] + } + ], + + "buffers" : [ + { + "uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAAA=", + "byteLength" : 80 + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteOffset" : 0, + "byteLength" : 6, + "target" : 34963 + }, + { + "buffer" : 0, + "byteOffset" : 8, + "byteLength" : 36, + "byteStride" : 24, + "target" : 34962 + } + ], + "accessors" : [ + { + "bufferView" : 0, + "byteOffset" : 0, + "componentType" : 5123, + "count" : 3, + "type" : "SCALAR", + "max" : [ 2 ], + "min" : [ 0 ] + }, + { + "bufferView" : 1, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 3, + "type" : "VEC3", + "max" : [ 1.0, 1.0, 0.0 ], + "min" : [ 0.0, 0.0, 0.0 ] + } + ], + + "asset" : { + "version" : "2.0" + } +} diff --git a/irr/src/tests/assets/triangle_without_indices.gltf b/irr/src/tests/assets/triangle_without_indices.gltf new file mode 100644 index 000000000..1c0736ab3 --- /dev/null +++ b/irr/src/tests/assets/triangle_without_indices.gltf @@ -0,0 +1,54 @@ +{ + "scene" : 0, + "scenes" : [ + { + "nodes" : [ 0 ] + } + ], + + "nodes" : [ + { + "mesh" : 0 + } + ], + + "meshes" : [ + { + "primitives" : [ { + "attributes" : { + "POSITION" : 0 + } + } ] + } + ], + + "buffers" : [ + { + "uri" : "data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAA", + "byteLength" : 36 + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteOffset" : 0, + "byteLength" : 36, + "target" : 34962 + } + ], + "accessors" : [ + { + "bufferView" : 0, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 3, + "type" : "VEC3", + "max" : [ 1.0, 1.0, 0.0 ], + "min" : [ 0.0, 0.0, 0.0 ] + } + ], + + "asset" : { + "version" : "2.0" + } +} \ No newline at end of file diff --git a/irr/src/tests/testCGLTFMeshFileLoader.cpp b/irr/src/tests/testCGLTFMeshFileLoader.cpp new file mode 100644 index 000000000..c70c03a24 --- /dev/null +++ b/irr/src/tests/testCGLTFMeshFileLoader.cpp @@ -0,0 +1,334 @@ +#include "CReadFile.h" +#include "vector3d.h" + +#include + +// Catch needs to be included after Irrlicht so that it sees operator<< +// declarations. +#define CATCH_CONFIG_MAIN +#include + +#include + +using namespace std; + +class ScopedMesh +{ +public: + ScopedMesh(irr::io::IReadFile* file) + : m_device { irr::createDevice(irr::video::EDT_NULL) } + , m_mesh { nullptr } + { + auto* smgr = m_device->getSceneManager(); + m_mesh = smgr->getMesh(file); + } + + ScopedMesh(const irr::io::path& filepath) + : m_device { irr::createDevice(irr::video::EDT_NULL) } + , m_mesh { nullptr } + { + auto* smgr = m_device->getSceneManager(); + irr::io::CReadFile f = irr::io::CReadFile(filepath); + m_mesh = smgr->getMesh(&f); + } + + ~ScopedMesh() + { + m_device->drop(); + m_mesh = nullptr; + } + + const irr::scene::IAnimatedMesh* getMesh() const + { + return m_mesh; + } + +private: + irr::IrrlichtDevice* m_device; + irr::scene::IAnimatedMesh* m_mesh; +}; + +TEST_CASE("load empty gltf file") { + ScopedMesh sm("source/Irrlicht/tests/assets/empty.gltf"); + CHECK(sm.getMesh() == nullptr); +} + +TEST_CASE("minimal triangle") { + auto path = GENERATE( + "source/Irrlicht/tests/assets/minimal_triangle.gltf", + "source/Irrlicht/tests/assets/triangle_with_vertex_stride.gltf", + // Test non-indexed geometry. + "source/Irrlicht/tests/assets/triangle_without_indices.gltf"); + INFO(path); + ScopedMesh sm(path); + REQUIRE(sm.getMesh() != nullptr); + REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); + + SECTION("vertex coordinates are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 3); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->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(sm.getMesh()->getMeshBuffer(0)->getIndexCount() == 3); + const auto* indices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getIndices()); + CHECK(indices[0] == 2); + CHECK(indices[1] == 1); + CHECK(indices[2] == 0); + } +} + +TEST_CASE("blender cube") { + ScopedMesh sm("source/Irrlicht/tests/assets/blender_cube.gltf"); + REQUIRE(sm.getMesh() != nullptr); + REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); + SECTION("vertex coordinates are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + CHECK(vertices[0].Pos == irr::core::vector3df{-10.0f, -10.0f, -10.0f}); + CHECK(vertices[3].Pos == irr::core::vector3df{-10.0f, 10.0f, -10.0f}); + CHECK(vertices[6].Pos == irr::core::vector3df{-10.0f, -10.0f, 10.0f}); + CHECK(vertices[9].Pos == irr::core::vector3df{-10.0f, 10.0f, 10.0f}); + CHECK(vertices[12].Pos == irr::core::vector3df{10.0f, -10.0f, -10.0f}); + CHECK(vertices[15].Pos == irr::core::vector3df{10.0f, 10.0f, -10.0f}); + CHECK(vertices[18].Pos == irr::core::vector3df{10.0f, -10.0f, 10.0f}); + CHECK(vertices[21].Pos == irr::core::vector3df{10.0f, 10.0f, 10.0f}); + } + + SECTION("vertex indices are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getIndexCount() == 36); + const auto* indices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getIndices()); + CHECK(indices[0] == 16); + CHECK(indices[1] == 5); + CHECK(indices[2] == 22); + CHECK(indices[35] == 0); + } + + SECTION("vertex normals are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + CHECK(vertices[0].Normal == irr::core::vector3df{-1.0f, 0.0f, 0.0f}); + CHECK(vertices[1].Normal == irr::core::vector3df{0.0f, -1.0f, 0.0f}); + CHECK(vertices[2].Normal == irr::core::vector3df{0.0f, 0.0f, -1.0f}); + CHECK(vertices[3].Normal == irr::core::vector3df{-1.0f, 0.0f, 0.0f}); + CHECK(vertices[6].Normal == irr::core::vector3df{-1.0f, 0.0f, 0.0f}); + CHECK(vertices[23].Normal == irr::core::vector3df{1.0f, 0.0f, 0.0f}); + + } + + SECTION("texture coords are correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + CHECK(vertices[0].TCoords == irr::core::vector2df{0.375f, 1.0f}); + CHECK(vertices[1].TCoords == irr::core::vector2df{0.125f, 0.25f}); + CHECK(vertices[2].TCoords == irr::core::vector2df{0.375f, 0.0f}); + CHECK(vertices[3].TCoords == irr::core::vector2df{0.6250f, 1.0f}); + CHECK(vertices[6].TCoords == irr::core::vector2df{0.375f, 0.75f}); + } +} + +TEST_CASE("mesh loader returns nullptr when given null file pointer") { + ScopedMesh sm(nullptr); + CHECK(sm.getMesh() == nullptr); +} + +TEST_CASE("invalid JSON returns nullptr") { + ScopedMesh sm("source/Irrlicht/tests/assets/json_missing_brace.gltf"); + CHECK(sm.getMesh() == nullptr); +} + +TEST_CASE("blender cube scaled") { + ScopedMesh sm("source/Irrlicht/tests/assets/blender_cube_scaled.gltf"); + REQUIRE(sm.getMesh() != nullptr); + REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); + + SECTION("Scaling is correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + + CHECK(vertices[0].Pos == irr::core::vector3df{-150.0f, -1.0f, -21.5f}); + CHECK(vertices[3].Pos == irr::core::vector3df{-150.0f, 1.0f, -21.5f}); + CHECK(vertices[6].Pos == irr::core::vector3df{-150.0f, -1.0f, 21.5f}); + CHECK(vertices[9].Pos == irr::core::vector3df{-150.0f, 1.0f, 21.5f}); + CHECK(vertices[12].Pos == irr::core::vector3df{150.0f, -1.0f, -21.5f}); + CHECK(vertices[15].Pos == irr::core::vector3df{150.0f, 1.0f, -21.5f}); + CHECK(vertices[18].Pos == irr::core::vector3df{150.0f, -1.0f, 21.5f}); + CHECK(vertices[21].Pos == irr::core::vector3df{150.0f, 1.0f, 21.5f}); + } +} + +TEST_CASE("blender cube matrix transform") { + ScopedMesh sm("source/Irrlicht/tests/assets/blender_cube_matrix_transform.gltf"); + REQUIRE(sm.getMesh() != nullptr); + REQUIRE(sm.getMesh()->getMeshBufferCount() == 1); + + SECTION("Transformation is correct") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + const auto checkVertex = [&](const std::size_t i, irr::core::vector3df vec) { + // The transform scales by (1, 2, 3) and translates by (4, 5, 6). + CHECK(vertices[i].Pos == vec * irr::core::vector3df{1, 2, 3} + // The -6 is due to the coordinate system conversion. + + irr::core::vector3df{4, 5, -6}); + }; + checkVertex(0, irr::core::vector3df{-1, -1, -1}); + checkVertex(3, irr::core::vector3df{-1, 1, -1}); + checkVertex(6, irr::core::vector3df{-1, -1, 1}); + checkVertex(9, irr::core::vector3df{-1, 1, 1}); + checkVertex(12, irr::core::vector3df{1, -1, -1}); + checkVertex(15, irr::core::vector3df{1, 1, -1}); + checkVertex(18, irr::core::vector3df{1, -1, 1}); + checkVertex(21, irr::core::vector3df{1, 1, 1}); + } +} + +TEST_CASE("snow man") { + ScopedMesh sm("source/Irrlicht/tests/assets/snow_man.gltf"); + REQUIRE(sm.getMesh() != nullptr); + REQUIRE(sm.getMesh()->getMeshBufferCount() == 3); + + SECTION("vertex coordinates are correct for all buffers") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + + CHECK(vertices[0].Pos == irr::core::vector3df{3.0f, 24.0f, -3.0f}); + CHECK(vertices[3].Pos == irr::core::vector3df{3.0f, 18.0f, 3.0f}); + CHECK(vertices[6].Pos == irr::core::vector3df{-3.0f, 18.0f, -3.0f}); + CHECK(vertices[9].Pos == irr::core::vector3df{3.0f, 24.0f, 3.0f}); + CHECK(vertices[12].Pos == irr::core::vector3df{3.0f, 18.0f, -3.0f}); + CHECK(vertices[15].Pos == irr::core::vector3df{-3.0f, 18.0f, 3.0f}); + CHECK(vertices[18].Pos == irr::core::vector3df{3.0f, 18.0f, -3.0f}); + CHECK(vertices[21].Pos == irr::core::vector3df{3.0f, 18.0f, 3.0f}); + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(1)->getVertices()); + + CHECK(vertices[2].Pos == irr::core::vector3df{5.0f, 10.0f, 5.0f}); + CHECK(vertices[3].Pos == irr::core::vector3df{5.0f, 0.0f, 5.0f}); + CHECK(vertices[7].Pos == irr::core::vector3df{-5.0f, 0.0f, 5.0f}); + CHECK(vertices[8].Pos == irr::core::vector3df{5.0f, 10.0f, -5.0f}); + CHECK(vertices[14].Pos == irr::core::vector3df{5.0f, 0.0f, 5.0f}); + CHECK(vertices[16].Pos == irr::core::vector3df{5.0f, 10.0f, -5.0f}); + CHECK(vertices[22].Pos == irr::core::vector3df{-5.0f, 10.0f, 5.0f}); + CHECK(vertices[23].Pos == irr::core::vector3df{-5.0f, 0.0f, 5.0f}); + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(2)->getVertices()); + + CHECK(vertices[1].Pos == irr::core::vector3df{4.0f, 10.0f, -4.0f}); + CHECK(vertices[2].Pos == irr::core::vector3df{4.0f, 18.0f, 4.0f}); + CHECK(vertices[3].Pos == irr::core::vector3df{4.0f, 10.0f, 4.0f}); + CHECK(vertices[10].Pos == irr::core::vector3df{-4.0f, 18.0f, -4.0f}); + CHECK(vertices[11].Pos == irr::core::vector3df{-4.0f, 18.0f, 4.0f}); + CHECK(vertices[12].Pos == irr::core::vector3df{4.0f, 10.0f, -4.0f}); + CHECK(vertices[17].Pos == irr::core::vector3df{-4.0f, 18.0f, -4.0f}); + CHECK(vertices[18].Pos == irr::core::vector3df{4.0f, 10.0f, -4.0f}); + } + + SECTION("vertex indices are correct for all buffers") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getIndexCount() == 36); + const auto* indices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getIndices()); + CHECK(indices[0] == 23); + CHECK(indices[1] == 21); + CHECK(indices[2] == 22); + CHECK(indices[35] == 2); + + REQUIRE(sm.getMesh()->getMeshBuffer(1)->getIndexCount() == 36); + indices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(1)->getIndices()); + CHECK(indices[10] == 16); + CHECK(indices[11] == 18); + CHECK(indices[15] == 13); + CHECK(indices[27] == 5); + + REQUIRE(sm.getMesh()->getMeshBuffer(1)->getIndexCount() == 36); + indices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(2)->getIndices()); + CHECK(indices[26] == 6); + CHECK(indices[27] == 5); + CHECK(indices[29] == 6); + CHECK(indices[32] == 2); + } + + + SECTION("vertex normals are correct for all buffers") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + CHECK(vertices[0].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[1].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[2].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[3].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[6].Normal == irr::core::vector3df{-1.0f, 0.0f, -0.0f}); + CHECK(vertices[23].Normal == irr::core::vector3df{0.0f, 0.0f, 1.0f}); + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(1)->getVertices()); + + CHECK(vertices[0].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[1].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[3].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[6].Normal == irr::core::vector3df{-1.0f, 0.0f, -0.0f}); + CHECK(vertices[7].Normal == irr::core::vector3df{-1.0f, 0.0f, -0.0f}); + CHECK(vertices[22].Normal == irr::core::vector3df{0.0f, 0.0f, 1.0f}); + + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(2)->getVertices()); + + CHECK(vertices[3].Normal == irr::core::vector3df{1.0f, 0.0f, -0.0f}); + CHECK(vertices[4].Normal == irr::core::vector3df{-1.0f, 0.0f, -0.0f}); + CHECK(vertices[5].Normal == irr::core::vector3df{-1.0f, 0.0f, -0.0f}); + CHECK(vertices[10].Normal == irr::core::vector3df{0.0f, 1.0f, -0.0f}); + CHECK(vertices[11].Normal == irr::core::vector3df{0.0f, 1.0f, -0.0f}); + CHECK(vertices[19].Normal == irr::core::vector3df{0.0f, 0.0f, -1.0f}); + + } + + + SECTION("texture coords are correct for all buffers") { + REQUIRE(sm.getMesh()->getMeshBuffer(0)->getVertexCount() == 24); + const auto* vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(0)->getVertices()); + + CHECK(vertices[0].TCoords == irr::core::vector2df{0.583333, 0.791667}); + CHECK(vertices[1].TCoords == irr::core::vector2df{0.583333, 0.666667}); + CHECK(vertices[2].TCoords == irr::core::vector2df{0.708333, 0.791667}); + CHECK(vertices[5].TCoords == irr::core::vector2df{0.375, 0.416667}); + CHECK(vertices[6].TCoords == irr::core::vector2df{0.5, 0.291667}); + CHECK(vertices[19].TCoords == irr::core::vector2df{0.708333, 0.75}); + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(1)->getVertices()); + + CHECK(vertices[1].TCoords == irr::core::vector2df{0, 0.791667}); + CHECK(vertices[4].TCoords == irr::core::vector2df{0.208333, 0.791667}); + CHECK(vertices[5].TCoords == irr::core::vector2df{0, 0.791667}); + CHECK(vertices[6].TCoords == irr::core::vector2df{0.208333, 0.583333}); + CHECK(vertices[12].TCoords == irr::core::vector2df{0.416667, 0.791667}); + CHECK(vertices[15].TCoords == irr::core::vector2df{0.208333, 0.583333}); + + vertices = reinterpret_cast( + sm.getMesh()->getMeshBuffer(2)->getVertices()); + + CHECK(vertices[10].TCoords == irr::core::vector2df{0.375, 0.416667}); + CHECK(vertices[11].TCoords == irr::core::vector2df{0.375, 0.583333}); + CHECK(vertices[12].TCoords == irr::core::vector2df{0.708333, 0.625}); + CHECK(vertices[17].TCoords == irr::core::vector2df{0.541667, 0.458333}); + CHECK(vertices[20].TCoords == irr::core::vector2df{0.208333, 0.416667}); + CHECK(vertices[22].TCoords == irr::core::vector2df{0.375, 0.416667}); + } +} diff --git a/lib/tiniergltf/.gitignore b/lib/tiniergltf/.gitignore new file mode 100644 index 000000000..6d105f52b --- /dev/null +++ b/lib/tiniergltf/.gitignore @@ -0,0 +1,6 @@ +cmake +CMakeCache.txt +CMakeFiles +.cache +compile_commands.json +build \ No newline at end of file diff --git a/lib/tiniergltf/CMakeLists.txt b/lib/tiniergltf/CMakeLists.txt new file mode 100644 index 000000000..1fbca89f5 --- /dev/null +++ b/lib/tiniergltf/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.12) + +project(tiniergltf + VERSION 1.0.0 + LANGUAGES CXX +) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_library(tiniergltf OBJECT tiniergltf.hpp base64.cpp base64.h) +add_library(tiniergltf::tiniergltf ALIAS tiniergltf) + +target_include_directories(tiniergltf + INTERFACE + "$" + "${JSON_INCLUDE_DIR}" # Set in FindJson.cmake +) + +target_link_libraries(tiniergltf) diff --git a/lib/tiniergltf/Readme.md b/lib/tiniergltf/Readme.md new file mode 100644 index 000000000..825085a04 --- /dev/null +++ b/lib/tiniergltf/Readme.md @@ -0,0 +1,67 @@ +# TinierGLTF + +A safe, modern, tiny glTF loader for C++ 17. + +What this is: + +* A tiny glTF deserializer which maps JSON objects to appropriate C++ structures. +* Intended to be safe for loading untrusted input. +* Slightly tailored to the needs of [Minetest](https://github.com/minetest/minetest). + +What this doesn't and shouldn't do: + +* Serialization +* Loading images +* Resolving resources +* Support glTF extensions + +## TODOs + +- [ ] Add GLB support. +- [ ] Add further checks according to the specification. + - Everything in the JSON schema (+ indices and misc. stuff) is already validated. + Much of the code was generated by a Lua script from the JSON schemata. +- [ ] Consider base64 rewrite. + +## License + +`tiniergltf.hpp` was written by Lars Müller and is licensed under the MIT license: + +> Copyright 2024 Lars Müller +> +> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +The base64 library used is licensed as follows: + +> base64.cpp and base64.h +> +> Copyright (C) 2004-2008 René Nyffenegger +> Modified by the Minetest Contributors. +> +> This source code is provided 'as-is', without any express or implied +> warranty. In no event will the author be held liable for any damages +> arising from the use of this software. +> +> Permission is granted to anyone to use this software for any purpose, +> including commercial applications, and to alter it and redistribute it +> freely, subject to the following restrictions: +> +> 1. The origin of this source code must not be misrepresented; you must not +> claim that you wrote the original source code. If you use this source code +> in a product, an acknowledgment in the product documentation would be +> appreciated but is not required. +> +> 2. Altered source versions must be plainly marked as such, and must not be +> misrepresented as being the original source code. +> +> 3. This notice may not be removed or altered from any source distribution. +> +> René Nyffenegger rene.nyffenegger@adp-gmbh.ch + +## Bug Bounty + +I offer a reward of one (1) virtual headpat per valid bug report. diff --git a/lib/tiniergltf/base64.cpp b/lib/tiniergltf/base64.cpp new file mode 100644 index 000000000..c782cacf0 --- /dev/null +++ b/lib/tiniergltf/base64.cpp @@ -0,0 +1,112 @@ +/* +base64.cpp and base64.h + +Copyright (C) 2004-2008 René Nyffenegger +Modified by the Minetest Contributors. + +This source code is provided 'as-is', without any express or implied +warranty. In no event will the author be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + +3. This notice may not be removed or altered from any source distribution. + +René Nyffenegger rene.nyffenegger@adp-gmbh.ch + +*/ + +#include "base64.h" +#include +#include + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +static const std::string base64_chars_padding_1 = "AEIMQUYcgkosw048"; +static const std::string base64_chars_padding_2 = "AQgw"; + +static inline bool is_base64(unsigned char c) +{ + return (c >= '0' && c <= '9') + || (c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z') + || c == '+' || c == '/'; +} + +bool base64_is_valid(const std::string_view &s) +{ + size_t i = 0; + for (; i < s.size(); ++i) + if (!is_base64(s[i])) + break; + unsigned char padding = 3 - ((i + 3) % 4); + if ((padding == 1 && base64_chars_padding_1.find(s[i - 1]) == std::string::npos) + || (padding == 2 && base64_chars_padding_2.find(s[i - 1]) == std::string::npos) + || padding == 3) + return false; + int actual_padding = s.size() - i; + // omission of padding characters is allowed + if (actual_padding == 0) + return true; + + // remaining characters (max. 2) may only be padding + for (; i < s.size(); ++i) + if (s[i] != '=') + return false; + // number of padding characters needs to match + return padding == actual_padding; +} + +std::vector base64_decode(const std::string_view &encoded_string) { + std::size_t in_len = encoded_string.size(); + std::size_t i = 0, j = 0, in_ = 0; + unsigned char char_array_4[4], char_array_3[3]; + std::vector ret; + ret.reserve(3 * in_len / 4 + 1); + + while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { + char_array_4[i++] = encoded_string[in_]; in_++; + if (i ==4) { + for (i = 0; i <4; i++) + char_array_4[i] = base64_chars.find(char_array_4[i]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (i = 0; (i < 3); i++) + ret.push_back(char_array_3[i]); + i = 0; + } + } + + if (i) { + for (j = i; j <4; j++) + char_array_4[j] = 0; + + for (j = 0; j <4; j++) + char_array_4[j] = base64_chars.find(char_array_4[j]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (j = 0; (j < i - 1); j++) + ret.push_back(char_array_3[j]); + } + + return ret; +} diff --git a/lib/tiniergltf/base64.h b/lib/tiniergltf/base64.h new file mode 100644 index 000000000..fab9ec4bc --- /dev/null +++ b/lib/tiniergltf/base64.h @@ -0,0 +1,35 @@ +/* +base64.cpp and base64.h + +Copyright (C) 2004-2008 René Nyffenegger +Minor modifications by the Minetest Contributors. + +This source code is provided 'as-is', without any express or implied +warranty. In no event will the author be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + +3. This notice may not be removed or altered from any source distribution. + +René Nyffenegger rene.nyffenegger@adp-gmbh.ch + +*/ + +#pragma once + +#include +#include + +bool base64_is_valid(const std::string_view &s); +std::vector base64_decode(const std::string_view &s); diff --git a/lib/tiniergltf/tiniergltf.hpp b/lib/tiniergltf/tiniergltf.hpp new file mode 100644 index 000000000..c7317c1db --- /dev/null +++ b/lib/tiniergltf/tiniergltf.hpp @@ -0,0 +1,1367 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "base64.h" + +namespace tiniergltf { + +static inline void check(bool cond) { + if (!cond) + throw std::runtime_error("invalid glTF"); +} + +template +static inline void checkIndex(const std::optional> &vec, + const std::optional &i) { + if (!i.has_value()) return; + check(vec.has_value()); + check(i < vec->size()); +} + +template +static inline void checkIndex(const std::vector &vec, + const std::optional &i) { + if (!i.has_value()) return; + check(i < vec.size()); +} + +template +static inline void checkForall(const std::optional> &vec, const F &cond) { + if (!vec.has_value()) + return; + for (const T &v : vec.value()) + cond(v); +} + +template +static inline void checkDuplicateFree(const std::vector &vec) { + check(std::unordered_set(vec.begin(), vec.end()).size() == vec.size()); +} + +template +static inline T as(const Json::Value &o); + +template<> +bool as(const Json::Value &o) { + check(o.isBool()); + return o.asBool(); +} + +template<> +double as (const Json::Value &o) { + check(o.isDouble()); + return o.asDouble(); +} + +template<> +std::size_t as(const Json::Value &o) { + check(o.isUInt64()); + auto u = o.asUInt64(); + check(u <= std::numeric_limits::max()); + return u; +} + +template<> +std::string as(const Json::Value &o) { + check(o.isString()); + return o.asString(); +} + +template +std::vector asVec(const Json::Value &o) { + check(o.isArray()); + std::vector res; + res.reserve(o.size()); + for (Json::ArrayIndex i = 0; i < o.size(); ++i) { + res.push_back(as(o[i])); + } + return res; +} + +template +std::array asArr(const Json::Value &o) { + check(o.isArray()); + check(o.size() == n); + std::array res; + for (Json::ArrayIndex i = 0; i < n; ++i) { + res[i] = as(o[i]); + } + return res; +} + +struct AccessorSparseIndices { + std::size_t bufferView; + std::size_t byteOffset; + enum class ComponentType { + UNSIGNED_BYTE, + UNSIGNED_SHORT, + UNSIGNED_INT, + }; + static std::size_t componentSize(ComponentType type) { + switch (type) { + case ComponentType::UNSIGNED_BYTE: + return 1; + case ComponentType::UNSIGNED_SHORT: + return 2; + case ComponentType::UNSIGNED_INT: + return 4; + } + throw std::logic_error("invalid component type"); + } + ComponentType componentType; + std::size_t elementSize() const { + return componentSize(componentType); + } + AccessorSparseIndices(const Json::Value &o) + : bufferView(as(o["bufferView"])) + , byteOffset(0) + { + check(o.isObject()); + if (o.isMember("byteOffset")) { + byteOffset = as(o["byteOffset"]); + check(byteOffset >= 0); + } + { + static std::unordered_map map = { + {5121, ComponentType::UNSIGNED_BYTE}, + {5123, ComponentType::UNSIGNED_SHORT}, + {5125, ComponentType::UNSIGNED_INT}, + }; + auto v = o["componentType"]; check(v.isUInt64()); + componentType = map.at(v.asUInt64()); + } + } +}; +template<> AccessorSparseIndices as(const Json::Value &o) { return o; } + +struct AccessorSparseValues { + std::size_t bufferView; + std::size_t byteOffset; + AccessorSparseValues(const Json::Value &o) + : bufferView(as(o["bufferView"])) + , byteOffset(0) + { + check(o.isObject()); + if (o.isMember("byteOffset")) { + byteOffset = as(o["byteOffset"]); + check(byteOffset >= 0); + } + } +}; +template<> AccessorSparseValues as(const Json::Value &o) { return o; } + +struct AccessorSparse { + std::size_t count; + AccessorSparseIndices indices; + AccessorSparseValues values; + AccessorSparse(const Json::Value &o) + : count(as(o["count"])) + , indices(as(o["indices"])) + , values(as(o["values"])) + { + check(o.isObject()); + check(count >= 1); + } +}; +template<> AccessorSparse as(const Json::Value &o) { return o; } + +struct Accessor { + std::optional bufferView; + std::size_t byteOffset; + enum class ComponentType { + BYTE, + UNSIGNED_BYTE, + SHORT, + UNSIGNED_SHORT, + UNSIGNED_INT, + FLOAT, + }; + ComponentType componentType; + std::size_t componentSize() const { + switch (componentType) { + case ComponentType::BYTE: + case ComponentType::UNSIGNED_BYTE: + return 1; + case ComponentType::SHORT: + case ComponentType::UNSIGNED_SHORT: + return 2; + case ComponentType::UNSIGNED_INT: + case ComponentType::FLOAT: + return 4; + } + throw std::logic_error("invalid component type"); + } + std::size_t count; + std::optional> max; + std::optional> min; + std::optional name; + bool normalized; + std::optional sparse; + enum class Type { + MAT2, + MAT3, + MAT4, + SCALAR, + VEC2, + VEC3, + VEC4, + }; + std::size_t typeCount() const { + switch (type) { + case Type::SCALAR: + return 1; + case Type::VEC2: + return 2; + case Type::VEC3: + return 3; + case Type::MAT2: + case Type::VEC4: + return 4; + case Type::MAT3: + return 9; + case Type::MAT4: + return 16; + } + throw std::logic_error("invalid type"); + } + Type type; + std::size_t elementSize() const { + return componentSize() * typeCount(); + } + Accessor(const Json::Value &o) + : byteOffset(0) + , count(as(o["count"])) + , normalized(false) + { + check(o.isObject()); + if (o.isMember("bufferView")) { + bufferView = as(o["bufferView"]); + } + { + static std::unordered_map map = { + {5120, ComponentType::BYTE}, + {5121, ComponentType::UNSIGNED_BYTE}, + {5122, ComponentType::SHORT}, + {5123, ComponentType::UNSIGNED_SHORT}, + {5125, ComponentType::UNSIGNED_INT}, + {5126, ComponentType::FLOAT}, + }; + auto v = o["componentType"]; check(v.isUInt64()); + componentType = map.at(v.asUInt64()); + } + if (o.isMember("byteOffset")) { + byteOffset = as(o["byteOffset"]); + check(byteOffset >= 0); + check(byteOffset % componentSize() == 0); + } + check(count >= 1); + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("normalized")) { + normalized = as(o["normalized"]); + } + if (o.isMember("sparse")) { + sparse = as(o["sparse"]); + } + { + static std::unordered_map map = { + {"MAT2", Type::MAT2}, + {"MAT3", Type::MAT3}, + {"MAT4", Type::MAT4}, + {"SCALAR", Type::SCALAR}, + {"VEC2", Type::VEC2}, + {"VEC3", Type::VEC3}, + {"VEC4", Type::VEC4}, + }; + auto v = o["type"]; check(v.isString()); + type = map.at(v.asString()); + } + if (o.isMember("max")) { + max = asVec(o["max"]); + check(max->size() == typeCount()); + } + if (o.isMember("min")) { + min = asVec(o["min"]); + check(min->size() == typeCount()); + } + } +}; +template<> Accessor as(const Json::Value &o) { return o; } + +struct AnimationChannelTarget { + std::optional node; + enum class Path { + ROTATION, + SCALE, + TRANSLATION, + WEIGHTS, + }; + Path path; + AnimationChannelTarget(const Json::Value &o) + { + check(o.isObject()); + if (o.isMember("node")) { + node = as(o["node"]); + } + { + static std::unordered_map map = { + {"rotation", Path::ROTATION}, + {"scale", Path::SCALE}, + {"translation", Path::TRANSLATION}, + {"weights", Path::WEIGHTS}, + }; + auto v = o["path"]; check(v.isString()); + path = map.at(v.asString()); + } + } +}; +template<> AnimationChannelTarget as(const Json::Value &o) { return o; } + +struct AnimationChannel { + std::size_t sampler; + AnimationChannelTarget target; + AnimationChannel(const Json::Value &o) + : sampler(as(o["sampler"])) + , target(as(o["target"])) + { + check(o.isObject()); + } +}; +template<> AnimationChannel as(const Json::Value &o) { return o; } + +struct AnimationSampler { + std::size_t input; + enum class Interpolation { + CUBICSPLINE, + LINEAR, + STEP, + }; + Interpolation interpolation; + std::size_t output; + AnimationSampler(const Json::Value &o) + : input(as(o["input"])) + , interpolation(Interpolation::LINEAR) + , output(as(o["output"])) + { + check(o.isObject()); + if (o.isMember("interpolation")) { + static std::unordered_map map = { + {"CUBICSPLINE", Interpolation::CUBICSPLINE}, + {"LINEAR", Interpolation::LINEAR}, + {"STEP", Interpolation::STEP}, + }; + auto v = o["interpolation"]; check(v.isString()); + interpolation = map.at(v.asString()); + } + } +}; +template<> AnimationSampler as(const Json::Value &o) { return o; } + +struct Animation { + std::vector channels; + std::optional name; + std::vector samplers; + Animation(const Json::Value &o) + : channels(asVec(o["channels"])) + , samplers(asVec(o["samplers"])) + { + check(o.isObject()); + check(channels.size() >= 1); + if (o.isMember("name")) { + name = as(o["name"]); + } + check(samplers.size() >= 1); + } +}; +template<> Animation as(const Json::Value &o) { return o; } + +struct Asset { + std::optional copyright; + std::optional generator; + std::optional minVersion; + std::string version; + Asset(const Json::Value &o) + : version(as(o["version"])) + { + check(o.isObject()); + if (o.isMember("copyright")) { + copyright = as(o["copyright"]); + } + if (o.isMember("generator")) { + generator = as(o["generator"]); + } + if (o.isMember("minVersion")) { + minVersion = as(o["minVersion"]); + } + } +}; +template<> Asset as(const Json::Value &o) { return o; } + +struct BufferView { + std::size_t buffer; + std::size_t byteLength; + std::size_t byteOffset; + std::optional byteStride; + std::optional name; + enum class Target { + ARRAY_BUFFER, + ELEMENT_ARRAY_BUFFER, + }; + std::optional target; + BufferView(const Json::Value &o) + : buffer(as(o["buffer"])) + , byteLength(as(o["byteLength"])) + , byteOffset(0) + { + check(o.isObject()); + check(byteLength >= 1); + if (o.isMember("byteOffset")) { + byteOffset = as(o["byteOffset"]); + check(byteOffset >= 0); + } + if (o.isMember("byteStride")) { + byteStride = as(o["byteStride"]); + check(byteStride.value() >= 4); + check(byteStride.value() <= 252); + check(byteStride.value() % 4 == 0); + } + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("target")) { + static std::unordered_map map = { + {34962, Target::ARRAY_BUFFER}, + {34963, Target::ELEMENT_ARRAY_BUFFER}, + }; + auto v = o["target"]; check(v.isUInt64()); + target = map.at(v.asUInt64()); + } + } +}; +template<> BufferView as(const Json::Value &o) { return o; } + +struct Buffer { + std::size_t byteLength; + std::optional name; + std::vector data; + Buffer(const Json::Value &o, + const std::function(const std::string &uri)> &resolveURI) + : byteLength(as(o["byteLength"])) + { + check(o.isObject()); + check(byteLength >= 1); + if (o.isMember("name")) { + name = as(o["name"]); + } + check(o.isMember("uri")); + bool dataURI = false; + const std::string uri = as(o["uri"]); + for (auto &prefix : std::array { + "data:application/octet-stream;base64,", + "data:application/gltf-buffer;base64," + }) { + if (std::string_view(uri).substr(0, prefix.length()) == prefix) { + auto view = std::string_view(uri).substr(prefix.length()); + check(base64_is_valid(view)); + data = base64_decode(view); + dataURI = true; + break; + } + } + if (!dataURI) + data = resolveURI(uri); + check(data.size() >= byteLength); + data.resize(byteLength); + } +}; + +struct CameraOrthographic { + double xmag; + double ymag; + double zfar; + double znear; + CameraOrthographic(const Json::Value &o) + : xmag(as(o["xmag"])) + , ymag(as(o["ymag"])) + , zfar(as(o["zfar"])) + , znear(as(o["znear"])) + { + check(o.isObject()); + check(zfar > 0); + check(znear >= 0); + } +}; +template<> CameraOrthographic as(const Json::Value &o) { return o; } + +struct CameraPerspective { + std::optional aspectRatio; + double yfov; + std::optional zfar; + double znear; + CameraPerspective(const Json::Value &o) + : yfov(as(o["yfov"])) + , znear(as(o["znear"])) + { + check(o.isObject()); + if (o.isMember("aspectRatio")) { + aspectRatio = as(o["aspectRatio"]); + check(aspectRatio.value() > 0); + } + check(yfov > 0); + if (o.isMember("zfar")) { + zfar = as(o["zfar"]); + check(zfar.value() > 0); + } + check(znear > 0); + } +}; +template<> CameraPerspective as(const Json::Value &o) { return o; } + +struct Camera { + std::optional name; + std::optional orthographic; + std::optional perspective; + enum class Type { + ORTHOGRAPHIC, + PERSPECTIVE, + }; + Type type; + Camera(const Json::Value &o) + { + check(o.isObject()); + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("orthographic")) { + orthographic = as(o["orthographic"]); + } + if (o.isMember("perspective")) { + perspective = as(o["perspective"]); + } + { + static std::unordered_map map = { + {"orthographic", Type::ORTHOGRAPHIC}, + {"perspective", Type::PERSPECTIVE}, + }; + auto v = o["type"]; check(v.isString()); + type = map.at(v.asString()); + } + } +}; +template<> Camera as(const Json::Value &o) { return o; } + +struct Image { + std::optional bufferView; + enum class MimeType { + IMAGE_JPEG, + IMAGE_PNG, + }; + std::optional mimeType; + std::optional name; + std::optional uri; + Image(const Json::Value &o) + { + check(o.isObject()); + if (o.isMember("bufferView")) { + bufferView = as(o["bufferView"]); + } + if (o.isMember("mimeType")) { + static std::unordered_map map = { + {"image/jpeg", MimeType::IMAGE_JPEG}, + {"image/png", MimeType::IMAGE_PNG}, + }; + auto v = o["mimeType"]; check(v.isString()); + mimeType = map.at(v.asString()); + } + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("uri")) { + uri = as(o["uri"]); + } + } +}; +template<> Image as(const Json::Value &o) { return o; } + +struct TextureInfo { + std::size_t index; + std::size_t texCoord; + TextureInfo(const Json::Value &o) + : index(as(o["index"])) + , texCoord(0) + { + check(o.isObject()); + if (o.isMember("texCoord")) { + texCoord = as(o["texCoord"]); + check(texCoord >= 0); + } + } +}; +template<> TextureInfo as(const Json::Value &o) { return o; } + +struct MaterialNormalTextureInfo { + std::size_t index; + double scale; + std::size_t texCoord; + MaterialNormalTextureInfo(const Json::Value &o) + : index(as(o["index"])) + , scale(1) + , texCoord(0) + { + check(o.isObject()); + if (o.isMember("scale")) { + scale = as(o["scale"]); + } + if (o.isMember("texCoord")) { + texCoord = as(o["texCoord"]); + } + } +}; +template<> MaterialNormalTextureInfo as(const Json::Value &o) { return o; } + +struct MaterialOcclusionTextureInfo { + std::size_t index; + double strength; + std::size_t texCoord; + MaterialOcclusionTextureInfo(const Json::Value &o) + : index(as(o["index"])) + , strength(1) + , texCoord(0) + { + check(o.isObject()); + if (o.isMember("strength")) { + strength = as(o["strength"]); + check(strength >= 0); + check(strength <= 1); + } + if (o.isMember("texCoord")) { + texCoord = as(o["texCoord"]); + } + } +}; +template<> MaterialOcclusionTextureInfo as(const Json::Value &o) { return o; } + +struct MaterialPbrMetallicRoughness { + std::array baseColorFactor; + std::optional baseColorTexture; + double metallicFactor; + std::optional metallicRoughnessTexture; + double roughnessFactor; + MaterialPbrMetallicRoughness(const Json::Value &o) + : baseColorFactor{1, 1, 1, 1} + , metallicFactor(1) + , roughnessFactor(1) + { + check(o.isObject()); + if (o.isMember("baseColorFactor")) { + baseColorFactor = asArr(o["baseColorFactor"]); + for (auto v: baseColorFactor) { + check(v >= 0); + check(v <= 1); + } + } + if (o.isMember("baseColorTexture")) { + baseColorTexture = as(o["baseColorTexture"]); + } + if (o.isMember("metallicFactor")) { + metallicFactor = as(o["metallicFactor"]); + check(metallicFactor >= 0); + check(metallicFactor <= 1); + } + if (o.isMember("metallicRoughnessTexture")) { + metallicRoughnessTexture = as(o["metallicRoughnessTexture"]); + } + if (o.isMember("roughnessFactor")) { + roughnessFactor = as(o["roughnessFactor"]); + check(roughnessFactor >= 0); + check(roughnessFactor <= 1); + } + } +}; +template<> MaterialPbrMetallicRoughness as(const Json::Value &o) { return o; } + +struct Material { + double alphaCutoff; + enum class AlphaMode { + BLEND, + MASK, + OPAQUE, + }; + AlphaMode alphaMode; + bool doubleSided; + std::array emissiveFactor; + std::optional emissiveTexture; + std::optional name; + std::optional normalTexture; + std::optional occlusionTexture; + std::optional pbrMetallicRoughness; + Material(const Json::Value &o) + : alphaCutoff(0.5) + , alphaMode(AlphaMode::OPAQUE) + , doubleSided(false) + , emissiveFactor{0, 0, 0} + { + check(o.isObject()); + if (o.isMember("alphaCutoff")) { + alphaCutoff = as(o["alphaCutoff"]); + check(alphaCutoff >= 0); + } + if (o.isMember("alphaMode")){ + static std::unordered_map map = { + {"BLEND", AlphaMode::BLEND}, + {"MASK", AlphaMode::MASK}, + {"OPAQUE", AlphaMode::OPAQUE}, + }; + auto v = o["alphaMode"]; check(v.isString()); + alphaMode = map.at(v.asString()); + } + if (o.isMember("doubleSided")) { + doubleSided = as(o["doubleSided"]); + } + if (o.isMember("emissiveFactor")) { + emissiveFactor = asArr(o["emissiveFactor"]); + for (auto v: emissiveFactor) { + check(v >= 0); + check(v <= 1); + } + } + if (o.isMember("emissiveTexture")) { + emissiveTexture = as(o["emissiveTexture"]); + } + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("normalTexture")) { + normalTexture = as(o["normalTexture"]); + } + if (o.isMember("occlusionTexture")) { + occlusionTexture = as(o["occlusionTexture"]); + } + if (o.isMember("pbrMetallicRoughness")) { + pbrMetallicRoughness = as(o["pbrMetallicRoughness"]); + } + } +}; +template<> Material as(const Json::Value &o) { return o; } + +struct MeshPrimitive { + static void enumeratedProps(const Json::Value &o, const std::string &name, std::optional> &attr) { + for (std::size_t i = 0;; ++i) { + const std::string s = name + "_" + std::to_string(i); + if (!o.isMember(s)) break; + if (i == 0) { + attr = std::vector(); + } + attr->push_back(as(o[s])); + } + } + struct Attributes { + std::optional position, normal, tangent; + std::optional> texcoord, color, joints, weights; + Attributes(const Json::Value &o) { + if (o.isMember("POSITION")) + position = as(o["POSITION"]); + if (o.isMember("NORMAL")) + normal = as(o["NORMAL"]); + if (o.isMember("TANGENT")) + tangent = as(o["TANGENT"]); + enumeratedProps(o, "TEXCOORD", texcoord); + enumeratedProps(o, "COLOR", color); + enumeratedProps(o, "JOINTS", joints); + enumeratedProps(o, "WEIGHTS", weights); + check(joints.has_value() == weights.has_value()); + if (joints.has_value()) { + check(joints->size() == weights->size()); + } + check(position.has_value() + || normal.has_value() + || tangent.has_value() + || texcoord.has_value() + || color.has_value() + || joints.has_value() + || weights.has_value()); + } + }; + Attributes attributes; + std::optional indices; + std::optional material; + enum class Mode { + POINTS, + LINES, + LINE_LOOP, + LINE_STRIP, + TRIANGLES, + TRIANGLE_STRIP, + TRIANGLE_FAN, + }; + Mode mode; + struct MorphTargets { + std::optional position, normal, tangent; + std::optional> texcoord, color; + MorphTargets(const Json::Value &o) { + if (o.isMember("POSITION")) + position = as(o["POSITION"]); + if (o.isMember("NORMAL")) + normal = as(o["NORMAL"]); + if (o.isMember("TANGENT")) + tangent = as(o["TANGENT"]); + enumeratedProps(o, "TEXCOORD", texcoord); + enumeratedProps(o, "COLOR", color); + check(position.has_value() + || normal.has_value() + || tangent.has_value() + || texcoord.has_value() + || color.has_value()); + } + }; + std::optional> targets; + MeshPrimitive(const Json::Value &o) + : attributes(Attributes(o["attributes"])) + , mode(Mode::TRIANGLES) + { + check(o.isObject()); + if (o.isMember("indices")) { + indices = as(o["indices"]); + } + if (o.isMember("material")) { + material = as(o["material"]); + } + if (o.isMember("mode")) { + static std::unordered_map map = { + {0, Mode::POINTS}, + {1, Mode::LINES}, + {2, Mode::LINE_LOOP}, + {3, Mode::LINE_STRIP}, + {4, Mode::TRIANGLES}, + {5, Mode::TRIANGLE_STRIP}, + {6, Mode::TRIANGLE_FAN}, + }; + auto v = o["mode"]; check(v.isUInt64()); + mode = map.at(v.asUInt64()); + } + if (o.isMember("targets")) { + targets = asVec(o["targets"]); + check(targets->size() >= 1); + } + } +}; +template<> MeshPrimitive::MorphTargets as(const Json::Value &o) { return o; } +template<> MeshPrimitive as(const Json::Value &o) { return o; } + +struct Mesh { + std::optional name; + std::vector primitives; + std::optional> weights; + Mesh(const Json::Value &o) + : primitives(asVec(o["primitives"])) + { + check(o.isObject()); + if (o.isMember("name")) { + name = as(o["name"]); + } + check(primitives.size() >= 1); + if (o.isMember("weights")) { + weights = asVec(o["weights"]); + check(weights->size() >= 1); + } + } +}; +template<> Mesh as(const Json::Value &o) { return o; } + +struct Node { + std::optional camera; + std::optional> children; + typedef std::array Matrix; + struct TRS { + std::array translation = {0, 0, 0}; + std::array rotation = {0, 0, 0, 1}; + std::array scale = {1, 1, 1}; + }; + std::variant transform; + std::optional mesh; + std::optional name; + std::optional skin; + std::optional> weights; + Node(const Json::Value &o) + : transform(Matrix { + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + }) + { + check(o.isObject()); + if (o.isMember("camera")) { + camera = as(o["camera"]); + } + if (o.isMember("children")) { + children = asVec(o["children"]); + check(children->size() >= 1); + checkDuplicateFree(*children); + } + bool hasTRS = o.isMember("translation") || o.isMember("rotation") || o.isMember("scale"); + if (o.isMember("matrix")) { + check(!hasTRS); + transform = asArr(o["matrix"]); + } else if (hasTRS) { + TRS trs; + if (o.isMember("translation")) { + trs.translation = asArr(o["translation"]); + } + if (o.isMember("rotation")) { + trs.rotation = asArr(o["rotation"]); + for (auto v: trs.rotation) { + check(v >= -1); + check(v <= 1); + } + } + if (o.isMember("scale")) { + trs.scale = asArr(o["scale"]); + } + transform = std::move(trs); + } + if (o.isMember("mesh")) { + mesh = as(o["mesh"]); + } + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("skin")) { + check(mesh.has_value()); + skin = as(o["skin"]); + } + if (o.isMember("weights")) { + weights = asVec(o["weights"]); + check(weights->size() >= 1); + } + } +}; +template<> Node as(const Json::Value &o) { return o; } + +struct Sampler { + enum class MagFilter { + NEAREST, + LINEAR, + }; + std::optional magFilter; + enum class MinFilter { + NEAREST, + LINEAR, + NEAREST_MIPMAP_NEAREST, + LINEAR_MIPMAP_NEAREST, + NEAREST_MIPMAP_LINEAR, + LINEAR_MIPMAP_LINEAR, + }; + std::optional minFilter; + std::optional name; + enum class WrapS { + REPEAT, + CLAMP_TO_EDGE, + MIRRORED_REPEAT, + }; + WrapS wrapS; + enum class WrapT { + REPEAT, + CLAMP_TO_EDGE, + MIRRORED_REPEAT, + }; + WrapT wrapT; + Sampler(const Json::Value &o) + : wrapS(WrapS::REPEAT) + , wrapT(WrapT::REPEAT) + { + check(o.isObject()); + if (o.isMember("magFilter")) { + static std::unordered_map map = { + {9728, MagFilter::NEAREST}, + {9729, MagFilter::LINEAR}, + }; + auto v = o["magFilter"]; check(v.isUInt64()); + magFilter = map.at(v.asUInt64()); + } + if (o.isMember("minFilter")) { + static std::unordered_map map = { + {9728, MinFilter::NEAREST}, + {9729, MinFilter::LINEAR}, + {9984, MinFilter::NEAREST_MIPMAP_NEAREST}, + {9985, MinFilter::LINEAR_MIPMAP_NEAREST}, + {9986, MinFilter::NEAREST_MIPMAP_LINEAR}, + {9987, MinFilter::LINEAR_MIPMAP_LINEAR}, + }; + auto v = o["minFilter"]; check(v.isUInt64()); + minFilter = map.at(v.asUInt64()); + } + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("wrapS")) { + static std::unordered_map map = { + {10497, WrapS::REPEAT}, + {33071, WrapS::CLAMP_TO_EDGE}, + {33648, WrapS::MIRRORED_REPEAT}, + }; + auto v = o["wrapS"]; check(v.isUInt64()); + wrapS = map.at(v.asUInt64()); + } + if (o.isMember("wrapT")) { + static std::unordered_map map = { + {10497, WrapT::REPEAT}, + {33071, WrapT::CLAMP_TO_EDGE}, + {33648, WrapT::MIRRORED_REPEAT}, + }; + auto v = o["wrapT"]; check(v.isUInt64()); + wrapT = map.at(v.asUInt64()); + } + } +}; +template<> Sampler as(const Json::Value &o) { return o; } + +struct Scene { + std::optional name; + std::optional> nodes; + Scene(const Json::Value &o) + { + check(o.isObject()); + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("nodes")) { + nodes = asVec(o["nodes"]); + check(nodes->size() >= 1); + checkDuplicateFree(*nodes); + } + } +}; +template<> Scene as(const Json::Value &o) { return o; } + +struct Skin { + std::optional inverseBindMatrices; + std::vector joints; + std::optional name; + std::optional skeleton; + Skin(const Json::Value &o) + : joints(asVec(o["joints"])) + { + check(o.isObject()); + if (o.isMember("inverseBindMatrices")) { + inverseBindMatrices = as(o["inverseBindMatrices"]); + } + check(joints.size() >= 1); + checkDuplicateFree(joints); + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("skeleton")) { + skeleton = as(o["skeleton"]); + } + } +}; +template<> Skin as(const Json::Value &o) { return o; } + +struct Texture { + std::optional name; + std::optional sampler; + std::optional source; + Texture(const Json::Value &o) + { + check(o.isObject()); + if (o.isMember("name")) { + name = as(o["name"]); + } + if (o.isMember("sampler")) { + sampler = as(o["sampler"]); + } + if (o.isMember("source")) { + source = as(o["source"]); + } + } +}; +template<> Texture as(const Json::Value &o) { return o; } + +struct GlTF { + std::optional> accessors; + std::optional> animations; + Asset asset; + std::optional> bufferViews; + std::optional> buffers; + std::optional> cameras; + std::optional> extensionsRequired; + std::optional> extensionsUsed; + std::optional> images; + std::optional> materials; + std::optional> meshes; + std::optional> nodes; + std::optional> samplers; + std::optional scene; + std::optional> scenes; + std::optional> skins; + std::optional> textures; + static std::vector uriError(const std::string &uri) { + // only base64 data URI support by default + throw std::runtime_error("unsupported URI: " + uri); + } + GlTF(const Json::Value &o, + const std::function(const std::string &uri)> &resolveURI = uriError) + : asset(as(o["asset"])) + { + check(o.isObject()); + if (o.isMember("accessors")) { + accessors = asVec(o["accessors"]); + check(accessors->size() >= 1); + } + if (o.isMember("animations")) { + animations = asVec(o["animations"]); + check(animations->size() >= 1); + } + if (o.isMember("bufferViews")) { + bufferViews = asVec(o["bufferViews"]); + check(bufferViews->size() >= 1); + } + if (o.isMember("buffers")) { + auto b = o["buffers"]; + check(b.isArray()); + std::vector bufs; + bufs.reserve(b.size()); + for (Json::ArrayIndex i = 0; i < b.size(); ++i) { + bufs.push_back(Buffer(b[i], resolveURI)); + } + check(bufs.size() >= 1); + buffers = std::move(bufs); + } + if (o.isMember("cameras")) { + cameras = asVec(o["cameras"]); + check(cameras->size() >= 1); + } + if (o.isMember("extensionsRequired")) { + extensionsRequired = asVec(o["extensionsRequired"]); + check(extensionsRequired->size() >= 1); + checkDuplicateFree(*extensionsRequired); + } + if (o.isMember("extensionsUsed")) { + extensionsUsed = asVec(o["extensionsUsed"]); + check(extensionsUsed->size() >= 1); + checkDuplicateFree(*extensionsUsed); + } + if (o.isMember("images")) { + images = asVec(o["images"]); + check(images->size() >= 1); + } + if (o.isMember("materials")) { + materials = asVec(o["materials"]); + check(materials->size() >= 1); + } + if (o.isMember("meshes")) { + meshes = asVec(o["meshes"]); + check(meshes->size() >= 1); + } + if (o.isMember("nodes")) { + nodes = asVec(o["nodes"]); + check(nodes->size() >= 1); + // Nodes must be a forest: + // 1. Each node should have indegree 0 or 1: + std::vector indeg(nodes->size()); + for (std::size_t i = 0; i < nodes->size(); ++i) { + auto children = nodes->at(i).children; + if (!children.has_value()) continue; + for (auto child : children.value()) { + ++indeg.at(child); + } + } + for (const auto deg : indeg) { + check(deg <= 1); + } + // 2. There should be no cycles: + std::vector visited(nodes->size()); + std::stack> toVisit; + for (std::size_t i = 0; i < nodes->size(); ++i) { + // Only start DFS in roots. + if (indeg[i] > 0) + continue; + + toVisit.push(i); + do { + std::size_t j = toVisit.top(); + check(!visited.at(j)); + visited[j] = true; + toVisit.pop(); + auto children = nodes->at(j).children; + if (!children.has_value()) + continue; + for (auto child : *children) { + toVisit.push(child); + } + } while (!toVisit.empty()); + } + } + if (o.isMember("samplers")) { + samplers = asVec(o["samplers"]); + check(samplers->size() >= 1); + } + if (o.isMember("scene")) { + scene = as(o["scene"]); + } + if (o.isMember("scenes")) { + scenes = asVec(o["scenes"]); + check(scenes->size() >= 1); + } + if (o.isMember("skins")) { + skins = asVec(o["skins"]); + check(skins->size() >= 1); + } + if (o.isMember("textures")) { + textures = asVec(o["textures"]); + check(textures->size() >= 1); + } + + // Validation + + checkForall(bufferViews, [&](const BufferView &view) { + check(buffers.has_value()); + const Buffer &buf = buffers->at(view.buffer); + // Be careful because of possible integer overflows. + check(view.byteOffset < buf.byteLength); + check(view.byteLength <= buf.byteLength); + check(view.byteOffset <= buf.byteLength - view.byteLength); + }); + + checkForall(accessors, [&](const Accessor &accessor) { + checkIndex(bufferViews, accessor.bufferView); + if (accessor.bufferView.has_value()) { + const BufferView &view = bufferViews->at(accessor.bufferView.value()); + if (view.byteStride.has_value()) + check(view.byteStride.value() % accessor.componentSize() == 0); + check(accessor.byteOffset < view.byteLength); + // Use division to avoid overflows. + // TODO this should be (but written in such a way that overflows are avoided): + // `accessor.byteOffset + EFFECTIVE_BYTE_STRIDE * (accessor.count - 1) + SIZE_OF_COMPONENT * NUMBER_OF_COMPONENTS` + check(accessor.count <= view.byteLength / view.byteStride.value_or(accessor.elementSize())); + } + if (accessor.sparse.has_value()) { + // Again be careful because of possible integer overflows. + { + const auto &indices = accessor.sparse->indices; + checkIndex(bufferViews, indices.bufferView); + const BufferView &view = bufferViews->at(indices.bufferView); + check(indices.byteOffset < view.byteLength); + // TODO take byte stride into account + check(accessor.sparse->count <= (view.byteLength - indices.byteOffset) / indices.elementSize()); + } + { + const auto &values = accessor.sparse->values; + checkIndex(bufferViews, values.bufferView); + const BufferView &view = bufferViews->at(values.bufferView); + check(values.byteOffset < view.byteLength); + // TODO take byte stride into account + check(accessor.sparse->count <= (view.byteLength - values.byteOffset) / accessor.elementSize()); + } + } + }); + + checkForall(images, [&](const Image &image) { + checkIndex(bufferViews, image.bufferView); + }); + + checkForall(meshes, [&](const Mesh &mesh) { + for (const auto &primitive : mesh.primitives) { + checkIndex(accessors, primitive.indices); + checkIndex(materials, primitive.material); + checkIndex(accessors, primitive.attributes.normal); + checkIndex(accessors, primitive.attributes.position); + checkIndex(accessors, primitive.attributes.tangent); + checkForall(primitive.attributes.texcoord, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + checkForall(primitive.attributes.color, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + checkForall(primitive.attributes.joints, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + checkForall(primitive.attributes.weights, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + if (primitive.material.has_value()) { + const Material &material = materials->at(primitive.material.value()); + if (material.emissiveTexture.has_value()) { + check(primitive.attributes.texcoord.has_value()); + check(material.emissiveTexture->texCoord < primitive.attributes.texcoord->size()); + } + if (material.normalTexture.has_value()) { + check(primitive.attributes.texcoord.has_value()); + check(material.normalTexture->texCoord < primitive.attributes.texcoord->size()); + } + if (material.occlusionTexture.has_value()) { + check(primitive.attributes.texcoord.has_value()); + check(material.occlusionTexture->texCoord < primitive.attributes.texcoord->size()); + } + } + checkForall(primitive.targets, [&](const MeshPrimitive::MorphTargets &target) { + checkIndex(accessors, target.normal); + checkIndex(accessors, target.position); + checkIndex(accessors, target.tangent); + checkForall(target.texcoord, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + checkForall(target.color, [&](const std::size_t &i) { + checkIndex(accessors, i); + }); + }); + } + }); + + checkForall(nodes, [&](const Node &node) { + checkIndex(cameras, node.camera); + checkIndex(meshes, node.mesh); + checkIndex(skins, node.skin); + }); + + checkForall(scenes, [&](const Scene &scene) { + checkForall(scene.nodes, [&](const size_t &i) { + checkIndex(nodes, i); + }); + }); + + checkForall(skins, [&](const Skin &skin) { + checkIndex(accessors, skin.inverseBindMatrices); + for (const std::size_t &i : skin.joints) + checkIndex(nodes, i); + checkIndex(nodes, skin.skeleton); + }); + + checkForall(textures, [&](const Texture &texture) { + checkIndex(samplers, texture.sampler); + checkIndex(images, texture.source); + }); + + checkForall(animations, [&](const Animation &animation) { + for (const auto &sampler : animation.samplers) { + checkIndex(accessors, sampler.input); + const auto &accessor = accessors->at(sampler.input); + check(accessor.type == Accessor::Type::SCALAR); + check(accessor.componentType == Accessor::ComponentType::FLOAT); + checkIndex(accessors, sampler.output); + } + for (const auto &channel : animation.channels) { + checkIndex(nodes, channel.target.node); + checkIndex(animation.samplers, channel.sampler); + } + }); + + checkIndex(scenes, scene); + } +}; + +} \ No newline at end of file diff --git a/src/client/client.cpp b/src/client/client.cpp index 61140d87a..c7a8ae2c4 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -833,7 +833,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename, } const char *model_ext[] = { - ".x", ".b3d", ".obj", + ".x", ".b3d", ".obj", ".gltf", NULL }; name = removeStringEnd(filename, model_ext); diff --git a/src/server.cpp b/src/server.cpp index 316f349b2..a61209c4e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2531,7 +2531,7 @@ bool Server::addMediaFile(const std::string &filename, const char *supported_ext[] = { ".png", ".jpg", ".bmp", ".tga", ".ogg", - ".x", ".b3d", ".obj", + ".x", ".b3d", ".obj", ".gltf", // Custom translation file format ".tr", NULL