-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_reader.hpp
130 lines (102 loc) · 4.28 KB
/
json_reader.hpp
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
#ifndef JSON_READER_HPP
#define JSON_READER_HPP
#include "json.hpp"
#include "settings.hpp"
#include "main.hpp"
#include "camera.hpp"
#include "color.hpp"
#include "hittable_list.hpp"
#include "sphere.hpp"
#include "material.hpp"
Settings read_json(const char* file_path)
{
using namespace nlohmann;
Settings settings;
//Json Input Stuff
std::ifstream json_file;
json_file.open(file_path);
json parsed_json = json::parse(json_file);
json_file.close();
//End of Json input stuff
std::ofstream output_file;
std::string output_file_name = static_cast<std::string>(parsed_json["image"]["output_file_name"]);
// Image
const int aspect_ratio_0 = static_cast<int>(parsed_json["image"]["aspect_ratio"][0]);
const int aspect_ratio_1 = static_cast<int>(parsed_json["image"]["aspect_ratio"][1]);
settings.aspect_ratio = aspect_ratio_0 / aspect_ratio_1;
settings.image_width = static_cast<int>(parsed_json["image"]["image_width"]);
settings.image_height = static_cast<int>(settings.image_width / settings.aspect_ratio);
settings.samples_per_pixel = static_cast<int>(parsed_json["image"]["samples"]); // Anti aliasing
settings.max_depth = static_cast<int>(parsed_json["image"]["max_depth"]);
// World
for (auto surface : parsed_json["surfaces"])
{
if (surface["shape"] == "sphere")
{
auto json_material = surface["material"];
shared_ptr<material> sphere_material;
if (json_material["type"] != NULL)
{
if (json_material["type"] == "lambertian")
{
if (json_material["color"] != NULL)
{
color albedo(
static_cast<double>(json_material["albedo"][0]),
static_cast<double>(json_material["albedo"][1]),
static_cast<double>(json_material["albedo"][2]));
sphere_material = make_shared<lambertian>(albedo);
}
}
else if (json_material["type"] == "metal")
{
color albedo(
static_cast<double>(json_material["albedo"][0]),
static_cast<double>(json_material["albedo"][1]),
static_cast<double>(json_material["albedo"][2]));
double fuzz = static_cast<double>(json_material["fuzz"]);
sphere_material = make_shared<metal>(albedo, fuzz);
}
else if (json_material["type"] == "dielectric")
{
double ri = static_cast<double>(json_material["ri"]);
sphere_material = make_shared<dielectric>(ri);
}
// End of Matierial Set
}
point3 pos(
surface["posistion"][0],
surface["posistion"][1],
surface["posistion"][2]);
double radius = static_cast<double>(surface["radius"]);
settings.world.add(make_shared<sphere>(pos, radius, sphere_material));
}
}
// Camera
point3 look_from(
static_cast<int>(parsed_json["camera"]["look_from"][0]),
static_cast<int>(parsed_json["camera"]["look_from"][1]),
static_cast<int>(parsed_json["camera"]["look_from"][2]));
point3 look_at(
static_cast<int>(parsed_json["camera"]["look_at"][0]),
static_cast<int>(parsed_json["camera"]["look_at"][1]),
static_cast<int>(parsed_json["camera"]["look_at"][2]));
point3 vup(
static_cast<double>(parsed_json["camera"]["vup"][0]),
static_cast<double>(parsed_json["camera"]["vup"][1]),
static_cast<double>(parsed_json["camera"]["vup"][2]));
int v_fov = static_cast<int>(parsed_json["camera"]["v_fov"]);
double dist_to_focus;
if (parsed_json["camera"]["auto_focus"])
{
dist_to_focus = (look_from - look_at).length();
}
else
{
dist_to_focus = static_cast<double>(parsed_json["camera"]["dist_to_focus"]);
}
double aperture = static_cast<double>(parsed_json["camera"]["aperture"]);
settings.cam = camera(look_from, look_at, vup, v_fov, settings.aspect_ratio, aperture, dist_to_focus);
return settings;
}
#endif