diff options
| author | hachem <im@hachem.wtf> | 2025-08-18 05:09:57 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-18 05:09:57 +0200 |
| commit | 908db805bd4e8830dc385a07d55c45873babaf7a (patch) | |
| tree | fbe913600a6ae61da73babc72074d79eda0a4d87 /src | |
| parent | 0c77bdb5a48343005dfdf1509d004b36022efb32 (diff) | |
feat: load scenes from JSON
Diffstat (limited to 'src')
| -rw-r--r-- | src/states/world_builder_state.cpp | 103 |
1 files changed, 62 insertions, 41 deletions
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"); |
