-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.h
44 lines (35 loc) · 1.3 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
#ifndef RAYTRACER_SPHERE_H
#define RAYTRACER_SPHERE_H
#include "hittable.h"
class Sphere : public Hittable {
public:
Sphere(Point3 _center, double _radius, shared_ptr<Material> _material) : center(_center), radius(_radius), material(std::move(_material)) {}
bool hit(const Ray &ray, Interval ray_t, HitRecord& rec) const override {
Vec3 oc = ray.origin() - center; // Origin to center
auto a = ray.direction().lengthSquared();
auto half_b = dot(oc, ray.direction());
auto c = oc.lengthSquared() - radius * radius;
auto discriminant = half_b*half_b - a*c;
if (discriminant < 0) return false;
auto sqrt_disc = sqrt(discriminant);
// Find the nearest root that lies in the acceptable range
auto root = (-half_b - sqrt_disc) / a;
if (!ray_t.surrounds(root)) {
root = (-half_b + sqrt_disc) / a;
if (!ray_t.surrounds(root)) {
return false;
}
}
rec.t = root;
rec.point = ray.at(rec.t);
Vec3 outward_normal = (rec.point - center) / radius;
rec.setFaceNormal(ray, outward_normal);
rec.material = material;
return true;
}
private:
Point3 center;
double radius;
shared_ptr<Material> material;
};
#endif //RAYTRACER_SPHERE_H