Skip to content

Commit

Permalink
Merge pull request #629 from JoshuaBatty/develop
Browse files Browse the repository at this point in the history
fixes m_1_3_02 and m_1_3_03 generative examples
  • Loading branch information
JoshuaBatty authored Jul 14, 2020
2 parents e3c26c9 + cb00202 commit b127d30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
15 changes: 4 additions & 11 deletions generative_design/random_and_noise/m_1_3_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn model(app: &App) -> Model {
let win = window.rect();
let texture = wgpu::TextureBuilder::new()
.size([win.w() as u32, win.h() as u32])
.format(Frame::TEXTURE_FORMAT)
.format(wgpu::TextureFormat::Rgba8Unorm)
.usage(wgpu::TextureUsage::COPY_DST | wgpu::TextureUsage::SAMPLED)
.build(window.swap_chain_device());
Model {
Expand All @@ -70,16 +70,15 @@ fn view(app: &App, model: &Model, frame: Frame) {
let mut rng = SmallRng::seed_from_u64(model.act_random_seed);

let image = image::ImageBuffer::from_fn(win.w() as u32, win.h() as u32, |_x, _y| {
let r: u16 = rng.gen_range(0, std::u16::MAX);
nannou::image::Rgba([r, r, r, std::u16::MAX])
let r: u8 = rng.gen_range(0, std::u8::MAX);
nannou::image::Rgba([r, r, r, std::u8::MAX])
});

let flat_samples = image.as_flat_samples();
let img_bytes = slice_as_bytes(flat_samples.as_slice());
model.texture.upload_data(
app.main_window().swap_chain_device(),
&mut *frame.command_encoder(),
img_bytes,
&flat_samples.as_slice(),
);

let draw = app.draw();
Expand All @@ -89,12 +88,6 @@ fn view(app: &App, model: &Model, frame: Frame) {
draw.to_frame(app, &frame).unwrap();
}

fn slice_as_bytes(s: &[u16]) -> &[u8] {
let len = s.len() * std::mem::size_of::<u16>();
let ptr = s.as_ptr() as *const u8;
unsafe { std::slice::from_raw_parts(ptr, len) }
}

fn mouse_pressed(_app: &App, model: &mut Model, _button: MouseButton) {
model.act_random_seed = (random_f32() * 100000.0) as u64;
}
Expand Down
21 changes: 7 additions & 14 deletions generative_design/random_and_noise/m_1_3_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn model(app: &App) -> Model {
let win = window.rect();
let texture = wgpu::TextureBuilder::new()
.size([win.w() as u32, win.h() as u32])
.format(Frame::TEXTURE_FORMAT)
.format(wgpu::TextureFormat::Rgba8Unorm)
.usage(wgpu::TextureUsage::COPY_DST | wgpu::TextureUsage::SAMPLED)
.build(window.swap_chain_device());
Model {
Expand Down Expand Up @@ -96,28 +96,27 @@ fn view(app: &App, model: &Model, frame: Frame) {
1.0,
-1.0,
0.0,
std::u16::MAX as f64,
std::u8::MAX as f64,
);
} else if model.noise_mode == 2 {
let n = map_range(
noise.get([noise_x, noise_y]),
-1.0,
1.0,
0.0,
std::u16::MAX as f64 / 10.0,
std::u8::MAX as f64 / 10.0,
);
noise_value = (n - n.floor()) * std::u16::MAX as f64;
noise_value = (n - n.floor()) * std::u8::MAX as f64;
}
let n = noise_value as u16;
nannou::image::Rgba([n, n, n, std::u16::MAX])
let n = noise_value as u8;
nannou::image::Rgba([n, n, n, std::u8::MAX])
});

let flat_samples = image.as_flat_samples();
let img_bytes = slice_as_bytes(flat_samples.as_slice());
model.texture.upload_data(
app.main_window().swap_chain_device(),
&mut *frame.command_encoder(),
img_bytes,
&flat_samples.as_slice(),
);

let draw = app.draw();
Expand All @@ -127,12 +126,6 @@ fn view(app: &App, model: &Model, frame: Frame) {
draw.to_frame(app, &frame).unwrap();
}

fn slice_as_bytes(s: &[u16]) -> &[u8] {
let len = s.len() * std::mem::size_of::<u16>();
let ptr = s.as_ptr() as *const u8;
unsafe { std::slice::from_raw_parts(ptr, len) }
}

fn key_released(app: &App, model: &mut Model, key: Key) {
match key {
Key::S => {
Expand Down

0 comments on commit b127d30

Please sign in to comment.