-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.hpp
90 lines (71 loc) · 2.02 KB
/
sphere.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef SPHERE_H
#define SPHERE_H
#include "vec3.hpp"
#include "ray.hpp"
#include <math.h>
#include "shadingModel.hpp"
#include "hittable.hpp"
class sphere : public hittable
{
public:
sphere(vec3 c, double r, shadingModel* material)
{
s_center = c;
s_radius = r;
this->material = material;
this->calculate_extents();
}
point3 center()
{
return s_center;
}
double radius()
{
return s_radius;
}
bool hit(const ray& cam_ray, double t_min, double t_max, hit_record& rec) override;
void calculate_extents() override;
private:
point3 s_center;
double s_radius;
shadingModel* material;
};
bool sphere::hit(const ray& cam_ray, double t_min, double t_max, hit_record& rec)
{
vec3 oc = cam_ray.origin() - s_center;
auto a = cam_ray.direction().length_squared();
auto half_b = dot(oc, cam_ray.direction());
auto c = oc.length_squared() - s_radius*s_radius;
auto discriminant = half_b*half_b - a*c;
if (discriminant < 0) return false;
auto sqrtd = sqrt(discriminant);
auto root = (-half_b - sqrtd) / a;
if (root <= t_min || t_max < root) {
root = (-half_b + sqrtd) / a;
if (root < t_min || root > t_max)
return false;
}
//hit record
rec.t = root;
rec.p = cam_ray.at(rec.t);
rec.d = cam_ray.direction();
vec3 normal = (rec.p - s_center) / s_radius;
//uv coordinates
const double pi = 3.1415;
auto theta = acos(-normal.y());
auto phi = atan2(-normal.z(), normal.x()) + pi;
auto u = phi / (2*pi);
auto v = 1 - (theta / pi);
rec.uv = vec2(u, v);
rec.set_face_normal(cam_ray, normal);
//rec.normal = vec3(0,1,0);
rec.material = material;
return true;
}
void sphere::calculate_extents()
{
vec3 r_offset = vec3(s_radius, s_radius, s_radius);
this->lower_extent = s_center - r_offset;
this->upper_extent = s_center + r_offset;
}
#endif