-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.h
57 lines (47 loc) · 1.12 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
#ifndef SPHERE_H
# define SPHERE_H
#include "vec3.h"
#include "hittable.h"
typedef struct sphere
{
point3 center;
double radius;
} sphere;
hittable sphere_(point3 center, double radius, material material)
{
hittable h;
sphere *s;
h.geometry = _sphere;
h.material = material;
h.pointer = malloc(sizeof(sphere));
if ((s = h.pointer))
{
s->center = center;
s->radius = radius;
}
return (h);
}
int hit_sphere(sphere *s, ray *r, double t_min, double t_max, hit_record *rec)
{
vec3 oc = subtract(r->origin, s->center);
double a = length_squared(r->direction);
double half_b = dot(oc, r->direction);
double c = length_squared(oc) - s->radius*s->radius;
double discriminant = half_b*half_b - a*c;
if (discriminant < 0)
return (FALSE);
double sqrtd = sqrt(discriminant);
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 = at(r, rec->t);
vec3 outward_normal = divide(subtract(rec->p, s->center), s->radius);
set_face_normal(rec, r, outward_normal);
return (TRUE);
}
#endif