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/main.rs | |
parent | 444181ff1a47ff87d1b8db8e0300eb17698b2b21 (diff) | |
download | bezier-7b2524be8efae04a80691743b06018a6b0fdbe3e.tar.gz bezier-7b2524be8efae04a80691743b06018a6b0fdbe3e.zip |
Pass external data to the fragment shader
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 60 |
1 files changed, 32 insertions, 28 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; |