/ miniRT / practice / miniRT_practice / phong_lighting.c
phong_lighting.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   phong_lighting.c                                   :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: salee2 <salee2@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/05/24 20:22:31 by salee2            #+#    #+#             */
 9  /*   Updated: 2023/05/24 20:22:32 by salee2           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "include/minirt.h"
14  
15  t_bool	in_shadow(t_scene *scene, t_vec3 light_dir)
16  {
17  	const double	light_len = mag(light_dir);
18  	const t_color3	hit_point = add(scene->hit.point, \
19  	scl_mul(EPSILON, scene->hit.normal));
20  	t_hit			hit;
21  	t_ray			light_ray;
22  
23  	light_ray = ray_(hit_point, light_dir);
24  	hit = hit_(light_len);
25  	if (hit_objects(scene->objects, &light_ray, &hit))
26  		return (TRUE);
27  	return (FALSE);
28  }
29  
30  t_color3	point_light_get(t_scene *scene, t_light *light)
31  {
32  	const double	brightness = light->bright_ratio * LUMEN;
33  	const t_color3	ambient = scene->ambient;
34  	t_vec3			light_dir;
35  	t_color3		diffuse;
36  	t_color3		specular;
37  
38  	light_dir = sub(light->origin, scene->hit.point);
39  	if (in_shadow(scene, light_dir))
40  		return (color3_(0, 0, 0));
41  	light_dir = norm(light_dir);
42  	diffuse = get_diffuse(scene->hit.normal, light_dir, light->color);
43  	specular = get_specular(scene, light_dir, light->color);
44  	return (scl_mul(brightness, add(add(ambient, diffuse), specular)));
45  }
46  
47  t_color3	phong_lighting(t_scene *scene)
48  {
49  	t_object		*lights;
50  	t_color3		light_color;
51  
52  	light_color = color3_(0, 0, 0);
53  	lights = scene->light;
54  	while (lights)
55  	{
56  		if (lights->type == LIGHT_POINT)
57  		{
58  			light_color = add(light_color, \
59  			point_light_get(scene, lights->element));
60  		}
61  		lights = lights->next;
62  	}
63  	light_color = add(light_color, scene->ambient);
64  	light_color = hadam_prod(light_color, scene->hit.albedo);
65  	light_color = elem_min(light_color, color3_(1, 1, 1));
66  	return (light_color);
67  }