-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cc
75 lines (57 loc) · 1.79 KB
/
utils.cc
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
#include "utils.h"
namespace raytracer {
// Camera
Camera::Camera(const Vector3& position, const Vector3& look_at)
: pos(position),
forward((look_at - pos).Normal()),
right(1.5 * (forward.Cross(Vector3(0, -1, 0))).Normal()),
up(1.5 * (forward.Cross(right)).Normal()) { }
// Sphere
bool Sphere::Intersect(const Ray& ray, ISect* isect) const {
Vector3 eo(center - ray.start);
double v = eo.Dot(ray.dir);
double dist;
if (v < 0) {
dist = 0.0;
} else {
double disc = radius*radius - (eo.Dot(eo) - v*v);
dist = disc < 0.0 ? 0.0 : v - std::sqrt(disc);
}
if (dist == 0.0)
return false;
isect->thing = this; // Careful of potential segfaults. If *this is
// is stack-allocated it must be in the same scope
// as |isect|. Possible improvement is to do away with
// thing ptr and have a member with only required
// properties of object being intersected copied over.
isect->ray = ray;
isect->dist = dist;
return true;
}
Vector3 Sphere::Normal(const Vector3& pos) const {
return (pos - center).Normal();
}
// Plane
bool Plane::Intersect(const Ray& ray, ISect* isect) const {
double denominator = normal.Dot(ray.dir);
if (denominator >= 0.0)
return false;
isect->thing = this;
isect->ray = ray;
isect->dist = (normal.Dot(ray.start) + offset) / (-denominator);
return true;
}
Vector3 Plane::Normal(const Vector3& pos) const {
return normal;
}
// Scene
std::vector<ISect> Scene::Intersect(const Ray& ray) const {
std::vector<ISect> ret; // XXX: ISect*
ISect isect;
for (auto it = things.cbegin(); it != things.cend(); ++it) {
if ((*it)->Intersect(ray, &isect))
ret.push_back(isect);
}
return ret;
}
} // namesace raytracer