From 8b4b79500a7673be388214386b5be239b87efa35 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Wed, 4 Mar 2020 08:29:11 -0600 Subject: dynamic sin wave --- src/tom.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'src') 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> { 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 { + let mut vertices: Vec = 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>, + border: Vec>, + wave: Vec, + last_input: f32, tessalations: Vec, } 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(&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 } -- cgit v1.2.3