From 444181ff1a47ff87d1b8db8e0300eb17698b2b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 17 Jan 2021 15:39:20 -0300 Subject: Render hardcoded bezier curve --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index aa149e3..5768622 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,7 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { gl::ShaderSource(shader, 1, &source.as_ptr(), std::ptr::null()); gl::CompileShader(shader); let mut success: gl::types::GLint = 1; - unsafe { - gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success); - } + gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success); if success == 0 { println!("Compilation error: {}", source.to_str().unwrap()); } @@ -34,8 +32,8 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { } fn main() { - let a = Lerp::new(vec![2.0, -2.0, 2.0]); - let b = Lerp::new(vec![-1.0, 1.0, 2.0]); + 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); @@ -48,8 +46,8 @@ fn main() { gl_attr.set_context_profile(sdl2::video::GLProfile::Core); gl_attr.set_context_version(4, 1); - let window_w: u32 = 800; - let window_h: u32 = 600; + let window_w: u32 = 1920; + let window_h: u32 = 1080; let window = video_subsystem .window("bezier", window_w, window_h) @@ -87,12 +85,40 @@ fn main() { ".to_vec(); let fragment_shader_source = b" - #version 410 core - out vec4 FragColor; +#version 410 core +out vec4 FragColor; - void main() { - FragColor = vec4(0.4, 0.8, 0.2, 1.0); +float p1[3] = float[3](200, 400, 1300); +float p2[3] = float[3](100, 900, -970); +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 res = 100; + float thr = 32; + float closest = 10000; + float t = 0.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 n = vec2(-(b - a).y, (b - a).x); + float d = dot(a - pos, n) / length(n); + if (d < closest) { + closest = d; } + t = step; + } + return 1 - pow(closest / thr, 2); +} + +void main() { + float r = inside(gl_FragCoord.xy); + FragColor = vec4(r * 0.8, r * 0.2, r, 1.0); +} ".to_vec(); let mut vbo: GLuint = 0; -- cgit v1.2.3