Skip to content

Commit e7d4de4

Browse files
committed
-Dev: added physical material to scene saver
1 parent e607730 commit e7d4de4

File tree

3 files changed

+211
-6
lines changed

3 files changed

+211
-6
lines changed

include/engine/tools/loaders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class SceneLoader
8686
Core::Transform load_transform(tinyxml2::XMLElement* obj);
8787
void save_transform(const Core::Transform& transform, tinyxml2::XMLElement* parentElement);
8888
void load_children(tinyxml2::XMLElement* element, Core::Object3D* const parent, std::string resourcesPath);
89+
void save_children(tinyxml2::XMLElement* parentElement, Core::Object3D* const parent);
8990

9091
public:
9192
SceneLoader(bool asyncLoading = true)

src/tools/loaders.cpp

Lines changed: 208 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,211 @@ void VKFW::Tools::Loaders::SceneLoader::load_children(tinyxml2::XMLElement* elem
12041204
}
12051205
}
12061206

1207+
void VKFW::Tools::Loaders::SceneLoader::save_children(tinyxml2::XMLElement* parentElement,
1208+
Core::Object3D* const parent) {
1209+
1210+
tinyxml2::XMLDocument* doc = parentElement->GetDocument();
1211+
1212+
for (Core::Object3D* child : parent->get_children())
1213+
{
1214+
if (child->get_type() == ObjectType::MESH)
1215+
{
1216+
Core::Mesh* mesh = static_cast<Core::Mesh*>(child);
1217+
1218+
tinyxml2::XMLElement* meshElement = doc->NewElement("Mesh");
1219+
if (mesh->get_file_route() != "")
1220+
meshElement->SetAttribute("type", "file");
1221+
else
1222+
meshElement->SetAttribute("type", "primitive");
1223+
1224+
tinyxml2::XMLElement* filenameElement = doc->NewElement("Filename");
1225+
filenameElement->SetAttribute("value", mesh->get_file_route().c_str());
1226+
meshElement->InsertEndChild(filenameElement);
1227+
// Save transform
1228+
Core::Transform transform = mesh->get_transform();
1229+
save_transform(transform, meshElement);
1230+
1231+
meshElement->SetAttribute("name", mesh->get_name().c_str());
1232+
1233+
// // Save material
1234+
Core::IMaterial* mat = mesh->get_material();
1235+
if (mat)
1236+
{
1237+
tinyxml2::XMLElement* materialElement = doc->NewElement("Material");
1238+
materialElement->SetAttribute("type", mat->get_shaderpass_ID().c_str());
1239+
1240+
if (mat->get_shaderpass_ID() == "physical")
1241+
{
1242+
Core::PhysicalMaterial* material = static_cast<Core::PhysicalMaterial*>(mat);
1243+
// Save Albedo
1244+
tinyxml2::XMLElement* albedoElement = doc->NewElement("albedo");
1245+
Vec3 albedo = material->get_albedo();
1246+
albedoElement->SetAttribute("r", albedo.r);
1247+
albedoElement->SetAttribute("g", albedo.g);
1248+
albedoElement->SetAttribute("b", albedo.b);
1249+
materialElement->InsertEndChild(albedoElement);
1250+
tinyxml2::XMLElement* opElement = doc->NewElement("opacity");
1251+
opElement->SetAttribute("value", material->get_opacity());
1252+
materialElement->InsertEndChild(opElement);
1253+
1254+
// Save Roughness
1255+
tinyxml2::XMLElement* roughnessElement = doc->NewElement("roughness");
1256+
roughnessElement->SetAttribute("value", material->get_roughness());
1257+
materialElement->InsertEndChild(roughnessElement);
1258+
1259+
// Save Metallic
1260+
tinyxml2::XMLElement* metallicElement = doc->NewElement("metallic");
1261+
metallicElement->SetAttribute("value", material->get_metalness());
1262+
materialElement->InsertEndChild(metallicElement);
1263+
1264+
// Save Emission
1265+
tinyxml2::XMLElement* emissionElement = doc->NewElement("emission");
1266+
Vec3 emission = material->get_emissive_color();
1267+
emissionElement->SetAttribute("r", emission.r);
1268+
emissionElement->SetAttribute("g", emission.g);
1269+
emissionElement->SetAttribute("b", emission.b);
1270+
materialElement->InsertEndChild(emissionElement);
1271+
1272+
// Save Textures
1273+
if (!material->get_textures().empty())
1274+
{
1275+
tinyxml2::XMLElement* texturesElement = doc->NewElement("Textures");
1276+
1277+
if (material->get_albedo_texture())
1278+
{
1279+
tinyxml2::XMLElement* albedoTextureElement = doc->NewElement("albedo");
1280+
albedoTextureElement->SetAttribute(
1281+
"path", material->get_albedo_texture()->get_file_route().c_str());
1282+
texturesElement->InsertEndChild(albedoTextureElement);
1283+
}
1284+
1285+
if (material->get_normal_texture())
1286+
{
1287+
tinyxml2::XMLElement* normalsTextureElement = doc->NewElement("normals");
1288+
normalsTextureElement->SetAttribute(
1289+
"path", material->get_normal_texture()->get_file_route().c_str());
1290+
texturesElement->InsertEndChild(normalsTextureElement);
1291+
}
1292+
if (material->get_roughness_texture())
1293+
{
1294+
tinyxml2::XMLElement* normalsTextureElement = doc->NewElement("roughness");
1295+
normalsTextureElement->SetAttribute(
1296+
"path", material->get_roughness_texture()->get_file_route().c_str());
1297+
texturesElement->InsertEndChild(normalsTextureElement);
1298+
}
1299+
if (material->get_metallic_texture())
1300+
{
1301+
tinyxml2::XMLElement* normalsTextureElement = doc->NewElement("metalness");
1302+
normalsTextureElement->SetAttribute(
1303+
"path", material->get_metallic_texture()->get_file_route().c_str());
1304+
texturesElement->InsertEndChild(normalsTextureElement);
1305+
}
1306+
1307+
if (material->get_emissive_texture())
1308+
{
1309+
tinyxml2::XMLElement* emissionTextureElement = doc->NewElement("emission");
1310+
emissionTextureElement->SetAttribute(
1311+
"path", material->get_emissive_texture()->get_file_route().c_str());
1312+
texturesElement->InsertEndChild(emissionTextureElement);
1313+
}
1314+
1315+
materialElement->InsertEndChild(texturesElement);
1316+
}
1317+
}
1318+
// Append to parent element
1319+
meshElement->InsertEndChild(materialElement);
1320+
}
1321+
1322+
// Recursively save children
1323+
tinyxml2::XMLElement* childrenElement = doc->NewElement("Children");
1324+
save_children(childrenElement, mesh);
1325+
meshElement->InsertEndChild(childrenElement);
1326+
1327+
parentElement->InsertEndChild(meshElement);
1328+
}
1329+
if (child->get_type() == ObjectType::LIGHT)
1330+
{
1331+
Core::Light* light = static_cast<Core::Light*>(child);
1332+
1333+
tinyxml2::XMLElement* lightElement = doc->NewElement("Light");
1334+
tinyxml2::XMLElement* directionElement = nullptr;
1335+
tinyxml2::XMLElement* influenceElement = nullptr;
1336+
switch (light->get_light_type())
1337+
{
1338+
case LightType::POINT:
1339+
lightElement->SetAttribute("type", "point");
1340+
influenceElement = doc->NewElement("influence");
1341+
influenceElement->SetAttribute("value", static_cast<Core::PointLight*>(light)->get_area_of_effect());
1342+
lightElement->InsertEndChild(influenceElement);
1343+
break;
1344+
case LightType::DIRECTIONAL:
1345+
lightElement->SetAttribute("type", "directional");
1346+
directionElement = doc->NewElement("direction");
1347+
Vec3 direction = static_cast<Core::DirectionalLight*>(light)->get_direction();
1348+
directionElement->SetAttribute("x", direction.x);
1349+
directionElement->SetAttribute("y", direction.y);
1350+
directionElement->SetAttribute("z", direction.z);
1351+
lightElement->InsertEndChild(directionElement);
1352+
break;
1353+
case LightType::SPOT:
1354+
lightElement->SetAttribute("type", "spot");
1355+
break;
1356+
}
1357+
lightElement->SetAttribute("name", light->get_name().c_str());
1358+
1359+
// Save transform
1360+
Core::Transform transform = light->get_transform();
1361+
save_transform(transform, lightElement);
1362+
1363+
// Save Intensity
1364+
tinyxml2::XMLElement* intensityElement = doc->NewElement("intensity");
1365+
intensityElement->SetAttribute("value", light->get_intensity());
1366+
lightElement->InsertEndChild(intensityElement);
1367+
1368+
// Save Color
1369+
tinyxml2::XMLElement* colorElement = doc->NewElement("color");
1370+
Vec3 color = light->get_color();
1371+
colorElement->SetAttribute("r", color.r);
1372+
colorElement->SetAttribute("g", color.g);
1373+
colorElement->SetAttribute("b", color.b);
1374+
lightElement->InsertEndChild(colorElement);
1375+
1376+
if (light->get_cast_shadows())
1377+
{
1378+
tinyxml2::XMLElement* shadowElement = doc->NewElement("Shadow");
1379+
switch (light->get_shadow_type())
1380+
{
1381+
case ShadowType::BASIC_SHADOW:
1382+
shadowElement->SetAttribute("type", "basic");
1383+
break;
1384+
case ShadowType::VSM_SHADOW:
1385+
shadowElement->SetAttribute("type", "vsm");
1386+
break;
1387+
case ShadowType::RAYTRACED_SHADOW:
1388+
shadowElement->SetAttribute("type", "rt");
1389+
break;
1390+
}
1391+
1392+
tinyxml2::XMLElement* samplesElement = doc->NewElement("samples");
1393+
samplesElement->SetAttribute("value", light->get_shadow_ray_samples());
1394+
shadowElement->InsertEndChild(samplesElement);
1395+
1396+
tinyxml2::XMLElement* areaElement = doc->NewElement("area");
1397+
areaElement->SetAttribute("value", light->get_area());
1398+
shadowElement->InsertEndChild(areaElement);
1399+
1400+
lightElement->InsertEndChild(shadowElement);
1401+
}
1402+
1403+
// Recursively save children
1404+
tinyxml2::XMLElement* childrenElement = doc->NewElement("Children");
1405+
save_children(childrenElement, light);
1406+
lightElement->InsertEndChild(childrenElement);
1407+
1408+
parentElement->InsertEndChild(lightElement);
1409+
}
1410+
}
1411+
}
12071412
void VKFW::Tools::Loaders::SceneLoader::load_scene(Core::Scene* const scene, const std::string fileName) {
12081413

12091414
if (!scene)
@@ -1326,10 +1531,8 @@ void VKFW::Tools::Loaders::SceneLoader::save_scene(Core::Scene* const scene, con
13261531
sceneElement->InsertEndChild(cameraElement);
13271532
}
13281533

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);
1534+
// Recursively save children
1535+
save_children(sceneElement, scene); // Assume get_root() gets the root object
13331536

13341537
// Save environment settings
13351538
tinyxml2::XMLElement* environmentElement = doc.NewElement("Enviroment");
@@ -1346,7 +1549,7 @@ void VKFW::Tools::Loaders::SceneLoader::save_scene(Core::Scene* const scene, con
13461549
} else
13471550
{
13481551
environmentElement->SetAttribute("type", "constant");
1349-
1552+
13501553
tinyxml2::XMLElement* colorElement = doc.NewElement("color");
13511554
colorElement->SetAttribute("r", ambientColor.r);
13521555
colorElement->SetAttribute("g", ambientColor.g);

src/tools/widgets.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ void SceneExplorerWidget::render() {
151151
if (ImGuiFileDialog::Instance()->IsOk())
152152
{
153153
Loaders::SceneLoader sceneLoader;
154-
sceneLoader.save_scene(m_scene, "scene.xml");
154+
std::string filePath = ImGuiFileDialog::Instance()->GetFilePathName();
155+
sceneLoader.save_scene(m_scene, filePath);
155156
}
156157

157158
ImGuiFileDialog::Instance()->Close();

0 commit comments

Comments
 (0)