-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_sphere.h
More file actions
77 lines (60 loc) · 1.63 KB
/
moving_sphere.h
File metadata and controls
77 lines (60 loc) · 1.63 KB
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
70
71
72
73
74
75
76
77
#pragma once
__device__
void get_moving_sphere_screen_coordinates(const point3 &p,double &u,double &v);
class moving_sphere :public hittable
{
public:
__device__ moving_sphere() {}
__device__ moving_sphere(path* p, double r,material* m) : path_ptr(p), radius(r),mat_ptr(m)
{
center = (*path_ptr).position();
}
__device__ bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const override;
__device__ void move()
{
center = (*path_ptr).position();
}
__device__ ~moving_sphere()
{
delete mat_ptr;
}
public:
point3 center;
path* path_ptr;
double radius;
material* mat_ptr;
};
__device__
bool moving_sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) const
{
vec3 oc = r.origin() - center;
double a = r.direction().length_squared();
double half_b = dot(oc, r.direction());
double c = oc.length_squared() - radius*radius;
double discriminant = half_b*half_b - a*c;
if (discriminant < 0) return false;
double sqrtd = sqrt(discriminant);
// Find the nearest root that lies in the acceptable range.
double 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_moving_sphere_screen_coordinates(outward_normal,rec.u,rec.v);
rec.mat_ptr = mat_ptr;
return true;
}
__device__
void get_moving_sphere_screen_coordinates(const point3 &p,double &u,double &v)
{
double theta = acos(-p.y());
double phi = atan2(-p.z(),p.x()) + pi;
u = phi/(2*pi);
v = theta/pi;
}