diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-17 16:14:08 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-17 16:14:08 -0300 |
commit | 7b2524be8efae04a80691743b06018a6b0fdbe3e (patch) | |
tree | 6952e47fdac5f0ce4afbf4eb4e25227918783812 /src | |
parent | 444181ff1a47ff87d1b8db8e0300eb17698b2b21 (diff) | |
download | bezier-7b2524be8efae04a80691743b06018a6b0fdbe3e.tar.gz bezier-7b2524be8efae04a80691743b06018a6b0fdbe3e.zip |
Pass external data to the fragment shader
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 60 | ||||
-rw-r--r-- | src/poly.rs | 27 |
2 files changed, 44 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs index 5768622..becb510 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,17 +3,12 @@ mod number; mod poly; use lerp::Lerp; -use number::Number; -use std::thread; -use std::time::Duration; use std::os::raw::c_void; use std::ffi::{CString, CStr}; use sdl2::event::Event; use sdl2::keyboard::Keycode; -use sdl2::pixels::Color; -use sdl2::rect::Rect; use gl::types::*; @@ -32,11 +27,8 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { } fn main() { - let a = Lerp::new(vec![200.0, 200.0, 200.0]); - let b = Lerp::new(vec![100.0, 550.0, 1000.0]); - let pa = lerp::lp(a); - let pb = lerp::lp(b); - let p = poly::gcd(&pa, &pb); + let a = lerp::lp(Lerp::new(vec![100.0, 200.0, 800.0])); + let b = lerp::lp(Lerp::new(vec![100.0, 550.0, 500.0])); let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -84,42 +76,54 @@ fn main() { } ".to_vec(); - let fragment_shader_source = b" + let fragment_shader_source = String::from(format!(" #version 410 core out vec4 FragColor; -float p1[3] = float[3](200, 400, 1300); -float p2[3] = float[3](100, 900, -970); +float pa[{sa}] = float[{sa}]({pa}); +float pb[{sb}] = float[{sb}]({pb}); float threshold = 0.1; -float eval(float[3] v, float t) { - return v[2] * t * t + v[1] * t + v[0]; -} - -float inside(vec2 pos) { +float eval_a(float[{sa}] v, float t) {{ + float result = 0.0; + for (int i = 0; i < {sa}; i++) {{ + result += v[i] * pow(t, i); + }} + return result; +}} + +float eval_b(float[{sb}] v, float t) {{ + float result = 0.0; + for (int i = 0; i < {sb}; i++) {{ + result += v[i] * pow(t, i); + }} + return result; +}} + +float inside(vec2 pos) {{ float res = 100; float thr = 32; float closest = 10000; float t = 0.0; - while (t < 1.0) { + while (t < 1.0) {{ float step = t + 1.0 / res; - vec2 a = vec2(eval(p1, t), eval(p2, t)); - vec2 b = vec2(eval(p1, step), eval(p2, step)); + vec2 a = vec2(eval_a(pa, t), eval_b(pb, t)); + vec2 b = vec2(eval_a(pa, step), eval_b(pb, step)); vec2 n = vec2(-(b - a).y, (b - a).x); float d = dot(a - pos, n) / length(n); - if (d < closest) { + if (d < closest) {{ closest = d; - } + }} t = step; - } + }} return 1 - pow(closest / thr, 2); -} +}} -void main() { +void main() {{ float r = inside(gl_FragCoord.xy); FragColor = vec4(r * 0.8, r * 0.2, r, 1.0); -} - ".to_vec(); +}} + ", sa = a.degree() + 1, sb = b.degree() + 1, pa = &a, pb = &b)).into_bytes(); let mut vbo: GLuint = 0; let mut vao: GLuint = 0; diff --git a/src/poly.rs b/src/poly.rs index 7d964a4..9c70048 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -3,6 +3,7 @@ mod iter; use crate::number::Number; use iter::Iter; use std::ops::{Add, Div, Mul, Sub}; +use std::fmt; #[derive(PartialEq, Debug, Clone)] pub struct Poly { @@ -29,7 +30,7 @@ impl Poly { } } - fn degree(&self) -> usize { + pub fn degree(&self) -> usize { self.degree } @@ -109,21 +110,17 @@ impl Div for &Poly { } } -pub fn gcd(a: &Poly, b: &Poly) -> Poly { - let (_, c) = a / b; - if c.is_zero() { - b.clone() - } else { - gcd(b, &c) - } -} - -pub fn derivative(p: &Poly) -> Poly { - let mut v = vec![0.0; p.degree()]; - for i in 0..v.len() { - v[i] = p.data[i + 1] * (i + 1) as Number; +impl fmt::Display for &Poly { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut s = String::new(); + for i in 0..self.data.len() { + s.push_str(&self.data[i].clone().to_string()); + if i < self.data.len() - 1 { + s.push_str(", "); + } + } + write!(f, "{}", s) } - Poly::new(v) } #[cfg(test)] |