-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.c
103 lines (94 loc) · 2.62 KB
/
render.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvonsovs <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 15:00:19 by fvonsovs #+# #+# */
/* Updated: 2024/09/14 14:40:56 by fvonsovs ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
t_trace *closest_obj(t_ray ray, t_trace *closest, t_obj *object)
{
float t;
closest->t = INFINITY;
closest->hit_object.object = NULL;
closest->color = 0x000000;
t = INFINITY;
while (object)
{
if (intersect(ray, object, &t) && t < closest->t)
{
closest->t = t;
closest->hit_object = *object;
closest->ray = ray;
closest->hit_point = vec_add(ray.orig, \
vec_mul(ray.dir, closest->t));
closest->normal = shape_normal(closest, ray);
closest->color = closest->hit_object.color;
}
object = object->next;
}
if (closest->hit_object.object != NULL)
return (closest);
return (NULL);
}
void render_ray(t_win *win, int x, int y)
{
t_ray ray;
t_trace closest;
t_float_3 vec;
vec = pixels_to_viewport(x, y);
ray = throw_ray(win->map, vec);
if (closest_obj(ray, &closest, win->map->objects))
illuminate(win->map, &closest);
pixel_to_img(win, x, y, closest.color);
}
void *threaded_render(void *arg)
{
t_thread *data;
int x;
int y;
data = (t_thread *)arg;
y = data->start_y;
while (y < data->end_y)
{
x = 0;
while (x < WINDOW_WIDTH)
{
render_ray(data->win, x, y);
x++;
}
y++;
}
return (NULL);
}
int render(t_win *win)
{
pthread_t threads[NUM_THREADS];
t_thread thread_data[NUM_THREADS];
int rows;
int i;
i = 0;
rows = WINDOW_HEIGHT / NUM_THREADS;
while (i < NUM_THREADS)
{
thread_data[i].win = win;
thread_data[i].start_y = i * rows;
thread_data[i].end_y = (i + 1) * rows;
if (i == NUM_THREADS - 1)
thread_data[i].end_y = WINDOW_HEIGHT;
pthread_create(&threads[i], NULL, threaded_render, &thread_data[i]);
i++;
}
i = 0;
while (i < NUM_THREADS)
{
pthread_join(threads[i], NULL);
i++;
}
mlx_put_image_to_window(win->mlx, win->win, win->img, 0, 0);
return (0);
}