-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_cone.c
126 lines (115 loc) · 3.21 KB
/
f_cone.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* f_cone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sboeuf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/16 19:18:10 by sboeuf #+# #+# */
/* Updated: 2014/02/16 22:03:33 by sboeuf ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/rtv1.h"
t_vect *get_normal_at_cone(t_cone *cone, t_vect *point)
{
t_vect *v;
v = normalize(vect_add(point, negative(cone->center)));
return (v);
}
t_inter *find_cones_intersection(t_ray *ray)
{
double mininter;
double inter;
t_vect *normal;
t_color *c;
t_cone *cone;
mininter = -1;
cone = get_scene()->cones;
while (cone != NULL)
{
inter = find_cone_intersection(cone, ray);
if (inter > ACCURACY && (inter < mininter || mininter == -1))
{
mininter = inter;
normal = get_normal_at_cone(cone, vect_add(ray->origin,
vect_mult(ray->direction, inter)));
c = cone->color;
}
cone = cone->next;
}
return (new_inter(normal, mininter, c));
}
double find_cone_intersection(t_cone *cone, t_ray *r)
{
double a;
double b;
double c;
double d;
double rslt;
a = pow(cos(cone->alpha), 2) *
pow((r->direction->x + r->direction->z), 2) -
pow(sin(cone->alpha), 2) *
pow(r->direction->y, 2);
b = 2 * pow(cos(cone->alpha), 2) *
(r->direction->x * (r->origin->x - cone->center->x) +
r->direction->z * (r->origin->z - cone->center->z)) -
2 * pow(sin(cone->alpha), 2) *
r->direction->y * (r->origin->y - cone->center->y);
c = pow(cos(cone->alpha), 2) *
pow(r->origin->x - cone->center->x + r->origin->z - cone->center->z, 2)
- pow(sin(cone->alpha), 2) *
pow(r->origin->y - cone->center->y, 2);
d = b * b - 4 * a * c;
if (d > 0)
{
rslt = ((-b - sqrt(d)) / (2 * a)) - 0.000001 > 0 ?
(-b - sqrt(d)) / (2 * a) - 0.000001 :
(-b + sqrt(d)) / (2 * a) - 0.000001;
}
else
rslt = -1;
return (rslt);
}
t_cone *get_cones(int fd)
{
int r;
char *line;
t_cone *c;
c = NULL;
while ((r = get_next_line(fd, &line)) > 0 && ft_strcmp(line, "----------"))
{
if (!ft_strcmp("new:", line))
{
if (c == NULL)
c = get_cone(fd);
else
add_cone(c, get_cone(fd));
}
}
if (r == -1)
exit (-1);
return (c);
}
t_cone *get_cone(int fd)
{
int r;
char *line;
t_vect *center;
double alpha;
t_color *color;
while ((r = get_next_line(fd, &line)) > 0 && ft_strcmp(line, "----------"))
{
if (!ft_strcmp("pos:", line))
center = get_vector(fd);
if (!ft_strcmp("alpha:", line))
{
r = get_next_line(fd, &line);
alpha = ft_atodouble(&line);
}
if (!ft_strcmp("color:", line))
color = get_color(fd);
}
if (r == -1)
exit(-1);
return (new_cone(center, alpha, color));
}