♻️ Extract get_attribute_locations

main
_ 2020-02-16 23:18:23 +00:00
parent 22e15530bf
commit 94f963bca6
2 changed files with 29 additions and 19 deletions

View File

@ -247,28 +247,11 @@ fn main () {
"texture", "texture",
].into_iter ()); ].into_iter ());
let attrs: HashMap <_, Option <u32>> = vec! [ let attrs = shader_program.get_attribute_locations (vec! [
"pos", "pos",
"uv", "uv",
"normal", "normal",
].iter () ].into_iter ());
.map (|name| {
let mut s = String::from ("attr_");
s.push_str (name);
let c_str = CString::new (s.as_bytes ()).unwrap ();
let loc = shader_program.get_attribute_location (&c_str);
let loc = match loc.try_into () {
Ok (i) => Some (i),
_ => {
println! ("Attribute {} not found - Optimized out?", name);
None
},
};
(String::from (*name), loc)
})
.collect ();
let texture = Texture::from_file ("sky.png"); let texture = Texture::from_file ("sky.png");
texture.bind (); texture.bind ();

View File

@ -146,6 +146,33 @@ impl ShaderProgram {
gl::GetAttribLocation (self.id, name.as_ptr ()) gl::GetAttribLocation (self.id, name.as_ptr ())
} }
} }
pub fn get_attribute_locations <'a, I> (&self, names: I)
-> HashMap <String, Option <u32>>
where I: Iterator <Item = &'a str>
{
unsafe {
gl::UseProgram (self.id);
}
names
.map (|name| {
let mut s = String::from ("attr_");
s.push_str (name);
let c_str = CString::new (s.as_bytes ()).unwrap ();
let loc = unsafe {
gl::GetAttribLocation (self.id, c_str.as_ptr ())
};
let loc = match loc.try_into () {
Ok (loc) => Some (loc),
_ => None,
};
(String::from (name), loc)
})
.collect ()
}
} }
impl Drop for ShaderProgram { impl Drop for ShaderProgram {