-
Notifications
You must be signed in to change notification settings - Fork 1
/
hittable_list.h
99 lines (85 loc) · 1.47 KB
/
hittable_list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef HITTABLE_LIST_H
# define HITTABLE_LIST_H
# include "hittable.h"
# include "sphere.h"
# include "moving_sphere.h"
typedef struct list
{
hittable object;
struct list *next;
} list;
list *list_(hittable object)
{
list *new;
new = malloc(sizeof(list));
if (new)
{
new->object = object;
new->next = NULL;
}
return (new);
}
void push(list **lst, list *new)
{
list *temp;
temp = *lst;
*lst = new;
(*lst)->next = temp;
}
void drop(list *lst)
{
if (lst)
{
free(lst->object.pointer);
free(lst);
}
}
void clear(list **lst)
{
list *temp;
if (lst)
{
while (*lst)
{
temp = (*lst)->next;
drop(*lst);
(*lst) = temp;
}
lst = NULL;
}
}
static int hit_(hittable *object, ray *r, double t_min, double t_max, hit_record *rec)
{
int is_hit;
switch (object->geometry)
{
case _sphere:
is_hit = hit_sphere(object->pointer, r, t_min, t_max, rec);
break;
case _moving_sphere:
is_hit = hit_moving_sphere(object->pointer, r, t_min, t_max, rec);
break;
}
if (is_hit)
rec->material = object->material;
return (is_hit);
}
int hit(list *current, ray *r, double t_min, double t_max, hit_record *rec)
{
hit_record temp_rec;
double closest_so_far = t_max;
int hit_anything = FALSE;
while (current)
{
if (hit_(¤t->object, r, t_min, t_max, &temp_rec))
if (temp_rec.t < closest_so_far)
{
hit_anything = TRUE;
closest_so_far = temp_rec.t;
*rec = temp_rec;
}
current = current->next;
}
return (hit_anything);
}
#endif