miniRT
By Marcelo Magalhães and Ygor G. Sena, 2023.
shapes.h
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* shapes.h :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2023/03/28 18:05:41 by mdias-ma #+# #+# */
9 /* Updated: 2023/06/16 13:40:21 by yde-goes ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
14 #ifndef SHAPES_H
15 # define SHAPES_H
16 
17 # include "rays.h"
18 # include "materials.h"
19 # include "patterns.h"
20 
21 # define EPSILON 0.00001
22 
29 typedef struct s_sphere
30 {
31  t_point origin;
32  float radius;
34 
40 typedef struct s_plane
41 {
42  t_point origin;
44 
55 typedef struct s_cylinder
56 {
57  t_point origin;
58  float radius;
59  float minimum;
60  float maximum;
61  t_bool closed;
63 
73 typedef struct s_cone
74 {
75  t_point origin;
76  float minimum;
77  float maximum;
78  t_bool closed;
80 
81 typedef struct s_shape t_shape;
82 typedef struct s_hit t_hit;
83 typedef t_bool (*t_hit_record)(t_hit **, t_shape *, t_ray);
84 typedef t_tuple (*t_normal_at)(t_shape *, t_tuple);
85 
106 typedef struct s_shape
107 {
108  union {
109  t_sphere sphere;
110  t_plane plane;
111  t_cylinder cylinder;
112  t_cone cone;
113  };
114  t_matrix transform;
115  t_matrix inverse;
116  t_matrix transpose;
117  t_material material;
118  t_hit_record intersect;
119  t_normal_at normal_at;
120  t_checker checker;
121  t_uv_map map;
122 } t_shape;
123 
133 typedef struct s_hit
134 {
135  double t;
136  t_shape *object;
137  t_hit *next;
138 } t_hit;
139 
167 typedef struct s_discriminant
168 {
169  double a;
170  double b;
171  double c;
172  double t1;
173  double t2;
174  double discriminant;
176 
177 /* ************************************************************************** */
178 /* INTERSECTIONS.C */
179 /* ************************************************************************** */
180 
195 t_bool intersect(t_hit **xs, t_shape *shape, t_ray ray);
196 
213 t_hit *intersection(float t, t_shape *shape);
214 
233 void insert_intersection(t_hit **xs, t_hit *i);
234 
249 t_hit *hit(t_hit *xs);
250 
251 /* ************************************************************************** */
252 /* SHAPES.C */
253 /* ************************************************************************** */
254 
265 t_shape new_shape(void);
266 
286 t_vector normal_at(t_shape *shape, t_point world_point);
287 
301 
302 /* ************************************************************************** */
303 /* SPHERES.C */
304 /* ************************************************************************** */
305 
316 t_shape new_sphere(void);
317 
318 /* ************************************************************************** */
319 /* PLANES.C */
320 /* ************************************************************************** */
321 
332 t_shape new_plane(void);
333 
334 /* ************************************************************************** */
335 /* PATTERNS.C */
336 /* ************************************************************************** */
337 
350  t_shape *shape, t_tuple world_point);
351 
352 /* ************************************************************************** */
353 /* MATERIALS.C */
354 /* ************************************************************************** */
355 
373 t_color lighting(t_shape *shape, t_light light,
374  t_tuple point, t_sight sight);
375 
376 /* ************************************************************************** */
377 /* CYLINDERS.C */
378 /* ************************************************************************** */
379 
392 t_shape new_cylinder(void);
393 
394 /* ************************************************************************** */
395 /* CONES.C */
396 /* ************************************************************************** */
397 
410 t_shape new_cone(void);
411 
412 /* ************************************************************************** */
413 /* DISCRIMINANTS.C */
414 /* ************************************************************************** */
415 
432 
447 
462 
463 #endif
t_ray transform(t_ray ray, t_matrix matrix)
The function transform() multiplies a given ray by a given matrix, which results in an unnormalized r...
Definition: rays.c:27
struct s_ray t_ray
The struct of type t_ray represents a ray, which is the foundation for ray casting....
t_shape new_cylinder(void)
Creates a new cylinder with default properties.
Definition: cylinders.c:20
t_bool intersect(t_hit **xs, t_shape *shape, t_ray ray)
This function gets shape's intersection from ray's origin. If the ray is inside the shape,...
Definition: intersections.c:31
t_shape new_cone(void)
Creates a new cone with default properties.
Definition: cones.c:20
t_hit * intersection(float t, t_shape *shape)
Creates a hit record for an intersection.
Definition: intersections.c:18
struct s_sphere t_sphere
The structure t_sphere represents a sphere.
struct s_cone t_cone
The structure t_cone represents a cone.
struct s_hit t_hit
The structure t_hit stores a linked list of ray-shape intersections' values.
Definition: shapes.h:82
struct s_plane t_plane
The structure t_plane represents a plane.
t_color pattern_at_shape(t_pattern pattern, t_shape *shape, t_tuple world_point)
This function applies a pattern texture on a given object at a given world location.
Definition: patterns.c:52
t_color lighting(t_shape *shape, t_light light, t_tuple point, t_sight sight)
In a nutshell, the function adds together the material's ambient diffuse, and specular componentes,...
Definition: materials.c:32
t_shape new_sphere(void)
Creates a new sphere with default properties.
Definition: spheres.c:19
void insert_intersection(t_hit **xs, t_hit *i)
Inserts a hit record into a list of intersections in order of increasing t value.
Definition: intersections.c:40
t_discriminant sphere_discriminant(t_sphere *sphere, t_ray ray)
This function returns the necessary data to evaluate a ray-sphere intersection. To do that,...
Definition: discriminants.c:15
struct s_cylinder t_cylinder
The structure t_cylinder represents a cylinder.
t_discriminant cone_discriminant(t_ray ray)
This function returns the necessary data to evaluate a ray-cone intersection. To do that,...
Definition: discriminants.c:60
t_vector normal_at(t_shape *shape, t_point world_point)
Calculates the normal vector at a given point on a shape's surface.
Definition: shapes.c:32
t_discriminant cylinder_discriminant(t_ray ray)
This function returns the necessary data to evaluate a ray-cylinder intersection. To do that,...
Definition: discriminants.c:38
t_hit * hit(t_hit *xs)
Determines the first hit from a list of intersections.
Definition: intersections.c:62
struct s_discriminant t_discriminant
This struct stores the necessary data to evaluate a ray-sphere intersection. To do that,...
t_shape new_shape(void)
Creates a new shape with default properties.
Definition: shapes.c:15
t_shape new_plane(void)
Creates a new plane with default properties.
Definition: planes.c:18
void set_transform(t_shape *shape, t_matrix transform)
Assigns a transformation matrix to a shape.
Definition: shapes.c:25
This structure encapsulates the function uv_checkers().
Definition: patterns.h:44
The struct s_color stores the color values of ray tracing. It contains a field for red,...
Definition: tuples.h:63
The structure t_cone represents a cone.
Definition: shapes.h:74
The structure t_cylinder represents a cylinder.
Definition: shapes.h:56
This struct stores the necessary data to evaluate a ray-sphere intersection. To do that,...
Definition: shapes.h:168
The structure t_hit stores a linked list of ray-shape intersections' values.
Definition: shapes.h:134
The struct s_light represents a light's source. This source has a position in space and an intensity ...
Definition: lights.h:44
A material has a surface color and four attributes from the Phong Reflection Model: ambient,...
Definition: materials.h:43
The struct t_matrix stores a matrix up to 4x4 and size of 16. A matrix is a grid of numbers that can ...
Definition: matrices.h:34
The type t_pattern contatins all the necessary information to apply a texture on a given shape's surf...
Definition: patterns.h:123
The structure t_plane represents a plane.
Definition: shapes.h:41
The struct of type t_ray represents a ray, which is the foundation for ray casting....
Definition: rays.h:28
Definition: shapes.h:107
The struct s_sight represents the human eye looking forwards.
Definition: lights.h:62
The structure t_sphere represents a sphere.
Definition: shapes.h:30
Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordin...
Definition: tuples.h:33
Represents a shape in a scene.
struct s_tuple t_tuple
Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordin...
t_tuple point(double x, double y, double z)
This function is a simplification of the function tuple(). It will create a point with the given coor...
Definition: models.c:20