diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | .gitmodules | 3 | ||||
| m--------- | ext/json | 0 | ||||
| -rw-r--r-- | premake5.lua | 14 | ||||
| -rw-r--r-- | src/states/world_builder_state.cpp | 103 |
5 files changed, 74 insertions, 47 deletions
@@ -3,6 +3,7 @@ config/ *.log imgui.ini +Scene.json bin/ bin-int/ diff --git a/.gitmodules b/.gitmodules index bd018a2..78a45c0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,6 @@ [submodule "ext/ImGuizmo"] path = ext/ImGuizmo url = https://github.com/CedricGuillemet/ImGuizmo.git +[submodule "ext/json"] + path = ext/json + url = https://github.com/nlohmann/json.git diff --git a/ext/json b/ext/json new file mode 160000 +Subproject 52d3f6e034504b4097741b2da5b644a38ee273f diff --git a/premake5.lua b/premake5.lua index 57f577d..d420e46 100644 --- a/premake5.lua +++ b/premake5.lua @@ -24,13 +24,14 @@ workspace "Donut" outputdir = "%{cfg.buildcfg}-%{cfg.system}/%{prj.name}" IncludeDir = {} -IncludeDir["glm"] = "ext/glm" -IncludeDir["glfw"] = "ext/glfw/include" -IncludeDir["glad"] = "ext/glad/include" -IncludeDir["imgui"] = "ext/imgui" +IncludeDir["glm"] = "ext/glm" +IncludeDir["glfw"] = "ext/glfw/include" +IncludeDir["glad"] = "ext/glad/include" +IncludeDir["imgui"] = "ext/imgui" IncludeDir["imgui_backends"] = "ext/imgui/backends" -IncludeDir["imguizmo"] = "ext/ImGuizmo" -IncludeDir["toml11"] = "ext/toml11/include" +IncludeDir["imguizmo"] = "ext/ImGuizmo" +IncludeDir["toml11"] = "ext/toml11/include" +IncludeDir["nlohmann"] = "ext/json/single_include" group "Dependencies" @@ -331,6 +332,7 @@ project "Donut" "%{IncludeDir.imgui_backends}", "%{IncludeDir.imguizmo}", "%{IncludeDir.toml11}", + "%{IncludeDir.nlohmann}", } links diff --git a/src/states/world_builder_state.cpp b/src/states/world_builder_state.cpp index ad0e57b..1067d41 100644 --- a/src/states/world_builder_state.cpp +++ b/src/states/world_builder_state.cpp @@ -17,6 +17,8 @@ #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> +#include <nlohmann/json.hpp> + #include <numbers> #include <fstream> #include <sstream> @@ -40,18 +42,6 @@ namespace Donut m_camera.set_elevation(static_cast<float>(std::numbers::pi) / 3.0f); m_camera.update_orbital(); - Material default_material(glm::vec3(0.8f, 0.2f, 0.2f), 0.5f, 0.0f); - Object sphere1(glm::vec3(-3.0f, 0.0f, 0.0f), 1.0f, default_material); - m_scene.objs.push_back(sphere1); - - Material material2(glm::vec3(0.2f, 0.8f, 0.2f), 0.3f, 0.0f); - Object sphere2(glm::vec3(3.0f, 0.0f, 0.0f), 1.5f, material2); - m_scene.objs.push_back(sphere2); - - Material material3(glm::vec3(0.2f, 0.2f, 0.8f), 0.7f, 0.0f); - Object sphere3(glm::vec3(0.0f, 2.0f, 0.0f), 0.8f, material3); - m_scene.objs.push_back(sphere3); - m_sphere_shader = Ref<Shader>(Shader::create("assets/shaders/sphere.glsl")); initialize_sphere_geometry(); @@ -514,20 +504,38 @@ namespace Donut void WorldBuilderState::save_scene() { - std::ofstream file("scene.txt"); + nlohmann::json scene_data; + scene_data["objects"] = nlohmann::json::array(); - if (file.is_open()) + for (const auto& obj : m_scene.objs) { - file << m_scene.objs.size() << std::endl; - for (const auto& obj : m_scene.objs) + nlohmann::json sphere_data; + sphere_data["position"] = + { + obj.m_centre.x, + obj.m_centre.y, + obj.m_centre.z + }; + + sphere_data["color"] = { - file << obj.m_centre.x << " " << obj.m_centre.y << " " << obj.m_centre.z << " "; - file << obj.m_radius << " "; - file << obj.m_material.m_color.x << " " << obj.m_material.m_color.y << " " << obj.m_material.m_color.z << " "; - file << obj.m_material.m_specular << " " << obj.m_material.m_emission << std::endl; - } + obj.m_material.m_color.x, + obj.m_material.m_color.y, + obj.m_material.m_color.z + }; + + sphere_data["radius"] = obj.m_radius; + sphere_data["specular"] = obj.m_material.m_specular; + sphere_data["emission"] = obj.m_material.m_emission; + scene_data["objects"].push_back(sphere_data); + } + + std::ofstream file("Scene.json"); + if (file.is_open()) + { + file << scene_data.dump(4); file.close(); - DONUT_INFO("Scene saved to scene.txt"); + DONUT_INFO("Scene saved to Scene.json"); } else DONUT_ERROR("Failed to save scene"); @@ -535,34 +543,47 @@ namespace Donut void WorldBuilderState::load_scene() { - std::ifstream file("scene.txt"); + std::ifstream file("Scene.json"); if (file.is_open()) { m_scene.objs.clear(); m_selected_object_index = -1; - size_t object_count; - file >> object_count; - - for (size_t i = 0; i < object_count; ++i) + nlohmann::json scene_data; + file >> scene_data; + + if (scene_data.contains("objects")) { - glm::vec3 position; - float radius; - glm::vec3 color; - float specular, emission; - - file >> position.x >> position.y >> position.z; - file >> radius; - file >> color.x >> color.y >> color.z; - file >> specular >> emission; - - Material material(color, specular, emission); - Object sphere(position, radius, material); - m_scene.objs.push_back(sphere); + for (const auto& sphere_data : scene_data["objects"]) + { + glm::vec3 position; + glm::vec3 color; + float radius; + float specular; + float emission; + + auto pos_array = sphere_data["position"]; + position.x = pos_array[0].get<float>(); + position.y = pos_array[1].get<float>(); + position.z = pos_array[2].get<float>(); + + auto color_array = sphere_data["color"]; + color.x = color_array[0].get<float>(); + color.y = color_array[1].get<float>(); + color.z = color_array[2].get<float>(); + + radius = sphere_data["radius"].get<float>(); + specular = sphere_data["specular"].get<float>(); + emission = sphere_data["emission"].get<float>(); + + Material material(color, specular, emission); + Object sphere(position, radius, material); + m_scene.objs.push_back(sphere); + } } file.close(); - DONUT_INFO("Scene loaded from scene.txt"); + DONUT_INFO("Scene loaded from Scene.json"); } else DONUT_ERROR("Failed to load scene"); |
