-
Notifications
You must be signed in to change notification settings - Fork 0
/
Objects.hpp
67 lines (54 loc) · 1.65 KB
/
Objects.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
#ifndef OBJECT
#define OBJECT
#include "Interval.hpp"
#include "Vector.hpp"
#include "Ray.hpp"
struct Hit {
bool occured = false;
Vector3 point;
Vector3 normal;
double t;
bool frontFace;
inline void setFaceNormal(const Ray& ray, const Vector3& outwardNormal) {
frontFace = ray.direction.dot(outwardNormal) < 0;
normal = frontFace ? outwardNormal : -outwardNormal;
}
};
class Object {
public:
virtual Hit hit (const Ray& ray, Interval rayT) const = 0;
};
class Sphere : public Object {
public:
Vector3 center;
double radius;
Sphere() {}
Sphere(Vector3 c, double r) : center(c), radius(r) {};
virtual Hit hit(const Ray& ray, Interval rayT) const override {
Hit hit;
Vector3 sphereToRay = ray.origin - center;
double a = ray.direction.dot(ray.direction);
double half_b = sphereToRay.dot(ray.direction);
double c = sphereToRay.dot(sphereToRay) - radius*radius;
double discriminant = half_b*half_b - a*c;
if (discriminant < 0)
return hit;
float sqrtd = sqrt(discriminant);
// Find the nearest root that lies in the acceptable range.
double root = (-half_b - sqrtd) / a;
if (!rayT.surrounds(root)) {
root = (-half_b + sqrtd) / a;
if (!rayT.surrounds(root)) {
return hit;
}
}
hit.t = root;
hit.point = ray.at(hit.t);
hit.normal = (hit.point - center) / radius;
hit.occured = true;
Vector3 outwardNormal = (hit.point - center) / radius;
hit.setFaceNormal(ray, outwardNormal);
return hit;
};
};
#endif