🚧 checkpoint

main
_ 2020-03-08 04:40:15 +00:00
parent 07d925dc1e
commit cd4a66d764
2 changed files with 20 additions and 27 deletions

View File

@ -208,14 +208,14 @@ fn main () {
ShaderProgram::new (&vert_shader, &frag_shader).unwrap () ShaderProgram::new (&vert_shader, &frag_shader).unwrap ()
}; };
let attr_lookup: HashMap <_, &str> = HashMap::from_iter ({ let attr_lookup: Vec <(_, &str)> = {
use renderable_model::attributes::*; use renderable_model::attributes::*;
vec! [ vec! [
(POS, "pos"), (POS, "attr_pos"),
(UV, "uv"), (UV, "attr_uv"),
(NORMAL, "normal"), (NORMAL, "attr_normal"),
].into_iter () ]
}); };
let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({ let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({
use uniforms::*; use uniforms::*;
@ -230,23 +230,17 @@ fn main () {
].into_iter () ].into_iter ()
}); });
let attrs = { let get_shader_attrs = |shader: &ShaderProgram| {
let attrs = shader_program.get_attribute_locations (attr_lookup.values ().copied ()); let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ());
HashMap::from_iter ( HashMap::from_iter (
attr_lookup.iter () attr_lookup.iter ().enumerate ()
.map (|(key, name)| (*key, *attrs.get (*name).unwrap ())) .map (|(i, _)| (i, attrs [i]))
) )
}; };
let shadow_attrs = { let attrs = get_shader_attrs (&shader_program);
let attrs = shadow_shader.get_attribute_locations (attr_lookup.values ().copied ()); let shadow_attrs = get_shader_attrs (&shadow_shader);
HashMap::from_iter (
attr_lookup.iter ()
.map (|(key, name)| (*key, *attrs.get (*name).unwrap ()))
)
};
let unis = { let unis = {
let unis = shader_program.get_uniform_locations (uni_lookup.values ().copied ()); let unis = shader_program.get_uniform_locations (uni_lookup.values ().copied ());

View File

@ -164,27 +164,26 @@ impl ShaderProgram {
} }
} }
// This could be an iterator adapter but the code is too long
// and it's only called during startup so I don't care.
pub fn get_attribute_locations <'a, I> (&self, names: I) pub fn get_attribute_locations <'a, I> (&self, names: I)
-> HashMap <String, Option <u32>> -> Vec <Option <u32>>
where I: Iterator <Item = &'a str> where I: Iterator <Item = &'a str>
{ {
self.use_program (); self.use_program ();
names names
.map (|name| { .map (|name| {
let mut s = String::from ("attr_"); let c_str = CString::new (name.as_bytes ()).unwrap ();
s.push_str (name);
let c_str = CString::new (s.as_bytes ()).unwrap ();
let loc = unsafe { let loc = unsafe {
gl::GetAttribLocation (self.id, c_str.as_ptr ()) gl::GetAttribLocation (self.id, c_str.as_ptr ())
}; };
let loc = match loc.try_into () {
match loc.try_into () {
Ok (loc) => Some (loc), Ok (loc) => Some (loc),
_ => None, _ => None,
}; }
(String::from (name), loc)
}) })
.collect () .collect ()
} }