| 212 | |
| 213 | |
| 214 | void parse_scene() { |
| 215 | // setup default camera (now that resolution is finalized) |
| 216 | camera = Camera(Vec3(0,0,0), Vec3(0,0,-1), Vec3(0,1,0), 90.0f, xres, yres); |
| 217 | |
| 218 | // load entire text file into a buffer |
| 219 | std::ifstream file(scenefile.c_str(), std::ios::binary); |
| 220 | std::vector<char> text((std::istreambuf_iterator<char>(file)), |
| 221 | std::istreambuf_iterator<char>()); |
| 222 | if (text.empty()) { |
| 223 | std::cerr << "Error reading " << scenefile << "\n" |
| 224 | << "File is either missing or empty\n"; |
| 225 | exit (EXIT_FAILURE); |
| 226 | } |
| 227 | text.push_back(0); // make sure text ends with trailing 0 |
| 228 | |
| 229 | // build DOM tree |
| 230 | pugi::xml_document doc; |
| 231 | pugi::xml_parse_result parse_result = doc.load_file(scenefile.c_str()); |
| 232 | if (!parse_result) { |
| 233 | std::cerr << "XML parsed with errors: " << parse_result.description() << ", at offset " << parse_result.offset << "\n"; |
| 234 | exit (EXIT_FAILURE); |
| 235 | } |
| 236 | pugi::xml_node root = doc.child("World"); |
| 237 | if (!root) { |
| 238 | std::cerr << "Error reading " << scenefile << "\n" |
| 239 | << "Root element <World> is missing\n"; |
| 240 | exit (EXIT_FAILURE); |
| 241 | } |
| 242 | // loop over all children of world |
| 243 | for (pugi::xml_node node = root.first_child(); node; node = node.next_sibling()) { |
| 244 | if (strcmp(node.name(), "Option") == 0) { |
| 245 | for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) { |
| 246 | int i = 0; |
| 247 | if (sscanf(attr.value(), " int %d ", &i) == 1) { |
| 248 | if (strcmp(attr.name(), "max_bounces") == 0) |
| 249 | max_bounces = i; |
| 250 | else if (strcmp(attr.name(), "rr_depth") == 0) |
| 251 | rr_depth = i; |
| 252 | } |
| 253 | // TODO: pass any extra options to shading system (or texture system?) |
| 254 | } |
| 255 | } else if (strcmp(node.name(), "Camera") == 0) { |
| 256 | // defaults |
| 257 | Vec3 eye(0,0,0); |
| 258 | Vec3 dir(0,0,-1); |
| 259 | Vec3 up(0,1,0); |
| 260 | float fov = 90.f; |
| 261 | |
| 262 | // load camera (only first attribute counts if duplicates) |
| 263 | pugi::xml_attribute eye_attr = node.attribute("eye"); |
| 264 | pugi::xml_attribute dir_attr = node.attribute("dir"); |
| 265 | pugi::xml_attribute at_attr = node.attribute("look_at"); |
| 266 | pugi::xml_attribute up_attr = node.attribute("up"); |
| 267 | pugi::xml_attribute fov_attr = node.attribute("fov"); |
| 268 | |
| 269 | if (eye_attr) eye = strtovec(eye_attr.value()); |
| 270 | if (dir_attr) dir = strtovec(dir_attr.value()); else |
| 271 | if ( at_attr) dir = strtovec( at_attr.value()) - eye; |
no test coverage detected