Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
gltf.h
1#ifndef OE_SCENE_LOADER_GLTF_H
2#define OE_SCENE_LOADER_GLTF_H
3
4#include "../../lib/json_fwd.hpp"
5#include <functional>
6#include <variant>
7#include "base.h"
8#include "../node/multi_mesh.h"
9#include "../mesh.h"
10
11#include "gltf_extensions.h"
12
13namespace oe::scene
14{
15 class SkinnedMeshNode;
16}
17
18namespace oe::render
19{
20 class TextureManager;
21}
22
23namespace tinygltf
24{
25 class TinyGLTF;
26 class Model;
27}
28
29namespace oe::scene::loader
30{
66 class Gltf : public BaseLoader
67 {
68 public:
74 Gltf(const std::string& filename);
75 Gltf(Gltf&&);
76
77 ~Gltf();
78
86 oe::scene::Node& addIntoSceneManager(oe::scene::Manager& scene_manager, uint16_t id_scene = 0);
87
96 oe::scene::Node& addIntoSceneManager(oe::scene::Manager& scene_manager, oe::scene::Node& root_node, uint16_t id_scene = 0);
97
102
106 void setMaterialFiller(std::function<void(const PbrMaterial& gltf_material, Material& oe_material)>);
107
111 void setNodeMaterialModifier(std::function<void(Node& node, Material& node_material)>);
112
119 std::vector<Material> getMaterials();
120
121 //void setOnTexture(???);
122
123 /*std::shared_ptr<oe::scene::Node>& getScene(uint16_t i)
124 {
125 return _scenes.at(i);
126 }
127
128 std::shared_ptr<oe::scene::Node>& getNode(uint16_t i)
129 {
130 return _nodes.at(i);
131 }
132
133 std::shared_ptr<oe::scene::Material>& getMaterial(uint16_t i = 0);
134
135 std::shared_ptr<oe::scene::SkinnedMeshNode>& getSkinnedNode(uint16_t i = 0);*/
136
137 protected:
138 Gltf();
139
140 std::vector<std::shared_ptr<oe::scene::Node>> _scenes;
141
142 // Todo clean unused members...
143 //std::vector<oe::scene::Node*> _nodes;
144 /* std::vector<std::shared_ptr<oe::scene::SkinnedMeshNode>> _skinned_nodes;
145*/
146 // std::vector<std::shared_ptr<oe::scene::Material>> _materials;
147
148 // For each mesh, contain the primitive data + material id
149 // Maybe make a difference between classic vertex and skinned primitives ?
151
152 // For each mesh, contains the primitive data + material id
153 std::unordered_map<uint32_t, std::list<std::pair<std::shared_ptr<oe::scene::Mesh>, int32_t>>> _meshes;
154
155 // For each skinned mesh, contains the primitive data + material id
156 std::unordered_map<uint32_t, std::list<std::pair<std::shared_ptr<oe::scene::SkinnedMesh>, int32_t>>> _skinned_meshes;
157
158 // For each skinned, contain the skinnedMeshNode + skin id
159 std::unordered_map<oe::scene::SkinnedMeshNode*, uint32_t> _skinned_nodes_skins;
160
161 // std::vector<oe::scene::MultiMeshNode> _meshes;
162 //std::vector<oe::scene::MultiSkinnedMeshNode> _skinned_meshes;
163 // OU
164 //std::vector<oe::scene::MultiMeshNode<Mesh>> _skinned_meshes;
165
166 std::vector<std::shared_ptr<oe::render::Texture>> _textures;
167
168 std::function<void(const PbrMaterial&, Material&)> _material_filler = {};
169 std::function<void(Node& node, Material& node_material)> _node_material_modifier = {};
170
171 oe::scene::Material _default_material;
172
173 std::unique_ptr<tinygltf::TinyGLTF> _loader;
174 std::unique_ptr<tinygltf::Model> _model;
175
176 void _custom_load(const std::string& filename);
177
178 std::function<void(const std::string& extension, Node& root_node, const nlohmann::json& extension_data, const bool pre_load)> _custom_asset_extension_handler = {};
179 std::function<void(const std::string& extension, Node& node, const nlohmann::json& extension_data)> _custom_node_extension_handler = {};
180 std::function<void(const std::string& extension, Material& material, const nlohmann::json& extension_data)> _custom_material_extension_handler = {};
181 };
182
209 template <typename ExtensionManager = gltf::ExtensionManager<gltf::Extension>>
211 {
212 public:
218 GltfWithExtensions(const ExtensionManager& extension_manager):
219 Gltf(),
220 extensions(extension_manager)
221 {
222 _custom_asset_extension_handler = [this] (const std::string& extension, Node& root_node, const nlohmann::json& extension_data, const bool pre_load)
223 {
224 extensions.applyOnAsset(extension, root_node, extension_data, pre_load);
225 };
226
227 _custom_node_extension_handler = [this] (const std::string& extension, Node& node, const nlohmann::json& extension_data)
228 {
229 extensions.applyOnNode(extension, node, extension_data);
230 };
231 }
232
238 void load(const std::string& filename)
239 {
240 _custom_load(filename);
241 }
242
243 ExtensionManager extensions;
244 };
245}
246
247#endif
handles the load and deletion of textures/cubemaps
Definition texture_manager.h:51
Scene manager.
Definition manager.h:21
Render agnostic material.
Definition material.h:26
Definition node.h:31
Definition base.h:11
Load a glTF asset with custom extensions registered.
Definition gltf.h:211
void load(const std::string &filename)
Load a glTF asset from a file.
Definition gltf.h:238
GltfWithExtensions(const ExtensionManager &extension_manager)
Init the glTF loader with a custom list of extensions.
Definition gltf.h:218
Loader to load Khronos glTF assets.
Definition gltf.h:67
std::unordered_map< uint32_t, std::list< std::pair< std::shared_ptr< oe::scene::Mesh >, int32_t > > > _meshes
std::list<std::pair<oe::scene::Mesh, int32_t>> _primitives;
Definition gltf.h:153
void setNodeMaterialModifier(std::function< void(Node &node, Material &node_material)>)
Apply application specific properties on the material that will be applied to a node.
oe::scene::Node & addIntoSceneManager(oe::scene::Manager &scene_manager, uint16_t id_scene=0)
Load the glTF asset into a scene (under a new root node)
std::vector< Material > getMaterials()
Convert and return materials.
Gltf(const std::string &filename)
Load a glTF asset from a file.
oe::scene::Node & addIntoSceneManager(oe::scene::Manager &scene_manager, oe::scene::Node &root_node, uint16_t id_scene=0)
Load the glTF asset into a scene (using a specified node as root node)
void setMaterialFiller(std::function< void(const PbrMaterial &gltf_material, Material &oe_material)>)
Apply application specific properties on the materials.
void convertTextures(render::TextureManager &)
Load model textures.
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures)
Definition opengl.h:10
Generators of geometry / scene by computation (eg. Bezier) or from files (eg. Gltf)
Definition base.h:9
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19
Common properties/textures tied to a PBR material.
Definition material.h:238