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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */
/* Updated: 2020/02/26 18:21:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
#define SUPERSAMPLE_SIZE 3
static t_color st_take_sample(t_state *state, t_complex z, double step_r, double step_i)
{
double u;
double v;
t_complex sz;
t_color color;
t_color tmp;
int r;
int g;
int b;
color.hexcode = 0x0;
r = 0;
g = 0;
b = 0;
u = 0.0;
while (u < state->samples)
{
v = 0.0;
while (v < state->samples)
{
sz.i = z.i + step_i * (u / state->samples);
sz.r = z.r + step_r * (v / state->samples);
tmp = state->palette[state->func(state, sz)];
r += tmp.rgb.r;
g += tmp.rgb.g;
b += tmp.rgb.b;
v += 1.0;
}
u += 1.0;
}
int s = (int)(state->samples * state->samples);
color.rgb.r = r / s;
color.rgb.g = g / s;
color.rgb.b = b / s;
return (color);
}
static void *st_render_routine(void *void_arg)
{
int j;
t_complex z;
t_state *state;
int offset;
double step_r;
t_render_routine_arg *arg;
arg = void_arg;
state = arg->state;
z = arg->z;
offset = arg->offset;
j = -1;
step_r = state->plane.r / (double)WINDOW_WIDTH;
z.r = state->center.r - state->plane.r / 2.0;
while (++j < WINDOW_WIDTH)
{
((t_color*)state->window.data)[offset] = state->samples == 1.0 ?
state->palette[state->func(state, z)] : st_take_sample(state, z, step_r, arg->step_i);
offset++;
z.r += step_r;
}
return (NULL);
}
static void st_render_fractal(t_state *state)
{
int i;
pthread_t threads[WINDOW_HEIGHT];
t_render_routine_arg routine_args[WINDOW_HEIGHT];
double step_i;
double z_i;
step_i = state->plane.i / (double)WINDOW_HEIGHT;
z_i = state->center.i - state->plane.i / 2.0;
i = -1;
while (++i < WINDOW_HEIGHT)
{
routine_args[i].state = state;
routine_args[i].offset = i * WINDOW_WIDTH;
routine_args[i].z.i = z_i;
routine_args[i].step_i = step_i;
if (pthread_create(threads + i, NULL, st_render_routine, routine_args + i) < 0)
{
state->running = false;
break;
}
z_i += step_i;
}
while (i-- > 0)
pthread_join(threads[i], NULL);
}
int render_update(t_state *state)
{
if (!state->running)
{
state_destroy(state);
exit(EXIT_SUCCESS);
}
if (state->updated)
return (0);
st_render_fractal(state);
mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0);
char *iterations_str = ft_itoa(state->iterations);
char *samples_str = ft_itoa((int)state->samples);
char center_buf[1024];
sprintf(center_buf, "%f + %fi", state->center.r, state->center.i); // no
mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 20, 0x000000, iterations_str);
mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 30, 0x000000, samples_str);
mlx_string_put (state->mlx_ptr, state->window_ptr, 10, 40, 0x000000, center_buf);
free(iterations_str);
free(samples_str);
state->updated = true;
return (0);
}
|