11{
12 libconfig::Config cfg;
14 cfg.setAutoConvert(true);
15
16 try {
17 cfg.readFile(file.c_str());
18 } catch (const libconfig::FileIOException &fioex) {
20 } catch (const libconfig::ConfigException &cfgex) {
21 throw RayTracerException("Error: Invalid configuration file format");
22 }
23 try {
24
25
26 cfg.lookupValue(
"camera.resolution.width", scene.
camera.
width);
27 cfg.lookupValue(
"camera.resolution.height", scene.
camera.
height);
28
32
36
38
39
40
41 const libconfig::Setting &spheres = cfg.lookup("primitives.spheres");
42 for (int i = 0; i < spheres.getLength(); ++i) {
43 const libconfig::Setting &config_sphere = spheres[i];
44 Config::Sphere s;
45
46
47 s.
center.
x = config_sphere.lookup(
"x");
48 s.
center.
y = config_sphere.lookup(
"y");
49 s.
center.
z = config_sphere.lookup(
"z");
50
51 s.
radius = config_sphere.lookup(
"r");
52
53 const libconfig::Setting &config_color = config_sphere.lookup("color");
55 config_color.lookup("r"),
56 config_color.lookup("g"),
57 config_color.lookup("b")
58 );
59
61 }
62
63
64 const libconfig::Setting &planes = cfg.lookup("primitives.planes");
65 for (int i = 0; i < planes.getLength(); ++i) {
66 const libconfig::Setting &config_plane = planes[i];
67 Config::Plane p;
68
69
70 std::string ax;
71 config_plane.lookupValue("axis", ax);
73
74 p.
position = config_plane.lookup(
"position");
75
76 const libconfig::Setting &config_color = config_plane.lookup("color");
78 config_color.lookup("r"),
79 config_color.lookup("g"),
80 config_color.lookup("b")
81 );
82
84 }
85
86
87
88 cfg.lookupValue(
"lights.ambient", scene.
ambient);
89
90 cfg.lookupValue(
"lights.diffuse", scene.
diffuse);
91
92 const libconfig::Setting &points = cfg.lookup("lights.point");
93 for (int i = 0; i < points.getLength(); ++i) {
94 const libconfig::Setting &config_point = points[i];
95 Config::Point p;
96
97
101
102 scene.
points.push_back(p);
103 }
104
105
106 const libconfig::Setting &directionals = cfg.lookup("lights.directional");
107 for (int i = 0; i < directionals.getLength(); ++i) {
108 const libconfig::Setting &config_directional = directionals[i];
109 Config::Directional d;
110
111
112 d.
direction.
x = config_directional.lookup(
"x");
113 d.
direction.
y = config_directional.lookup(
"y");
114 d.
direction.
z = config_directional.lookup(
"z");
115
117 }
118 } catch (const libconfig::SettingNotFoundException &nf) {
119
120 }
121
122 return scene;
123}
std::vector< Plane > planes
std::vector< Point > points
std::vector< Sphere > spheres
std::vector< Directional > directionals