Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
ModelImporter.hpp
1#pragma once
2
3#include <filesystem>
4#include <string>
5#include <vector>
6#include <map>
7
8#include <assimp/scene.h>
9#include <glm/glm.hpp>
10
11#include <Common/Formats/Model.hpp>
12#include <Common/ResourcePipeline/Uuid.hpp>
13#include <Common/Editor/Importer.hpp>
14
15namespace Grindstone::Editor::Importers {
16 const uint16_t NUM_BONES_PER_VERTEX = 4;
17
18 class ModelImporter : public Importer {
19 public:
20 void Import(Grindstone::Editor::AssetRegistry& assetRegistry, Grindstone::Assets::AssetManager& assetManager, const std::filesystem::path& path) override;
21 private:
22 struct Prefab {
23
24 };
25
26 struct ScenePrefab {
27
28 };
29
30 struct Submesh {
31 uint32_t indexCount = 0;
32 uint32_t baseVertex = 0;
33 uint32_t baseIndex = 0;
34 uint32_t materialIndex = UINT32_MAX;
35 };
36
37 private:
38 // Members
39 Grindstone::Assets::AssetManager* assetManager = nullptr;
40 Grindstone::Editor::AssetRegistry* assetRegistry = nullptr;
41 std::filesystem::path path;
42 std::filesystem::path baseFolderPath;
43 std::map<std::string, glm::mat4> tempOffsetMatrices; // Save string->offset matrix so we can use it when constructing the bone data
44 std::map<std::string, unsigned int> boneMapping;
45 const aiScene* scene = nullptr;
46 bool hasExtraWeights = false;
47 bool isSkeletalMesh = false;
48
49 struct BoneData {
50 uint16_t parentIndex;
51 glm::mat4 offsetMatrix;
52 glm::mat4 inverseModelMatrix;
53
54 BoneData(uint16_t parentIndex, glm::mat4& offsetMatrix, glm::mat4& inverseMatrix) {
55 this->parentIndex = parentIndex;
56 this->offsetMatrix = offsetMatrix;
57 this->inverseModelMatrix = inverseMatrix;
58 }
59 };
60
61 struct OutputMesh {
62 std::string name;
64 uint32_t vertexCount = 0;
65 uint16_t boneCount = 0;
66 struct VertexArray {
67 std::vector<float> position;
68 std::vector<float> normal;
69 std::vector<float> tangent;
70 std::vector<uint16_t> boneIds; // For animation
71 std::vector<float> boneWeights; // For animation
72 std::vector<std::vector<float>> texCoordArray;
73 } vertexArray;
74 std::vector<uint16_t> indices;
75 std::vector<Submesh> submeshes;
76 std::vector<BoneData> bones;
77 std::vector<std::string> boneNames;
78 };
79
80 struct OutputNode {
81 size_t parentNode = SIZE_MAX;
82 std::string name;
83 aiVector3D position;
84 aiVector3D scale = aiVector3D(1.0f, 1.0f, 1.0f);
85 aiQuaternion rotation;
86 size_t meshIndex = SIZE_MAX;
87 aiLight* lightData = nullptr;
88 aiCamera* cameraData = nullptr;
89 };
90
91 std::vector<std::string> outputMaterialUuids;
92 std::vector<std::string> outputMeshUuids;
93 std::vector<OutputMesh> outputMeshes;
94 std::vector<OutputNode> outputNodes;
95
96 void ProcessLight(aiLight* light);
97 void ProcessCamera(aiCamera* camera);
98 void ProcessNodeTree(aiNode* node, size_t parentIndex);
99 void ProcessMaterial(size_t materialIndex, aiMaterial* inputMaterial);
100 void ProcessSkeleton(aiSkeleton* skeleton);
101 void ProcessVertexBoneWeights(aiMesh* inputMesh, OutputMesh& outputMesh);
102 void NormalizeBoneWeights(OutputMesh& outputMesh);
103 void ProcessAnimation(aiAnimation* animation);
104 void AddBoneData(OutputMesh& outputMesh, unsigned int vertexId, unsigned int boneId, float vertexWeight);
105 void InitSubmeshes(aiMesh* inputMesh, OutputMesh& outputMesh, bool hasBones);
106 void ProcessVertices(aiMesh* inputMesh, OutputMesh& outputMesh);
107 void WritePrefab();
108 void WriteMesh(size_t index, const OutputMesh& mesh);
109 void WriteSkeleton(size_t index) const;
110 };
111
112 const Grindstone::Editor::ImporterVersion modelImporterVersion = 1;
113
114 void ImportModel(Grindstone::Editor::AssetRegistry& assetRegistry, Grindstone::Assets::AssetManager& assetManager, const std::filesystem::path& path);
115}
Definition AssetManager.hpp:15
Definition AssetRegistry.hpp:15
Definition Importer.hpp:15
Definition ModelImporter.hpp:18