3#include <Misc/ExplorerControls.h>
14#define GLFW_EXPOSE_NATIVE_WIN32
15#include <GLFW/glfw3.h>
16#include <GLFW/glfw3native.h>
20#include "Base/EntryPoint.h"
23#include "Data/ProjectData.h"
24#include "Data/ApplicationState.h"
25#include "Data/VersionInfo.h"
26#include "Generators/MeshGeneratorManager.h"
27#include "TextureStore/TextureStore.h"
28#include "Foliage/FoliagePlacement.h"
29#include "Misc/ExportManager.h"
30#include "Misc/OSLiscences.h"
31#include "Sky/SkySettings.h"
32#include "Misc/AppStyles.h"
33#include "Misc/SupportersTribute.h"
36#include "Utils/Utils.h"
41#include "json/json.hpp"
46#include <dirent/dirent.h>
55#include "NoiseLayers/LayeredNoiseManager.h"
61void OnAppClose(
int x,
int y)
64 appState->mainApp->Close();
68static void ShowStats()
70 ImGui::Begin(
"Statistics", &appState->windows.statsWindow);
71 ImGui::Text((
"Vertex Count :" + std::to_string(appState->stats.vertexCount)).c_str());
72 ImGui::Text((
"Triangles Count :" + std::to_string(appState->stats.triangles)).c_str());
73 ImGui::Text((
"Framerate :" + std::to_string(appState->stats.frameRate)).c_str());
77static void ShowGeneralControls()
79 ImGui::Begin(
"General Controls");
81 ImGui::Checkbox(
"VSync ", &appState->states.vSync);
83 ImGui::DragFloat(
"Mouse Speed", &appState->globals.mouseSpeed);
84 ImGui::DragFloat(
"Zoom Speed", &appState->globals.scrollSpeed);
86 if (ImGui::Button(
"Exit"))
94static void ResetShader()
98 if(appState->shaders.foliage)
100 delete appState->shaders.foliage;
103 if (!appState->shaders.wireframe)
105 appState->shaders.wireframe =
new Shader(ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"default" PATH_SEPARATOR
"vert.glsl", &res),
106 ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"default" PATH_SEPARATOR
"frag.glsl", &res),
107 ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"default" PATH_SEPARATOR
"geom.glsl", &res));
118 appState->shaders.foliage =
new Shader(ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"foliage" PATH_SEPARATOR
"vert.glsl", &res),
119 ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"foliage" PATH_SEPARATOR
"frag.glsl", &res),
120 ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"foliage" PATH_SEPARATOR
"geom.glsl", &res));
123static void RegenerateMesh()
125 appState->meshGenerator->Generate();
129static void DoTheRederThing(
float deltaTime,
bool renderWater =
false,
bool bakeTexture =
false,
bool bakeTextureFinal =
false)
133 appState->cameras.main.UpdateCamera();
137 if (appState->states.textureBake)
144 if (appState->states.skyboxEnabled)
146 appState->skyManager->RenderSky(appState->cameras.main.view, appState->cameras.main.pers);
149 glEnable(GL_DEPTH_TEST);
151 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
153 if (appState->states.wireFrameMode)
155 shader = appState->shaders.wireframe;
160 shader = appState->shaders.terrain;
163 shader->SetUniformf(
"_TextureBake", 0.0f);
165 shader->SetTime(&time);
166 shader->SetMPV(appState->cameras.main.pv);
167 appState->models.coreTerrain->Update();
168 shader->SetUniformMat4(
"_Model", appState->models.coreTerrain->modelMatrix);
169 shader->SetLightCol(appState->lightManager->color);
170 shader->SetLightPos(appState->lightManager->position);
171 shader->SetUniformf(
"_LightStrength", appState->lightManager->strength);
173 tmp[0] = appState->globals.viewportMousePosX;
174 tmp[1] = appState->globals.viewportMousePosY;
175 tmp[2] = ImGui::GetIO().MouseDown[0];
176 shader->SetUniform3f(
"_MousePos", tmp);
177 tmp[0] = appState->frameBuffers.main->GetWidth();
178 tmp[1] = appState->frameBuffers.main->GetHeight();
180 shader->SetUniform3f(
"_Resolution", tmp);
181 shader->SetUniformf(
"_SeaLevel", appState->seaManager->level);
182 shader->SetUniform3f(
"_CameraPos", appState->cameras.main.position);
183 shader->SetUniformf(
"_CameraNear", appState->cameras.main.cNear);
184 shader->SetUniformf(
"_CameraFar", appState->cameras.main.cFar);
185 shader->SetUniformf(
"_FlatShade", appState->states.useGPUForNormals ? 1.0f : 0.0f);
186 appState->shadingManager->UpdateShaders();
188 if(appState->mode == ApplicationMode::TERRAIN)
190 appState->models.coreTerrain->Render();
193 else if (appState->mode == ApplicationMode::CUSTOM_BASE)
195 appState->models.customBase->Render();
198 if (appState->states.showFoliage)
200 shader = appState->shaders.foliage;
202 shader->SetTime(&time);
203 shader->SetMPV(appState->cameras.main.pv);
204 shader->SetUniformMat4(
"_Model", appState->models.coreTerrain->modelMatrix);
205 shader->SetLightCol(appState->lightManager->color);
206 shader->SetLightPos(appState->lightManager->position);
207 shader->SetUniformf(
"_LightStrength", appState->lightManager->strength);
209 tmp[0] = appState->globals.viewportMousePosX;
210 tmp[1] = appState->globals.viewportMousePosY;
211 tmp[2] = ImGui::GetIO().MouseDown[0];
212 shader->SetUniform3f(
"_MousePos", tmp);
213 tmp[0] = appState->frameBuffers.main->GetWidth();
214 tmp[1] = appState->frameBuffers.main->GetHeight();
216 shader->SetUniform3f(
"_Resolution", tmp);
217 shader->SetUniformf(
"_SeaLevel", appState->seaManager->level);
218 shader->SetUniform3f(
"_CameraPos", appState->cameras.main.position);
219 shader->SetUniformf(
"_CameraNear", appState->cameras.main.cNear);
220 shader->SetUniformf(
"_CameraFar", appState->cameras.main.cFar);
221 appState->foliageManager->RenderFoliage(appState->cameras.main);
228 if (appState->seaManager->enabled && renderWater)
230 appState->seaManager->Render(appState->cameras.main, appState->lightManager, appState->frameBuffers.reflection, time);
235void PostProcess(
float deltatime)
237 float pos[3] = { 0, 0, 1.8 };
238 float rot[3] = {0, 0, 0};
239 appState->cameras.postPorcess.aspect = 1;
240 appState->cameras.postPorcess.position[0] = 0.0f;
241 appState->cameras.postPorcess.position[0] = 0.0f;
242 appState->cameras.postPorcess.position[0] = 1.8f;
243 appState->cameras.postPorcess.rotation[0] = 0.0f;
244 appState->cameras.postPorcess.rotation[1] = 0.0f;
245 appState->cameras.postPorcess.rotation[2] = 0.0f;
246 appState->cameras.postPorcess.UpdateCamera();
247 float viewDir[3] = { glm::value_ptr(appState->cameras.main.pv)[2], glm::value_ptr(appState->cameras.main.pv)[6], glm::value_ptr(appState->cameras.main.pv)[10] };
248 appState->shaders.postProcess->Bind();
249 appState->shaders.postProcess->SetUniform3f(
"_CameraPosition", appState->cameras.main.position);
250 appState->shaders.postProcess->SetUniform3f(
"_ViewDir", viewDir);
251 float cloudsBoxMinMax[3] = {0.0f};
252 appState->shaders.postProcess->SetUniform3f(
"cloudsBoxBoundsMin", cloudsBoxMinMax);
253 appState->shaders.postProcess->SetUniform3f(
"cloudsBoxBoundsMax", cloudsBoxMinMax);
254 appState->shaders.postProcess->SetMPV(appState->cameras.postPorcess.pv);
255 glActiveTexture(GL_TEXTURE5);
256 glBindTexture(GL_TEXTURE_2D, appState->frameBuffers.main->GetColorTexture());
257 appState->shaders.postProcess->SetUniformi(
"_ColorTexture", 5);
258 glActiveTexture(GL_TEXTURE6);
259 glBindTexture(GL_TEXTURE_2D, appState->frameBuffers.main->GetDepthTexture());
260 appState->shaders.postProcess->SetUniformi(
"_DepthTexture", 6);
261 appState->models.screenQuad->Render();
264static void ChangeCustomModel(std::string mdstr = ShowOpenFileDialog(
"*.obj"))
266 while (appState->states.remeshing);
268 if(!appState->states.usingBase)
270 delete appState->models.customBase;
271 delete appState->models.customBaseCopy;
276 appState->globals.currentBaseModelPath = mdstr;
277 appState->states.usingBase =
false;
278 appState->models.customBase = LoadModel(appState->globals.currentBaseModelPath);
279 appState->models.customBaseCopy = LoadModel(appState->globals.currentBaseModelPath);
280 appState->mode = ApplicationMode::CUSTOM_BASE;
284static void ShowChooseBaseModelPopup()
287 static std::shared_ptr<Texture2D> plane = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"plane.png",
false,
false);
288 static std::shared_ptr<Texture2D> sphere = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"sphere.png",
false,
false);
289 static std::shared_ptr<Texture2D> cube = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"cube.png",
false,
false);
290 static std::shared_ptr<Texture2D> cone = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"cone.png",
false,
false);
291 static std::shared_ptr<Texture2D> cyllinder = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"cylinder.png",
false,
false);
292 static std::shared_ptr<Texture2D> torus = std::make_shared<Texture2D>(appState->constants.texturesDir + PATH_SEPARATOR
"ui_thumbs" PATH_SEPARATOR
"torus.png",
false,
false);
295 if (ImGui::BeginPopup(
"Choose Base Model"))
297 if (ImGui::Button(
"Open From File"))
300 ImGui::CloseCurrentPopup();
303 if (ImGui::Button(
"Close"))
305 ImGui::CloseCurrentPopup();
308 ImGui::BeginChild(
"Choose Base Model##Child", ImVec2(650, 450));
310 if (ImGui::ImageButton((ImTextureID)plane->GetRendererID(), ImVec2(200, 200)) )
312 while (appState->states.remeshing);
314 delete appState->models.customBase;
315 delete appState->models.customBaseCopy;
316 appState->states.usingBase =
true;
317 appState->mode = ApplicationMode::TERRAIN;
318 ImGui::CloseCurrentPopup();
323 if (ImGui::ImageButton((ImTextureID)sphere->GetRendererID(), ImVec2(200, 200)))
325 ChangeCustomModel(appState->constants.modelsDir + PATH_SEPARATOR
"Sphere.obj");
326 ImGui::CloseCurrentPopup();
331 if (ImGui::ImageButton((ImTextureID)cube->GetRendererID(), ImVec2(200, 200)))
333 ChangeCustomModel(appState->constants.modelsDir + PATH_SEPARATOR
"cube.obj");
334 ImGui::CloseCurrentPopup();
337 if (ImGui::ImageButton((ImTextureID)torus->GetRendererID(), ImVec2(200, 200)))
339 ChangeCustomModel(appState->constants.modelsDir + PATH_SEPARATOR
"Torus.obj");
340 ImGui::CloseCurrentPopup();
345 if (ImGui::ImageButton((ImTextureID)cone->GetRendererID(), ImVec2(200, 200)))
347 ChangeCustomModel(appState->constants.modelsDir + PATH_SEPARATOR
"cone.obj");
348 ImGui::CloseCurrentPopup();
353 if (ImGui::ImageButton((ImTextureID)cyllinder->GetRendererID(), ImVec2(200, 200)))
355 ChangeCustomModel(appState->constants.modelsDir + PATH_SEPARATOR
"cylinder.obj");
356 ImGui::CloseCurrentPopup();
364static void ShowTerrainControls()
366 static bool exp =
false;
367 ImGui::Begin(
"Dashboard");
368 ShowChooseBaseModelPopup();
370 if(appState->mode != ApplicationMode::TERRAIN)
372 ImGui::Text((
"Current Base Model : " + appState->globals.currentBaseModelPath).c_str());
374 if(ImGui::Button(
"Change Current Base"))
376 ImGui::OpenPopup(
"Choose Base Model");
379 if(ImGui::Button(
"Switch to Plane"))
381 while(appState->states.remeshing);
383 delete appState->models.customBase;
384 delete appState->models.customBaseCopy;
385 appState->states.usingBase =
true;
386 appState->mode = ApplicationMode::TERRAIN;
392 ImGui::Text(
"Current Base Model : Default Plane");
394 if (ImGui::Button(
"Change Current Base"))
396 ImGui::OpenPopup(
"Choose Base Model");
400 if(appState->mode == ApplicationMode::TERRAIN)
402 ImGui::DragInt(
"Mesh Resolution", &appState->globals.resolution, 1, 2, 16 * 4096);
405 if(appState->mode == ApplicationMode::TERRAIN || appState->mode == ApplicationMode::CUSTOM_BASE)
407 ImGui::DragFloat(
"Mesh Scale", &appState->globals.scale, 0.1f, 1.0f, 5000.0f);
411 ImGui::Checkbox(
"Auto Update", &appState->states.autoUpdate);
412 ImGui::Checkbox(
"Post Processing", &appState->states.postProcess);
413 ImGui::Checkbox(
"Auto Save", &appState->states.autoSave);
414 ImGui::Checkbox(
"Wireframe Mode", &appState->states.wireFrameMode);
415 ImGui::Checkbox(
"Show Skybox", &appState->states.skyboxEnabled);
416 ImGui::Checkbox(
"Show Sea", &appState->seaManager->enabled);
417 ImGui::Checkbox(
"Show Foliage", &appState->states.showFoliage);
418 ImGui::Checkbox(
"Infinite Explorer Mode", &appState->states.iExploreMode);
419 ImGui::Checkbox(
"Explorer Mode", &appState->states.exploreMode);
420 ImGui::Checkbox(
"Texture Bake Mode", &appState->states.textureBake);
423 if (ImGui::Button(
"Update Mesh"))
428 if (ImGui::Button(
"Recalculate Normals"))
430 while (appState->states.remeshing);
432 if (appState->mode == ApplicationMode::TERRAIN)
434 appState->models.coreTerrain->mesh->RecalculateNormals();
435 appState->models.coreTerrain->UploadToGPU();
438 else if (appState->mode == ApplicationMode::CUSTOM_BASE)
440 appState->models.customBase->mesh->RecalculateNormals();
441 appState->models.customBase->UploadToGPU();
446 Log(
"Unsupported App Mode!");
450 if (ImGui::Button(
"Refresh Shaders"))
455 if (ImGui::Button(
"Use Custom Shaders"))
457 appState->windows.shaderEditorWindow =
true;
460 if (ImGui::Button(
"Texture Settings"))
462 appState->windows.texturEditorWindow =
true;
465 if (ImGui::Button(
"Sea Settings"))
467 appState->windows.seaEditor =
true;
470 if (ImGui::Button(
"Filter Settings"))
472 appState->windows.filtersManager =
true;
475 if (ImGui::Button(
"Export Frame"))
477 ExportTexture(appState->frameBuffers.main->GetRendererID(), ShowSaveFileDialog(
".png"), appState->frameBuffers.main->GetWidth(), appState->frameBuffers.main->GetHeight());
491static void MouseMoveCallback(
float x,
float y)
493 static glm::vec2 prevMousePos;
494 float deltaX = x - prevMousePos.x;
495 float deltaY = y - prevMousePos.y;
497 if (appState->states.mouseButton2)
509 appState->cameras.main.rotation[0] += deltaX * appState->globals.mouseSpeed;
510 appState->cameras.main.rotation[1] += deltaY * appState->globals.mouseSpeed;
517static void MouseScrollCallback(
float amount)
519 appState->globals.mouseScrollAmount = amount;
520 glm::vec3 pPos = glm::vec3(appState->cameras.main.position[0], appState->cameras.main.position[1], appState->cameras.main.position[2]);
521 pPos += appState->constants.FRONT * amount * appState->globals.scrollSpeed;
522 appState->cameras.main.position[0] = pPos.x;
523 appState->cameras.main.position[1] = pPos.y;
524 appState->cameras.main.position[2] = pPos.z;
528static void ShowMainScene()
530 auto viewportMinRegion = ImGui::GetWindowContentRegionMin();
531 auto viewportMaxRegion = ImGui::GetWindowContentRegionMax();
532 auto viewportOffset = ImGui::GetWindowPos();
533 ImGui::Begin(
"Viewport");
535 ImGui::BeginChild(
"MainRender");
537 if (ImGui::IsWindowHovered())
539 ImGuiIO io = ImGui::GetIO();
540 MouseMoveCallback(io.MousePos.x, io.MousePos.y);
541 MouseScrollCallback(io.MouseWheel);
542 appState->states.mouseButton1 = io.MouseDown[0];
543 appState->states.mouseButton2 = io.MouseDown[2];
544 appState->states.mouseButton3 = io.MouseDown[1];
546 if (ImGui::GetIO().MouseDown[1])
548 appState->cameras.main.position[0] += -io.MouseDelta.x * 0.005f * glm::distance(glm::vec3(0.0f), glm::vec3(appState->cameras.main.position[0], appState->cameras.main.position[1], appState->cameras.main.position[2]));
549 appState->cameras.main.position[1] += io.MouseDelta.y * 0.005f * glm::distance(glm::vec3(0.0f), glm::vec3(appState->cameras.main.position[0], appState->cameras.main.position[1], appState->cameras.main.position[2]));
552 appState->globals.viewportMousePosX = ImGui::GetIO().MousePos.x - viewportOffset.x;
553 appState->globals.viewportMousePosY = ImGui::GetIO().MousePos.y - viewportOffset.y;
556 ImVec2 wsize = ImGui::GetWindowSize();
557 appState->globals.viewportSize[0] = wsize.x;
558 appState->globals.viewportSize[1] = wsize.y;
561 if (appState->states.postProcess)
563 ImGui::Image((ImTextureID)appState->frameBuffers.postProcess->GetColorTexture(), wsize, ImVec2(0, 1), ImVec2(1, 0));
568 ImGui::Image((ImTextureID)appState->frameBuffers.main->GetColorTexture(), wsize, ImVec2(0, 1), ImVec2(1, 0));
576static void SaveFile(std::string file = ShowSaveFileDialog())
578 appState->serailizer->SaveFile(file);
581static void OpenSaveFile(std::string file = ShowOpenFileDialog(
".terr3d"))
583 appState->serailizer->LoadFile(file);
587static void PackProject(std::string path = ShowSaveFileDialog())
589 appState->serailizer->PackProject(path);
592static void LoadPackedProject(std::string path = ShowOpenFileDialog())
594 appState->serailizer->LoadPackedProject(path);
598static void ShowModuleManager()
600 appState->modules.manager->ShowSettings(&appState->windows.modulesManager);
603static void OnBeforeImGuiRender()
605 static bool dockspaceOpen =
true;
606 static bool opt_fullscreen_persistant =
true;
607 bool opt_fullscreen = opt_fullscreen_persistant;
608 static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;
609 ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
613 ImGuiViewport *viewport = ImGui::GetMainViewport();
614 ImGui::SetNextWindowPos(viewport->Pos);
615 ImGui::SetNextWindowSize(viewport->Size);
616 ImGui::SetNextWindowViewport(viewport->ID);
617 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.1f);
618 ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
619 window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
620 window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
623 if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode)
625 window_flags |= ImGuiWindowFlags_NoBackground;
628 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
629 ImGui::Begin(
"DockSpace", &dockspaceOpen, window_flags);
630 ImGui::PopStyleVar();
634 ImGui::PopStyleVar(2);
638 ImGuiIO &io = ImGui::GetIO();
639 ImGuiStyle &style = ImGui::GetStyle();
640 float minWinSizeX = style.WindowMinSize.x;
641 style.WindowMinSize.x = 370.0f;
643 if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
645 ImGuiID dockspace_id = ImGui::GetID(
"MyDockSpace");
646 ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
649 style.WindowMinSize.x = minWinSizeX;
652static void OnImGuiRenderEnd()
657static void SetUpIcon()
660 HWND hwnd = glfwGetWin32Window(mainApp->GetWindow()->GetNativeWindow());
661 HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
666 SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
667 SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
669 SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
670 SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, (LPARAM)hIcon);
680 virtual void OnPreload()
override
682 SetTitle(
"TerraForge3D - Jaysmito Mukherjee");
683 MkDir(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"logs");
684 SetLogsDir(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"logs");
685 SetWindowConfigPath(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"configs" PATH_SEPARATOR
"windowconfigs.terr3d");
686 MkDir(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"cache" PATH_SEPARATOR
"autosave\"");
687 MkDir(GetExecutableDir() + PATH_SEPARATOR
"Data" PATH_SEPARATOR
"temp\"");
690 virtual void OnUpdate(
float deltatime)
override
692 if (!appState->states.ruinning)
697 if (!appState->states.exploreMode)
700 if ((glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_LEFT_CONTROL) || glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_RIGHT_CONTROL)))
703 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_O))
709 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_Q))
715 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_S))
717 if (appState->globals.currentOpenFilePath.size() > 3)
719 Log(
"Saved to " + appState->globals.currentOpenFilePath);
720 SaveFile(appState->globals.currentOpenFilePath);
730 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_W))
732 if (appState->globals.currentOpenFilePath.size() > 3)
734 Log(
"CLosed file " + appState->globals.currentOpenFilePath);
735 appState->globals.currentOpenFilePath =
"";
740 Log(
"Shutting Down");
746 if ((glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_LEFT_SHIFT) || glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_RIGHT_SHIFT)))
749 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_S))
751 appState->globals.currentOpenFilePath =
"";
756 if (glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_X))
758 Log(
"Toggle Explorer Mode");
759 appState->states.exploreMode =
true;
764 appState->stats.deltatime = deltatime;
767 appState->frameBuffers.reflection->Begin();
768 glViewport(0, 0, appState->frameBuffers.reflection->GetWidth(), appState->frameBuffers.reflection->GetHeight());
769 GetWindow()->Clear();
770 DoTheRederThing(deltatime);
771 glBindFramebuffer(GL_FRAMEBUFFER, appState->frameBuffers.main->GetRendererID());
772 glViewport(0, 0, appState->frameBuffers.main->GetWidth(), appState->frameBuffers.main->GetHeight());
773 GetWindow()->Clear();
774 DoTheRederThing(deltatime,
true);
776 if (appState->states.postProcess)
778 appState->frameBuffers.postProcess->Begin();
779 GetWindow()->Clear();
780 PostProcess(deltatime);
784 glBindFramebuffer(GL_FRAMEBUFFER, 0);
790 static bool expH =
false;
794 GetWindow()->SetFullScreen(
true);
795 glfwSetInputMode(GetWindow()->GetNativeWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
796 ExPSaveCamera(appState->cameras.main.position, appState->cameras.main.rotation);
799 expH = appState->states.exploreMode;
800 appState->stats.deltatime = deltatime;
801 appState->frameBuffers.reflection->Begin();
802 glViewport(0, 0, appState->frameBuffers.reflection->GetWidth(), appState->frameBuffers.reflection->GetHeight());
803 GetWindow()->Clear();
804 DoTheRederThing(deltatime);
805 appState->frameBuffers.reflection->End();
806 glBindFramebuffer(GL_FRAMEBUFFER, 0);
808 glfwGetWindowSize(GetWindow()->GetNativeWindow(), &w, &h);
809 glViewport(0, 0, w, h);
810 GetWindow()->Clear();
811 UpdateExplorerControls(appState->cameras.main.position, appState->cameras.main.rotation, appState->states.iExploreMode, &appState->globals.offset[0], &appState->globals.offset[1]);
812 DoTheRederThing(deltatime,
true);
814 if (appState->states.postProcess)
816 appState->frameBuffers.postProcess->Begin();
817 PostProcess(deltatime);
820 if ((glfwGetKey(GetWindow()->GetNativeWindow(), GLFW_KEY_ESCAPE)))
822 appState->states.exploreMode =
false;
824 glfwSetInputMode(GetWindow()->GetNativeWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
825 GetWindow()->SetFullScreen(
false);
826 ExPRestoreCamera(appState->cameras.main.position, appState->cameras.main.rotation);
830 if (appState->states.autoUpdate)
835 appState->modules.manager->UpdateModules();
838 virtual void OnOneSecondTick()
override
840 if (!appState->states.ruinning)
845 appState->globals.secondCounter++;
847 if (appState->globals.secondCounter % 5 == 0)
849 if (appState->states.autoSave)
851 SaveFile(appState->constants.cacheDir + PATH_SEPARATOR
"autosave" PATH_SEPARATOR
"autosave.terr3d");
853 if (appState->globals.currentOpenFilePath.size() > 3)
855 SaveFile(appState->globals.currentOpenFilePath);
860 GetWindow()->SetVSync(appState->states.vSync);
861 appState->stats.frameRate = 1 / appState->stats.deltatime;
864 virtual void OnImGuiRender()
override
866 OnBeforeImGuiRender();
867 appState->mainMenu->ShowMainMenu();
868 ShowGeneralControls();
870 if (appState->windows.cameraControls)
872 ImGui::Begin(
"Camera Controls", &appState->windows.cameraControls);
873 appState->cameras.main.ShowSettings();
874 ImGui::Checkbox(
"Auto Calculate Aspect Ratio", &appState->states.autoAspectCalcRatio);
878 if (appState->states.autoAspectCalcRatio && (appState->globals.viewportSize[1] != 0 && appState->globals.viewportSize[0] != 0))
880 appState->cameras.main.aspect = appState->globals.viewportSize[0] / appState->globals.viewportSize[1];
883 appState->lightManager->ShowSettings(
true, &appState->windows.lightControls);
884 appState->meshGenerator->ShowSettings();
885 ShowTerrainControls();
890 if (appState->windows.statsWindow)
895 appState->seaManager->ShowSettings(&appState->windows.seaEditor);
897 if (appState->windows.modulesManager)
902 if (appState->windows.styleEditor)
904 ShowStyleEditor(&appState->windows.styleEditor);
907 if (appState->windows.foliageManager)
909 appState->foliageManager->ShowSettings(&appState->windows.foliageManager);
912 if(appState->windows.textureStore)
914 appState->textureStore->ShowSettings(&appState->windows.textureStore);
917 if(appState->windows.shadingManager)
919 appState->shadingManager->ShowSettings(&appState->windows.shadingManager);
922 if (appState->windows.filtersManager)
924 appState->filtersManager->ShowSettings(&appState->windows.filtersManager);
927 if (appState->windows.osLisc)
929 appState->osLiscences->ShowSettings(&appState->windows.osLisc);
932 if (appState->windows.supportersTribute)
934 appState->supportersTribute->ShowSettings(&appState->windows.supportersTribute);
937 if (appState->windows.skySettings)
939 appState->skyManager->ShowSettings(&appState->windows.skySettings);
942 if(appState->windows.textureBaker)
944 appState->textureBaker->ShowSettings(&appState->windows.textureBaker);
950 virtual void OnStart(std::string loadFile)
override
953 srand((
unsigned int)time(NULL));
957 appState->mainApp = mainApp;
958 appState->constants.executableDir = GetExecutableDir();
959 appState->constants.dataDir = appState->constants.executableDir + PATH_SEPARATOR
"Data";
960 appState->constants.cacheDir = appState->constants.dataDir + PATH_SEPARATOR
"cache";
961 appState->constants.texturesDir = appState->constants.dataDir + PATH_SEPARATOR
"textures";
962 appState->constants.projectsDir = appState->constants.cacheDir + PATH_SEPARATOR
"project_data";
963 appState->constants.tempDir = appState->constants.dataDir + PATH_SEPARATOR
"temp";
964 appState->constants.shadersDir = appState->constants.dataDir + PATH_SEPARATOR
"shaders";
965 appState->constants.kernelsDir = appState->constants.dataDir + PATH_SEPARATOR
"kernels";
966 appState->constants.fontsDir = appState->constants.dataDir + PATH_SEPARATOR
"fonts";
967 appState->constants.liscensesDir = appState->constants.dataDir + PATH_SEPARATOR
"licenses";
968 appState->constants.skyboxDir = appState->constants.dataDir + PATH_SEPARATOR
"skybox";
969 appState->constants.modulesDir = appState->constants.dataDir + PATH_SEPARATOR
"modules";
970 appState->constants.configsDir = appState->constants.dataDir + PATH_SEPARATOR
"configs";
971 appState->constants.logsDir = appState->constants.dataDir + PATH_SEPARATOR
"logs";
972 appState->constants.modelsDir = appState->constants.dataDir + PATH_SEPARATOR
"models";
974 SetupExplorerControls();
975 ImGui::GetStyle().WindowMenuButtonPosition = ImGuiDir_None;
977 GetWindow()->SetShouldCloseCallback(OnAppClose);
978 glfwSetFramebufferSizeCallback(GetWindow()->GetNativeWindow(), [](GLFWwindow* window,
int w,
int h)
980 glfwSwapBuffers(window);
982 glfwSetScrollCallback(GetWindow()->GetNativeWindow(), [](GLFWwindow *,
double x,
double y)
984 ImGuiIO &io = ImGui::GetIO();
985 io.MouseWheel = (float)y;
987 glfwSetDropCallback(GetWindow()->GetNativeWindow(), [](GLFWwindow *,
int count,
const char ** paths)
989 for (
int i = 0; i < count; i++)
991 std::string path = paths[i];
992 if (path.find(
".terr3d") != std::string::npos)
994 appState->serailizer->LoadFile(path);
996 else if(path.find(
".terr3dpack") != std::string::npos)
998 appState->serailizer->LoadPackedProject(path);
1002 GetWindow()->SetClearColor({ 0.1f, 0.1f, 0.1f });
1003 appState->globals.kernelsIncludeDir =
"\""+ appState->constants.kernelsDir +
"\"";
1006 appState->mainMenu =
new MainMenu(appState);
1007 appState->seaManager =
new SeaManager(appState);
1009 appState->skyManager =
new SkyManager(appState);
1012 appState->serailizer =
new Serializer(appState);
1013 appState->osLiscences =
new OSLiscences(appState);
1018 appState->meshGenerator->GenerateSync();
1019 appState->models.coreTerrain->SetupMeshOnGPU();
1020 appState->models.screenQuad->SetupMeshOnGPU();
1021 appState->models.screenQuad->mesh->GenerateScreenQuad();
1022 appState->models.screenQuad->mesh->RecalculateNormals();
1023 appState->models.screenQuad->UploadToGPU();
1024 glEnable(GL_DEPTH_TEST);
1025 appState->globals.scale = 1;
1027 if (loadFile.size() > 0)
1029 Log(
"Loading File from " + loadFile);
1030 OpenSaveFile(loadFile);
1033 appState->projectManager->SetId(GenerateId(32));
1037 LoadUIFont(
"Open-Sans-Regular", 18, appState->constants.fontsDir + PATH_SEPARATOR
"OpenSans-Regular.ttf");
1038 LoadUIFont(
"OpenSans-Bold", 25, appState->constants.fontsDir + PATH_SEPARATOR
"OpenSans-Bold.ttf");
1039 LoadUIFont(
"OpenSans-Semi-Bold", 22, appState->constants.fontsDir + PATH_SEPARATOR
"OpenSans-Bold.ttf");
1041 appState->frameBuffers.reflection =
new FrameBuffer();
1043 appState->frameBuffers.main =
new FrameBuffer(1280, 720);
1045 appState->frameBuffers.postProcess =
new FrameBuffer(1280, 720);
1047 appState->shaders.postProcess =
new Shader(ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"post_processing" PATH_SEPARATOR
"vert.glsl", &tpp),
1048 ReadShaderSourceFile(appState->constants.shadersDir + PATH_SEPARATOR
"post_processing" PATH_SEPARATOR
"frag.glsl", &tpp));
1049 appState->states.autoUpdate =
true;
1051 if(IsNetWorkConnected())
1053 if(FileExists(appState->constants.configsDir + PATH_SEPARATOR
"server.terr3d"))
1056 std::string uid = ReadShaderSourceFile(appState->constants.configsDir + PATH_SEPARATOR
"server.terr3d", &ttmp);
1057 Log(
"Connection to Backend : " + FetchURL(
"https://terraforge3d.maxalpha.repl.co",
"/startup/" + uid));
1062 DownloadFile(
"https://terraforge3d.maxalpha.repl.co",
"/register", appState->constants.configsDir + PATH_SEPARATOR
"server.terr3d");
1064 std::string uid = ReadShaderSourceFile(appState->constants.configsDir + PATH_SEPARATOR
"server.terr3d", &ttmp);
1065 Log(
"Connection to Backend : " + FetchURL(
"https://terraforge3d.maxalpha.repl.co",
"/startup/" + uid));
1069 appState->windows.shadingManager =
true;
1070 Log(
"Started Up App!");
1075 while (appState->states.remeshing);
1077 using namespace std::chrono_literals;
1078 std::this_thread::sleep_for(500ms);
1079 delete appState->shaders.terrain;
1080 delete appState->shaders.foliage;
1081 delete appState->shaders.wireframe;
1082 delete appState->shaders.postProcess;
1083 delete appState->supportersTribute;
1084 delete appState->mainMenu;
1085 delete appState->skyManager;
1086 delete appState->frameBuffers.main;
1087 delete appState->frameBuffers.postProcess;
1088 delete appState->frameBuffers.reflection;
1089 delete appState->filtersManager;
1090 delete appState->osLiscences;
1091 delete appState->textureBaker;
1092 delete appState->projectManager;
1093 delete appState->foliageManager;
1094 delete appState->shadingManager;
1096 delete appState->lightManager;
1097 delete appState->serailizer;
1104 mainApp =
new MyApp();