From 0d819f2b9a71e862bce8799c6b0c1304e62219b3 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 22 Aug 2025 12:24:05 +0200 Subject: [add]: add HDRI --- src/Core/HDRIManager.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Core/HDRIManager.h | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/Core/HDRIManager.cpp create mode 100644 src/Core/HDRIManager.h (limited to 'src/Core') diff --git a/src/Core/HDRIManager.cpp b/src/Core/HDRIManager.cpp new file mode 100644 index 0000000..6e420a6 --- /dev/null +++ b/src/Core/HDRIManager.cpp @@ -0,0 +1,64 @@ +#include "HDRIManager.h" +#include + +namespace Donut +{ + Ref HDRIManager::LoadHDRI(const std::string& path) + { + auto it = m_HDRICache.find(path); + if (it != m_HDRICache.end()) + { + DONUT_INFO("HDRI already cached: {}", path); + return it->second; + } + + DONUT_INFO("Loading HDRI: {}", path); + auto hdri = CubemapTexture::CreateFromHDRI(path); + + if (hdri) + { + m_HDRICache[path] = hdri; + DONUT_INFO("Successfully loaded and cached HDRI: {}", path); + } + else + DONUT_ERROR("Failed to load HDRI: {}", path); + + return hdri; + } + + void HDRIManager::SetCurrentHDRI(const std::string& path) + { + auto hdri = LoadHDRI(path); + if (hdri) + { + m_CurrentHDRI = hdri; + DONUT_INFO("Set current HDRI to: {}", path); + } + else + DONUT_ERROR("Failed to set current HDRI: {}", path); + } + + std::string HDRIManager::GetHDRIName(const std::string& path) const + { + std::filesystem::path filePath(path); + std::string filename = filePath.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::ClearCache() + { + m_HDRICache.clear(); + m_CurrentHDRI.reset(); + DONUT_INFO("HDRI cache cleared"); + } +}; diff --git a/src/Core/HDRIManager.h b/src/Core/HDRIManager.h new file mode 100644 index 0000000..ebefb3a --- /dev/null +++ b/src/Core/HDRIManager.h @@ -0,0 +1,46 @@ +#pragma once + +#include "Core/Log.h" +#include "Rendering/Texture.h" + +#include +#include +#include + +namespace Donut +{ + class HDRIManager + { + public: + static HDRIManager& Get() + { + static HDRIManager instance; + return instance; + } + + Ref LoadHDRI(const std::string& path); + Ref GetCurrentHDRI() const { return m_CurrentHDRI; } + + void SetCurrentHDRI(const std::string& path); + const std::vector& GetAvailableHDRI() const { return m_AvailableHDRI; } + std::string GetHDRIName(const std::string& path) const; + void ClearCache(); + private: + HDRIManager() = default; + ~HDRIManager() = default; + + HDRIManager(const HDRIManager&) = delete; + HDRIManager& operator=(const HDRIManager&) = delete; + + std::unordered_map> m_HDRICache; + + Ref m_CurrentHDRI; + + std::vector m_AvailableHDRI = + { + "Assets/HDRI/HDR_blue_nebulae-1.hdr", + "Assets/HDRI/HDR_subdued_blue_nebulae.hdr", + "Assets/HDRI/HDR_subdued_multi_nebulae.hdr" + }; + }; +}; -- cgit v1.3