-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.h
53 lines (48 loc) · 1.36 KB
/
sphere.h
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
//
// Created by xiaoc on 2018/10/8.
//
#ifndef PATH_TRACER_SPHERE_H
#define PATH_TRACER_SPHERE_H
#include "util.h"
#include "renderobject.h"
class Sphere : public RenderObject {
public:
double radius;
Sphere(const Vector3 &c, const Material &m, double r) : RenderObject() {
material = m;
radius = r;
center = c;
}
Hit intersect(const Ray &ray) override {
auto a = 1;
auto b = 2 * (ray.direction * (ray.origin - center));
auto c = (ray.origin - center).lengthSqaured() - radius * radius;
auto delta = b * b - 4 * a * c;
if (delta < 0)
return {nullptr, -1, {}};
else {
delta = sqrt(delta);
auto d1 = (-b + delta) / (2 * a);
auto d2 = (-b - delta) / (2 * a);
double d;
if (d1 < 0)
d = d2;
else if (d2 < 0)
d = d1;
else {
d = std::min(d1, d2);
}
if (d < eps) {
return {nullptr, -1, {}};
}
auto i = ray.getIntersectionPoint(d);
auto norm = i - center;
norm.normalize();
if (norm * ray.direction > 0) {
// throw std::runtime_error("");
}
return {nullptr, d, norm};
}
}
};
#endif //PATH_TRACER_SPHERE_H