2023-05-18 07:11:33 -05:00
|
|
|
#ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__
|
|
|
|
#define __C_GLTF_MESH_FILE_LOADER_INCLUDED__
|
|
|
|
|
|
|
|
#include "IAnimatedMesh.h"
|
|
|
|
#include "IMeshLoader.h"
|
|
|
|
#include "IReadFile.h"
|
|
|
|
#include "irrTypes.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "S3DVertex.h"
|
|
|
|
#include "SMesh.h"
|
|
|
|
#include "vector2d.h"
|
|
|
|
#include "vector3d.h"
|
|
|
|
|
|
|
|
#include <tiny_gltf.h>
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
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<unsigned char>& 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<unsigned char>& m_buf;
|
|
|
|
std::size_t m_offset;
|
|
|
|
int m_filesize;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MeshExtractor {
|
|
|
|
public:
|
2023-05-18 07:33:04 -05:00
|
|
|
using vertex_t = video::S3DVertex;
|
|
|
|
|
2023-05-18 07:11:33 -05:00
|
|
|
MeshExtractor(const tinygltf::Model& model) noexcept;
|
|
|
|
|
|
|
|
MeshExtractor(const tinygltf::Model&& model) noexcept;
|
|
|
|
|
|
|
|
/* Gets indices for the given mesh/primitive.
|
|
|
|
*
|
|
|
|
* Values are return in Irrlicht winding order.
|
|
|
|
*/
|
|
|
|
std::vector<u16> getIndices(const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const;
|
|
|
|
|
2023-05-18 07:33:04 -05:00
|
|
|
std::vector<vertex_t> getVertices(std::size_t meshIdx,
|
2023-05-18 07:11:33 -05:00
|
|
|
const std::size_t primitiveIdx) const;
|
|
|
|
|
|
|
|
std::size_t getMeshCount() const;
|
|
|
|
|
|
|
|
std::size_t getPrimitiveCount(const std::size_t meshIdx) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
tinygltf::Model m_model;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
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,
|
2023-12-01 17:10:01 -05:00
|
|
|
const float scale);
|
2023-05-18 07:11:33 -05:00
|
|
|
|
2023-05-18 07:33:04 -05:00
|
|
|
void copyPositions(const std::size_t accessorIdx,
|
|
|
|
std::vector<vertex_t>& vertices) const;
|
2023-05-18 07:11:33 -05:00
|
|
|
|
2023-05-18 07:33:04 -05:00
|
|
|
void copyNormals(const std::size_t accessorIdx,
|
|
|
|
std::vector<vertex_t>& vertices) const;
|
2023-05-18 07:11:33 -05:00
|
|
|
|
2023-05-18 07:33:04 -05:00
|
|
|
void copyTCoords(const std::size_t accessorIdx,
|
|
|
|
std::vector<vertex_t>& vertices) const;
|
2023-05-18 07:11:33 -05:00
|
|
|
|
|
|
|
/* Get the scale factor from the glTF mesh information.
|
|
|
|
*
|
2023-12-01 17:10:01 -05:00
|
|
|
* Returns vec3(1.0, 1.0, 1.0) if no scale factor is present.
|
2023-05-18 07:11:33 -05:00
|
|
|
*/
|
|
|
|
float getScale() const;
|
|
|
|
|
|
|
|
std::size_t getElemCount(const std::size_t accessorIdx) const;
|
|
|
|
|
2023-11-29 19:57:58 -06:00
|
|
|
std::size_t getByteStride(const std::size_t accessorIdx) const;
|
|
|
|
|
2023-12-01 17:10:01 -05:00
|
|
|
bool isAccessorNormalized(const std::size_t accessorIdx) const;
|
|
|
|
|
2023-05-18 07:33:04 -05:00
|
|
|
BufferOffset getBuffer(const std::size_t accessorIdx) const;
|
2023-05-18 07:11:33 -05:00
|
|
|
|
|
|
|
std::size_t getIndicesAccessorIdx(const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const;
|
|
|
|
|
|
|
|
std::size_t getPositionAccessorIdx(const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const;
|
2023-05-18 07:33:04 -05:00
|
|
|
|
|
|
|
/* Get the accessor id of the normals of a primitive.
|
|
|
|
*
|
|
|
|
* -1 is returned if none are present.
|
|
|
|
*/
|
|
|
|
std::size_t getNormalAccessorIdx(const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const;
|
|
|
|
|
|
|
|
/* Get the accessor id for the tcoords of a primitive.
|
|
|
|
*
|
|
|
|
* -1 is returned if none are present.
|
|
|
|
*/
|
|
|
|
std::size_t getTCoordAccessorIdx(const std::size_t meshIdx,
|
|
|
|
const std::size_t primitiveIdx) const;
|
2023-05-18 07:11:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void loadPrimitives(const MeshExtractor& parser, SMesh* mesh);
|
|
|
|
|
|
|
|
static bool tryParseGLTF(io::IReadFile* file,
|
|
|
|
tinygltf::Model& model);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace scene
|
|
|
|
|
|
|
|
} // namespace irr
|
|
|
|
|
|
|
|
#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__
|
|
|
|
|