-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
30 lines (24 loc) · 698 Bytes
/
Sphere.cpp
File metadata and controls
30 lines (24 loc) · 698 Bytes
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
//
// Created by mtakagi on 2024/02/19.
//
#include "Sphere.h"
#include "Isect.h"
#include "Ray.h"
#include <cmath>
namespace aobench {
const Isect Sphere::rayIntersect(const Isect &isect, const Ray &ray) const noexcept {
auto rs = ray.org() - center_;
auto B = rs.dot(ray.dir());
auto C = rs.dot(rs) - radius_ * radius_;
auto D = B * B - C;
if (D > 0) {
auto t = -B - sqrt(D);
if (t > 0.0 && (t < isect.t())) {
auto p = ray.org() + ray.dir() * t;
auto n = p - center_;
return Isect(t, p, n.normalize(), true);
}
}
return isect;
}
} // aobench