-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathp_4_1_2_01.rs
98 lines (88 loc) · 3.08 KB
/
p_4_1_2_01.rs
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
// P_4_1_2_01
//
// Generative Gestaltung – Creative Coding im Web
// ISBN: 978-3-87439-902-9, First Edition, Hermann Schmidt, Mainz, 2018
// Benedikt Groß, Hartmut Bohnacker, Julia Laub, Claudius Lazzeroni
// with contributions by Joey Lee and Niels Poldervaart
// Copyright 2018
//
// http://www.generative-gestaltung.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* image feedback process.
*
* KEYS
* del, backspace : clear screen
* s : save png
*/
use nannou::prelude::*;
fn main() {
nannou::app(model).run();
}
struct Model {
// Store the window ID so we can refer to this specific window later if needed.
texture: wgpu::Texture,
}
fn model(app: &App) -> Model {
// Create a new window! Store the ID so we can refer to it later.
app.new_window()
.size(1024, 780)
.view(view)
.key_released(key_released)
.build()
.unwrap();
// Load the image from disk and upload it to a GPU texture.
let assets = app.assets_path().unwrap();
let img_path = assets
.join("images")
.join("generative_examples")
.join("p_4_1_2_01.png");
let texture = wgpu::Texture::from_path(app, img_path).unwrap();
Model { texture }
}
// Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model, frame: Frame) {
let win = app.window_rect();
let x1 = random_range(win.left(), win.right());
let y1 = 0.0;
let x2 = x1 + random_range(-7.0, 7.0);
let y2 = y1 + random_range(-5.0, 5.0);
let w = random_range(10.0, 40.0);
let h = win.h();
let area = geom::Rect::from_x_y_w_h(
map_range(x1, win.left(), win.right(), 0.0, 1.0),
map_range(y1, win.bottom(), win.top(), 0.0, 1.0),
map_range(w, 0.0, win.w(), 0.0, 1.0),
map_range(h, 0.0, win.h(), 0.0, 1.0),
);
// Don't interpolate between pixels.model
let sampler = wgpu::SamplerBuilder::new()
.min_filter(wgpu::FilterMode::Nearest)
.mag_filter(wgpu::FilterMode::Nearest)
.into_descriptor();
let draw = app.draw().sampler(sampler);
if frame.nth() == 0 || app.keys.down.contains(&Key::Delete) {
frame.clear(WHITE);
draw.texture(&model.texture);
} else {
draw.texture(&frame.resolve_target().unwrap_or(frame.texture_view()))
.x_y(x2, y2)
.w_h(w, h)
.area(area);
}
draw.to_frame(app, &frame).unwrap();
}
fn key_released(app: &App, _model: &mut Model, key: Key) {
if key == Key::S {
app.main_window()
.capture_frame(app.exe_name().unwrap() + ".png");
}
}