Loading...
Searching...
No Matches
ModelLoader.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "assimp/postprocess.h"
10#include "assimp/scene.h"
11#include "hellfire/graphics/Vertex.h"
12#include "hellfire/graphics/material/Material.h"
13
14
15namespace hellfire {
16 class Mesh;
17 using EntityID = uint32_t;
18 class Scene;
19}
20
21namespace hellfire::Addons {
23 public:
24 // Updated method signatures to include ShaderManager
25 static EntityID load_model(Scene *scene, const std::filesystem::path &filepath, unsigned int import_flags = 0);
26
27 static void clear_cache();
28
29 static void print_cache_stats();
30
31 // Import flag presets
32 struct ImportFlags {
33 // For editor preview - preserve hierarchy, minimal processing
34 static constexpr unsigned int PREVIEW =
40
41 // For runtime use - preserve hierarchy but optimize vertices
42 static constexpr unsigned int RUNTIME =
53
54 // For high quality/production builds
55 static constexpr unsigned int HIGH_QUALITY =
56 RUNTIME |
62
63 // For baked/optimized static meshes
64 static constexpr unsigned int OPTIMIZED =
65 RUNTIME |
66 aiProcess_OptimizeMeshes | // Merge meshes
67 aiProcess_OptimizeGraph | // Flatten hierarchy
68 aiProcess_PreTransformVertices; // Bake transforms
69 };
70
71 private:
72 enum class MaterialType {
73 LAMBERT,
74 PHONG,
75 PBR
76 };
77
78 // Node processing
79 static EntityID process_node(Scene *scene, aiNode *node, const aiScene *ai_scene, const std::string &filepath,
80 EntityID parent_id = 0);
81
82 static bool is_identity_transform(const aiMatrix4x4 &matrix);
83
84 // Mesh processing
85 static void process_mesh_vertices(aiMesh *mesh, std::vector<Vertex> &vertices,
86 std::vector<unsigned int> &indices);
87
88 static std::shared_ptr<Mesh> process_mesh(aiMesh *mesh, const aiScene *scene, const std::string &filepath);
89
90 // Material processing
91 static void preprocess_materials(const aiScene *scene, const std::string &filepath);
92
93 static std::shared_ptr<Material> create_material(const aiMaterial *ai_material, const aiScene *scene,
94 const std::string &filepath);
95
96 static void load_essential_material_properties(const aiMaterial *ai_material, Material &material);
97
98 static void load_material_textures(const aiMaterial *ai_material, Material &material, const aiScene *scene,
99 const std::string &filepath);
100
101 static bool try_load_embedded_texture_unified(const std::string &path_str, const aiScene *scene,
102 TextureType type,
103 Material &material);
104
105 static bool try_load_external_texture_unified(const std::string &path_str, const std::string &filepath,
106 TextureType type,
107 Material &material);
108
109 // Texture processing
110 static bool try_load_embedded_texture(const std::string &path_str, const aiScene *scene, TextureType dcr_type,
111 Material &material, const std::string &property_name);
112
113 static bool try_load_external_texture(const std::string &path_str, const std::string &filepath,
114 TextureType dcr_type, Material &material,
115 const std::string &property_name);
116
117 static std::shared_ptr<Texture> load_cached_texture(const std::string &path, TextureType type);
118
119 // Utility methods
120 static std::string create_mesh_key(aiMesh *mesh, const std::string &filepath);
121
122 static std::string create_material_key(const aiMaterial *ai_material, const std::string &filepath,
123 unsigned int material_index);
124
125 static std::string capitalize_first(const std::string &str);
126
127 static void debug_scene_info(const aiScene *scene, const std::string &filepath);
128
129
130 static std::vector<std::string> get_texture_search_paths(const std::string &filepath);
131
132 private:
133 // Caching
137 };
138}
#define MODEL_LOADER_DEBUG
static std::shared_ptr< Texture > load_cached_texture(const std::string &path, TextureType type)
static void process_mesh_vertices(aiMesh *mesh, std::vector< Vertex > &vertices, std::vector< unsigned int > &indices)
static bool is_identity_transform(const aiMatrix4x4 &matrix)
static std::shared_ptr< Mesh > process_mesh(aiMesh *mesh, const aiScene *scene, const std::string &filepath)
static EntityID process_node(Scene *scene, aiNode *node, const aiScene *ai_scene, const std::string &filepath, EntityID parent_id=0)
static std::string create_material_key(const aiMaterial *ai_material, const std::string &filepath, unsigned int material_index)
static void load_material_textures(const aiMaterial *ai_material, Material &material, const aiScene *scene, const std::string &filepath)
static std::vector< std::string > get_texture_search_paths(const std::string &filepath)
static std::string create_mesh_key(aiMesh *mesh, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Texture > > texture_cache
static bool try_load_external_texture_unified(const std::string &path_str, const std::string &filepath, TextureType type, Material &material)
static bool try_load_embedded_texture_unified(const std::string &path_str, const aiScene *scene, TextureType type, Material &material)
static bool try_load_embedded_texture(const std::string &path_str, const aiScene *scene, TextureType dcr_type, Material &material, const std::string &property_name)
static std::shared_ptr< Material > create_material(const aiMaterial *ai_material, const aiScene *scene, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Mesh > > mesh_cache
static std::string capitalize_first(const std::string &str)
static std::unordered_map< std::string, std::shared_ptr< Material > > material_cache
static bool try_load_external_texture(const std::string &path_str, const std::string &filepath, TextureType dcr_type, Material &material, const std::string &property_name)
static void debug_scene_info(const aiScene *scene, const std::string &filepath)
static EntityID load_model(Scene *scene, const std::filesystem::path &filepath, unsigned int import_flags=0)
static void load_essential_material_properties(const aiMaterial *ai_material, Material &material)
static void preprocess_materials(const aiScene *scene, const std::string &filepath)
TransformComponent * transform()
Definition Entity.cpp:42
void set_roughness(float roughness)
Definition Material.h:177
void set_metallic(float metallic)
Definition Material.h:173
void set_opacity(float opacity)
Definition Material.h:181
void set_shininess(float shininess)
Definition Material.h:169
Manages a collection of entities and their hierarchical relationships.
Definition Scene.h:24
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Definition Scene.cpp:105
void update_world_matrices()
Updates world transformation matrices for all entities Propagates transformations through the entity ...
Definition Scene.cpp:168
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:81
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:29
void set_position(float x, float y, float z)
void set_rotation(float x, float y, float z)
glm::mat4 aiMatrix4x4ToGlm(const aiMatrix4x4 &from)
TextureType
Definition Texture.h:13
Definition Vertex.h:5
static constexpr unsigned int PREVIEW
Definition ModelLoader.h:34
static constexpr unsigned int OPTIMIZED
Definition ModelLoader.h:64
static constexpr unsigned int HIGH_QUALITY
Definition ModelLoader.h:55
static constexpr unsigned int RUNTIME
Definition ModelLoader.h:42