1 #include "render-config.h"
2 
3 #include "picojson.h"
4 
5 #include <fstream>
6 #include <istream>
7 
8 namespace example {
9 
LoadRenderConfig(example::RenderConfig * config,const char * filename)10 bool LoadRenderConfig(example::RenderConfig* config, const char* filename) {
11   std::ifstream is(filename);
12   if (is.fail()) {
13     std::cerr << "Cannot open " << filename << std::endl;
14     return false;
15   }
16 
17   std::istream_iterator<char> input(is);
18   std::string err;
19   picojson::value v;
20   input = picojson::parse(v, input, std::istream_iterator<char>(), &err);
21   if (!err.empty()) {
22     std::cerr << err << std::endl;
23   }
24 
25   if (!v.is<picojson::object>()) {
26     std::cerr << "Not a JSON object" << std::endl;
27     return false;
28   }
29 
30   picojson::object o = v.get<picojson::object>();
31 
32   if (o.find("obj_filename") != o.end()) {
33     if (o["obj_filename"].is<std::string>()) {
34       config->obj_filename = o["obj_filename"].get<std::string>();
35     }
36   }
37 
38   if (o.find("eson_filename") != o.end()) {
39     if (o["eson_filename"].is<std::string>()) {
40       config->eson_filename = o["eson_filename"].get<std::string>();
41     }
42   }
43 
44   config->scene_scale = 1.0f;
45   if (o.find("scene_scale") != o.end()) {
46     if (o["scene_scale"].is<double>()) {
47       config->scene_scale = static_cast<float>(o["scene_scale"].get<double>());
48     }
49   }
50 
51   config->eye[0] = 0.0f;
52   config->eye[1] = 0.0f;
53   config->eye[2] = 5.0f;
54   if (o.find("eye") != o.end()) {
55     if (o["eye"].is<picojson::array>()) {
56       picojson::array arr = o["eye"].get<picojson::array>();
57       if (arr.size() == 3) {
58         config->eye[0] = static_cast<float>(arr[0].get<double>());
59         config->eye[1] = static_cast<float>(arr[1].get<double>());
60         config->eye[2] = static_cast<float>(arr[2].get<double>());
61       }
62     }
63   }
64 
65   config->up[0] = 0.0f;
66   config->up[1] = 1.0f;
67   config->up[2] = 0.0f;
68   if (o.find("up") != o.end()) {
69     if (o["up"].is<picojson::array>()) {
70       picojson::array arr = o["up"].get<picojson::array>();
71       if (arr.size() == 3) {
72         config->up[0] = static_cast<float>(arr[0].get<double>());
73         config->up[1] = static_cast<float>(arr[1].get<double>());
74         config->up[2] = static_cast<float>(arr[2].get<double>());
75       }
76     }
77   }
78 
79   config->look_at[0] = 0.0f;
80   config->look_at[1] = 0.0f;
81   config->look_at[2] = 0.0f;
82   if (o.find("look_at") != o.end()) {
83     if (o["look_at"].is<picojson::array>()) {
84       picojson::array arr = o["look_at"].get<picojson::array>();
85       if (arr.size() == 3) {
86         config->look_at[0] = static_cast<float>(arr[0].get<double>());
87         config->look_at[1] = static_cast<float>(arr[1].get<double>());
88         config->look_at[2] = static_cast<float>(arr[2].get<double>());
89       }
90     }
91   }
92 
93   config->fov = 45.0f;
94   if (o.find("fov") != o.end()) {
95     if (o["fov"].is<double>()) {
96       config->fov = static_cast<float>(o["fov"].get<double>());
97     }
98   }
99 
100   config->width = 512;
101   if (o.find("width") != o.end()) {
102     if (o["width"].is<double>()) {
103       config->width = static_cast<int>(o["width"].get<double>());
104     }
105   }
106 
107   config->height = 512;
108   if (o.find("height") != o.end()) {
109     if (o["height"].is<double>()) {
110       config->height = static_cast<int>(o["height"].get<double>());
111     }
112   }
113 
114   return true;
115 }
116 }
117