Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reflection functions for refraction calculus #19

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SOURCES := main.c models.c basic_math.c vector_math.c colors.c canvas.c
SOURCES += mx_attributes.c mx_operations.c mx_rotations.c mx_transformations.c
SOURCES += mx_utils.c rays.c intersections.c spheres.c
SOURCES += lights.c materials.c guards.c world.c view_transform.c camera.c
SOURCES += render.c controls.c shapes.c shadows.c planes.c
SOURCES += render.c controls.c shapes.c shadows.c planes.c textures.c

OBJS := $(addprefix $(OBJ_DIR)/, $(SOURCES:.c=.o))

Expand Down
2 changes: 1 addition & 1 deletion include/lights.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 11:06:23 by yde-goes #+# #+# */
/* Updated: 2023/04/25 14:57:57 by yde-goes ### ########.fr */
/* Updated: 2023/05/11 17:51:28 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
16 changes: 13 additions & 3 deletions include/materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 11:05:52 by yde-goes #+# #+# */
/* Updated: 2023/04/20 10:58:42 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 11:28:00 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,6 +34,13 @@
* by the parameter shininess.
* @param shininess Represents the shininess. The higher the shininess, the
* smaller and tigher the specular highlight.
* @param reflective Represents the material's surface reflection value.
* @param transparency Represents the material's transparency. If transparency
* is zero, then the surface is opaque.
* @param refractive_index Represents the degree to which light will bend when
* entering or exiting the material, compared to other
* materials. If variable value is 1.0, it means that
* the object is empty, vacuum-filled shells.
*/
typedef struct s_material
{
Expand All @@ -42,6 +49,9 @@ typedef struct s_material
float diffuse;
float specular;
float shininess;
float reflective;
float transparency;
float refractive_index;
} t_material;

/* ************************************************************************** */
Expand Down Expand Up @@ -80,7 +90,7 @@ typedef struct s_exposure
* t_material with the following default values: color = {1, 1, 1},
* ambient = 0.1, diffuse = 0.9, specular = 0.9 and shininess = 200.
*
* @return (t_material) Returns a default instance of the new material.
* @return Returns a default instance of the new material.
*/
t_material material(void);

Expand All @@ -99,7 +109,7 @@ t_material material(void);
* @param point A struct of type t_tuple of the point being illuminated.
* @param sight A struct of type t_sight with the values of eye and normal
* vectors obtained from the Phong Reflection Model algorithm.
* @return (t_color) The function returns the final shading of that point.
* @return The function returns the final shading of that point.
*/
t_color lighting(t_material m, t_light light, t_tuple point, t_sight sight);

Expand Down
10 changes: 8 additions & 2 deletions include/shapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* shapes.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 18:05:41 by mdias-ma #+# #+# */
/* Updated: 2023/05/09 14:13:17 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 17:33:27 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -266,4 +266,10 @@ t_tuple normal_at_plane(t_shape *shape, t_tuple world_point);
*/
t_bool intersect_plane(t_hit **xs, t_shape *shape, t_ray ray);

/* ************************************************************************** */
/* TEXTURES.C */
/* ************************************************************************** */

void glassy_shape(t_shape *shape);

#endif
34 changes: 30 additions & 4 deletions include/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* world.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/13 14:17:44 by mdias-ma #+# #+# */
/* Updated: 2023/05/08 16:48:57 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 14:11:54 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,8 @@
# include "lights.h"
# include "shapes.h"

# define MAX_RECURSION 5

typedef struct s_world
{
int object_count;
Expand All @@ -32,6 +34,7 @@ typedef struct s_comps
t_sight sight;
t_bool inside;
t_tuple over_point;
t_tuple reflectv;
} t_comps;

/* ************************************************************************** */
Expand Down Expand Up @@ -78,9 +81,12 @@ t_comps prepare_computations(t_hit *intersection, t_ray ray);
*
* @param world The world in which the intersection occurred.
* @param comps The precomputed information about the intersection.
* @param remaining Specifies the maximum recursive depth for the function if it
* needs to handle infinite recursion caused by two objects that
* mutually reflect rays between themselves.
* @return The color of the intersection point.
*/
t_color shade_hit(t_world world, t_comps comps);
t_color shade_hit(t_world world, t_comps comps, size_t remaining);

/**
* @brief Computes the color at the intersection of a given ray with a world.
Expand All @@ -96,10 +102,30 @@ t_color shade_hit(t_world world, t_comps comps);
*
* @param world The world in which the intersection occurred.
* @param ray The ray that intersected with the shapes in the world.
* @param remaining Specifies the maximum recursive depth for the function if it
* needs to handle infinite recursion caused by two objects that
* mutually reflect rays between themselves.
* reflect rays between themselves
* @return The color of the intersection point, or black if there is
* no such intersection.
*/
t_color color_at(t_world world, t_ray ray);
t_color color_at(t_world world, t_ray ray, size_t remaining);

/**
* @brief This function calcutes the reflected ray and its color from any
* reflective material specified in variable of type t_world.
*
* @param world A pointer to a structure of type `t_world` representing the
* world containing objects, rays and light sources.
* @param comps The precomputed information about the world.
* @param remaining Specifies the maximum recursive depth for the function if it
* needs to handle infinite recursion caused by two objects that
* mutually reflect rays between themselves.
* @return If remaining is zero or if material reflection is nonexistent, the
* function returns a color equivalent to black. Otherwise, it returns
* the reflected color from a reflective material.
*/
t_color reflected_color(t_world world, t_comps comps, size_t remaining);

/* ************************************************************************** */
/* SHADOWS.C */
Expand Down
6 changes: 3 additions & 3 deletions src/canvas/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/21 12:16:18 by mdias-ma #+# #+# */
/* Updated: 2023/04/23 12:51:00 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 10:36:31 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,7 +28,7 @@ t_bool render_scene(t_canvas *canvas, t_world *world, t_camera *camera)
while (x < camera->hsize - 1)
{
ray = ray_for_pixel(camera, x, y);
color = color_at(*world, ray);
color = color_at(*world, ray, MAX_RECURSION);
write_pixel(canvas, x, y, rgb(color));
x++;
}
Expand Down
7 changes: 5 additions & 2 deletions src/materials/materials.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 11:11:46 by yde-goes #+# #+# */
/* Updated: 2023/04/25 17:30:42 by yde-goes ### ########.fr */
/* Updated: 2023/05/12 14:52:58 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,10 @@ t_material material(void)
.ambient = 0.1,
.diffuse = 0.9,
.specular = 0.9,
.shininess = 200.0
.shininess = 200.0,
.reflective = 0.0,
.transparency = 0.0,
.refractive_index = 1.0
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/shapes/spheres.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* spheres.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 18:30:21 by mdias-ma #+# #+# */
/* Updated: 2023/05/09 10:34:13 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 11:33:44 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
19 changes: 19 additions & 0 deletions src/shapes/textures.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/12 11:19:51 by yde-goes #+# #+# */
/* Updated: 2023/05/12 11:27:27 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

#include "shapes.h"

void glassy_shape(t_shape *shape)
{
shape->material.transparency = 1.0;
shape->material.refractive_index = 1.5;
}
4 changes: 2 additions & 2 deletions src/world/shadows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* shadows.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/19 16:22:34 by yde-goes #+# #+# */
/* Updated: 2023/05/09 14:13:16 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 10:36:50 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
37 changes: 25 additions & 12 deletions src/world/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* world.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdias-ma <mdias-ma@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/13 15:29:04 by mdias-ma #+# #+# */
/* Updated: 2023/05/09 14:13:14 by mdias-ma ### ########.fr */
/* Updated: 2023/05/12 10:50:09 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,23 +46,24 @@ t_comps prepare_computations(t_hit *intersection, t_ray ray)
return (comps);
}
comps.over_point = add(comps.point, multiply(comps.sight.normalv, EPSILON));
comps.reflectv = reflect(ray.direction, comps.sight.normalv);
comps.inside = FALSE;
return (comps);
}

t_color shade_hit(t_world world, t_comps comps)
t_color shade_hit(t_world world, t_comps comps, size_t remaining)
{
t_color surface;
t_color reflected;

world.lights->in_shadow = is_shadowed(&world, comps.over_point);
return (
lighting(
comps.object->material,
world.lights[0],
comps.point,
comps.sight
));
surface = lighting(comps.object->material,
world.lights[0], comps.point, comps.sight);
reflected = reflected_color(world, comps, remaining);
return (add_color(surface, reflected));
}

t_color color_at(t_world world, t_ray ray)
t_color color_at(t_world world, t_ray ray, size_t remaining)
{
t_hit *x;
t_comps comps;
Expand All @@ -73,6 +74,18 @@ t_color color_at(t_world world, t_ray ray)
if (x == NULL)
return (new_color(0, 0, 0));
comps = prepare_computations(x, ray);
color = shade_hit(world, comps);
color = shade_hit(world, comps, remaining);
return (color);
}

t_color reflected_color(t_world world, t_comps comps, size_t remaining)
{
t_ray reflect_ray;
t_color color;

if (!remaining || !comps.object->material.reflective)
return (new_color(0, 0, 0));
reflect_ray = new_ray(comps.over_point, comps.reflectv);
color = color_at(world, reflect_ray, remaining - 1);
return (multiply_color(color, comps.object->material.reflective));
}
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SOURCES := models.c basic_math.c vector_math.c colors.c
SOURCES += mx_attributes.c mx_operations.c mx_rotations.c mx_transformations.c
SOURCES += mx_utils.c rays.c intersections.c spheres.c
SOURCES += lights.c materials.c guards.c world.c view_transform.c camera.c
SOURCES += canvas.c render.c controls.c shadows.c shapes.c planes.c
SOURCES += canvas.c render.c controls.c shadows.c shapes.c planes.c textures.c

UTILS := utils.c
OBJS := $(addprefix $(OBJ_DIR)/, $(SOURCES:.c=.o))
Expand Down
Loading