This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_light.c
96 lines (87 loc) · 2.7 KB
/
get_light.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_light.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: drestles <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/10 15:45:05 by gkessler #+# #+# */
/* Updated: 2019/02/17 18:08:15 by drestles ### ########.fr */
/* */
/* ************************************************************************** */
#include "rtv1.h"
char get_rgb_color(double ia, char color, double ia_left)
{
unsigned int color_rgb;
double c_rgb;
color_rgb = (unsigned int)color >> 24;
c_rgb = (((double)(color_rgb) * (ia)));
color_rgb = (unsigned int)c_rgb;
if (color_rgb > 255)
color_rgb = 255;
if ((255 - color_rgb) > (int)ia_left)
color_rgb += ia_left;
else
color_rgb = (char)255;
color = (char)color_rgb;
return (color);
}
int get_color(double ia, t_obj obj)
{
double ia_left;
ia_left = 0;
if (ia > 1)
{
ia_left = (ia - 1.00000) / 3.0000;
ia_left *= 255;
ia = 1.00000;
}
obj.col.rgb.r = get_rgb_color(ia, obj.col.rgb.r, ia_left);
obj.col.rgb.g = get_rgb_color(ia, obj.col.rgb.g, ia_left);
obj.col.rgb.b = get_rgb_color(ia, obj.col.rgb.b, ia_left);
return (obj.col.value);
}
double compute_shadows(t_rt *rt, t_obj *obj, double sc, t_vec3 p)
{
int i;
double temp;
i = 0;
while (i < rt->obj_number)
{
if (i != rt->index && rt->objects[i].type == 0)
{
rt->objects[i].oc = vec_minus(rt->objects[i].dot, p);
temp = rt->objects[i].func(&rt->objects[i], rt);
if (temp != -1 && (fabs(temp) < vec_modul(rt->dir)))
sc = -1.000;
}
i++;
}
return (sc);
}
double get_light(t_obj *obj, t_rt *rt, t_obj light)
{
double sc;
double ia;
double ia_left;
t_vec3 n_n;
t_vec3 l_n;
ia = 0.000000;
ia_left = 0.000000;
obj->p = vec_plus(rt->cam, vec_mul(rt->dir, rt->res));
obj->n = vec_minus(obj->dot, obj->p);
n_n = vec_div(obj->n, vec_modul(obj->n));
obj->l = vec_minus(obj->p, light.dot);
l_n = vec_div(obj->l, vec_modul(obj->l));
sc = vec_sc(n_n, l_n);
ia_left = 0.0000000;
rt->dir = vec_minus(obj->p, light.dot);
sc = compute_shadows(rt, obj, sc, obj->p);
if (sc > -0.000001)
{
ia = light.inten * (sc / (vec_modul(obj->l) * vec_modul(n_n)));
obj->v = vec_minus(obj->p, rt->cam);
}
ia += rt->amb;
return (ia);
}