diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-24 13:48:17 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-24 13:48:17 +0100 |
| commit | 8db6918f320b792698f385bb2ea58ddd94cba6fe (patch) | |
| tree | 1df107a1357c11ba4473721b2ab608c842efbdf6 /src | |
| parent | ee4c9685acd70b9b12b22d967c2a45e9a63a714c (diff) | |
| download | fractol-8db6918f320b792698f385bb2ea58ddd94cba6fe.tar.gz fractol-8db6918f320b792698f385bb2ea58ddd94cba6fe.tar.bz2 fractol-8db6918f320b792698f385bb2ea58ddd94cba6fe.zip | |
window_complex position buffer, basic palette, mandelbrot working
Diffstat (limited to 'src')
| -rw-r--r-- | src/fractals/mandelbrot.c | 41 | ||||
| -rw-r--r-- | src/helper.c | 24 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/render.c | 35 | ||||
| -rw-r--r-- | src/state.c | 32 |
5 files changed, 126 insertions, 8 deletions
diff --git a/src/fractals/mandelbrot.c b/src/fractals/mandelbrot.c index e69de29..4211cfa 100644 --- a/src/fractals/mandelbrot.c +++ b/src/fractals/mandelbrot.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mandelbrot.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 11:07:41 by cacharle #+# #+# */ +/* Updated: 2020/02/24 13:46:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +#define MANDEL_MAX_ITERATION 20 +#define MANDEL_ESCAPE_RADIUS_SQUARED 100 + +int mandelbrot(t_complex z) +{ + int n; + double zr; + double zi; + double zr_square; + double zi_square; + + zr = z.r; + zi = z.i; + n = -1; + while (++n < MANDEL_MAX_ITERATION) + { + zi_square = zi * zi; + zr_square = zr * zr; + if (zr_square + zi_square > MANDEL_ESCAPE_RADIUS_SQUARED) + return (n); + zi = 2.0 * zr * zi; + zr = zr_square - zi_square; + zi += z.i; + zr += z.r; + } + return (n); +} diff --git a/src/helper.c b/src/helper.c index e69de29..f3b040b 100644 --- a/src/helper.c +++ b/src/helper.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 11:54:57 by cacharle #+# #+# */ +/* Updated: 2020/02/24 12:17:08 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +void h_offset_to_complex(t_state *state, t_complex *z, int offset) +{ + int y; // unoptimized + int x; + + y = offset / state->window.width; + x = offset % state->window.width; + z->a = y / state->window.height * state->plane.a; + z->b = x / state->window.width * state->plane.b; +} @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:27:44 by cacharle #+# #+# */ -/* Updated: 2020/02/24 10:17:11 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 12:01:02 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/render.c b/src/render.c index ab4fca9..b4ce29c 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ -/* Updated: 2020/02/24 10:44:32 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 13:46:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,9 +28,9 @@ static void st_render_fractal(t_state *state) while (++j < state->window.width) { ((t_color*)state->window.data)[offset] = - state->palette[ - state->func(state->window_complex[offset]) - ]; + state->palette[state->func(state->window_complex[offset])]; + /* printf("%06x\n", ((t_color*)state->window.data)[offset].hexcode); */ + /* printf("%d\n", state->func(state->window_complex[offset])); */ offset++; } } @@ -43,8 +43,33 @@ int render_update(t_state *state) state_destroy(state); exit(EXIT_SUCCESS); } - /* st_render_fractal(state); */ + st_render_fractal(state); mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); return (0); } +void render_update_window_complex(t_state *state) +{ + int i; + int j; + int offset; + + i = -1; + offset = 0; + while (++i < WINDOW_HEIGHT) + { + j = -1; + while (++j < WINDOW_WIDTH) + { + state->window_complex[offset].r = + ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0); + state->window_complex[offset].i = + ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0); + offset++; + /* printf("%f %f\n", state->window_complex[i * WINDOW_WIDTH + j].a, state->window_complex[i * WINDOW_WIDTH + j].b); */ + /* ((double)i / (double)WINDOW_HEIGHT) * state->plane.b - (state->plane.b / 2.0); */ + /* printf("%f\n", (double)i / (double)WINDOW_HEIGHT * state->plane.b- (state->plane.b / 2.0)); */ + /* h_offset_to_complex(state, state->window_complex + i, i); // helper bloat? */ + } + } +} diff --git a/src/state.c b/src/state.c index 4f33041..33aa98b 100644 --- a/src/state.c +++ b/src/state.c @@ -6,16 +6,37 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */ -/* Updated: 2020/02/24 10:43:08 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 12:34:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" +/* #define PALETTE_START 0x000022 */ +/* #define PALETTE_END 0xd62f2f */ +#define PALETTE_START 0x000000 +#define PALETTE_END 0xffffff + static void st_state_init_palette(t_state *state) { + t_color tmp; + int step_r; + int step_g; + int step_b; + int i; - + tmp.hexcode = PALETTE_START; + step_r = ft_abs((PALETTE_END >> 16) - (PALETTE_START >> 16) ) / PALETTE_SIZE; + step_g = ft_abs(((PALETTE_END >> 8) & 0xff) - ((PALETTE_START >> 8) & 0xff)) / PALETTE_SIZE; + step_b = ft_abs((PALETTE_END & 0xff) - (PALETTE_START & 0xff) ) / PALETTE_SIZE; + i = -1; + while (++i < PALETTE_SIZE) + { + state->palette[i] = tmp; + tmp.rgb.r += step_r; + tmp.rgb.g += step_g; + tmp.rgb.b += step_b; + } } int state_init(t_state *state, char *fractal_name) @@ -33,6 +54,13 @@ int state_init(t_state *state, char *fractal_name) state->window.data = mlx_get_data_addr(state->window.id, &state->window.depth, &state->window.size_line, &state->window.endian); state->running = true; + state->func = &mandelbrot; + state->center.a = 0.0; + state->center.b = 0.0; + state->plane.a = 4.0; + state->plane.b = 4.0; + render_update_window_complex(state); + st_state_init_palette(state); return (0); } |
