summaryrefslogtreecommitdiff
path: root/src/tom.rs
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2020-03-04 08:29:11 -0600
committerTom Barrett <tom@tombarrett.xyz>2020-03-04 08:29:11 -0600
commit8b4b79500a7673be388214386b5be239b87efa35 (patch)
tree8bbd131b3ac17b6bb3b2cb77ff08deaa1bd0dcd7 /src/tom.rs
parentc7d13e2721482772c12b44773777c5902f25848c (diff)
dynamic sin wave
Diffstat (limited to 'src/tom.rs')
-rw-r--r--src/tom.rs48
1 files changed, 44 insertions, 4 deletions
diff --git a/src/tom.rs b/src/tom.rs
index b4e89f7..b899b08 100644
--- a/src/tom.rs
+++ b/src/tom.rs
@@ -1,3 +1,5 @@
+use std::f32::consts::PI;
+
use luminance::context::GraphicsContext;
use luminance::framebuffer::Framebuffer;
use luminance::pipeline::PipelineState;
@@ -49,26 +51,56 @@ fn gen_border() -> Vec<Vec<Vertex>> {
vertices
}
+fn relative(x: f32, in_min: f32, in_max: f32, out_min: f32, out_max: f32) -> f32 {
+ (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
+}
+
+fn gen_sin(start: f32) -> Vec<Vertex> {
+ let mut vertices: Vec<Vertex> = Vec::new();
+
+ let end = start + (2.0 * PI);
+ let mut x = start;
+ while x < end {
+ vertices.push(Vertex {
+ position: VertexPosition::new([relative(x, start, end, -0.9, 0.9), x.sin() * 0.9]),
+ color: VertexRGB::new(constants::C64_GREEN),
+ });
+
+ x += 0.4;
+ }
+
+ vertices
+}
+
#[derive(Default)]
pub struct Tom {
- vertices: Vec<Vec<Vertex>>,
+ border: Vec<Vec<Vertex>>,
+ wave: Vec<Vertex>,
+ last_input: f32,
tessalations: Vec<Tess>,
}
impl Tom {
pub fn new() -> Tom {
- let vertices = gen_border();
+ let border = gen_border();
+ let wave = gen_sin(0.0);
let tessalations = Vec::new();
Tom {
- vertices,
+ border,
+ wave,
+ last_input: 0.0,
tessalations,
}
}
pub fn update<T: GraphicsContext>(&mut self, mut surface: T) -> T {
self.tessalations.clear();
- for vertices in self.vertices.iter() {
+
+ self.last_input += 0.1;
+ self.wave = gen_sin(self.last_input);
+
+ for vertices in self.border.iter() {
self.tessalations.push(
TessBuilder::new(&mut surface)
.add_vertices(vertices)
@@ -78,6 +110,14 @@ impl Tom {
);
}
+ self.tessalations.push(
+ TessBuilder::new(&mut surface)
+ .add_vertices(&self.wave)
+ .set_mode(Mode::LineStrip)
+ .build()
+ .unwrap(),
+ );
+
surface
}