From e751288dee6530ca087d7dedf2da6e80e25dd814 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 8 Mar 2020 05:06:19 +0000 Subject: [PATCH] :recycle: --- src/bin/pumpkin.rs | 64 ++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index d158c32..a53e3d9 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -140,13 +140,31 @@ struct BorrowedShaderVars <'a> { attrs: &'a renderable_model::AttrMap, } +fn get_shader_attrs ( + shader: &ShaderProgram, + attr_lookup: &Vec <(usize, &str)> +) +-> renderable_model::AttrMap +{ + let mut v = vec! [None; attr_lookup.iter ().map (|(i, _)| i).max ().unwrap () + 1]; + + let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ()); + + for ((i, _), loc) in attr_lookup.iter ().zip (attrs.iter ()) { + v [*i] = *loc; + } + + v +} + impl ShaderClosure { pub fn new ( program: ShaderProgram, uniforms: HashMap , - attributes: renderable_model::AttrMap + attr_lookup: &Vec <(usize, &str)> ) -> Self { + let attributes = get_shader_attrs (&program, attr_lookup); Self { program, uniforms, @@ -208,15 +226,6 @@ fn main () { ShaderProgram::new (&vert_shader, &frag_shader).unwrap () }; - let attr_lookup: Vec <(_, &str)> = { - use renderable_model::attributes::*; - vec! [ - (POS, "attr_pos"), - (UV, "attr_uv"), - (NORMAL, "attr_normal"), - ] - }; - let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({ use uniforms::*; vec! [ @@ -230,21 +239,6 @@ fn main () { ].into_iter () }); - let get_shader_attrs = |shader: &ShaderProgram| { - let mut v = vec! [None; attr_lookup.iter ().map (|(i, _)| i).max ().unwrap () + 1]; - - let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ()); - - for ((i, _), loc) in attr_lookup.iter ().zip (attrs.iter ()) { - v [*i] = *loc; - } - - v - }; - - let attrs = get_shader_attrs (&shader_program); - let shadow_attrs = get_shader_attrs (&shadow_shader); - let unis = { let unis = shader_program.get_uniform_locations (uni_lookup.values ().copied ()); @@ -265,15 +259,25 @@ fn main () { unis }; - { + let attr_lookup: Vec <(_, &str)> = { + use renderable_model::attributes::*; + vec! [ + (POS, "attr_pos"), + (UV, "attr_uv"), + (NORMAL, "attr_normal"), + ] + }; + + let shader_diffuse = ShaderClosure::new (shader_program, unis, &attr_lookup); + let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, &attr_lookup); + + shader_diffuse.with (|shader_vars| { + let attrs = shader_vars.attrs; use renderable_model::attributes::*; glezz::enable_vertex_attrib_array (attrs [POS]); glezz::enable_vertex_attrib_array (attrs [UV]); glezz::enable_vertex_attrib_array (attrs [NORMAL]); - } - - let shader_diffuse = ShaderClosure::new (shader_program, unis, attrs); - let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, shadow_attrs); + }); let texture = Texture::from_file ("sky.png"); texture.bind ();