♻️ extract get_uniform_locations

main
_ 2020-02-16 23:14:28 +00:00
parent 358e139c9c
commit 88e58d5677
2 changed files with 27 additions and 15 deletions

View File

@ -238,26 +238,14 @@ fn main () {
let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap ();
let unis: HashMap <_, _> = vec! [
let unis = shader_program.get_uniform_locations (vec! [
"mvp",
"object_space_light",
"albedo",
"min_albedo",
"min_bright",
"texture",
].iter ()
.map (|name| {
let mut s = String::from ("uni_");
s.push_str (name);
let c_str = CString::new (s.as_bytes ()).unwrap ();
let loc = shader_program.get_uniform_location (&c_str);
println! ("Uni {} --> {}", name, loc);
(String::from (*name), loc)
})
.collect ();
].into_iter ());
let attrs: HashMap <_, Option <u32>> = vec! [
"pos",

View File

@ -1,5 +1,6 @@
use std::collections::*;
use std::convert::TryInto;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
pub struct ShaderObject {
id: u32,
@ -116,6 +117,29 @@ impl ShaderProgram {
}
}
pub fn get_uniform_locations <'a, I> (&self, names: I)
-> HashMap <String, i32>
where I: Iterator <Item = &'a str>
{
unsafe {
gl::UseProgram (self.id);
}
names
.map (|name| {
let mut s = String::from ("uni_");
s.push_str (name);
let c_str = CString::new (s.as_bytes ()).unwrap ();
let loc = unsafe {
gl::GetUniformLocation (self.id, c_str.as_ptr ())
};
(String::from (name), loc)
})
.collect ()
}
pub fn get_attribute_location (&self, name: &CStr) -> i32 {
unsafe {
gl::UseProgram (self.id);