diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index a53e3d9..0340476 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -142,29 +142,44 @@ struct BorrowedShaderVars <'a> { fn get_shader_attrs ( shader: &ShaderProgram, - attr_lookup: &Vec <(usize, &str)> + attr_names: &Vec <(usize, &str)> ) -> renderable_model::AttrMap { - let mut v = vec! [None; attr_lookup.iter ().map (|(i, _)| i).max ().unwrap () + 1]; + let mut v = vec! [None; attr_names.iter ().map (|(i, _)| i).max ().unwrap () + 1]; - let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ()); + let attrs = shader.get_attribute_locations (attr_names.iter ().map (|(_, v)| v).copied ()); - for ((i, _), loc) in attr_lookup.iter ().zip (attrs.iter ()) { + for ((i, _), loc) in attr_names.iter ().zip (attrs.iter ()) { v [*i] = *loc; } v } +fn get_shader_uniforms ( + shader: &ShaderProgram, + uni_names: &Vec <(u32, &str)> +) +-> HashMap +{ + let unis = shader.get_uniform_locations (uni_names.iter ().map (|(_, v)| v).copied ()); + + HashMap::from_iter ( + uni_names.iter () + .map (|(key, name)| (*key, *unis.get (*name).unwrap ())) + ) +} + impl ShaderClosure { pub fn new ( program: ShaderProgram, - uniforms: HashMap , - attr_lookup: &Vec <(usize, &str)> + uniform_names: &Vec <(u32, &str)>, + attr_names: &Vec <(usize, &str)> ) -> Self { - let attributes = get_shader_attrs (&program, attr_lookup); + let uniforms = get_shader_uniforms (&program, uniform_names); + let attributes = get_shader_attrs (&program, attr_names); Self { program, uniforms, @@ -226,7 +241,7 @@ fn main () { ShaderProgram::new (&vert_shader, &frag_shader).unwrap () }; - let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({ + let uniform_names = { use uniforms::*; vec! [ (MVP, "mvp"), @@ -236,30 +251,10 @@ fn main () { (MIN_ALBEDO, "min_albedo"), (MIN_BRIGHT, "min_bright"), (TEXTURE, "texture"), - ].into_iter () - }); - - let unis = { - let unis = shader_program.get_uniform_locations (uni_lookup.values ().copied ()); - - let unis: HashMap = HashMap::from_iter ( - uni_lookup.iter () - .map (|(key, name)| (*key, *unis.get (*name).unwrap ())) - ); - unis + ] }; - let shadow_unis = { - let unis = shadow_shader.get_uniform_locations (uni_lookup.values ().copied ()); - - let unis: HashMap = HashMap::from_iter ( - uni_lookup.iter () - .map (|(key, name)| (*key, *unis.get (*name).unwrap ())) - ); - unis - }; - - let attr_lookup: Vec <(_, &str)> = { + let attr_names = { use renderable_model::attributes::*; vec! [ (POS, "attr_pos"), @@ -268,8 +263,8 @@ fn main () { ] }; - let shader_diffuse = ShaderClosure::new (shader_program, unis, &attr_lookup); - let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, &attr_lookup); + let shader_diffuse = ShaderClosure::new (shader_program, &uniform_names, &attr_names); + let shader_shadow = ShaderClosure::new (shadow_shader, &uniform_names, &attr_names); shader_diffuse.with (|shader_vars| { let attrs = shader_vars.attrs;