chore: add colours present in olc's video on mandelbrot
This commit is contained in:
parent
10c789f569
commit
256aee7503
|
@ -138,13 +138,9 @@ fn mandelbrot_render_system(
|
||||||
let y = ((-1.0 * z) - v, (1.0 * z) - v);
|
let y = ((-1.0 * z) - v, (1.0 * z) - v);
|
||||||
let bounds = Bounds::new(x, y);
|
let bounds = Bounds::new(x, y);
|
||||||
|
|
||||||
let start = Instant::now();
|
|
||||||
|
|
||||||
texture
|
texture
|
||||||
.data
|
.data
|
||||||
.copy_from_slice(fractal.scaled_image(bounds, limit));
|
.copy_from_slice(fractal.scaled_image(bounds, limit));
|
||||||
|
|
||||||
let _diff = Instant::now() - start;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,50 +65,54 @@ impl Default for Mandelbrot {
|
||||||
impl Mandelbrot {
|
impl Mandelbrot {
|
||||||
pub fn image(&mut self) -> &[u8] {
|
pub fn image(&mut self) -> &[u8] {
|
||||||
let limit = DEFAULT_ITERATION_LIMIT as f64;
|
let limit = DEFAULT_ITERATION_LIMIT as f64;
|
||||||
let error = f64::EPSILON;
|
|
||||||
|
|
||||||
self.frame_buffer
|
self.frame_buffer
|
||||||
.par_chunks_mut(4)
|
.par_chunks_mut(4)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.for_each(|(i, buf)| {
|
.for_each(|(i, buf)| {
|
||||||
let iters = Self::escape_time(i, DEFAULT_BOUNDS, DEFAULT_ITERATION_LIMIT);
|
let iters = Self::escape_time(i, DEFAULT_BOUNDS, DEFAULT_ITERATION_LIMIT);
|
||||||
let normalized = iters / limit;
|
|
||||||
|
|
||||||
let h = normalized * 350.0;
|
buf.copy_from_slice(&Self::default_colours(iters, limit))
|
||||||
let v = if (iters - limit).abs() < error {
|
|
||||||
0.0
|
|
||||||
} else {
|
|
||||||
1.0
|
|
||||||
};
|
|
||||||
|
|
||||||
buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.frame_buffer.as_ref()
|
self.frame_buffer.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scaled_image(&mut self, bounds: Bounds, limit: u32) -> &[u8] {
|
pub fn scaled_image(&mut self, bounds: Bounds, limit: u32) -> &[u8] {
|
||||||
let error = f64::EPSILON;
|
|
||||||
|
|
||||||
self.frame_buffer
|
self.frame_buffer
|
||||||
.par_chunks_mut(4)
|
.par_chunks_mut(4)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.for_each(|(i, buf)| {
|
.for_each(|(i, buf)| {
|
||||||
let iters = Self::escape_time(i, bounds, limit);
|
let iters = Self::escape_time(i, bounds, limit);
|
||||||
let limit = limit as f64;
|
let limit = limit as f64;
|
||||||
let normalized = iters / limit;
|
|
||||||
|
|
||||||
let h = normalized * 350.0;
|
buf.copy_from_slice(&Self::default_colours(iters, limit))
|
||||||
let v = if (iters - limit).abs() < error {
|
});
|
||||||
|
|
||||||
|
self.frame_buffer.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn olc_colours(iters: f64) -> [u8; 4] {
|
||||||
|
let a = 0.1;
|
||||||
|
|
||||||
|
let red = (0.5 * (a * iters).sin() + 0.5) * 255.0;
|
||||||
|
let green = (0.5 * (a * iters + 2.094).sin() + 0.5) * 255.0;
|
||||||
|
let blue = (0.5 * (a * iters + 4.188).sin() + 0.5) * 255.0;
|
||||||
|
|
||||||
|
[red as u8, green as u8, blue as u8, 0xFF]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_colours(iters: f64, limit: f64) -> [u8; 4] {
|
||||||
|
let normal = iters / limit;
|
||||||
|
|
||||||
|
let h = normal * 350.0;
|
||||||
|
let v = if (iters - limit).abs() < f64::EPSILON {
|
||||||
0.0
|
0.0
|
||||||
} else {
|
} else {
|
||||||
1.0
|
1.0
|
||||||
};
|
};
|
||||||
|
|
||||||
buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v))
|
Self::hsv_to_rgb(h, 1.0, v)
|
||||||
});
|
|
||||||
|
|
||||||
self.frame_buffer.as_ref()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn escape_time(i: usize, bounds: Bounds, limit: u32) -> f64 {
|
fn escape_time(i: usize, bounds: Bounds, limit: u32) -> f64 {
|
||||||
|
@ -118,7 +122,6 @@ impl Mandelbrot {
|
||||||
Self::count_iterations(c, limit)
|
Self::count_iterations(c, limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn hsv_to_rgb(h: f64, s: f64, v: f64) -> [u8; 4] {
|
fn hsv_to_rgb(h: f64, s: f64, v: f64) -> [u8; 4] {
|
||||||
let c = v * s;
|
let c = v * s;
|
||||||
let x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs());
|
let x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs());
|
||||||
|
@ -167,6 +170,8 @@ impl Mandelbrot {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For a performance booth (with the consequence of a less smooth gradient)
|
||||||
|
// just return count instead of doing all this math on it
|
||||||
if count < limit {
|
if count < limit {
|
||||||
(count as f64 + 1.0) - z.norm().ln().ln() / 2f64.ln()
|
(count as f64 + 1.0) - z.norm().ln().ln() / 2f64.ln()
|
||||||
} else {
|
} else {
|
||||||
|
@ -174,7 +179,6 @@ impl Mandelbrot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn scale_width(x: usize, bounds: (f64, f64)) -> f64 {
|
fn scale_width(x: usize, bounds: (f64, f64)) -> f64 {
|
||||||
// const X_MIN: f64 = -2.5;
|
// const X_MIN: f64 = -2.5;
|
||||||
// const X_MAX: f64 = 1.0;
|
// const X_MAX: f64 = 1.0;
|
||||||
|
@ -182,7 +186,6 @@ impl Mandelbrot {
|
||||||
bounds.0 + ((bounds.1 - bounds.0) * (x as f64 - 0.0)) / TEXTURE_WIDTH as f64 - 0.0
|
bounds.0 + ((bounds.1 - bounds.0) * (x as f64 - 0.0)) / TEXTURE_WIDTH as f64 - 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn scale_height(y: usize, bounds: (f64, f64)) -> f64 {
|
fn scale_height(y: usize, bounds: (f64, f64)) -> f64 {
|
||||||
// const Y_MIN: f64 = -1.0;
|
// const Y_MIN: f64 = -1.0;
|
||||||
// const Y_MAX: f64 = 1.0;
|
// const Y_MAX: f64 = 1.0;
|
||||||
|
|
Loading…
Reference in New Issue