Skip to content

Commit e607730

Browse files
committed
-Dev: working on scene saver
1 parent 96fa61f commit e607730

File tree

8 files changed

+157
-11
lines changed

8 files changed

+157
-11
lines changed

include/engine/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
#define ASSERT_PTR(ptr) assert((ptr) && "Pointer is null")
5252

53-
// #define ENABLE_OPTICK_PROFILING
53+
#define ENABLE_OPTICK_PROFILING
5454
#ifdef ENABLE_OPTICK_PROFILING
5555
#define PROFILING_EVENT() OPTICK_EVENT()
5656
#define PROFILING_FRAME() OPTICK_FRAME("MainThread");

include/engine/core/textures/texture.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ Interface class for all textures
3333
class ITexture
3434
{
3535
protected:
36-
TextureSettings m_settings{};
36+
TextureSettings m_settings = {};
3737

38-
Graphics::Image m_image{};
39-
uint16_t m_channels{0};
38+
Graphics::Image m_image = {};
39+
uint16_t m_channels = 0;
40+
std::string m_fileRoute = "None";
4041

4142
bool m_isDirty{true};
4243

@@ -122,6 +123,12 @@ class ITexture
122123
inline TextureTypeFlagBits get_type() const {
123124
return m_settings.type;
124125
}
126+
inline std::string get_file_route() const {
127+
return m_fileRoute;
128+
}
129+
inline void set_file_route(std::string r) {
130+
m_fileRoute = r;
131+
}
125132
};
126133

127134
Graphics::Image* const get_image(ITexture* t);

include/engine/tools/loaders.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class SceneLoader
8484
{
8585
bool m_asyncLoad;
8686
Core::Transform load_transform(tinyxml2::XMLElement* obj);
87+
void save_transform(const Core::Transform& transform, tinyxml2::XMLElement* parentElement);
8788
void load_children(tinyxml2::XMLElement* element, Core::Object3D* const parent, std::string resourcesPath);
8889

8990
public:
@@ -93,11 +94,7 @@ class SceneLoader
9394
/*Loads a scene from an XML file*/
9495
void load_scene(Core::Scene* const scene, const std::string fileName);
9596
/*Saves a scene to an XML file*/
96-
void save_scene(const std::string fileName) {
97-
/*
98-
TBD
99-
*/
100-
}
97+
void save_scene(Core::Scene* const scene, const std::string fileName);
10198
};
10299

103100
}; // namespace Tools::Loaders

src/core/passes/composition_pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ void CompositionPass::setup_shader_passes() {
157157
}
158158

159159
void CompositionPass::render(Graphics::Frame& currentFrame, Scene* const scene, uint32_t presentImageIndex) {
160+
PROFILING_EVENT()
160161

161162
CommandBuffer cmd = currentFrame.commandBuffer;
162163

src/core/passes/postprocess_pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void PostProcessPass::setup_shader_passes() {
7676
}
7777

7878
void PostProcessPass::render(Graphics::Frame& currentFrame, Scene* const scene, uint32_t presentImageIndex) {
79+
PROFILING_EVENT()
7980

8081
CommandBuffer cmd = currentFrame.commandBuffer;
8182
cmd.begin_renderpass(m_renderpass, m_framebuffers[m_isDefault ? presentImageIndex : 0]);

src/core/passes/precomposition_pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ void PreCompositionPass::setup_shader_passes() {
167167
}
168168

169169
void PreCompositionPass::render(Graphics::Frame& currentFrame, Scene* const scene, uint32_t presentImageIndex) {
170+
PROFILING_EVENT()
170171

171172
CommandBuffer cmd = currentFrame.commandBuffer;
172173

src/tools/loaders.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ void VKFW::Tools::Loaders::load_PNG(Core::Texture* const texture,
733733
texture->set_image_cache(imgCache, {static_cast<unsigned int>(w), static_cast<unsigned int>(h), 1}, 4);
734734
// Set automatically teh optimal format for each type.
735735
// User can override it after, I he need some other more specific format ...
736+
texture->set_file_route(fileName);
736737
switch (textureFormat)
737738
{
738739
case TEXTURE_FORMAT_SRGB:
@@ -765,6 +766,8 @@ void VKFW::Tools::Loaders::load_HDRi(Core::TextureHDR* const texture, const std:
765766
{
766767
texture->set_image_cache(HDRcache, {static_cast<unsigned int>(w), static_cast<unsigned int>(h), 1}, 4);
767768
texture->set_format(SRGBA_32F);
769+
texture->set_file_route(fileName);
770+
768771
} else
769772
{
770773
#ifndef NDEBUG
@@ -795,6 +798,8 @@ void VKFW::Tools::Loaders::load_3D_texture(Core::ITexture* const texture,
795798
4);
796799
// Set automatically the optimal format for each type.
797800
// User can override it after, I he need some other more specific format ...
801+
texture->set_file_route(fileName);
802+
798803
switch (textureFormat)
799804
{
800805
case TEXTURE_FORMAT_SRGB:
@@ -852,6 +857,39 @@ VKFW::Core::Transform VKFW::Tools::Loaders::SceneLoader::load_transform(tinyxml2
852857
return transform;
853858
}
854859

860+
void VKFW::Tools::Loaders::SceneLoader::save_transform(const Core::Transform& transform,
861+
tinyxml2::XMLElement* parentElement) {
862+
tinyxml2::XMLDocument* doc = parentElement->GetDocument();
863+
864+
if (!doc)
865+
return;
866+
867+
tinyxml2::XMLElement* transformElement = doc->NewElement("Transform");
868+
869+
// Save position
870+
tinyxml2::XMLElement* positionElement = doc->NewElement("translate");
871+
positionElement->SetAttribute("x", transform.position.x);
872+
positionElement->SetAttribute("y", transform.position.y);
873+
positionElement->SetAttribute("z", transform.position.z);
874+
transformElement->InsertEndChild(positionElement);
875+
876+
// Save rotation
877+
tinyxml2::XMLElement* rotationElement = doc->NewElement("rotate");
878+
rotationElement->SetAttribute("x", math::degrees(transform.rotation.x));
879+
rotationElement->SetAttribute("y", math::degrees(transform.rotation.y));
880+
rotationElement->SetAttribute("z", math::degrees(transform.rotation.z));
881+
transformElement->InsertEndChild(rotationElement);
882+
883+
// Save scale
884+
tinyxml2::XMLElement* scaleElement = doc->NewElement("scale");
885+
scaleElement->SetAttribute("x", transform.scale.x);
886+
scaleElement->SetAttribute("y", transform.scale.y);
887+
scaleElement->SetAttribute("z", transform.scale.z);
888+
transformElement->InsertEndChild(scaleElement);
889+
890+
// Attach the transform element to the parent
891+
parentElement->InsertEndChild(transformElement);
892+
}
855893
void VKFW::Tools::Loaders::SceneLoader::load_children(tinyxml2::XMLElement* element,
856894
Core::Object3D* const parent,
857895
std::string resourcesPath) {
@@ -1257,3 +1295,71 @@ void VKFW::Tools::Loaders::SceneLoader::load_scene(Core::Scene* const scene, con
12571295
scene->set_ambient_intensity(0.0f);
12581296
}
12591297
}
1298+
1299+
void VKFW::Tools::Loaders::SceneLoader::save_scene(Core::Scene* const scene, const std::string fileName) {
1300+
if (!scene)
1301+
{
1302+
throw VKFW_Exception("Scene is null pointer");
1303+
}
1304+
1305+
tinyxml2::XMLDocument doc;
1306+
tinyxml2::XMLElement* sceneElement = doc.NewElement("Scene");
1307+
1308+
doc.InsertFirstChild(sceneElement);
1309+
1310+
tinyxml2::XMLElement* resourcesElement = doc.NewElement("Resources");
1311+
resourcesElement->SetAttribute("path", "");
1312+
sceneElement->InsertEndChild(resourcesElement);
1313+
1314+
// Save camera
1315+
Core::Camera* camera = scene->get_active_camera();
1316+
if (camera)
1317+
{
1318+
tinyxml2::XMLElement* cameraElement = doc.NewElement("Camera");
1319+
1320+
Core::Transform transform = camera->get_transform();
1321+
save_transform(transform, cameraElement);
1322+
cameraElement->SetAttribute("far", camera->get_far());
1323+
cameraElement->SetAttribute("near", camera->get_near());
1324+
cameraElement->SetAttribute("fov", camera->get_field_of_view());
1325+
1326+
sceneElement->InsertEndChild(cameraElement);
1327+
}
1328+
1329+
// // Recursively save children
1330+
// tinyxml2::XMLElement* childrenElement = doc.NewElement("Children");
1331+
// save_children(scene->get_root(), childrenElement, doc, resourcesPath); // Assume get_root() gets the root object
1332+
// sceneElement->InsertEndChild(childrenElement);
1333+
1334+
// Save environment settings
1335+
tinyxml2::XMLElement* environmentElement = doc.NewElement("Enviroment");
1336+
Vec3 ambientColor = scene->get_ambient_color();
1337+
float ambientIntensity = scene->get_ambient_intensity();
1338+
1339+
if (scene->get_skybox())
1340+
{
1341+
environmentElement->SetAttribute("type", "skybox");
1342+
1343+
tinyxml2::XMLElement* filenameElement = doc.NewElement("Filename");
1344+
filenameElement->SetAttribute("value", scene->get_skybox()->get_enviroment_map()->get_file_route().c_str());
1345+
environmentElement->InsertEndChild(filenameElement);
1346+
} else
1347+
{
1348+
environmentElement->SetAttribute("type", "constant");
1349+
1350+
tinyxml2::XMLElement* colorElement = doc.NewElement("color");
1351+
colorElement->SetAttribute("r", ambientColor.r);
1352+
colorElement->SetAttribute("g", ambientColor.g);
1353+
colorElement->SetAttribute("b", ambientColor.b);
1354+
environmentElement->InsertEndChild(colorElement);
1355+
}
1356+
1357+
tinyxml2::XMLElement* intensityElement = doc.NewElement("intensity");
1358+
intensityElement->SetAttribute("value", ambientIntensity);
1359+
environmentElement->InsertEndChild(intensityElement);
1360+
1361+
sceneElement->InsertEndChild(environmentElement);
1362+
1363+
// Save the XML to file
1364+
doc.SaveFile(fileName.c_str());
1365+
}

src/tools/widgets.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ void SceneExplorerWidget::render() {
6565

6666
if (ImGui::BeginMenuBar())
6767
{
68+
if (ImGui::BeginMenu("File"))
69+
{
70+
if (ImGui::MenuItem("Save Scene"))
71+
{
72+
ImGuiFileDialog::Instance()->OpenDialog("SaveSceneFile", "Save Scene", ".xml");
73+
}
74+
if (ImGui::MenuItem("Import Scene"))
75+
{
76+
ImGuiFileDialog::Instance()->OpenDialog("LoadSceneFile", "Choose Scene", ".xml");
77+
}
78+
ImGui::EndMenu();
79+
}
6880
if (ImGui::BeginMenu("Add"))
6981
{
7082
if (ImGui::MenuItem("Plane"))
@@ -109,7 +121,7 @@ void SceneExplorerWidget::render() {
109121
}
110122
ImGui::Separator();
111123

112-
if (ImGui::MenuItem("Import from Disk"))
124+
if (ImGui::MenuItem("Import File"))
113125
{
114126
ImGuiFileDialog::Instance()->OpenDialog("ChooseMeshFile", "Choose a file", ".obj,.ply,.gltf");
115127
}
@@ -134,6 +146,27 @@ void SceneExplorerWidget::render() {
134146

135147
ImGuiFileDialog::Instance()->Close();
136148
}
149+
if (ImGuiFileDialog::Instance()->Display("SaveSceneFile", 32, {800.0f, 400.0f}, {1400.0f, 1000.0f}))
150+
{
151+
if (ImGuiFileDialog::Instance()->IsOk())
152+
{
153+
Loaders::SceneLoader sceneLoader;
154+
sceneLoader.save_scene(m_scene, "scene.xml");
155+
}
156+
157+
ImGuiFileDialog::Instance()->Close();
158+
}
159+
if (ImGuiFileDialog::Instance()->Display("LoadSceneFile", 32, {800.0f, 400.0f}, {1400.0f, 1000.0f}))
160+
{
161+
if (ImGuiFileDialog::Instance()->IsOk())
162+
{
163+
Loaders::SceneLoader sceneLoader;
164+
std::string filePath = ImGuiFileDialog::Instance()->GetFilePathName();
165+
sceneLoader.load_scene(m_scene, filePath);
166+
}
167+
168+
ImGuiFileDialog::Instance()->Close();
169+
}
137170
ImGui::Spacing();
138171

139172
ImGui::SeparatorText("SCENE EXPLORER");
@@ -482,7 +515,7 @@ void ObjectExplorerWidget::render() {
482515
if (model->get_material(i)->get_shaderpass_ID() == "physical")
483516
{
484517
PhysicalMaterial* mat = static_cast<PhysicalMaterial*>(model->get_material(i));
485-
Vec3 albedo = mat->get_albedo();
518+
Vec3 albedo = mat->get_albedo();
486519
if (ImGui::ColorEdit3("Albedo", (float*)&albedo))
487520
{
488521
mat->set_albedo(Vec4{albedo, 1.0f});

0 commit comments

Comments
 (0)