TerraForge3D  2.3.1
3D Terrain And Landscape Generator
SeaManager.cpp
1#include "Sea/SeaManager.h"
2
3#include "Platform.h"
4#include "Utils/Utils.h"
5#include "Base/Base.h"
6#include "Data/ProjectData.h"
7#include "Data/ApplicationState.h"
8
9#include <cstdint>
10
11SeaManager::SeaManager(ApplicationState *as)
12{
13 appState = as;
14 model = new Model("Sea");
15 model->SetupMeshOnGPU();
16 model->mesh->GeneratePlane(256, 120);
17 model->mesh->RecalculateNormals();
18 model->UploadToGPU();
19 bool tmp = false;
20 enabled = false;
21 color[0] = 0;
22 color[1] = 0;
23 color[2] = 0.7f;
24 color[3] = 0;
25 scale = 100;
26 std::string vertexShaderSource = ReadShaderSourceFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "shaders" PATH_SEPARATOR "water" PATH_SEPARATOR "vert.glsl", &tmp);
27 std::string fragmentShaderSource = ReadShaderSourceFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "shaders" PATH_SEPARATOR "water" PATH_SEPARATOR "frag.glsl", &tmp);
28 std::string geometryShaderSource = ReadShaderSourceFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "shaders" PATH_SEPARATOR "water" PATH_SEPARATOR "geom.glsl", &tmp);
29 shader = new Shader(vertexShaderSource, fragmentShaderSource, geometryShaderSource);
30 dudvMap = new Texture2D(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "textures" PATH_SEPARATOR "water_dudv.png");
31 normalMap = new Texture2D(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "textures" PATH_SEPARATOR "water_normal.png");
32}
33
34SeaManager::~SeaManager()
35{
36 if (model)
37 {
38 delete model;
39 }
40
41 if (shader)
42 {
43 delete shader;
44 }
45
46 if (dudvMap)
47 {
48 delete dudvMap;
49 }
50
51 if (normalMap)
52 {
53 delete normalMap;
54 }
55}
56
57void SeaManager::Load(nlohmann::json data)
58{
59 alpha = data["alpha"];
60 distrotionStrength = data["distrotionStrength"];
61 distrotionScale = data["distrotionScale"];
62 reflectivity = data["reflectivity"];
63 level = data["level"];
64 waveSpeed = data["waveSpeed"];
65 enabled = data["enabled"];
66 color[0] = data["color"]["r"];
67 color[1] = data["color"]["g"];
68 color[2] = data["color"]["b"];
69
70 if (dudvMap)
71 {
72 delete dudvMap;
73 }
74
75 dudvMap = new Texture2D(appState->projectManager->GetResourcePath() + PATH_SEPARATOR + appState->projectManager->GetAsset(data["dudvMap"]));
76
77 if (normalMap)
78 {
79 delete normalMap;
80 }
81
82 normalMap = new Texture2D(appState->projectManager->GetResourcePath() + PATH_SEPARATOR + appState->projectManager->GetAsset(data["normalMap"]));
83}
84
85nlohmann::json SeaManager::Save()
86{
87 nlohmann::json data;
88 data["type"] = "Sea Settings";
89 data["alpha"] = alpha;
90 data["distrotionStrength"] = distrotionStrength;
91 data["distrotionScale"] = distrotionScale;
92 data["reflectivity"] = reflectivity;
93 data["level"] = level;
94 data["waveSpeed"] = waveSpeed;
95 data["enabled"] = enabled;
96 nlohmann::json jcolor;
97 jcolor["r"] = color[0];
98 jcolor["g"] = color[1];
99 jcolor["b"] = color[2];
100 data["color"] = jcolor;
101 data["dudvMap"] = appState->projectManager->SaveTexture(dudvMap);
102 data["normalMap"] = appState->projectManager->SaveTexture(normalMap);
103 return data;
104}
105
106void SeaManager::Render(Camera &camera, LightManager *lights, void *reflectionTexture, float time)
107{
108 shader->Bind();
109 shader->SetTime(&time);
110 shader->SetUniformf("_SeaAlpha", alpha);
111 shader->SetUniformf("_SeaDistScale", distrotionScale);
112 shader->SetUniformf("_SeaDistStrength", distrotionStrength);
113 shader->SetUniformf("_SeaReflectivity", reflectivity);
114 shader->SetUniformf("_SeaLevel", level);
115 shader->SetUniformf("_SeaWaveSpeed", waveSpeed);
116 glActiveTexture(7);
117 glBindTexture(GL_TEXTURE_2D, (GLuint)((uintptr_t)reflectionTexture));
118 shader->SetUniformi("_ReflectionTexture", 7);
119
120 if (dudvMap)
121 {
122 dudvMap->Bind(8);
123 }
124
125 shader->SetUniformi("_DuDvMap", 8);
126
127 if (normalMap)
128 {
129 normalMap->Bind(9);
130 }
131
132 shader->SetUniformi("_NormalMap", 9);
133 model->position.y = level;
134 model->scale.x = scale;
135 model->scale.z = scale;
136 model->Update();
137 shader->SetUniform3f("_SeaColor", color);
138 glm::mat4 mpv = camera.pv * model->modelMatrix;
139 shader->SetMPV(mpv);
140 shader->SetLightCol(lights->color);
141 shader->SetLightPos(lights->position);
142 model->Render();
143}
144
145void SeaManager::ShowSettings(bool *pOpen)
146{
147 if (*pOpen)
148 {
149 ImGui::Begin("Sea Setings", pOpen);
150 ImGui::Checkbox("Sea Enabled", &enabled);
151 ImGui::DragFloat("Sea Level", &level, 0.1f);
152 ImGui::Text("Sea Color");
153 ImGui::ColorEdit3("##seaColor", color);
154 ImGui::DragFloat("Alpha", &alpha, 0.01f, 0, 1);
155 ImGui::DragFloat("Scale", &scale, 0.1f);
156 ImGui::DragFloat("Reflectvity", &reflectivity, 0.01f, 0, 1);
157 ImGui::DragFloat("Distortion Strength", &distrotionStrength, 0.001f, 0, 1);
158 ImGui::DragFloat("Distortion Scale", &distrotionScale, 0.001f, 0, 1);
159 ImGui::DragFloat("Wave Speed", &waveSpeed, 0.01f, 0, 1);
160 ImGui::Text("DuDv Map");
161 ImGui::SameLine();
162
163 if (ImGui::ImageButton((ImTextureID)dudvMap->GetRendererID(), ImVec2(50, 50)))
164 {
165 LoadTexture(dudvMap);
166 }
167
168 ImGui::Text("Normal Map");
169 ImGui::SameLine();
170
171 if (ImGui::ImageButton((ImTextureID)normalMap->GetRendererID(), ImVec2(50, 50)))
172 {
173 LoadTexture(normalMap);
174 }
175
176 ImGui::End();
177 }
178}
Definition: Camera.h:9
Definition: Model.h:9
Definition: Shader.h:7
a class to store JSON values
Definition: json.hpp:17860