-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlighting.c
More file actions
54 lines (47 loc) · 2 KB
/
lighting.c
File metadata and controls
54 lines (47 loc) · 2 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lighting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: youkhart <youkhart@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/25 22:10:46 by youkhart #+# #+# */
/* Updated: 2020/01/29 20:46:08 by youkhart ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
extern struct s_minirt g_rt;
int get_ambient_color(t_intersection *i)
{
return (add_colors(
mult_colors(g_rt.g_ambient_light.color,
g_rt.g_ambient_light.brightness),
mult_colors(i->object_color,
g_rt.g_ambient_light.brightness)));
}
int get_diffuse_color(t_intersection *i,
t_ray shadow_ray, t_light *light)
{
t_vector light_dir;
float dot;
light_dir = vec_normalize(vec_sub(light->pos, shadow_ray.pos));
if (!(dot = fmax(vec_dot(i->normal, light_dir), 0)))
return (0);
return (mult_colors(light->color, dot * i->diffuse * light->brightness));
}
int get_specular_color(t_intersection *i,
t_ray ray, t_ray shadow_ray, t_light *light)
{
t_vector light_dir;
t_vector reflection_dir;
t_vector view_dir;
float dot;
light_dir = vec_normalize(vec_sub(light->pos, shadow_ray.pos));
reflection_dir = vec_sub(ray.dir,
vec_mult(i->normal, 2 * vec_dot(ray.dir, i->normal)));
view_dir = vec_mult(ray.dir, -1);
if (!(dot = fmax(vec_dot(reflection_dir, light_dir), 0)))
return (0);
return (mult_colors(light->color,
i->specular * pow(dot, i->s_power) * light->brightness));
}