From c0089dd0567b8d6538360a21f042cbb0913fff99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sat, 30 Jan 2021 20:54:41 -0300 Subject: Output better error messages for shader programs --- src/lib.rs | 61 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 218d5b4..4662f08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,29 +16,6 @@ pub struct Bezier { pub dpy: Poly, } -pub fn to_poly(v: &Vec) -> Poly { - match v.len() { - 0 => Poly::new(vec![0.0]), - 1 => Poly::new(vec![v[0]]), - 2 => Poly::new(vec![v[0], v[1] - v[0]]), - _ => { - let mut pv = Vec::new(); - for i in 0..v.len() - 1 { - pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]])); - } - while pv.len() > 1 { - let mut npv = Vec::new(); - for i in 0..pv.len() - 1 { - let c = &pv[i + 1] - &pv[i]; - npv.push(&pv[i] + &c.shift()); - } - pv = npv; - } - pv[0].clone() - } - } -} - impl Bezier { pub fn new() -> Bezier { Bezier { @@ -169,10 +146,9 @@ impl Bezier { float inside(vec2 pos) {{ float closest = inf; float t = 0.0; - float dstep = 0.0; - while (t < 1.0 - dstep) {{ - dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t))); - float step = t + dstep; + while (t < 1.0) {{ + float dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t))); + float step = min(t + dstep, 1.0); vec2 a = vec2(eval_px(t), eval_py(t)); vec2 b = vec2(eval_px(step), eval_py(step)); float c = pow(length(a - b), 2); @@ -226,6 +202,29 @@ impl Bezier { } } +fn to_poly(v: &Vec) -> Poly { + match v.len() { + 0 => Poly::new(vec![0.0]), + 1 => Poly::new(vec![v[0]]), + 2 => Poly::new(vec![v[0], v[1] - v[0]]), + _ => { + let mut pv = Vec::new(); + for i in 0..v.len() - 1 { + pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]])); + } + while pv.len() > 1 { + let mut npv = Vec::new(); + for i in 0..pv.len() - 1 { + let c = &pv[i + 1] - &pv[i]; + npv.push(&pv[i] + &c.shift()); + } + pv = npv; + } + pv[0].clone() + } + } +} + fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { unsafe { let shader: GLuint = gl::CreateShader(shader_type); @@ -234,7 +233,13 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { let mut success: gl::types::GLint = 1; gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success); if success == 0 { - println!("Compilation error: {}", source.to_str().unwrap()); + let mut length: GLint = 0; + gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut length); + let mut buffer: Vec = Vec::with_capacity(length as usize + 1); + buffer.extend([b' '].iter().cycle().take(length as usize)); + let error: CString = CString::from_vec_unchecked(buffer); + gl::GetShaderInfoLog(shader, length, std::ptr::null_mut(), error.as_ptr() as *mut GLchar); + println!("Compilation error: {}", error.to_str().unwrap()); } shader } -- cgit v1.2.3