-
Notifications
You must be signed in to change notification settings - Fork 2
/
sphere.h
69 lines (57 loc) · 2.16 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef SPHERE_H
#define SPHERE_H
#include "hittable.h"
#include "vec3.h"
class sphere : public hittable {
public:
__host__ __device__ sphere() {}
__host__ __device__ sphere(point3 cen, float r, material* m)
: center(cen), radius(r), mat_ptr(m) {};
__device__ virtual bool hit(
const ray& r, float t_min, float t_max, hit_record& rec) const override;
__device__ virtual bool bounding_box(float time0, float time1, aabb& output_box) const override;
public:
point3 center;
float radius;
material* mat_ptr;
private:
__device__ static void get_sphere_uv(const point3& p, float& u, float& v) {
// p: a given point on the sphere of radius one, centered at the origin.
// u: returned value [0,1] of angle around the Y axis from X=-1.
// v: returned value [0,1] of angle from Y=-1 to Y=+1.
float theta = acos(-p.y());
float phi = atan2(-p.z(), p.x()) + pi;
u = phi / (2*pi);
v = theta / pi;
}
};
__device__ bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
vec3 oc = r.origin() - center;
float a = r.direction().length_squared();
float half_b = dot(oc, r.direction());
float c = oc.length_squared() - radius*radius;
float discriminant = half_b*half_b - a*c;
if (discriminant < 0.0f) return false;
float sqrtd = sqrt(discriminant);
// Find the nearest root that lies in the acceptable range.
float root = (-half_b - sqrtd) / a;
if (root < t_min || t_max < root) {
root = (-half_b + sqrtd) / a;
if (root < t_min || t_max < root)
return false;
}
rec.t = root;
rec.p = r.at(rec.t);
vec3 outward_normal = (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
get_sphere_uv(outward_normal, rec.u, rec.v);
rec.mat_ptr = mat_ptr;
return true;
}
__device__ bool sphere::bounding_box(float time0, float time1, aabb& output_box) const {
output_box = aabb(
center - vec3(radius, radius, radius),
center + vec3(radius, radius, radius));
return true;
}
#endif