aboutsummaryrefslogtreecommitdiff
path: root/src/Core/HDRIManager.cpp
blob: 6e420a6afcfbc27458ca89ff8c430123c7e35b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "HDRIManager.h"
#include <filesystem>

namespace Donut
{
    Ref<CubemapTexture> 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");
    }
};