TerraForge3D  2.3.1
3D Terrain And Landscape Generator
SkySettings.cpp
1#include "Sky/SkySettings.h"
2#include "Sky/CubeMap.h"
3#include "Utils/Utils.h"
4#include "Data/ApplicationState.h"
5
6#include <imgui/imgui.h>
7#include <hdritocubemap/HdriToCubemap.hpp>
8
9SkyManager::~SkyManager()
10{
11 if(cubemapManager)
12 {
13 delete cubemapManager;
14 }
15}
16
17SkyManager::SkyManager(ApplicationState *as)
18{
19 appState = as;
20 cubemapManager = new CubeMapManager(appState);
21 cubemap = cubemapManager->GetSkyboxCubemapTexture();
22}
23
24void SkyManager::ChangeCubemapTile(int face)
25{
26 std::string path = ShowOpenFileDialog(".png");
27
28 if (path.size() > 2)
29 {
30 cubemap->LoadFace(path, face);
31 cubemap->UploadDataToGPU();
32 }
33}
34
35
36
37void SkyManager::LoadHDRI(std::string path)
38{
39 // load hdr or ldr image
40 int cubemapResolution = 1024;
41 bool linearFilter = true;
42 HdriToCubemap<unsigned char> hdriToCube_ldr(path, cubemapResolution, linearFilter);
43 cubemap->facesData[TEXTURE_CUBEMAP_PX] = hdriToCube_ldr.getLeft();
44 cubemap->facesSizes[TEXTURE_CUBEMAP_PX] = IVec2(hdriToCube_ldr.getCubemapResolution());
45 cubemap->facesData[TEXTURE_CUBEMAP_NX] = hdriToCube_ldr.getRight();
46 cubemap->facesSizes[TEXTURE_CUBEMAP_NX] = IVec2(hdriToCube_ldr.getCubemapResolution());
47 cubemap->facesData[TEXTURE_CUBEMAP_PY] = hdriToCube_ldr.getDown();
48 cubemap->facesSizes[TEXTURE_CUBEMAP_PY] = IVec2(hdriToCube_ldr.getCubemapResolution());
49 cubemap->facesData[TEXTURE_CUBEMAP_NY] = hdriToCube_ldr.getUp();
50 cubemap->facesSizes[TEXTURE_CUBEMAP_NY] = IVec2(hdriToCube_ldr.getCubemapResolution());
51 cubemap->facesData[TEXTURE_CUBEMAP_PZ] = hdriToCube_ldr.getBack();
52 cubemap->facesSizes[TEXTURE_CUBEMAP_PZ] = IVec2(hdriToCube_ldr.getCubemapResolution());
53 cubemap->facesData[TEXTURE_CUBEMAP_NZ] = hdriToCube_ldr.getFront();
54 cubemap->facesSizes[TEXTURE_CUBEMAP_NZ] = IVec2(hdriToCube_ldr.getCubemapResolution());
55 cubemap->UploadDataToGPU();
56}
57
58void SkyManager::ShowSettings(bool *pOpen)
59{
60 ImGui::Begin("Sky Settings", pOpen);
61
62 if(!useProcedural )
63 {
64 if (ImGui::Button("Load HDRI"))
65 {
66 std::string path = ShowOpenFileDialog(".png");
67
68 if (path.size() > 2)
69 {
70 LoadHDRI(path);
71 }
72 }
73
74 ImGui::Separator();
75 ImGui::Text("Cubemap Faces");
76 ImGui::Text("PX");
77 ImGui::SameLine();
78
79 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_PX]->GetRendererID(), ImVec2(50, 50)))
80 {
81 ChangeCubemapTile(TEXTURE_CUBEMAP_PX);
82 }
83
84 ImGui::SameLine();
85 ImGui::Text("PY");
86 ImGui::SameLine();
87
88 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_PY]->GetRendererID(), ImVec2(50, 50)))
89 {
90 ChangeCubemapTile(TEXTURE_CUBEMAP_PY);
91 }
92
93 ImGui::SameLine();
94 ImGui::Text("PZ");
95 ImGui::SameLine();
96
97 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_PZ]->GetRendererID(), ImVec2(50, 50)))
98 {
99 ChangeCubemapTile(TEXTURE_CUBEMAP_PZ);
100 }
101
102 ImGui::Text("NX");
103 ImGui::SameLine();
104
105 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_NX]->GetRendererID(), ImVec2(50, 50)))
106 {
107 ChangeCubemapTile(TEXTURE_CUBEMAP_NX);
108 }
109
110 ImGui::SameLine();
111 ImGui::Text("NY");
112 ImGui::SameLine();
113
114 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_NY]->GetRendererID(), ImVec2(50, 50)))
115 {
116 ChangeCubemapTile(TEXTURE_CUBEMAP_NY);
117 }
118
119 ImGui::SameLine();
120 ImGui::Text("NZ");
121 ImGui::SameLine();
122
123 if (ImGui::ImageButton((ImTextureID)cubemap->textures[TEXTURE_CUBEMAP_NZ]->GetRendererID(), ImVec2(50, 50)))
124 {
125 ChangeCubemapTile(TEXTURE_CUBEMAP_NZ);
126 }
127
128 ImGui::Separator();
129 }
130
131 else
132 {
133 ImGui::DragFloat("Clouds Time", &cltime, 0.01f);
134 ImGui::DragFloat("Clouds UPF", &upf, 0.01f);
135 ImGui::DragFloat("Cirrus Clouds", &cirrus, 0.005f, -1, 1);
136 ImGui::DragFloat("Cumulus Clouds", &cumulus, 0.005f, -1, 1);
137 ImGui::DragFloat3("Sun Position", fsun, 0.01f);
138 }
139
140 if(!useProcedural)
141 {
142 useProcedural = ImGui::Button("Use Procedural Sky");
143 }
144
145 else
146 {
147 useProcedural = !ImGui::Button("Use Cubemap Sky");
148 }
149
150 if(!useBox)
151 {
152 useBox = ImGui::Button("Use Sky Box");
153 }
154
155 else
156 {
157 useBox = !ImGui::Button("Use Sky Sphere");
158 }
159
160 ImGui::End();
161}
162
163void SkyManager::RenderSky(glm::mat4 view, glm::mat4 pers)
164{
165 cubemapManager->RenderSkybox(view, pers, useBox, useProcedural, cirrus, cumulus, cltime, fsun, upf);
166}
167
Definition: BaseMath.h:4