From 5698a5ac7303b128f13e9b656e8446d24a2c1f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 17 Jan 2021 17:29:38 -0300 Subject: Fix artifacts when curve crosses itself --- src/main.rs | 66 +++++++++++++++++++++++++++++++++++++++---------------------- src/poly.rs | 2 +- 2 files changed, 43 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index becb510..242a1ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,7 @@ mod poly; use lerp::Lerp; -use std::os::raw::c_void; -use std::ffi::{CString, CStr}; +use std::ffi::{CStr, CString}; use sdl2::event::Event; use sdl2::keyboard::Keycode; @@ -27,8 +26,8 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { } fn main() { - 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 a = lerp::lp(Lerp::new(vec![100.0, 200.0, 800.0, 400.0, 800.0, 100.0])); + let b = lerp::lp(Lerp::new(vec![100.0, 550.0, 500.0, 300.0, 200.0, 800.0])); let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -48,7 +47,7 @@ fn main() { .build() .unwrap(); - let gl_context = window.gl_create_context().unwrap(); + let _gl_context = window.gl_create_context().unwrap(); gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const std::os::raw::c_void); @@ -57,14 +56,8 @@ fn main() { } let vertices: [f32; 18] = [ - // first triangle - 1.0, 1.0, 0.0, - 1.0, -1.0, 0.0, - -1.0, 1.0, 0.0, - // // second triangle - 1.0, -1.0, 0.0, - -1.0, -1.0, 0.0, - -1.0, 1.0, 0.0 + 1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, + 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0, ]; let vertex_shader_source = b" @@ -74,15 +67,16 @@ fn main() { void main() { gl_Position = vec4(aPos.xyz, 1.0); } - ".to_vec(); + " + .to_vec(); - let fragment_shader_source = String::from(format!(" + let fragment_shader_source = String::from(format!( + " #version 410 core out vec4 FragColor; float pa[{sa}] = float[{sa}]({pa}); float pb[{sb}] = float[{sb}]({pb}); -float threshold = 0.1; float eval_a(float[{sa}] v, float t) {{ float result = 0.0; @@ -103,14 +97,14 @@ float eval_b(float[{sb}] v, float t) {{ float inside(vec2 pos) {{ float res = 100; float thr = 32; - float closest = 10000; + float closest = 1000000; float t = 0.0; while (t < 1.0) {{ float step = t + 1.0 / res; 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); + float c = pow(length(a - b), 2); + float d = distance(pos, a + clamp(dot(pos - a, b - a) / c, 0, 1) * (b - a)); if (d < closest) {{ closest = d; }} @@ -123,7 +117,13 @@ void main() {{ float r = inside(gl_FragCoord.xy); FragColor = vec4(r * 0.8, r * 0.2, r, 1.0); }} - ", sa = a.degree() + 1, sb = b.degree() + 1, pa = &a, pb = &b)).into_bytes(); + ", + sa = a.degree() + 1, + sb = b.degree() + 1, + pa = &a, + pb = &b + )) + .into_bytes(); let mut vbo: GLuint = 0; let mut vao: GLuint = 0; @@ -132,15 +132,33 @@ void main() {{ gl::BindVertexArray(vao); gl::GenBuffers(1, &mut vbo); gl::BindBuffer(gl::ARRAY_BUFFER, vbo); - gl::BufferData(gl::ARRAY_BUFFER, (vertices.len() * std::mem::size_of::()) as GLsizeiptr, vertices.as_ptr() as *const GLvoid, gl::STATIC_DRAW); - gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, (3 * std::mem::size_of::()) as GLint, std::ptr::null()); + gl::BufferData( + gl::ARRAY_BUFFER, + (vertices.len() * std::mem::size_of::()) as GLsizeiptr, + vertices.as_ptr() as *const GLvoid, + gl::STATIC_DRAW, + ); + gl::VertexAttribPointer( + 0, + 3, + gl::FLOAT, + gl::FALSE, + (3 * std::mem::size_of::()) as GLint, + std::ptr::null(), + ); gl::EnableVertexAttribArray(0); } let mut shader_program: GLuint = 0; unsafe { - let vertex_shader = shader_from_source(&CString::from_vec_unchecked(vertex_shader_source), gl::VERTEX_SHADER); - let fragment_shader = shader_from_source(&CString::from_vec_unchecked(fragment_shader_source), gl::FRAGMENT_SHADER); + let vertex_shader = shader_from_source( + &CString::from_vec_unchecked(vertex_shader_source), + gl::VERTEX_SHADER, + ); + let fragment_shader = shader_from_source( + &CString::from_vec_unchecked(fragment_shader_source), + gl::FRAGMENT_SHADER, + ); shader_program = gl::CreateProgram(); gl::AttachShader(shader_program, vertex_shader); gl::AttachShader(shader_program, fragment_shader); diff --git a/src/poly.rs b/src/poly.rs index 9c70048..34df7a0 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -2,8 +2,8 @@ mod iter; use crate::number::Number; use iter::Iter; -use std::ops::{Add, Div, Mul, Sub}; use std::fmt; +use std::ops::{Add, Div, Mul, Sub}; #[derive(PartialEq, Debug, Clone)] pub struct Poly { -- cgit v1.2.3