Loading...
Searching...
No Matches
Texture.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <memory>
4#include <string>
5#include <unordered_map>
6
7#include "GL/glew.h"
8
9namespace hellfire {
10 class Texture;
11 class Material;
12
13 enum class TextureType {
14 DIFFUSE,
16 NORMAL,
21 HEIGHT,
23 };
24
25 enum class TextureFilter {
26 NEAREST,
27 LINEAR,
30 };
31
32 enum class TextureWrap {
33 REPEAT,
37 };
38
44 bool generate_mipmaps = true;
45 bool flip_vertically = false;
46
47 int max_size = -1;
48
50 };
51
52
54 public:
55 // Fluent interface for building texture sets
56 MaterialTextureSet &diffuse(const std::string &path);
57
58 MaterialTextureSet &normal(const std::string &path);
59
60 MaterialTextureSet &specular(const std::string &path);
61
62 MaterialTextureSet &roughness(const std::string &path);
63
64 MaterialTextureSet &metalness(const std::string &path);
65
66 MaterialTextureSet &ao(const std::string &path);
67
68 MaterialTextureSet &emissive(const std::string &path);
69
70 // Add texture with custom type
71 MaterialTextureSet &texture(TextureType type, const std::string &path);
72
73 // Getters
74 std::shared_ptr<Texture> get(TextureType type) const;
75
76 bool has(TextureType type) const;
77
78 // Apply to material
79 void apply_to_material(Material &material) const;
80
81 // Bind all textures for rendering
82 void bind_all() const;
83
84 // Load from directory (auto-detect based on naming conventions)
85 static MaterialTextureSet from_directory(const std::string &base_path, const std::string &material_name);
86
87 private:
89 };
90
91 class Texture {
92 public:
94
95 Texture(const std::string &path, TextureType type = TextureType::DIFFUSE);
96
97 Texture(const std::string &path, TextureType type, const TextureSettings &settings);
98
99 Texture(const Texture &) = delete;
100
101 Texture &operator=(const Texture &) = delete;
102
103 Texture(Texture &&other) noexcept;
104
105 Texture &operator=(Texture &&other) noexcept;
106
107 ~Texture();
108
109 void bind(unsigned int slot = 0) const;
110
111 void unbind() const;
112
113 TextureType get_type() const { return type_; }
114 uint32_t get_id() { return texture_id_; }
115 const std::string &get_path() { return path_; }
116
117 int get_slot() { return slot_ ; }
118 void set_slot(int slot) { slot_ = slot; }
119
120
121 // Texture parameters
123
124 void set_filter_mode(TextureFilter min_filter, TextureFilter mag_filter);
125
126 // Utility methods
127 static std::string type_to_string(TextureType type);
128
129 static std::string get_uniform_name(TextureType type);
130
131 [[nodiscard]] bool is_valid() const;
132
133 private:
135 std::string path_;
136 uint32_t texture_id_;
138 int slot_ = 0;
140
141 void load_texture_data();
142
143
145
147 };
148
150 public:
151 static std::shared_ptr<Texture> load(const std::string &path, TextureType type = TextureType::DIFFUSE,
152 const TextureSettings &settings = TextureSettings{});
153
154 static void clear_cache();
155
156 static size_t get_cache_size();
157
158 private:
160 };
161}
Definition IB.h:7
void pass_data(const T *data, size_t count) const
Definition IB.h:18
IB()
Definition IB.cpp:6
void pass_data(const std::vector< T > &data) const
Definition IB.h:24
uint32_t m_renderer_id_
Definition IB.h:8
void unbind()
Definition IB.cpp:21
~IB()
Definition IB.cpp:11
void bind()
Definition IB.cpp:16
Definition VA.h:7
void bind() const
Definition VA.cpp:16
uint32_t m_renderer_id_
Definition VA.h:8
~VA()
Definition VA.cpp:11
VA()
Definition VA.cpp:6
uint32_t get_id() const
Definition VA.cpp:25
void unbind()
Definition VA.cpp:20
Definition VB.h:7
void pass_data(const std::vector< T > &data) const
Definition VB.h:22
void unbind()
Definition VB.cpp:19
~VB()
Definition VB.cpp:9
void pass_dynamic_data(const std::vector< T > &data) const
Definition VB.h:28
uint32_t m_renderer_id_
Definition VB.h:8
void pass_data(const T *data, size_t count) const
Definition VB.h:16
void bind()
Definition VB.cpp:14
VB()
Definition VB.cpp:4
std::shared_ptr< Texture > get_texture(AssetID id)
size_t get_loaded_mesh_count() const
std::unordered_map< AssetID, std::shared_ptr< Texture > > texture_cache_
AssetManager(AssetRegistry &registry)
std::shared_ptr< Mesh > get_mesh(AssetID id)
size_t get_loaded_material_count() const
void unload(AssetID id)
AssetRegistry & registry_
std::unordered_map< AssetID, std::shared_ptr< Material > > material_cache_
size_t get_loaded_texture_count() const
std::shared_ptr< Material > get_material(AssetID id)
std::unordered_map< AssetID, std::shared_ptr< Mesh > > mesh_cache_
Registry for storing assets.
const std::filesystem::path & get_project_root()
std::optional< AssetID > get_uuid_by_path(const std::filesystem::path &filepath)
AssetID generate_uuid(const std::filesystem::path &filepath)
void unregister_asset(AssetID uuid)
bool has_asset_changed(AssetID uuid) const
void set_project_root(const std::filesystem::path &project_root)
std::filesystem::path get_absolute_path(AssetID uuid)
std::unordered_map< std::filesystem::path, AssetID > path_to_uuid_
std::filesystem::path registry_file_
AssetID register_asset(const std::filesystem::path &filepath, AssetType type)
std::unordered_map< AssetID, AssetMetadata > assets_
std::filesystem::path to_absolute_path(const std::filesystem::path &relative_path) const
static AssetType get_type_from_extension(const std::filesystem::path &filepath)
std::filesystem::path to_relative_path(const std::filesystem::path &absolute_path) const
AssetID register_asset(const std::filesystem::path &filepath)
std::vector< AssetID > register_directory(const std::filesystem::path &directory_path, bool recursive)
std::vector< AssetMetadata > get_assets_by_type(AssetType type)
std::filesystem::path get_relative_path(AssetID uuid)
bool asset_exists(AssetID uuid) const
size_t get_asset_count() const
std::vector< AssetID > get_modified_assets() const
AssetRegistry(const std::filesystem::path &registry_file, const std::filesystem::path &project_root)
std::optional< AssetMetadata > get_asset(AssetID uuid) const
std::filesystem::path project_root_
std::vector< AssetMetadata > get_all_assets() const
uint64_t get_file_last_modified(const std::filesystem::path &filepath) const
static void compile_shader_from_material(Material &material)
Definition Material.cpp:96
static std::optional< MaterialData > load(const std::filesystem::path &filepath)
void apply_to_material(Material &material) const
Definition Texture.cpp:359
std::unordered_map< TextureType, std::shared_ptr< Texture > > textures_
Definition Texture.h:88
bool has(TextureType type) const
Definition Texture.cpp:344
std::shared_ptr< Texture > get(TextureType type) const
Definition Texture.cpp:339
void set_ambient_color(const glm::vec3 &color)
Definition Material.h:156
void bind_all_properties(uint32_t shader_program, int &texture_unit) const
Definition Material.cpp:40
Material & set_texture(Texture *texture, int texture_slot=0)
Definition Material.h:147
std::map< std::string, Property > properties_
Definition Material.h:102
std::shared_ptr< Material > base_material_
Definition Material.h:107
std::string name_
Definition Material.h:101
std::vector< int > bound_texture_units_
Definition Material.h:254
Material & set_texture_internal(Texture *texture, TextureType type, int texture_slot)
Definition Material.h:256
void set_uv_offset(const glm::vec2 &offset)
Definition Material.h:198
void bind() const
Used to bind a Material for rendering.
Definition Material.cpp:11
bool is_transparent() const
Definition Material.h:185
uint32_t get_compiled_shader_id() const
Definition Material.h:239
void unbind_all_textures() const
Definition Material.cpp:31
std::unordered_map< std::string, Property > overrides_
Definition Material.h:108
void set_uv_tiling(float x, float y)
Definition Material.h:194
const std::string & get_name() const
Definition Material.h:251
void set_roughness(float roughness)
Definition Material.h:177
const auto & get_properties() const
Definition Material.h:250
void set_specular_color(const glm::vec3 &color)
Definition Material.h:160
std::optional< ShaderInfo > custom_shader_info_
Definition Material.h:103
std::unordered_set< std::string > touched_uniforms_
Definition Material.h:109
void set_uv_rotation(float rotation)
Definition Material.h:202
void set_compiled_shader_id(uint32_t shader_id)
Definition Material.h:235
Material & set_texture(const std::shared_ptr< Texture > &texture, int texture_slot=0)
Definition Material.h:143
void set_metallic(float metallic)
Definition Material.h:173
void set_uv_tiling(const glm::vec2 &tiling)
Definition Material.h:190
void set_diffuse_color(const glm::vec3 &color)
Definition Material.h:152
void set_opacity(float opacity)
Definition Material.h:181
uint32_t compiled_shader_id_
Definition Material.h:104
bool has_custom_shader() const
Definition Material.h:227
void set_emissive_color(const glm::vec3 &color)
Definition Material.h:164
void set_custom_shader(const ShaderInfo &shader_info)
Definition Material.h:211
void unbind() const
Definition Material.cpp:24
const ShaderInfo * get_shader_info() const
Definition Material.h:231
void set_shininess(float shininess)
Definition Material.h:169
static std::shared_ptr< Mesh > load(const std::filesystem::path &filepath)
bool is_wireframe
Definition Mesh.h:32
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices)
Definition Mesh.cpp:11
int get_index_count() const
Definition Mesh.cpp:96
void bind() const
Definition Mesh.cpp:22
std::unique_ptr< VA > vao_
Definition Mesh.h:41
void build()
Definition Mesh.cpp:30
std::unique_ptr< IB > ibo_
Definition Mesh.h:43
void create_mesh()
Definition Mesh.cpp:34
std::vector< Vertex > vertices
Definition Mesh.h:29
bool is_built() const
Definition Mesh.h:26
void draw() const
Definition Mesh.cpp:83
void unbind() const
Definition Mesh.cpp:26
Mesh(const std::vector< Vertex > &vertices, const std::vector< unsigned int > &indices, bool defer_build)
Definition Mesh.cpp:16
std::unique_ptr< VB > vbo_
Definition Mesh.h:42
std::vector< unsigned int > indices
Definition Mesh.h:30
void draw_instanced(size_t amount) const
Definition Mesh.cpp:89
int index_count_
Definition Mesh.h:45
static std::unordered_map< std::string, std::weak_ptr< Texture > > cache_
Definition Texture.h:159
static void clear_cache()
Definition Texture.cpp:284
static size_t get_cache_size()
Definition Texture.cpp:288
const std::string & get_path()
Definition Texture.h:115
void load_texture_data()
Definition Texture.cpp:83
static std::string type_to_string(TextureType type)
Definition Texture.cpp:211
bool is_valid() const
Definition Texture.cpp:198
Texture & operator=(const Texture &)=delete
Texture(Texture &&other) noexcept
Definition Texture.cpp:49
TextureSettings settings_
Definition Texture.h:137
std::string path_
Definition Texture.h:135
static std::string get_uniform_name(TextureType type)
Definition Texture.cpp:226
GLint get_gl_filter_mode(TextureFilter filter) const
Definition Texture.cpp:251
Texture(const Texture &)=delete
TextureType get_type() const
Definition Texture.h:113
void unbind() const
Definition Texture.cpp:207
void set_slot(int slot)
Definition Texture.h:118
void bind(unsigned int slot=0) const
Definition Texture.cpp:202
void set_filter_mode(TextureFilter min_filter, TextureFilter mag_filter)
TextureType type_
Definition Texture.h:134
GLint get_gl_wrap_mode(TextureWrap wrap) const
Definition Texture.cpp:241
void set_wrap_mode(TextureWrap wrap_s, TextureWrap wrap_t)
Texture & operator=(Texture &&other) noexcept
Definition Texture.cpp:56
uint32_t texture_id_
Definition Texture.h:136
uint32_t get_id()
Definition Texture.h:114
TextureWrap
Definition Texture.h:32
TextureType
Definition Texture.h:13
TextureFilter
Definition Texture.h:25
constexpr AssetID INVALID_ASSET_ID
Definition Vertex.h:5
glm::vec3 position
Definition Vertex.h:6
glm::vec3 color
Definition Vertex.h:8
glm::vec3 normal
Definition Vertex.h:7
glm::vec3 tangent
Definition Vertex.h:10
glm::vec3 bitangent
Definition Vertex.h:11
glm::vec2 texCoords
Definition Vertex.h:9
std::filesystem::path filepath
static constexpr const char * EMISSIVE_COLOR
static constexpr const char * UV_ROTATION
static constexpr const char * ROUGHNESS_TEXTURE
static constexpr const char * DIFFUSE_TEXTURE
static constexpr const char * SPECULAR_COLOR
static constexpr const char * METALLIC
static const char * get_texture_flag_name(TextureType type)
static constexpr const char * NORMAL_TEXTURE
static constexpr const char * USE_SPECULAR_TEXTURE
static constexpr const char * SPECULAR_TEXTURE
static constexpr const char * USE_ROUGHNESS_TEXTURE
static constexpr const char * ROUGHNESS
static constexpr const char * SHININESS
static constexpr const char * DIFFUSE_COLOR
static constexpr const char * AMBIENT_COLOR
static constexpr const char * AO_TEXTURE
static constexpr const char * UV_TILING
static constexpr const char * USE_AO_TEXTURE
static constexpr const char * UV_OFFSET
static constexpr const char * USE_EMISSIVE_TEXTURE
static constexpr const char * METALLIC_TEXTURE
static constexpr const char * EMISSIVE_TEXTURE
static constexpr const char * USE_NORMAL_TEXTURE
static constexpr const char * USE_DIFFUSE_TEXTURE
static const char * get_texture_uniform_name(TextureType type)
static constexpr const char * USE_METALLIC_TEXTURE
static constexpr const char * OPACITY
std::variant< float, glm::vec2, glm::vec3, glm::vec4, Texture *, bool, int, glm::mat3, glm::mat4 > value
Definition Material.h:30
std::unordered_map< std::string, std::string > uniform_mappings
Definition Material.h:93
std::unordered_set< std::string > defines
Definition Material.h:92
std::optional< std::string > geometry_path
Definition Material.h:91
TextureFilter mag_filter
Definition Texture.h:41
TextureFilter min_filter
Definition Texture.h:40
static TextureSettings for_type(TextureType type)
Definition Texture.cpp:16