From 00a51efb19fafbf9bd029af16be6214bf2fd12c5 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 8 Mar 2020 05:26:11 +0000 Subject: [PATCH] :recycle: --- src/bin/pumpkin.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index 996f0d9..bcb95a3 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -129,6 +129,15 @@ where P: AsRef RenderableModel::from_iqm (&model) } +fn shader_from_files

(vert: P, frag: P) -> ShaderProgram +where P: AsRef +{ + let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, vert).unwrap (); + let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, frag).unwrap (); + + ShaderProgram::new (&vert_shader, &frag_shader).unwrap () +} + struct ShaderClosure { program: ShaderProgram, uniforms: HashMap , @@ -222,28 +231,6 @@ fn main () { window.gl_make_current (&gl_ctx).unwrap (); - // I love how with Rust I can throw unwrap ()s everywhere - // It's safer than C / C++'s default behavior of unchecked errors - // It's more programmer-friendly and explicit than C#'s unchecked - // exceptions that can appear almost anywhere at runtime with no - // compile-time warning - // And I'm still not actually checking errors - Just checkmarking - // that I know where they are. - - let shader_program = { - let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, "shaders/pumpkin-vert.glsl").unwrap (); - let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, "shaders/pumpkin-frag.glsl").unwrap (); - - ShaderProgram::new (&vert_shader, &frag_shader).unwrap () - }; - - let shadow_shader = { - let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, "shaders/shadow-vert.glsl").unwrap (); - let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, "shaders/shadow-frag.glsl").unwrap (); - - ShaderProgram::new (&vert_shader, &frag_shader).unwrap () - }; - let uniform_names = { use uniforms::*; vec! [ @@ -266,8 +253,8 @@ fn main () { ] }; - let shader_diffuse = ShaderClosure::new (shader_program, &uniform_names, &attr_names); - let shader_shadow = ShaderClosure::new (shadow_shader, &uniform_names, &attr_names); + let shader_diffuse = ShaderClosure::new (shader_from_files ("shaders/pumpkin-vert.glsl", "shaders/pumpkin-frag.glsl"), &uniform_names, &attr_names); + let shader_shadow = ShaderClosure::new (shader_from_files ("shaders/shadow-vert.glsl", "shaders/shadow-frag.glsl"), &uniform_names, &attr_names); shader_diffuse.with (|shader_vars| { let attrs = shader_vars.attrs;