diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/hdri_manager.cpp | 64 | ||||
| -rw-r--r-- | src/core/hdri_manager.h | 46 |
2 files changed, 110 insertions, 0 deletions
diff --git a/src/core/hdri_manager.cpp b/src/core/hdri_manager.cpp new file mode 100644 index 0000000..1a5521e --- /dev/null +++ b/src/core/hdri_manager.cpp @@ -0,0 +1,64 @@ +#include "hdri_manager.h" +#include <filesystem> + +namespace Donut +{ + Ref<CubemapTexture> HDRIManager::load_hdri(const std::string& path) + { + auto it = m_hdri_cache.find(path); + if (it != m_hdri_cache.end()) + { + DONUT_INFO("HDRI already cached: {}", path); + return it->second; + } + + DONUT_INFO("Loading HDRI: {}", path); + auto hdri = CubemapTexture::create_from_hdri(path); + + if (hdri) + { + m_hdri_cache[path] = hdri; + DONUT_INFO("Successfully loaded and cached HDRI: {}", path); + } + else + DONUT_ERROR("Failed to load HDRI: {}", path); + + return hdri; + } + + void HDRIManager::set_current_hdri(const std::string& path) + { + auto hdri = load_hdri(path); + if (hdri) + { + m_current_hdri = hdri; + DONUT_INFO("Set current HDRI to: {}", path); + } + else + DONUT_ERROR("Failed to set current HDRI: {}", path); + } + + std::string HDRIManager::get_hdri_name(const std::string& path) const + { + std::filesystem::path file_path(path); + std::string filename = file_path.stem().string(); + std::string name = filename; + + for (size_t i = 0; i < name.length(); ++i) + { + if (name[i] == '_') + name[i] = ' '; + else if (i == 0 || name[i-1] == ' ') + name[i] = std::toupper(name[i]); + } + + return name; + } + + void HDRIManager::clear_cache() + { + m_hdri_cache.clear(); + m_current_hdri.reset(); + DONUT_INFO("HDRI cache cleared"); + } +}; diff --git a/src/core/hdri_manager.h b/src/core/hdri_manager.h new file mode 100644 index 0000000..5ccd402 --- /dev/null +++ b/src/core/hdri_manager.h @@ -0,0 +1,46 @@ +#pragma once + +#include "core/log.h" +#include "rendering/texture.h" + +#include <string> +#include <unordered_map> +#include <memory> + +namespace Donut +{ + class HDRIManager + { + public: + static HDRIManager& get() + { + static HDRIManager instance; + return instance; + } + + Ref<CubemapTexture> load_hdri(const std::string& path); + Ref<CubemapTexture> get_current_hdri() const { return m_current_hdri; } + + void set_current_hdri(const std::string& path); + const std::vector<std::string>& get_available_hdri() const { return m_available_hdri; } + std::string get_hdri_name(const std::string& path) const; + void clear_cache(); + private: + HDRIManager() = default; + ~HDRIManager() = default; + + HDRIManager(const HDRIManager&) = delete; + HDRIManager& operator=(const HDRIManager&) = delete; + + std::unordered_map<std::string, Ref<CubemapTexture>> m_hdri_cache; + + Ref<CubemapTexture> m_current_hdri; + + std::vector<std::string> m_available_hdri = + { + "assets/hdri/hdr_blue_nebulae_1.hdr", + "assets/hdri/hdr_subdued_blue_nebulae.hdr", + "assets/hdri/hdr_subdued_multi_nebulae.hdr" + }; + }; +}; |
