-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.c
92 lines (82 loc) · 2.21 KB
/
plane.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* plane.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sboeuf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/16 19:20:54 by sboeuf #+# #+# */
/* Updated: 2014/02/16 22:18:42 by sboeuf ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/rtv1.h"
t_plane *new_plane(t_vect *normal, double distance, t_color *c)
{
t_plane *p;
p = (t_plane*)malloc(sizeof(t_plane));
p->normal = normal;
p->distance = distance;
p->color = c;
p->next = NULL;
return (p);
}
void add_plane(t_plane *start, t_plane *new)
{
if (start == NULL)
start = new;
else
{
while (start->next != NULL)
start = start->next;
start->next = new;
}
}
t_inter *find_planes_intersection(t_ray *ray)
{
double mininter;
double inter;
t_vect *normal;
t_color *c;
t_plane *p;
mininter = -1;
p = get_scene()->planes;
while (p != NULL)
{
inter = find_plane_intersection(p, ray);
if (inter > ACCURACY && (inter < mininter || mininter == -1))
{
mininter = inter;
normal = p->normal;
c = p->color;
}
p = p->next;
}
return (new_inter(normal, mininter, c));
}
double find_plane_intersection(t_plane *p, t_ray *ray)
{
double a;
double b;
a = dot_product(p->normal, ray->direction);
if (a == 0)
return (-1);
else
{
b = dot_product(p->normal, vect_add(ray->origin,
negative(vect_mult(p->normal, p->distance))));
return (-b / a);
}
}
void delete_planes(t_plane **start)
{
t_plane *tmp;
while (*start != NULL)
{
tmp = (*start)->next;
delete_vect((*start)->normal);
delete_color((*start)->color);
(*start)->next = NULL;
free(*start);
*start = tmp;
}
}