diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 963d19e6..50b147a4 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -181,7 +181,7 @@ int main() EditorInterface editor; editor.BuildFonts(); - CreateScene(); + //CreateScene(); while (!Engine::GetCurrentWindow()->ShouldClose()) { diff --git a/Editor/src/EditorInterface.cpp b/Editor/src/EditorInterface.cpp index 20ec36bb..5a0d3573 100644 --- a/Editor/src/EditorInterface.cpp +++ b/Editor/src/EditorInterface.cpp @@ -16,6 +16,7 @@ #include #include #include +#include "src/Resource/Project.h" ImFont* normalFont; ImFont* bigIconFont; @@ -180,6 +181,8 @@ void EditorInterface::DrawSceneTree() { Ref scene = Engine::GetCurrentScene(); + if (!scene) + return; if (ImGui::Begin("Environnement")) { auto env = Engine::GetCurrentScene()->GetEnvironment(); @@ -842,13 +845,36 @@ void EditorInterface::Draw() { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("New project", "CTRL+N")) {} - if (ImGui::MenuItem("Open...", "CTRL+O")) { - FileDialog::OpenFile(".project"); + if (ImGui::MenuItem("New project", "CTRL+N")) + { + if(Engine::GetProject()) + Engine::GetProject()->Save(); + + // Parse the project and load it. + std::string selectedProject = FileDialog::SaveFile(".project") + ".project"; + + Ref project = Project::New("Unnamed project", "no description", selectedProject); + Engine::LoadProject(project); + + Ref scene = Scene::New(); + Engine::LoadScene(scene); } - if (ImGui::MenuItem("Save", "CTRL+S")) {} - if (ImGui::MenuItem("Save as...", "CTRL+SHIFT+S")) { - FileDialog::SaveFile(".project"); + if (ImGui::MenuItem("Open...", "CTRL+O")) + { + // Parse the project and load it. + std::string selectedProject = FileDialog::OpenFile(".project"); + Ref project = Project::Load(selectedProject); + Engine::LoadProject(project); + } + if (ImGui::MenuItem("Save", "CTRL+S")) + { + Engine::GetProject()->Save(); + Engine::GetCurrentScene()->Save(); + } + if (ImGui::MenuItem("Save as...", "CTRL+SHIFT+S")) + { + std::string savePath = FileDialog::SaveFile("*.project"); + Engine::GetProject()->SaveAs(savePath); } ImGui::EndMenu(); } diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index f5d1edb1..d9551c6e 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -7,20 +7,24 @@ float Engine::m_LastFrameTime = 0.0f; Ref Engine::CurrentWindow; bool Engine::IsPlayMode = false; +Ref Engine::CurrentProject; + #include "../Rendering/Renderer.h" +#include +#include void Engine::Init() { - PhysicsManager::Get()->Init(); FileSystem::Scan(); CurrentWindow = std::make_shared(); - - } void Engine::Tick() { + if (!CurrentWindow->GetScene()) + return; + float time = (float)glfwGetTime(); Timestep timestep = time - m_LastFrameTime; m_LastFrameTime = time; @@ -49,6 +53,11 @@ void Engine::ExitPlayMode() void Engine::Draw() { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + + ImGui::NewFrame(); + Window::Get()->Draw(); } @@ -72,6 +81,27 @@ Ref Engine::GetCurrentScene() return CurrentWindow->GetScene(); } +bool Engine::LoadScene(Ref scene) +{ + CurrentWindow->SetScene(scene); + return true; +} + +Ref Engine::GetProject() +{ + return CurrentProject; +} + +bool Engine::LoadProject(Ref project) +{ + CurrentProject = project; + + Ref newScene = CreateRef(); + Engine::LoadScene(newScene); + + return true; +} + int Engine::HelloWorld() { return 1; diff --git a/Nuake/Engine.h b/Nuake/Engine.h index 901ebf83..622c690d 100644 --- a/Nuake/Engine.h +++ b/Nuake/Engine.h @@ -2,13 +2,13 @@ #include "src/Core/Core.h" #include "src/Window.h" #include "src/Scene/Scene.h" +#include "src/Resource/Project.h" - -class __declspec(dllexport) Engine { +class Engine { private: static float m_LastFrameTime; static Ref CurrentWindow; - + static Ref CurrentProject; public: static bool IsPlayMode; @@ -26,4 +26,8 @@ public: static int HelloWorld(); static Ref GetCurrentWindow(); static Ref GetCurrentScene(); + static bool LoadScene(Ref scene); + + static Ref GetProject(); + static bool LoadProject(Ref project); }; \ No newline at end of file diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index 0ae6aec3..93088fbf 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -30,7 +30,6 @@ std::string FileDialog::OpenFile(const char* filter) return std::string(); } - std::string FileDialog::SaveFile(const char* filter) { @@ -51,11 +50,9 @@ std::string FileDialog::SaveFile(const char* filter) return std::string(); } - - std::string FileSystem::Root = "resources\\"; -Ref FileSystem::RootDirectory; +Ref FileSystem::RootDirectory; void FileSystem::ScanDirectory(Ref directory) { for (const auto& entry : std::filesystem::directory_iterator(directory->fullPath)) @@ -82,7 +79,10 @@ void FileSystem::ScanDirectory(Ref directory) } } } - +bool FileSystem::DirectoryExists(const std::string path) +{ + return false; +} void FileSystem::Scan() { RootDirectory = CreateRef(); diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index 11d9559b..78fa1163 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -37,7 +37,12 @@ public: static Ref RootDirectory; static void Scan(); + static Ref GetFileTree(); static void ScanDirectory(Ref directory); static void GetDirectories(); - static Ref GetFileTree(); + + static bool DirectoryExists(const std::string path); + static bool FileExists(const std::string path); + + }; \ No newline at end of file diff --git a/Nuake/src/Core/Physics/PhysicsShapes.h b/Nuake/src/Core/Physics/PhysicsShapes.h index 7b242bff..0cc2054e 100644 --- a/Nuake/src/Core/Physics/PhysicsShapes.h +++ b/Nuake/src/Core/Physics/PhysicsShapes.h @@ -7,7 +7,7 @@ namespace Physics { enum RigidbodyShapes { BOX, SPHERE, CAPSULE, MESH }; - class __declspec(dllexport) PhysicShape { + class PhysicShape { protected: btCollisionShape* bShape; RigidbodyShapes m_Type; diff --git a/Nuake/src/Rendering/Camera.cpp b/Nuake/src/Rendering/Camera.cpp index 974be661..e16f9af7 100644 --- a/Nuake/src/Rendering/Camera.cpp +++ b/Nuake/src/Rendering/Camera.cpp @@ -64,5 +64,21 @@ glm::mat4 Camera::GetTransformRotation() return lookAt(glm::vec3(), cameraFront, cameraUp); } +json Camera::Serialize() +{ + BEGIN_SERIALIZE(); + SERIALIZE_VAL(m_Type); + SERIALIZE_VAL(AspectRatio); + SERIALIZE_VAL(Fov); + SERIALIZE_VAL(Exposure); + SERIALIZE_VAL(Speed); + END_SERIALIZE(); +} + +bool Camera::Deserialize(const std::string& str) +{ + return false; +} + diff --git a/Nuake/src/Rendering/Camera.h b/Nuake/src/Rendering/Camera.h index 8c48df87..db695aac 100644 --- a/Nuake/src/Rendering/Camera.h +++ b/Nuake/src/Rendering/Camera.h @@ -2,19 +2,20 @@ #include "../Core/Timestep.h" #include #include +#include "../Resource/Serializable.h" + enum CAMERA_TYPE { ORTHO, PERSPECTIVE }; class EditorCamera; -class __declspec(dllexport) Camera +class Camera : public ISerializable { private: CAMERA_TYPE m_Type; float AspectRatio = 16.0f / 9.0f; - glm::vec3 Rotation = { 0.0f, 0.0f, 0.0f }; glm::vec3 Scale = { 1.0f, 1.0f, 1.0f }; @@ -56,5 +57,8 @@ public: glm::mat4 GetTransform(); glm::mat4 GetTransformRotation(); + json Serialize() override; + bool Deserialize(const std::string& str) override; + friend EditorCamera; }; diff --git a/Nuake/src/Rendering/ProceduralSky.cpp b/Nuake/src/Rendering/ProceduralSky.cpp index 6795e100..5f7878fe 100644 --- a/Nuake/src/Rendering/ProceduralSky.cpp +++ b/Nuake/src/Rendering/ProceduralSky.cpp @@ -84,4 +84,21 @@ void ProceduralSky::Draw(Ref cam) { glm::vec3 ProceduralSky::GetSunDirection() { return SunDirection; -} \ No newline at end of file +} + +json ProceduralSky::Serialize() +{ + BEGIN_SERIALIZE() + SERIALIZE_VAL(SurfaceRadius); + SERIALIZE_VAL(AtmosphereRadius); + SERIALIZE_VEC3(RayleighScattering); + SERIALIZE_VEC3(MieScattering); + SERIALIZE_VAL(SunIntensity); + SERIALIZE_VEC3(SunDirection); + END_SERIALIZE(); +} + +bool ProceduralSky::Deserialize(const std::string& str) +{ + return false; +} diff --git a/Nuake/src/Rendering/ProceduralSky.h b/Nuake/src/Rendering/ProceduralSky.h index e5fa04a0..fd16fbee 100644 --- a/Nuake/src/Rendering/ProceduralSky.h +++ b/Nuake/src/Rendering/ProceduralSky.h @@ -2,14 +2,17 @@ #include #include "Vertex.h" #include "../Core/Core.h" +#include "../Resource/Serializable.h" + class Camera; -class ProceduralSky { +class ProceduralSky : ISerializable { public: float SurfaceRadius = 6360e3; float AtmosphereRadius = 6380e3; glm::vec3 RayleighScattering = glm::vec3(58e-7, 135e-7, 331e-7); glm::vec3 MieScattering = glm::vec3(2e-5); float SunIntensity = 100.0; + glm::vec3 CenterPoint = glm::vec3(0, -SurfaceRadius, 0); glm::vec3 SunDirection = glm::vec3(0.20000, 0.95917, 0.20000); unsigned int VAO; @@ -19,4 +22,6 @@ public: glm::vec3 GetSunDirection(); + json Serialize() override; + bool Deserialize(const std::string& str) override; }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/CameraComponent.h b/Nuake/src/Scene/Entities/Components/CameraComponent.h index c6f924ca..2ba0f051 100644 --- a/Nuake/src/Scene/Entities/Components/CameraComponent.h +++ b/Nuake/src/Scene/Entities/Components/CameraComponent.h @@ -1,7 +1,8 @@ #pragma once #include "TransformComponent.h" #include "../Core/Core.h" -class __declspec(dllexport) CameraComponent { +#include "../Resource/Serializable.h" +class CameraComponent { public: Ref CameraInstance; TransformComponent* transformComponent; @@ -9,4 +10,11 @@ public: CameraComponent(); void DrawEditor(); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_OBJECT(CameraInstance); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/LightComponent.h b/Nuake/src/Scene/Entities/Components/LightComponent.h index 5240ff16..a9908cea 100644 --- a/Nuake/src/Scene/Entities/Components/LightComponent.h +++ b/Nuake/src/Scene/Entities/Components/LightComponent.h @@ -5,13 +5,13 @@ #include "../Rendering/Camera.h" #include "../../../Rendering/Framebuffer.h" #include "BaseComponent.h"; - +#include "../Resource/Serializable.h" enum LightType { Directional, Point, Spot }; -class __declspec(dllexport) LightComponent { +class LightComponent { public: glm::vec2 yes = glm::vec2(2, 2); @@ -201,4 +201,17 @@ public: void SetType(LightType type); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(Type); + SERIALIZE_VEC3(Direction); + SERIALIZE_VEC3(Color); + SERIALIZE_VAL(IsVolumetric); + SERIALIZE_VAL(Strength); + SERIALIZE_VAL(SyncDirectionWithSky); + SERIALIZE_VAL(CastShadows); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/MeshComponent.h b/Nuake/src/Scene/Entities/Components/MeshComponent.h index 49fcc5cc..33a4f835 100644 --- a/Nuake/src/Scene/Entities/Components/MeshComponent.h +++ b/Nuake/src/Scene/Entities/Components/MeshComponent.h @@ -3,7 +3,7 @@ #include #include "BaseComponent.h"; -class __declspec(dllexport) MeshComponent { +class MeshComponent { private: unsigned int VAO; diff --git a/Nuake/src/Scene/Entities/Components/NameComponent.h b/Nuake/src/Scene/Entities/Components/NameComponent.h index a55222cf..038ec979 100644 --- a/Nuake/src/Scene/Entities/Components/NameComponent.h +++ b/Nuake/src/Scene/Entities/Components/NameComponent.h @@ -1,5 +1,14 @@ #pragma once +#include "../Resource/Serializable.h" + class NameComponent { public: std::string Name = "Entity"; + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(Name); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/ParentComponent.h b/Nuake/src/Scene/Entities/Components/ParentComponent.h index 162c9096..13a1bb61 100644 --- a/Nuake/src/Scene/Entities/Components/ParentComponent.h +++ b/Nuake/src/Scene/Entities/Components/ParentComponent.h @@ -2,10 +2,18 @@ #include "../Entity.h" - struct ParentComponent { Entity Parent; bool HasParent = false; std::vector Children = std::vector(); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(HasParent); + if(HasParent) + SERIALIZE_VAL_LBL("Parent", Parent.GetHandle()); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/QuakeMap.h b/Nuake/src/Scene/Entities/Components/QuakeMap.h index 36f21e57..3fe9e0f4 100644 --- a/Nuake/src/Scene/Entities/Components/QuakeMap.h +++ b/Nuake/src/Scene/Entities/Components/QuakeMap.h @@ -3,7 +3,9 @@ #include #include "../Rendering/Mesh/Mesh.h" #include "../Resource/TrenchbroomMap.h" -class __declspec(dllexport) QuakeMap { +#include "../Resource/Serializable.h" + +class QuakeMap { private: @@ -18,4 +20,11 @@ public: void Draw(); void DrawEditor(); + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(HasCollisions); + SERIALIZE_VAL(Path); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Components/RigidbodyComponent.cpp b/Nuake/src/Scene/Entities/Components/RigidbodyComponent.cpp index cf43757d..9f3d2937 100644 --- a/Nuake/src/Scene/Entities/Components/RigidbodyComponent.cpp +++ b/Nuake/src/Scene/Entities/Components/RigidbodyComponent.cpp @@ -32,7 +32,6 @@ float RigidBodyComponent::GetMass() { return 0.0f; } - void RigidBodyComponent::SetMass(float m) { if (!m_Rigidbody) diff --git a/Nuake/src/Scene/Entities/Components/TransformComponent.h b/Nuake/src/Scene/Entities/Components/TransformComponent.h index 02eaef25..ed19b09f 100644 --- a/Nuake/src/Scene/Entities/Components/TransformComponent.h +++ b/Nuake/src/Scene/Entities/Components/TransformComponent.h @@ -1,7 +1,8 @@ #pragma once #include #include "BaseComponent.h"; -class __declspec(dllexport) TransformComponent { +#include "../Resource/Serializable.h" +class TransformComponent { public: glm::vec3 Translation; glm::vec3 Rotation; @@ -11,4 +12,14 @@ public: glm::mat4 GetTransform(); void DrawEditor(); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL_LBL("Type", "TransformComponent"); + SERIALIZE_VEC3(Translation); + SERIALIZE_VEC3(Rotation); + SERIALIZE_VEC3(Scale); + END_SERIALIZE(); + } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 9fc2c37c..dd3717f8 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -2,6 +2,11 @@ #include "Components/ParentComponent.h" #include "Entity.h" +#include "Components/NameComponent.h" +#include "Components/TransformComponent.h" +#include "Components/CameraComponent.h" +#include "Components/QuakeMap.h" +#include "Components/LightComponent.h" void Entity::AddChild(Entity ent) { if ((int)m_EntityHandle != ent.GetHandle()) @@ -13,6 +18,26 @@ void Entity::AddChild(Entity ent) } } +json Entity::Serialize() +{ + BEGIN_SERIALIZE(); + SERIALIZE_OBJECT_REF_LBL("NameComponent", GetComponent()); + SERIALIZE_OBJECT_REF_LBL("ParentComponent", GetComponent()); + SERIALIZE_OBJECT_REF_LBL("TransformComponent", GetComponent()); + if(HasComponent()) + SERIALIZE_OBJECT_REF_LBL("CameraComponent", GetComponent()); + if(HasComponent()) + SERIALIZE_OBJECT_REF_LBL("QuakemapComponent", GetComponent()); + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("LightComponent", GetComponent()); + END_SERIALIZE(); +} + +bool Entity::Deserialize(const std::string& str) +{ + return false; +} + Entity::Entity(entt::entity handle, Scene* scene) { m_EntityHandle = handle; diff --git a/Nuake/src/Scene/Entities/Entity.h b/Nuake/src/Scene/Entities/Entity.h index e631d2f6..0196427d 100644 --- a/Nuake/src/Scene/Entities/Entity.h +++ b/Nuake/src/Scene/Entities/Entity.h @@ -3,7 +3,9 @@ #include #include "../Scene.h" #include "Components/BaseComponent.h" -class __declspec(dllexport) Entity +#include "../Resource/Serializable.h" + +class __declspec(dllexport) Entity : public ISerializable { public: Entity(entt::entity handle, Scene* scene); @@ -49,6 +51,10 @@ public: { return !(*this == other); } + + json Serialize() override; + bool Deserialize(const std::string& str); + private: entt::entity m_EntityHandle; Scene* m_Scene; diff --git a/Nuake/src/Scene/Lighting/Environment.cpp b/Nuake/src/Scene/Lighting/Environment.cpp index 773613fc..3196d73c 100644 --- a/Nuake/src/Scene/Lighting/Environment.cpp +++ b/Nuake/src/Scene/Lighting/Environment.cpp @@ -24,4 +24,19 @@ void Environment::Push() { //Renderer::m_Shader->SetUniform4f("u_AmbientColor", 1.0f, 1.0f, 1.0f, 1.0f); Renderer::m_Shader->SetUniform1f("u_FogAmount", VolumetricFog); Renderer::m_Shader->SetUniform1f("u_FogStepCount", VolumetricStepCount); -} \ No newline at end of file +} + +json Environment::Serialize() +{ + BEGIN_SERIALIZE(); + SERIALIZE_VAL(VolumetricFog); + SERIALIZE_VAL(VolumetricStepCount); + SERIALIZE_VEC4(AmbientColor); + SERIALIZE_OBJECT(ProceduralSkybox); + END_SERIALIZE(); +} + +bool Environment::Deserialize(const std::string& str) +{ + return false; +} diff --git a/Nuake/src/Scene/Lighting/Environment.h b/Nuake/src/Scene/Lighting/Environment.h index fa21f7de..3863f398 100644 --- a/Nuake/src/Scene/Lighting/Environment.h +++ b/Nuake/src/Scene/Lighting/Environment.h @@ -4,7 +4,8 @@ #include "../Core/Core.h" #include "../Rendering/ProceduralSky.h" -class __declspec(dllexport) Environment +#include "../Resource/Serializable.h" +class Environment : public ISerializable { public: Environment(); @@ -15,10 +16,11 @@ public: Ref ProceduralSkybox; glm::vec4 m_AmbientColor; - glm::vec4 GetAmbientColor(); - void SetAmbientColor(glm::vec4 color); void Push(); + + json Serialize() override; + bool Deserialize(const std::string& str) override; }; diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 39141b7b..2c889423 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -9,29 +9,40 @@ #include #include "Entities/Components/BoxCollider.h" #include "../../Engine.h" +#include "../Core/FileSystem.h" +#include +#include + +Ref Scene::New() +{ + return CreateRef(); +} Scene::Scene() { - m_Environement = CreateRef(); - auto camEntity = CreateEntity("Camera"); camEntity.AddComponent().transformComponent = &camEntity.GetComponent(); m_EditorCamera = CreateRef(); -} - - -Scene::~Scene() { -} - - -void Scene::Init() { - //m_Skybox = new SkyboxHDR("Res/Textures/Skyboxes/HDR/lilienstein_4k.hdr"); - m_Environement = CreateRef(); } +Scene::~Scene() {} + +std::string Scene::GetName() +{ + return this->Name; +} + +bool Scene::SetName(std::string& newName) +{ + if (newName == "") + return false; + + this->Name = newName; + return true; +} void Scene::OnInit() { @@ -145,7 +156,6 @@ void Scene::Update(Timestep ts) void Scene::EditorUpdate(Timestep ts) { m_EditorCamera->Update(ts); - } @@ -206,72 +216,6 @@ void Scene::DrawShadows() } } -void Scene::DrawGBuffer() -{ - Renderer::m_GBufferShader->Bind(); - Ref cam = nullptr; - { - auto view = m_Registry.view(); - for (auto e : view) { - auto [transform, camera] = view.get(e); - cam = camera.CameraInstance; - cam->Translation = transform.Translation; - break; - } - } - if (cam) - { - auto view = m_Registry.view(); - for (auto e : view) { - auto [transform, model] = view.get(e); - Renderer::m_GBufferShader->SetUniformMat4f("u_View", cam->GetTransform()); - Renderer::m_GBufferShader->SetUniformMat4f("u_Projection", cam->GetPerspective()); - Renderer::m_GBufferShader->SetUniformMat4f("u_Model", transform.GetTransform()); - model.Draw(); - } - - auto view2 = m_Registry.view(); - for (auto e : view2) { - auto [transform, model] = view2.get(e); - Renderer::m_GBufferShader->SetUniformMat4f("u_View", cam->GetTransform()); - Renderer::m_GBufferShader->SetUniformMat4f("u_Projection", cam->GetPerspective()); - Renderer::m_GBufferShader->SetUniformMat4f("u_Model", transform.GetTransform()); - model.Draw(); - } - - } -} - -void Scene::DrawDeferred() -{ - // Find the camera of the scene. - Ref cam = nullptr; - { - auto view = m_Registry.view(); - for (auto e : view) { - auto [transform, camera] = view.get(e); - cam = camera.CameraInstance; - break; - } - } - - { - auto view = m_Registry.view(); - for (auto l : view) { - auto [transform, light] = view.get(l); - //light.DrawDeferred(transform, cam); - } - } - - //m_Skybox->Draw(cam->GetPerspective(), cam->GetTransform()); - if (m_Skybox != nullptr) - m_Skybox->Push(); - glm::vec3 camPos = cam->GetTranslation(); - Renderer::m_DeferredShader->SetUniform1f("u_Exposure", cam->Exposure); - Renderer::m_DeferredShader->SetUniform3f("u_EyePosition", camPos.x, camPos.y, camPos.z); - Renderer::m_DeferredShader->SetUniformMat4f("u_View", cam->GetTransform()); - Renderer::m_DeferredShader->SetUniformMat4f("u_Projection", cam->GetPerspective()); -} void Scene::Draw() { @@ -433,7 +377,6 @@ void Scene::EditorDraw() { currentParent = currentParent.GetComponent().Parent; globalOffset += currentParent.GetComponent().Translation; - } copyT.Translation = globalOffset; @@ -589,7 +532,7 @@ glm::vec3 Scene::GetGlobalPosition(Entity ent) } -Entity Scene::GetEntity(const std::string name) +Entity Scene::GetEntity(const std::string& name) { std::vector allEntities; auto view = m_Registry.view(); @@ -600,7 +543,7 @@ Entity Scene::GetEntity(const std::string name) } } -Entity Scene::CreateEntity(const std::string name) { +Entity Scene::CreateEntity(const std::string& name) { Entity entity = { m_Registry.create(), this }; // Must have transform @@ -635,7 +578,6 @@ Ref Scene::GetCurrentCamera() } } return cam; - } return m_EditorCamera; @@ -644,3 +586,39 @@ Ref Scene::GetCurrentCamera() Ref Scene::GetEnvironment() { return m_Environement; } + +bool Scene::Save() +{ + if (Path == "") + Path = FileDialog::SaveFile("*.scene") + ".scene"; + + return SaveAs(Path); +} + +bool Scene::SaveAs(const std::string& path) +{ + std::ofstream sceneFile; + sceneFile.open(path); + sceneFile << Serialize().dump(4); + sceneFile.close(); + return true; +} + +json Scene::Serialize() +{ + BEGIN_SERIALIZE(); + SERIALIZE_VAL(Name); + SERIALIZE_OBJECT(m_Environement) + std::vector entities = std::vector(); + for (Entity e : GetAllEntities()) + entities.push_back(e.Serialize()); + SERIALIZE_VAL_LBL("Entities", entities); + END_SERIALIZE(); +} + + + +bool Scene::Deserialize(const std::string& str) +{ + return false; +} diff --git a/Nuake/src/Scene/Scene.h b/Nuake/src/Scene/Scene.h index 863347a2..75f7569d 100644 --- a/Nuake/src/Scene/Scene.h +++ b/Nuake/src/Scene/Scene.h @@ -8,9 +8,10 @@ #include "../Rendering/ProceduralSky.h" #include "../Core/Core.h" #include "EditorCamera.h" +#include "../Resource/Serializable.h" class Entity; -class __declspec(dllexport) Scene +class Scene : public ISerializable { friend Entity; @@ -18,13 +19,17 @@ private: Ref m_Environement; entt::registry m_Registry; Ref m_EditorCamera; - + std::string Name; + std::string Path = ""; + bool has_changed = true; public: - - SkyboxHDR* m_Skybox; + static Ref New(); Scene(); ~Scene(); + std::string GetName(); + bool SetName(std::string& newName); + void Init(); void OnInit(); void OnExit(); @@ -39,10 +44,17 @@ public: std::vector GetAllEntities(); glm::vec3 GetGlobalPosition(Entity ent); - Entity GetEntity(const std::string name); - Entity CreateEntity(const std::string name); + Entity GetEntity(const std::string& name); + Entity CreateEntity(const std::string& name); void DestroyEntity(Entity entity); Ref GetCurrentCamera(); Ref GetEnvironment(); + + bool Save(); + bool SaveAs(const std::string& path); + + + json Serialize() override; + bool Deserialize(const std::string& str) override; }; \ No newline at end of file diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index cc45a23c..7aea9c08 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -39,7 +39,7 @@ Window::Window() Init(); Renderer::Init(); - m_Scene->Init(); + //m_Scene->Init(); } Window::~Window() @@ -62,6 +62,12 @@ GLFWwindow* Window::GetHandle() return m_Window; } +bool Window::SetScene(Ref scene) +{ + m_Scene = scene; + return true; +} + Ref Window::GetScene() { return m_Scene; @@ -205,7 +211,7 @@ int Window::Init() ImGui_ImplGlfw_InitForOpenGL(m_Window, true); ImGui_ImplOpenGL3_Init("#version 330"); - m_Scene = CreateRef(); + //m_Scene = CreateRef(); return 0; } @@ -224,163 +230,14 @@ bool init = false; void Window::Draw() { - Ref cam = m_Scene->GetCurrentCamera(); - + if (!m_Scene) + return; + Ref cam = m_Scene->GetCurrentCamera(); if (!cam) return; - - - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - - ImGui::NewFrame(); - /* - - static int selected = 0; - { - //QuadEntity* selectedEntity = m_Scene->GetEntity(selected); - ImGui::Begin("Scene"); - { - ImGui::BeginChild("Buttons", ImVec2(300, 20), false); - if (ImGui::Button("Add")) { - m_Scene->CreateEntity("Entity"); - } - ImGui::SameLine(); - if (ImGui::Button("Remove")) - { - m_Scene->DestroyEntity(selectedEntity); - selectedEntity = m_Scene->GetAllEntities().at(0); - }; - - ImGui::EndChild(); - - int idx = 0; - for (Entity e : m_Scene->GetAllEntities()) { - std::string name = e.GetComponent().Name; - if (ImGui::Selectable(name.c_str(), selected == idx)) { - selected = idx; - selectedEntity = e; - } - if (ImGui::BeginPopupContextItem()) - { - // your popup code - ImGui::EndPopup(); - } - idx++; - } - } - - ImGui::End(); - } - - bool show = true; - int id = 0; - - // - //m_Scene->DrawShadows(); - // - // Drawing to texture. - - - - ImGui::Begin("ShadowMap"); - { - - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - if (selectedEntity.HasComponent()) - ImGui::Image((void*)selectedEntity.GetComponent().m_Framebuffer->GetTexture()->GetID(), regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - - } - - - - //m_GBuffer->Bind(); - //m_Scene->DrawGBuffer(); - //m_GBuffer->Unbind(); - //// - //DrawQuad(); - - ImGui::Begin("Deferred output"); - { - - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - - ImGui::Image((void*)m_DeferredFrambuffer->GetTexture()->GetID(), regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - - } - - // Draw rect - //Renderer::m_DeferredShader->Bind(); - - ImGui::Begin("depth"); - { - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - - ImGui::Image((void*)m_GBuffer->gDepth, regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - } - - ImGui::Begin("Albedo"); - { - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - // If viewport is resized - if (m_GBuffer->GetSize() != viewportPanelSize) - { - // Update FBO size and camera aspect ratio. - //m_Framebuffer->UpdateSize(viewportPanelSize); - //cam->OnWindowResize(viewportPanelSize.x, viewportPanelSize.y); - } - - ImGui::Image((void*)m_GBuffer->gAlbedo, regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - } - - ImGui::Begin("Material"); - { - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - // If viewport is resized - if (m_GBuffer->GetSize() != viewportPanelSize) - { - // Update FBO size and camera aspect ratio. - //m_Framebuffer->UpdateSize(viewportPanelSize); - //cam->OnWindowResize(viewportPanelSize.x, viewportPanelSize.y); - } - - ImGui::Image((void*)m_GBuffer->gMaterial, regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - } - - ImGui::Begin("Normal"); - { - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y); - - // If viewport is resized - if (m_GBuffer->GetSize() != viewportPanelSize) - { - // Update FBO size and camera aspect ratio. - //m_Framebuffer->UpdateSize(viewportPanelSize); - //cam->OnWindowResize(viewportPanelSize.x, viewportPanelSize.y); - } - - ImGui::Image((void*)m_GBuffer->gNormal, regionAvail, ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - } - */ + Renderer::BeginDraw(cam); glCullFace(GL_FRONT); @@ -411,33 +268,33 @@ void Window::EndDraw() void Window::DrawQuad() { - Renderer::m_DeferredShader->Bind(); + //Renderer::m_DeferredShader->Bind(); - m_DeferredFrambuffer->Bind(); + //m_DeferredFrambuffer->Bind(); - m_Scene->DrawDeferred(); + //m_Scene->DrawDeferred(); - glActiveTexture(GL_TEXTURE0 + 5); - glBindTexture(GL_TEXTURE_2D, m_GBuffer->gAlbedo); - - glActiveTexture(GL_TEXTURE0 + 6); - glBindTexture(GL_TEXTURE_2D, m_GBuffer->gNormal); - - glActiveTexture(GL_TEXTURE0 + 7); - glBindTexture(GL_TEXTURE_2D, m_GBuffer->gMaterial); - - glActiveTexture(GL_TEXTURE0 + 8); - glBindTexture(GL_TEXTURE_2D, m_GBuffer->gDepth); - - Renderer::m_DeferredShader->SetUniform1i("m_Albedo", 5); - Renderer::m_DeferredShader->SetUniform1i("m_Depth", 8); - Renderer::m_DeferredShader->SetUniform1i("m_Normal", 6); - Renderer::m_DeferredShader->SetUniform1i("m_Material", 7); - - glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, 6); - - m_DeferredFrambuffer->Unbind(); + //glActiveTexture(GL_TEXTURE0 + 5); + //glBindTexture(GL_TEXTURE_2D, m_GBuffer->gAlbedo); + // + //glActiveTexture(GL_TEXTURE0 + 6); + //glBindTexture(GL_TEXTURE_2D, m_GBuffer->gNormal); + // + //glActiveTexture(GL_TEXTURE0 + 7); + //glBindTexture(GL_TEXTURE_2D, m_GBuffer->gMaterial); + // + //glActiveTexture(GL_TEXTURE0 + 8); + //glBindTexture(GL_TEXTURE_2D, m_GBuffer->gDepth); + // + //Renderer::m_DeferredShader->SetUniform1i("m_Albedo", 5); + //Renderer::m_DeferredShader->SetUniform1i("m_Depth", 8); + //Renderer::m_DeferredShader->SetUniform1i("m_Normal", 6); + //Renderer::m_DeferredShader->SetUniform1i("m_Material", 7); + // + //glBindVertexArray(vao); + //glDrawArrays(GL_TRIANGLES, 0, 6); + // + //m_DeferredFrambuffer->Unbind(); } diff --git a/Nuake/src/Window.h b/Nuake/src/Window.h index f35817b8..b2dcc275 100644 --- a/Nuake/src/Window.h +++ b/Nuake/src/Window.h @@ -22,6 +22,8 @@ public: static Window* Get(); GLFWwindow* GetHandle(); + + int Init(); void Update(Timestep ts); void Draw(); @@ -33,5 +35,7 @@ public: void DrawQuad(); Ref GetScene(); + bool SetScene(Ref scene); + Ref GetFrameBuffer() const; };