-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscene.cpp
More file actions
1213 lines (1047 loc) · 44.5 KB
/
scene.cpp
File metadata and controls
1213 lines (1047 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "scene.h"
#include "hydraxml.h"
#include "loadutil.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <cassert>
#include <locale>
#include <codecvt>
namespace LiteScene
{
std::wstring s2ws(const std::string& str)
{
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
std::string ws2s(const std::wstring& wstr)
{
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(wstr);
}
std::vector<std::string> split(std::string aStr, char aDelim)
{
std::vector<std::string> res;
std::string str = aStr.substr(0, aStr.find(aDelim));
while (str.size() < aStr.size())
{
res.push_back(str);
aStr = aStr.substr(aStr.find(aDelim) + 1);
str = aStr.substr(0, aStr.find(aDelim));
}
res.push_back(str);
return res;
}
std::wstring save_float_array_to_string(const std::vector<float> &array)
{
std::wstringstream outputStream;
for (int i = 0; i < array.size(); i++)
{
outputStream << array[i] << L" ";
}
return outputStream.str();
}
std::wstring float4x4ToString(const LiteMath::float4x4 &matrix)
{
return LM_to_wstring(matrix);
/*return save_float_array_to_string({ matrix.get_row(0).x, matrix.get_row(0).y, matrix.get_row(0).z, matrix.get_row(0).w,
matrix.get_row(1).x, matrix.get_row(1).y, matrix.get_row(1).z, matrix.get_row(1).w,
matrix.get_row(2).x, matrix.get_row(2).y, matrix.get_row(2).z, matrix.get_row(2).w,
matrix.get_row(3).x, matrix.get_row(3).y, matrix.get_row(3).z, matrix.get_row(3).w });
*/}
AABB AABBFromString(const std::wstring &str)
{
auto data = wstring_to_float_arr(str, 6);
AABB result;
result.boxMin = LiteMath::float3(data[0], data[1], data[2]);
result.boxMax = LiteMath::float3(data[3], data[4], data[5]);
return result;
}
std::wstring AABBToString(const AABB &aabb)
{
return save_float_array_to_string({ aabb.boxMin.x, aabb.boxMin.y, aabb.boxMin.z, aabb.boxMax.x, aabb.boxMax.y, aabb.boxMax.z });
}
bool Geometry::load_node_base(pugi::xml_node node)
{
custom_data = node;
id = node.attribute(L"id").as_uint(INVALID_ID);
bytesize = node.attribute(L"bytesize").as_uint(0);
name = ws2s(node.attribute(L"name").as_string());
type_name = ws2s(node.attribute(L"type").as_string());
if (id == INVALID_ID)
{
printf("[Geometry::load_node_base] Invalid id\n");
return false;
}
return true;
}
void Geometry::save_node_base(pugi::xml_node &node) const
{
set_attr(node, L"id", id);
set_attr(node, L"bytesize", bytesize);
set_attr(node, L"name", s2ws(name));
set_attr(node, L"type", s2ws(type_name));
}
bool MeshGeometry::load_node(pugi::xml_node node)
{
bool ok = load_node_base(node);
if (!ok) return false;
if (node.attribute(L"loc").empty())
{
relative_file_path = INVALID_PATH;
printf("[MeshGeometry::load_node] No location\n");
return false;
}
relative_file_path = ws2s(node.attribute(L"loc").as_string());
type_id = MESH_TYPE_ID;
return true;
}
bool MeshGeometry::save_node(pugi::xml_node &node) const
{
if (relative_file_path == INVALID_PATH)
{
printf("[MeshGeometry::save_node] No location is specified. Save data first\n");
return false;
}
save_node_base(node);
node.set_name(L"mesh");
set_attr(node, L"loc", s2ws(relative_file_path));
return true;
}
bool MeshGeometry::load_data(const SceneMetadata &metadata)
{
if (is_loaded)
return true;
if (relative_file_path == INVALID_PATH)
{
printf("[MeshGeometry::load_data] No location is specified. Load node first\n");
return false;
}
std::string path = metadata.scene_xml_folder + "/" + relative_file_path;
mesh = cmesh4::LoadMeshFromVSGF(path.c_str());
bool ok = mesh.VerticesNum() > 0 && mesh.IndicesNum() > 0;
if (!ok)
{
printf("[MeshGeometry::load_data] Failed to load mesh %s\n", path.c_str());
return false;
}
is_loaded = true;
return true;
}
bool MeshGeometry::save_data(const SceneMetadata &metadata)
{
if (!is_loaded)
{
printf("[MeshGeometry::save_data] Mesh is not loaded\n");
return false;
}
std::string mesh_name = "mesh_" + std::to_string(id);
relative_file_path = metadata.geometry_folder_relative + "/" + mesh_name + ".vsgf";
std::string file_path = metadata.scene_xml_folder == "" ? relative_file_path :
metadata.scene_xml_folder + "/" + relative_file_path;
cmesh4::SaveMeshToVSGF(file_path.c_str(), mesh);
return true;
}
bool CustomGeometry::load_node(pugi::xml_node node)
{
bool ok = load_node_base(node);
if (!ok) return false;
type_id = CUSTOM_TYPE_ID;
return true;
}
bool CustomGeometry::save_node(pugi::xml_node &node) const
{
save_node_base(node);
return true;
}
bool CustomGeometry::load_data(const SceneMetadata &metadata)
{
printf("[CustomGeometry::load_data] Cannot load data for custom geometry of unknown type\n");
return false;
}
bool CustomGeometry::save_data(const SceneMetadata &metadata)
{
printf("[CustomGeometry::save_data] Cannot save data for custom geometry of unknown type\n");
return false;
}
void HydraScene::initialize_empty_scene()
{
metadata.xml_doc.load_string(LR""""(
<?xml version="1.0"?>
<textures_lib />
<materials_lib />
<geometry_lib />
<lights_lib />
<cam_lib />
<render_lib />
<scenes />
)"""");
}
void HydraScene::clear()
{
//for (int i = 0; i < textures.size(); i++)
// delete textures[i];
for (int i = 0; i < materials.size(); i++)
delete materials[i];
for (int i = 0; i < geometries.size(); i++)
delete geometries[i];
for (int i = 0; i < light_sources.size(); i++)
delete light_sources[i];
metadata = SceneMetadata();
textures.clear();
materials.clear();
geometries.clear();
light_sources.clear();
cameras.clear();
render_settings.clear();
scenes.clear();
initialize_empty_scene();
}
bool load_geometry(HydraScene &scene, pugi::xml_node lib_node)
{
bool ok = true;
for (pugi::xml_node geom_node = lib_node.first_child(); geom_node != nullptr && ok; geom_node = geom_node.next_sibling())
{
if (std::wstring(geom_node.name()) == L"mesh")
{
MeshGeometry *geom = new MeshGeometry();
ok = geom->load_node(geom_node);
if (ok)
scene.geometries[geom->id] = geom;
}
else
{
CustomGeometry *geom = new CustomGeometry();
ok = geom->load_node(geom_node);
if (ok)
scene.geometries[geom->id] = geom;
}
}
if (scene.geometries.size() == 0)
{
printf("[HydraScene::load_geometry] No geometries loaded\n");
return false;
}
return true;
}
bool load_camera(Camera &cam, const pugi::xml_node &cam_node)
{
cam.id = cam_node.attribute(L"id").as_uint();
cam.name = ws2s(cam_node.attribute(L"name").as_string());
cam.fov = hydra_xml::readval1f(cam_node.child(L"fov"));
cam.nearPlane = hydra_xml::readval1f(cam_node.child(L"nearClipPlane"));
cam.farPlane = hydra_xml::readval1f(cam_node.child(L"farClipPlane"));
auto expNode = cam_node.child(L"exposure_mult");
if(expNode)
cam.exposureMult = hydra_xml::readval1f(expNode);
else
cam.exposureMult = 1.0f;
cam.pos = hydra_xml::readval3f(cam_node.child(L"position"));
cam.lookAt = hydra_xml::readval3f(cam_node.child(L"look_at"));
cam.up = hydra_xml::readval3f(cam_node.child(L"up"));
cam.has_matrix = false;
if(cam_node.child(L"matrix"))
{
cam.matrix = LiteMath::transpose(wstring_to_float4x4(cam_node.child(L"matrix").attribute(L"val").as_string()));
cam.has_matrix = true;
}
cam.custom_data = cam_node;
return true;
}
bool load_cameras(HydraScene &scene, const pugi::xml_node &lib_node)
{
bool ok = true;
for (pugi::xml_node cam_node = lib_node.first_child(); cam_node != nullptr && ok; cam_node = cam_node.next_sibling())
{
if(std::wstring(cam_node.name()) == L"camera")
{
Camera camera;
if(!load_camera(camera, cam_node)) return false;
scene.cameras[camera.id] = std::move(camera);
}
}
return ok;
}
bool load_render_settings(RenderSettings &settings, const pugi::xml_node &node)
{
settings.id = node.attribute(L"id").as_uint();
settings.name = ws2s(node.attribute(L"name").as_string());
settings.width = hydra_xml::readval1u(node.child(L"width"));
settings.height = hydra_xml::readval1u(node.child(L"height"));
settings.depth = hydra_xml::readval1u(node.child(L"trace_depth"));
settings.depthDiffuse = hydra_xml::readval1u(node.child(L"diff_trace_depth"));
settings.spp = hydra_xml::readval1u(node.child(L"maxRaysPerPixel"));
settings.custom_data = node;
return true;
}
bool load_all_render_settings(HydraScene &scene, const pugi::xml_node &lib_node)
{
bool ok = true;
for (pugi::xml_node set_node = lib_node.first_child(); set_node != nullptr && ok; set_node = set_node.next_sibling())
{
if(std::wstring(set_node.name()) == L"render_settings")
{
RenderSettings settings;
if(!load_render_settings(settings, set_node)) return false;
scene.render_settings[settings.id] = std::move(settings);
}
}
return ok;
}
bool load_textures(HydraScene &scene, const pugi::xml_node &lib_node)
{
bool ok = true;
for (pugi::xml_node tex_node = lib_node.first_child(); tex_node != nullptr && ok; tex_node = tex_node.next_sibling())
{
if(std::wstring(tex_node.name()) == L"texture")
{
Texture tex;
if(!tex.load_info(tex_node, scene.metadata.scene_xml_folder)) return false;
scene.textures[tex.id] = std::move(tex);
}
}
return ok;
}
bool find_texture(const pugi::xml_node &colorNode, TextureInstance &inst);
bool load_color_holder(const pugi::xml_node &node, bool allow_spectrum, ColorHolder &clr);
LightSource *load_lightsource(pugi::xml_node &node, const SceneMetadata &meta)
{
const uint32_t id = node.attribute(L"id").as_uint();
const std::string name = ws2s(node.attribute(L"name").as_string());
const uint32_t mat_id = node.attribute(L"mat_id").as_uint();
const std::wstring type = node.attribute(L"type").as_string();
const std::wstring shape = node.attribute(L"shape").as_string();
const std::wstring dist = node.attribute(L"distribution").as_string();
std::unique_ptr<LightSource> lgt;
if(type == L"sky") {
LightSourceSky *ptr = new LightSourceSky();
const auto &colNode = node.child(L"intensity").child(L"color");
if(colNode) {
TextureInstance inst;
if(find_texture(colNode, inst)) {
ptr->texture = std::move(inst);
}
}
const auto &backNode = node.child(L"back");
if(backNode) {
TextureInstance inst;
if(find_texture(backNode, inst)) {
ptr->camera_back = std::move(inst);
}
}
lgt.reset(ptr);
}
else if(type == L"directional") {
lgt.reset(new LightSource(LightSource::Type::DIRECTIONAL));
}
else if(shape == L"rect") {
lgt.reset(new LightSource(LightSource::Type::RECT));
auto sizeNode = node.child(L"size");
lgt->half_width = sizeNode.attribute(L"half_width").as_float();
lgt->half_length = sizeNode.attribute(L"half_length").as_float();
}
else if(shape == L"disk") {
lgt.reset(new LightSource(LightSource::Type::DISK));
lgt->radius = node.child(L"size").attribute(L"radius").as_float();
}
else if(shape == L"sphere") {
lgt.reset(new LightSource(LightSource::Type::SPHERE));
lgt->radius = node.child(L"size").attribute(L"radius").as_float();
}
else if(shape == L"point") {
if(dist == L"spot") {
LightSourceSpot *ptr = new LightSourceSpot();
ptr->angle1 = hydra_xml::readval1f(node.child(L"falloff_angle"));
ptr->angle2 = hydra_xml::readval1f(node.child(L"falloff_angle2"));
const auto &projNode = node.child(L"projective");
if(projNode) {
LightSourceSpot::Proj proj;
proj.fov = hydra_xml::readval1f(projNode.child(L"fov"));
proj.nearClipPlane = hydra_xml::readval1f(projNode.child(L"nearClipPlane"));
proj.farClipPlane = hydra_xml::readval1f(projNode.child(L"farClipPlane"));
const auto &projTexNode = projNode.child(L"texture");
if(projTexNode) {
TextureInstance inst;
if(find_texture(projNode, inst)) {
proj.texture = std::move(inst);
}
}
ptr->projective = std::move(proj);
}
lgt.reset(ptr);
}
else {
lgt.reset(new LightSource(LightSource::Type::POINT));
if(dist == L"uniform" || dist == L"omni" || dist == L"ies") {
lgt->distribution = LightSource::Dist::OMNI;
}
}
}
else {
return nullptr;
}
auto intNode = node.child(L"intensity");
if(!load_color_holder(intNode.child(L"color"), true, lgt->color)) {
return nullptr;
}
lgt->power = intNode.child(L"multiplier").attribute(L"val").as_float();
const auto &iesNode = node.child(L"ies");
if(iesNode) {
LightSource::IES ies;
ies.file_path = (fs::path(meta.scene_xml_folder) / ws2s(iesNode.attribute(L"loc").as_string())).string();
ies.point_area = iesNode.attribute(L"point_area").as_int() != 0;
const auto &matrixAttrib = iesNode.attribute(L"matrix");
if(matrixAttrib) {
ies.matrix = wstring_to_float4x4(matrixAttrib.as_string());
}
lgt->ies = std::move(ies);
}
lgt->id = id;
lgt->name = std::move(name);
lgt->mat_id = mat_id;
return lgt.release();
}
bool load_lightsources(HydraScene &scene, const pugi::xml_node &lib_node)
{
bool ok = true;
for (pugi::xml_node node = lib_node.first_child(); node != nullptr && ok; node = node.next_sibling())
{
if(std::wstring(node.name()) == L"light")
{
LightSource *lgt;
if((lgt = load_lightsource(node, scene.metadata)) == nullptr) return false;
lgt->raw_xml = node;
scene.light_sources[lgt->id] = lgt;
}
}
return ok;
}
bool load_instanced_scene(InstancedScene &scene, pugi::xml_node scene_node)
{
bool ok = true;
scene.custom_data = scene_node;
scene.id = scene_node.attribute(L"id").as_uint(INVALID_ID);
scene.name = ws2s(scene_node.attribute(L"name").as_string());
if (!scene_node.attribute(L"bbox").empty())
{
scene.bbox = AABBFromString(scene_node.attribute(L"bbox").as_string());
}
//parse remap lists
pugi::xml_node rl_nodes = scene_node.child(L"remap_lists");
if (!rl_nodes.empty())
{
for (pugi::xml_node rl_node = rl_nodes.first_child(); rl_node != nullptr && ok; rl_node = rl_node.next_sibling())
{
uint32_t remap_list_id = rl_node.attribute(L"id").as_uint(INVALID_ID);
if (remap_list_id == INVALID_ID)
{
ok = false;
printf("[HydraScene::load_instanced_scene] Invalid remap list id\n");
}
else
{
InstancedScene::RemapList remap_list;
remap_list.id = remap_list_id;
std::string rl_array_str = ws2s(rl_node.attribute(L"val").as_string());
auto indices = split(rl_array_str, ' ');
for (auto index : indices)
{
const char *str_c = index.c_str();
char *end_c = nullptr;
uint32_t value = strtol(index.c_str(), &end_c, 10);
if (end_c != str_c) //conversion happened
remap_list.remap.push_back((uint32_t)value);
}
if (remap_list.remap.size() == 0)
{
printf("[HydraScene::load_instanced_scene] Invalid remap list\n");
ok = false;
}
else
scene.remap_lists[remap_list_id] = remap_list;
}
}
}
if (!ok)
return false;
//parse instances and light instances
for (pugi::xml_node inst_node = scene_node.first_child(); inst_node != nullptr && ok; inst_node = inst_node.next_sibling())
{
if (std::wstring(inst_node.name()) == L"instance")
{
Instance inst;
inst.custom_data = inst_node;
inst.id = inst_node.attribute(L"id").as_uint(INVALID_ID);
inst.mesh_id = inst_node.attribute(L"mesh_id").as_uint(INVALID_ID);
inst.rmap_id = inst_node.attribute(L"rmap_id").as_uint(INVALID_ID);
inst.scn_id = inst_node.attribute(L"scn_id").as_uint(INVALID_ID);
inst.scn_sid = inst_node.attribute(L"scn_sid").as_uint(INVALID_ID);
inst.light_id = inst_node.attribute(L"light_id").as_uint(INVALID_ID);
inst.linst_id = inst_node.attribute(L"linst_id").as_uint(INVALID_ID);
if (inst.id == INVALID_ID || inst.mesh_id == INVALID_ID)
{
printf("[HydraScene::load_instanced_scene] Invalid instance, each instance must have a unique id and a valid mesh (geom) id\n");
ok = false;
}
else if (inst_node.attribute(L"matrix").empty())
{
printf("[HydraScene::load_instanced_scene] Invalid instance, each instance must have a transform matrix\n");
ok = false;
}
else
{
inst.matrix = wstring_to_float4x4(inst_node.attribute(L"matrix").as_string());
scene.instances[inst.id] = inst;
}
}
else if (std::wstring(inst_node.name()) == L"instance_light")
{
LightInstance inst;
inst.custom_data = inst_node;
inst.id = inst_node.attribute(L"id").as_uint(INVALID_ID);
inst.mesh_id = inst_node.attribute(L"mesh_id").as_uint(INVALID_ID);
inst.light_id = inst_node.attribute(L"light_id").as_uint(INVALID_ID);
inst.lgroup_id = inst_node.attribute(L"lgroup_id").as_int(INVALID_ID); //it can be -1
inst.matrix = wstring_to_float4x4(inst_node.attribute(L"matrix").as_string());
if (inst.id == INVALID_ID || inst.light_id == INVALID_ID)
{
printf("[HydraScene::load_instanced_scene] Invalid light instance, each light instance must have a unique id and a valid light id\n");
ok = false;
}
else if (inst_node.attribute(L"matrix").empty())
{
printf("[HydraScene::load_instanced_scene] Invalid instance, each instance must have a transform matrix\n");
ok = false;
}
else
{
inst.matrix = wstring_to_float4x4(inst_node.attribute(L"matrix").as_string());
scene.light_instances[inst.id] = inst;
}
}
}
if (scene.instances.size() == 0)
{
printf("[HydraScene::load_instanced_scene] No valid instances found on scene %d\n", scene.id);
return false;
}
return true;
}
bool load_all_instanced_scenes(HydraScene &scene, pugi::xml_node lib_node)
{
bool ok = true;
for (pugi::xml_node scene_node = lib_node.first_child(); scene_node != nullptr && ok; scene_node = scene_node.next_sibling())
{
if (std::wstring(scene_node.name()) == L"scene")
{
uint32_t id = scene_node.attribute(L"id").as_uint(INVALID_ID);
if (id == INVALID_ID)
{
printf("[HydraScene::load_instanced_scenes] Invalid scene id\n");
ok = false;
}
else
{
InstancedScene inst_scene;
ok = load_instanced_scene(inst_scene, scene_node);
if (ok)
scene.scenes[id] = inst_scene;
}
}
}
return ok;
}
bool load_materials(HydraScene &scene, pugi::xml_node &lib_node);
bool HydraScene::load(const std::string &filename)
{
clear();
std::filesystem::path path(filename);
if (!std::filesystem::exists(path))
{
printf("[HydraScene::load] Scene file %s does not exist\n", filename.c_str());
return false;
}
metadata.scene_xml_path = filename;
metadata.scene_xml_folder = path.parent_path().string();
auto loaded = metadata.xml_doc.load_file(path.c_str());
if (!loaded)
{
printf("[HydraScene::load] Failed to load xml file %s. xml_parse_status: %d\n", filename.c_str(), (int)loaded.status);
return false;
}
pugi::xml_node root = metadata.xml_doc;
if(metadata.xml_doc.child(L"root") != nullptr)
root = metadata.xml_doc.child(L"root");
metadata.custom_data = root;
pugi::xml_node texturesLib = root.child(L"textures_lib");
pugi::xml_node materialsLib = root.child(L"materials_lib");
pugi::xml_node geometryLib = root.child(L"geometry_lib");
pugi::xml_node lightsLib = root.child(L"lights_lib");
//pugi::xml_node spectraLib = root.child(L"spectra_lib");
pugi::xml_node cameraLib = root.child(L"cam_lib");
pugi::xml_node settingsNode = root.child(L"render_lib");
pugi::xml_node scenesNode = root.child(L"scenes");
bool all_part_found = true;
if (texturesLib.empty())
{
printf("[HydraScene::load] No textures lib\n");
all_part_found = false;
}
if (materialsLib.empty())
{
printf("[HydraScene::load] No materials lib\n");
all_part_found = false;
}
if (geometryLib.empty())
{
printf("[HydraScene::load] No geometry lib\n");
all_part_found = false;
}
if (lightsLib.empty())
{
printf("[HydraScene::load] No lights lib\n");
all_part_found = false;
}
// if (spectraLib.empty())
// {
// printf("[HydraScene::load] No spectra lib\n");
// all_part_found = false;
// }
if (cameraLib.empty())
{
printf("[HydraScene::load] No camera lib\n");
all_part_found = false;
}
if (settingsNode.empty())
{
printf("[HydraScene::load] No settings lib\n");
all_part_found = false;
}
if (scenesNode.empty())
{
printf("[HydraScene::load] No scenes lib\n");
all_part_found = false;
}
if (!all_part_found)
return false;
bool t_loaded = load_textures(*this, texturesLib);
bool m_loaded = load_materials(*this, materialsLib);
bool g_loaded = load_geometry(*this, geometryLib);
bool l_loaded = load_lightsources(*this, lightsLib);
//load_spectra(*this, spectraLib);
bool c_loaded = load_cameras(*this, cameraLib);
bool rs_loaded = load_all_render_settings(*this, settingsNode);
bool s_loaded = load_all_instanced_scenes(*this, scenesNode);
return t_loaded && m_loaded && g_loaded && l_loaded && c_loaded && rs_loaded && s_loaded;
}
bool save_geometry(const HydraScene &scene, const SceneMetadata &save_metadata, pugi::xml_node &lib_node)
{
for (const auto &[id, geom] : scene.geometries)
{
auto node = geom->custom_data ? lib_node.append_copy(geom->custom_data) : lib_node.append_child(L"geometry");
geom->load_data(scene.metadata);
geom->save_data(save_metadata);
if(!geom->save_node(node)) return false;
}
return !lib_node.empty();
}
void set_texture(pugi::xml_node &colorNode, const TextureInstance &inst);
void save_color_holder(pugi::xml_node &node, const ColorHolder &clr, bool allow_spectrum = true);
bool save_lightsource(const LightSource *lgt, const SceneMetadata &newmeta, pugi::xml_node &node) {
set_attr(node, L"id", lgt->id);
set_attr(node, L"name", s2ws(lgt->name));
set_attr(node, L"mat_id", lgt->mat_id);
auto intNode = set_child(node, L"intensity");
auto colNode = set_child(intNode, L"color");
switch(lgt->type()) {
case LightSource::Type::SKY:
{
set_attr(node, L"type", L"sky");
//set_attr(node, L"shape", L"point"); is it necessary?
set_attr(node, L"distribution", L"uniform");
const LightSourceSky *sptr = static_cast<const LightSourceSky *>(lgt);
if(sptr->texture) {
set_texture(colNode, *sptr->texture);
}
if(sptr->camera_back) {
auto backNode = set_child(node, L"back");
set_texture(backNode, *sptr->camera_back);
}
} break;
case LightSource::Type::DIRECTIONAL:
set_attr(node, L"type", L"directional");
break;
case LightSource::Type::RECT:
{
set_attr(node, L"type", L"area");
set_attr(node, L"shape", L"rect");
auto sizeNode = set_child(node, L"size");
set_attr(sizeNode, L"half_length", lgt->half_width);
set_attr(sizeNode, L"half_length", lgt->half_length);
}
break;
case LightSource::Type::DISK:
set_attr(node, L"type", L"area");
set_attr(node, L"shape", L"disk");
set_attr(set_child(node, L"size"), L"radius", lgt->radius);
break;
case LightSource::Type::SPHERE:
set_attr(node, L"type", L"area");
set_attr(node, L"shape", L"sphere");
set_attr(set_child(node, L"size"), L"radius", lgt->radius);
case LightSource::Type::POINT:
set_attr(node, L"type", L"point");
set_attr(node, L"shape", L"point");
if(lgt->distribution == LightSource::Dist::SPOT) {
set_attr(node, L"distribution", L"spot");
const LightSourceSpot *ptr = static_cast<const LightSourceSpot *>(lgt);
set_attr(set_child(node, L"falloff_angle"), L"val", ptr->angle1);
set_attr(set_child(node, L"falloff_angle2"), L"val", ptr->angle2);
if(ptr->projective) {
auto &proj = *ptr->projective;
auto projNode = set_child(node, L"projective");
set_attr(set_child(projNode, L"fov"), L"val", proj.fov);
set_attr(set_child(projNode, L"nearClipPlane"), L"val", proj.nearClipPlane);
set_attr(set_child(projNode, L"farClipPlane"), L"val", proj.farClipPlane);
if(proj.texture) {
auto projTexNode = set_child(projNode, L"texture");
set_texture(projTexNode, *proj.texture);
}
}
}
else {
if(lgt->distribution == LightSource::Dist::LAMBERT) {
set_attr(node, L"distribution", "lambert");
}
else if(lgt->distribution == LightSource::Dist::OMNI) {
set_attr(node, L"distribution", "uniform");
}
else return false; //error
}
break;
}
save_color_holder(colNode, lgt->color, true);
set_attr(set_child(intNode, L"multiplier"), L"val", lgt->power);
if(lgt->ies) {
auto iesNode = set_child(node, L"ies");
const auto &ies = *lgt->ies;
fs::path path{ies.file_path}; //is always absolute
fs::path abs_new_path = fs::path(newmeta.geometry_folder) / "ies" / path.filename();
fs::path rel_new_path = get_relative_if_possible(fs::path(newmeta.scene_xml_folder), abs_new_path);
fs::copy(path, abs_new_path, fs::copy_options::update_existing
| fs::copy_options::recursive);
set_attr(iesNode, L"loc", s2ws(rel_new_path.string()));
set_attr(iesNode, L"point_area", int(ies.point_area));
if(ies.matrix) {
set_attr(iesNode, L"matrix", LM_to_wstring(*ies.matrix));
}
}
return true;
}
bool save_lightsources(const HydraScene &scene, const SceneMetadata &meta, pugi::xml_node &lib_node)
{
for (const auto &[id, lgt] : scene.light_sources)
{
pugi::xml_node node = lgt->raw_xml ? lib_node.append_copy(lgt->raw_xml) : lib_node.append_child(L"light");
if(!save_lightsource(lgt, meta, node)) return false;
}
return !lib_node.empty();
}
bool save_instanced_scene(const InstancedScene &scene, pugi::xml_node &scene_node)
{
set_attr(scene_node, L"id", scene.id);
std::wstring bbox_str = AABBToString(scene.bbox);
set_attr(scene_node, L"bbox", bbox_str);
set_attr(scene_node, L"name", s2ws(scene.name));
if (scene.remap_lists.size() > 0)
{
pugi::xml_node all_remap_lists_node = scene_node.append_child(L"remap_lists");
for (const auto &[id, remap_list] : scene.remap_lists)
{
std::wstring list_str;
for (const auto &elem : remap_list.remap)
list_str += std::to_wstring(elem) + L" ";
pugi::xml_node remap_list_node = all_remap_lists_node.append_child(L"remap_list");
remap_list_node.append_attribute(L"id").set_value(id);
remap_list_node.append_attribute(L"size").set_value(remap_list.remap.size());
remap_list_node.append_attribute(L"val").set_value(list_str.c_str());
}
}
if (scene.instances.size() > 0)
{
for (const auto &[id, instance] : scene.instances)
{
pugi::xml_node inst_node;
if (instance.custom_data) {
inst_node = scene_node.append_copy(instance.custom_data);
}
else {
inst_node = scene_node.append_child(L"instance");
}
set_attr(inst_node, L"id", id);
set_attr(inst_node, L"mesh_id", instance.mesh_id);
set_attr(inst_node, L"matrix", float4x4ToString(instance.matrix));
if (instance.rmap_id != INVALID_ID)
set_attr(inst_node, L"rmap_id", instance.rmap_id);
if (instance.scn_id != INVALID_ID)
set_attr(inst_node, L"scn_id", instance.scn_id);
if (instance.scn_sid != INVALID_ID)
set_attr(inst_node, L"scn_sid", instance.scn_sid);
if (instance.light_id != INVALID_ID)
set_attr(inst_node, L"light_id", instance.light_id);
if (instance.linst_id != INVALID_ID)
set_attr(inst_node, L"linst_id", instance.linst_id);
}
}
if (scene.light_instances.size() > 0)
{
for (const auto &[id, linst] : scene.light_instances)
{
pugi::xml_node linst_node;
if (linst.custom_data) {
linst_node = scene_node.append_copy(linst.custom_data);
}
else {
linst_node = scene_node.append_child(L"instance_light");
}
set_attr(linst_node, L"id", id);
set_attr(linst_node, L"light_id", linst.light_id);
set_attr(linst_node, L"matrix", float4x4ToString(linst.matrix));
if (linst.mesh_id != INVALID_ID)
set_attr(linst_node, L"mesh_id", linst.mesh_id);
if (linst.lgroup_id != INVALID_ID)
set_attr(linst_node, L"lgroup_id", linst.lgroup_id);
}
}
return !scene_node.empty();
}
bool save_instanced_scenes(const HydraScene &scene, pugi::xml_node lib_node)
{
for (const auto &[id, inst_scene] : scene.scenes)
{
pugi::xml_node scene_node;
if (inst_scene.custom_data) {
scene_node = lib_node.append_copy(inst_scene.custom_data);
}
else {
scene_node = lib_node.append_child(L"scene");
}
//clear all instances, light_instances and remap lists, as they will be saved below
pugi::xml_node child_node = scene_node.first_child();
while (!child_node.empty())
{
pugi::xml_node next_node = child_node.next_sibling();
if (std::wstring(child_node.name()) == L"instance" ||
std::wstring(child_node.name()) == L"instance_light" ||
std::wstring(child_node.name()) == L"remap_lists")
{
scene_node.remove_child(child_node);
}
child_node = next_node;
}
save_instanced_scene(inst_scene, scene_node);
}
return !lib_node.empty();
}
bool save_camera(const Camera &cam, pugi::xml_node &cam_node)
{
set_attr(cam_node, L"id", cam.id);
set_attr(cam_node, L"name", s2ws(cam.name));
set_child(cam_node, L"fov", cam.fov);
set_child(cam_node, L"nearClipPlane", cam.nearPlane);
set_child(cam_node, L"farClipPlane", cam.farPlane);
if(cam.exposureMult != 1.0f) {
set_child(cam_node, L"exposure_mult", cam.exposureMult);
}
set_child(cam_node, L"position", LM_to_wstring(cam.pos));
set_child(cam_node, L"look_at", LM_to_wstring(cam.lookAt));
set_child(cam_node, L"up", LM_to_wstring(cam.up));
if(cam.has_matrix)