Extract loadPrimitives method from createMesh

This commit is contained in:
JosiahWI 2023-05-18 07:11:33 -05:00 committed by Josiah VanderZee
parent f383dd3c84
commit 311a176cf0
2 changed files with 146 additions and 137 deletions

View File

@ -92,7 +92,18 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
MeshExtractor parser(std::move(model)); MeshExtractor parser(std::move(model));
SMesh* baseMesh(new SMesh {}); SMesh* baseMesh(new SMesh {});
loadPrimitives(parser, baseMesh);
SAnimatedMesh* animatedMesh(new SAnimatedMesh {});
animatedMesh->addMesh(baseMesh);
return animatedMesh;
}
void CGLTFMeshFileLoader::loadPrimitives(
const MeshExtractor& parser,
SMesh* mesh)
{
for (std::size_t i = 0; i < parser.getMeshCount(); ++i) { for (std::size_t i = 0; i < parser.getMeshCount(); ++i) {
for (std::size_t j = 0; j < parser.getPrimitiveCount(i); ++j) { for (std::size_t j = 0; j < parser.getPrimitiveCount(i); ++j) {
auto indices = parser.getIndices(i, j); auto indices = parser.getIndices(i, j);
@ -101,14 +112,9 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
SMeshBuffer* meshbuf(new SMeshBuffer {}); SMeshBuffer* meshbuf(new SMeshBuffer {});
meshbuf->append(vertices.data(), vertices.size(), meshbuf->append(vertices.data(), vertices.size(),
indices.data(), indices.size()); indices.data(), indices.size());
baseMesh->addMeshBuffer(meshbuf); mesh->addMeshBuffer(meshbuf);
} }
} }
SAnimatedMesh* animatedMesh(new SAnimatedMesh {});
animatedMesh->addMesh(baseMesh);
return animatedMesh;
} }
CGLTFMeshFileLoader::MeshExtractor::MeshExtractor( CGLTFMeshFileLoader::MeshExtractor::MeshExtractor(

View File

@ -1,131 +1,134 @@
#ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__ #ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#define __C_GLTF_MESH_FILE_LOADER_INCLUDED__ #define __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#include "IAnimatedMesh.h" #include "IAnimatedMesh.h"
#include "IMeshLoader.h" #include "IMeshLoader.h"
#include "IReadFile.h" #include "IReadFile.h"
#include "irrTypes.h" #include "irrTypes.h"
#include "path.h" #include "path.h"
#include "S3DVertex.h" #include "S3DVertex.h"
#include "vector2d.h" #include "SMesh.h"
#include "vector3d.h" #include "vector2d.h"
#include "vector3d.h"
#include <tiny_gltf.h>
#include <tiny_gltf.h>
#include <cstddef>
#include <vector> #include <cstddef>
#include <vector>
namespace irr
{ namespace irr
{
namespace scene
{ namespace scene
{
class CGLTFMeshFileLoader : public IMeshLoader
{ class CGLTFMeshFileLoader : public IMeshLoader
public: {
CGLTFMeshFileLoader() noexcept; public:
CGLTFMeshFileLoader() noexcept;
bool isALoadableFileExtension(const io::path& filename) const override;
bool isALoadableFileExtension(const io::path& filename) const override;
IAnimatedMesh* createMesh(io::IReadFile* file) override;
IAnimatedMesh* createMesh(io::IReadFile* file) override;
private:
template <typename T> private:
struct Span template <typename T>
{ struct Span
T* buffer = nullptr; {
std::size_t size = 0; T* buffer = nullptr;
}; std::size_t size = 0;
};
class BufferOffset
{ class BufferOffset
public: {
BufferOffset(const std::vector<unsigned char>& buf, public:
const std::size_t offset); BufferOffset(const std::vector<unsigned char>& buf,
const std::size_t offset);
BufferOffset(const BufferOffset& other,
const std::size_t fromOffset); BufferOffset(const BufferOffset& other,
const std::size_t fromOffset);
unsigned char at(const std::size_t fromOffset) const;
private: unsigned char at(const std::size_t fromOffset) const;
const std::vector<unsigned char>& m_buf; private:
std::size_t m_offset; const std::vector<unsigned char>& m_buf;
int m_filesize; std::size_t m_offset;
}; int m_filesize;
};
class MeshExtractor {
public: class MeshExtractor {
MeshExtractor(const tinygltf::Model& model) noexcept; public:
MeshExtractor(const tinygltf::Model& model) noexcept;
MeshExtractor(const tinygltf::Model&& model) noexcept;
MeshExtractor(const tinygltf::Model&& model) noexcept;
/* Gets indices for the given mesh/primitive.
* /* Gets indices for the given mesh/primitive.
* Values are return in Irrlicht winding order. *
*/ * Values are return in Irrlicht winding order.
std::vector<u16> getIndices(const std::size_t meshIdx, */
const std::size_t primitiveIdx) const; std::vector<u16> getIndices(const std::size_t meshIdx,
const std::size_t primitiveIdx) const;
std::vector<video::S3DVertex> getVertices(std::size_t meshIdx,
const std::size_t primitiveIdx) const; std::vector<video::S3DVertex> getVertices(std::size_t meshIdx,
const std::size_t primitiveIdx) const;
std::size_t getMeshCount() const;
std::size_t getMeshCount() const;
std::size_t getPrimitiveCount(const std::size_t meshIdx) const;
std::size_t getPrimitiveCount(const std::size_t meshIdx) const;
private:
tinygltf::Model m_model; private:
tinygltf::Model m_model;
template <typename T>
static T readPrimitive(const BufferOffset& readFrom); template <typename T>
static T readPrimitive(const BufferOffset& readFrom);
static core::vector2df readVec2DF(
const BufferOffset& readFrom); static core::vector2df readVec2DF(
const BufferOffset& readFrom);
/* Read a vec3df from a buffer with transformations applied.
* /* Read a vec3df from a buffer with transformations applied.
* Values are returned in Irrlicht coordinates. *
*/ * Values are returned in Irrlicht coordinates.
static core::vector3df readVec3DF( */
const BufferOffset& readFrom, static core::vector3df readVec3DF(
const float scale = 1.0f); const BufferOffset& readFrom,
const float scale = 1.0f);
void copyPositions(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const; void copyPositions(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const;
void copyNormals(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const; void copyNormals(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const;
void copyTCoords(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const; void copyTCoords(const Span<video::S3DVertex> vertices,
const std::size_t accessorId) const;
/* Get the scale factor from the glTF mesh information.
* /* Get the scale factor from the glTF mesh information.
* Returns 1.0f if no scale factor is present. *
*/ * Returns 1.0f if no scale factor is present.
float getScale() const; */
float getScale() const;
std::size_t getElemCount(const std::size_t accessorIdx) const;
std::size_t getElemCount(const std::size_t accessorIdx) const;
BufferOffset getBuffer(const std::size_t meshIdx,
const std::size_t primitiveIdx, BufferOffset getBuffer(const std::size_t meshIdx,
const std::size_t accessorIdx) const; const std::size_t primitiveIdx,
const std::size_t accessorIdx) const;
std::size_t getIndicesAccessorIdx(const std::size_t meshIdx,
const std::size_t primitiveIdx) const; 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; std::size_t getPositionAccessorIdx(const std::size_t meshIdx,
}; const std::size_t primitiveIdx) const;
};
static bool tryParseGLTF(io::IReadFile* file,
tinygltf::Model& model); void loadPrimitives(const MeshExtractor& parser, SMesh* mesh);
};
static bool tryParseGLTF(io::IReadFile* file,
} // namespace scene tinygltf::Model& model);
};
} // namespace irr
} // namespace scene
#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__
} // namespace irr
#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__