-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
133 lines (105 loc) · 3.57 KB
/
Copy pathworld.cpp
File metadata and controls
133 lines (105 loc) · 3.57 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
#include "world.h"
World::World() : light_source(nullptr) {}
World::~World() { clear(); }
void World::clear() {
if (light_source != nullptr) {
delete light_source;
light_source = nullptr;
}
for (Shape *obj : objects) {
delete obj;
}
objects.clear();
}
std::vector<Intersection> World::intersect_world(const Ray &r) const {
std::vector<Intersection> xs;
for (Shape *o : objects) {
std::vector<Intersection> obj_xs = o->intersects(r);
xs.insert(xs.end(), obj_xs.begin(), obj_xs.end());
}
std::sort(xs.begin(), xs.end());
return xs;
}
Color World::shade_hit(const Computations &comps, const bool dont_shadow,
int remaining) const {
if (light_source == nullptr)
return Color(0, 0, 0);
bool in_shadow = false;
if (!dont_shadow) {
if (is_shadowed(comps.over_point)) {
in_shadow = true;
}
}
Color surface = light_source->lighting(comps.object->material, comps.object,
comps.over_point, comps.eyev,
comps.normalv, in_shadow);
Color reflected = reflected_color(comps, remaining);
Color refracted = refracted_color(comps, remaining);
Material *m = &(comps.object->material);
if (m->reflective > 0.0 && m->transparency > 0.0) {
double reflectance = schlick(comps);
return surface + reflected * reflectance + refracted * (1 - reflectance);
} else {
return surface + reflected + refracted;
}
}
Color World::color_at(const Ray &ray, int reamining) const {
std::vector<Intersection> xs = this->intersect_world(ray);
const Intersection *closest_hit = hit(xs);
if (!closest_hit) {
return Color(0, 0, 0);
}
Computations comps(*closest_hit, ray, xs);
return shade_hit(comps, false, reamining);
}
Color World::reflected_color(const Computations &comps, int remaining) const {
if (remaining <= 0 || comps.object->material.reflective == 0.0) {
return COLOR_BLACK;
}
Ray reflect_ray(Ray(comps.over_point, comps.reflectv));
Color color = color_at(reflect_ray, remaining - 1);
return color * comps.object->material.reflective;
}
Color World::refracted_color(const Computations &comps, int remaining) const {
if (remaining <= 0 || comps.object->material.transparency == 0.0) {
return COLOR_BLACK;
}
double n_ratio = comps.n1 / comps.n2;
double cos_i = comps.eyev.dot(comps.normalv);
double sin2_t = (n_ratio * n_ratio) * (1.0 - (cos_i * cos_i));
if (sin2_t > 1.0) {
return COLOR_BLACK;
}
double cos_t = std::sqrt(1.0 - sin2_t);
RayVector refract_direction =
comps.normalv * (n_ratio * cos_i - cos_t) - comps.eyev * n_ratio;
Ray refract_ray(comps.under_point, refract_direction);
Color color = color_at(refract_ray, remaining - 1);
return color * comps.object->material.transparency;
}
DefaultWorld::DefaultWorld() {
light_source = new LightSource(Color(1, 1, 1), RayPoint(-10, 10, -10));
Sphere *s1 = new Sphere();
s1->material.color = Color(0.8, 1.0, 0.6);
s1->material.diffuse = 0.7;
s1->material.specular = 0.2;
objects.push_back(s1);
Sphere *s2 = new Sphere();
s2->set_transform(Matrix::scaling(0.5, 0.5, 0.5));
objects.push_back(s2);
}
bool World::is_shadowed(const RayPoint &p) const {
RayVector v = light_source->position - p;
double distance = v.magnitude();
RayVector direction = v.normalize();
Ray r(p, direction);
std::vector<Intersection> intersections = intersect_world(r);
const Intersection *i = hit(intersections);
if (i && i->t < distance) {
if (i->object->casts_shadow) {
return true;
}
return false;
}
return false;
}