From dab9efb7b745fe884fb72017591dce95978c19c4 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 27 Feb 2020 14:47:23 +0100 Subject: Optimization and norming a bit --- src/render.c | 120 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 56 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index 16bb920..a57e3f7 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ -/* Updated: 2020/02/26 18:21:33 by cacharle ### ########.fr */ +/* Updated: 2020/02/27 14:40:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,73 +14,77 @@ #define SUPERSAMPLE_SIZE 3 -static t_color st_take_sample(t_state *state, t_complex z, double step_r, double step_i) +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; + t_complex epsilon; + t_complex sample; + t_color color; + t_color tmp; + uint32_t r; + uint32_t g; + uint32_t b; + int s; color.hexcode = 0x0; r = 0; g = 0; b = 0; - u = 0.0; - while (u < state->samples) + epsilon.i = 0.0; + while (epsilon.i < state->samples) { - v = 0.0; - while (v < state->samples) + epsilon.r = 0.0; + while (epsilon.r < 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)]; - + sample.i = z.i + step_i * (epsilon.i / state->samples); + sample.r = z.r + step_r * (epsilon.r / state->samples); + tmp = state->palette[state->func(state, sample)]; r += tmp.rgb.r; g += tmp.rgb.g; b += tmp.rgb.b; - v += 1.0; + epsilon.r += 1.0; } - u += 1.0; + epsilon.i += 1.0; } - int s = (int)(state->samples * state->samples); + 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) +static void *st_render_routine(void *arg) { - int j; - t_complex z; - t_state *state; - int offset; - double step_r; - t_render_routine_arg *arg; + int j; + t_complex z; + t_state *state; + double step_r; - arg = void_arg; - state = arg->state; - z = arg->z; - offset = arg->offset; - j = -1; - step_r = state->plane.r / (double)WINDOW_WIDTH; + state = ((t_render_routine_arg*)arg)->state; + z.i = ((t_render_routine_arg*)arg)->z_i; + step_r = state->plane.r / FWINDOW_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; - } + j = -1; + if (state->samples == 1.0) + while (++j < WINDOW_WIDTH) + { + state->window.data[((t_render_routine_arg*)arg)->offset] = + state->palette[state->func(state, z)]; + z.r += step_r; + ((t_render_routine_arg*)arg)->offset++; + } + else + while (++j < WINDOW_WIDTH) + { + state->window.data[((t_render_routine_arg*)arg)->offset] = + st_take_sample(state, z, step_r, ((t_render_routine_arg*)arg)->step_i); + z.r += step_r; + ((t_render_routine_arg*)arg)->offset++; + } return (NULL); } -static void st_render_fractal(t_state *state) +static void st_render_fractal(t_state *state) { int i; pthread_t threads[WINDOW_HEIGHT]; @@ -88,19 +92,20 @@ static void st_render_fractal(t_state *state) double step_i; double z_i; - step_i = state->plane.i / (double)WINDOW_HEIGHT; + step_i = state->plane.i / FWINDOW_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].z_i = z_i; routine_args[i].step_i = step_i; - if (pthread_create(threads + i, NULL, st_render_routine, routine_args + i) < 0) + if (pthread_create(threads + i, NULL, + st_render_routine, routine_args + i) < 0) { state->running = false; - break; + break ; } z_i += step_i; } @@ -108,8 +113,11 @@ static void st_render_fractal(t_state *state) pthread_join(threads[i], NULL); } -int render_update(t_state *state) +int render_update(t_state *state) { + char *iterations_str; + char *samples_str; + if (!state->running) { state_destroy(state); @@ -118,14 +126,14 @@ int render_update(t_state *state) 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); + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, + state->window.id, 0, 0); + iterations_str = ft_itoa(state->iterations); + samples_str = ft_itoa((int)state->samples * (int)state->samples); + mlx_string_put(state->mlx_ptr, state->window_ptr, 10, 20, + 0xeeeeee, iterations_str); + mlx_string_put(state->mlx_ptr, state->window_ptr, 10, 30, + 0xeeeeee, samples_str); free(iterations_str); free(samples_str); state->updated = true; -- cgit