-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel_shade.c
86 lines (79 loc) · 2.96 KB
/
pixel_shade.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pixel_shade.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hlimouni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/16 15:01:40 by hlimouni #+# #+# */
/* Updated: 2021/02/03 09:49:08 by hlimouni ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
t_vect phong_diffuse_specular(t_hit *hit, t_light *light,
t_vect color)
{
t_vect diffuse_specular;
double refle_cst;
t_vect reflect;
t_vect diffuse;
double specular;
diffuse = vect_const_prod(DIFU_C, color);
refle_cst = 2 * vect_dot(hit->normal, hit->to_light);
reflect = vect_const_prod(refle_cst, hit->normal);
reflect = vect_diff(reflect, hit->to_light);
reflect = vect_unit(reflect);
specular = vect_dot(hit->view, reflect);
specular = SPEC_C * pow(specular, SHINE);
diffuse_specular = vect_const_sum(specular, diffuse);
hit->surface_illumi = fmax(0, vect_dot(hit->normal, hit->to_light));
diffuse_specular = vect_const_prod(hit->surface_illumi, diffuse_specular);
diffuse_specular = vect_prod(light->coeff, diffuse_specular);
return (diffuse_specular);
}
int shadow_intersect(t_hit *hit, t_list *obj_node,
t_light *light)
{
t_ray sh_ray;
double o_l_dis;
double t;
sh_ray.cam_up = hit->cam_up;
sh_ray.dir = vect_unit(vect_diff(light->l, hit->ray_obj));
hit->to_light = sh_ray.dir;
if (vect_dot(hit->ray_dir, hit->to_light) < 0 &&
vect_dot(hit->normal, hit->to_light) < 0
&& vect_dot(hit->normal, hit->ray_dir) > 0)
hit->normal = vect_unit(vect_const_prod(-1, hit->normal));
sh_ray.origin = vect_sum(hit->ray_obj, vect_const_prod(BIAS, hit->normal));
o_l_dis = vect_norm(vect_diff(light->l, hit->ray_obj));
while (obj_node)
{
if ((t = one_obj_intersect(&sh_ray, obj_node)) >= 0 && t <= o_l_dis)
return (1);
obj_node = obj_node->next;
}
return (0);
}
int pixel_shade(t_hit *hit, t_scene *scene)
{
t_list *light_lst;
t_light *light;
t_vect amb;
t_vect difu_spec;
t_vect color;
light_lst = scene->lights;
amb = vect_prod(scene->amb->coeff, hit->color);
difu_spec = (t_vect){.x = 0, .y = 0, .z = 0};
while (light_lst)
{
light = light_lst->content;
if (shadow_intersect(hit, scene->objs, light) == 0)
{
difu_spec = vect_sum(difu_spec,
phong_diffuse_specular(hit, light, hit->color));
}
light_lst = light_lst->next;
}
color = vect_sum(amb, difu_spec);
return (vectoi(color));
}