-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphere.c
More file actions
58 lines (45 loc) · 1.63 KB
/
sphere.c
File metadata and controls
58 lines (45 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
#include <math.h>
#include "sphere.h"
static bool _hit(const void *t, const Ray r, float t_min, float t_max, Hit_Record *rec)
{
const Sphere s = *(const Sphere *) t;
Vector oc = new_vector(new_point(0, 0, 0));
oc = oc.sub(r.origin(r), s.center);
float a = oc.dot(r.direction(r), r.direction(r));
float b = oc.dot(oc, r.direction(r));
float c = oc.dot(oc, oc) - s.radius * s.radius;
float discriminant = b * b - a * c;
if (discriminant > 0) {
float temp = (-b - sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec->t = temp;
rec->p = r.point_at_parameter(r, rec->t);
rec->normal = rec->p.div_scalar(rec->p.sub(rec->p, s.center), s.radius);
rec->mat_ptr.inst = s.mat_ptr.inst;
rec->mat_ptr.scatter = s.mat_ptr.scatter;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec->t = temp;
rec->p = r.point_at_parameter(r, rec->t);
rec->normal = rec->p.div_scalar(rec->p.sub(rec->p, s.center), s.radius);
rec->mat_ptr.inst = s.mat_ptr.inst;
rec->mat_ptr.scatter = s.mat_ptr.scatter;
return true;
}
}
return false;
}
Sphere new_sphere(Vector v, float r, void *m, bool (*scatter)(const void *t, const Ray r_in, const Hit_Record rec, Vector *attenuation, Ray *scattered))
{
return (Sphere) {
.center = v,
.radius = r,
.mat_ptr.inst = m,
.mat_ptr.scatter = scatter,
.hitable = (Hitable) {
.hit = _hit
},
};
}