main
_ 2020-03-08 05:23:15 +00:00
parent fcac505286
commit 6c964c2dd2
2 changed files with 23 additions and 23 deletions

View File

@ -142,7 +142,7 @@ struct BorrowedShaderVars <'a> {
fn get_shader_attrs ( fn get_shader_attrs (
shader: &ShaderProgram, shader: &ShaderProgram,
attr_names: &Vec <(usize, &str)> attr_names: &[(usize, &str)]
) )
-> renderable_model::AttrMap -> renderable_model::AttrMap
{ {
@ -150,8 +150,8 @@ fn get_shader_attrs (
let attrs = shader.get_attribute_locations (attr_names.iter ().map (|(_, v)| v).copied ()); let attrs = shader.get_attribute_locations (attr_names.iter ().map (|(_, v)| v).copied ());
for ((i, _), loc) in attr_names.iter ().zip (attrs.iter ()) { for ((i, _), loc) in attr_names.iter ().zip (attrs.into_iter ()) {
v [*i] = *loc; v [*i] = loc;
} }
v v
@ -159,23 +159,26 @@ fn get_shader_attrs (
fn get_shader_uniforms ( fn get_shader_uniforms (
shader: &ShaderProgram, shader: &ShaderProgram,
uni_names: &Vec <(u32, &str)> uni_names: &[(u32, &str)]
) )
-> HashMap <u32, i32> -> HashMap <u32, i32>
{ {
let unis = shader.get_uniform_locations (uni_names.iter ().map (|(_, v)| v).copied ()); let unis = shader.get_uniform_locations (uni_names.iter ().map (|(_, v)| v).copied ());
HashMap::from_iter ( let pairs =
uni_names.iter () uni_names.iter ().map (|(i, _)| i).copied ()
.map (|(key, name)| (*key, *unis.get (*name).unwrap ())) .zip (unis.into_iter ())
) .filter (|(_, v)| *v >= 0)
;
HashMap::from_iter (pairs)
} }
impl ShaderClosure { impl ShaderClosure {
pub fn new ( pub fn new (
program: ShaderProgram, program: ShaderProgram,
uniform_names: &Vec <(u32, &str)>, uniform_names: &[(u32, &str)],
attr_names: &Vec <(usize, &str)> attr_names: &[(usize, &str)]
) -> Self ) -> Self
{ {
let uniforms = get_shader_uniforms (&program, uniform_names); let uniforms = get_shader_uniforms (&program, uniform_names);
@ -244,13 +247,13 @@ fn main () {
let uniform_names = { let uniform_names = {
use uniforms::*; use uniforms::*;
vec! [ vec! [
(MVP, "mvp"), (MVP, "uni_mvp"),
(OBJECT_SPACE_LIGHT, "object_space_light"), (OBJECT_SPACE_LIGHT, "uni_object_space_light"),
(OBJECT_SPACE_SKY, "object_space_sky"), (OBJECT_SPACE_SKY, "uni_object_space_sky"),
(ALBEDO, "albedo"), (ALBEDO, "uni_albedo"),
(MIN_ALBEDO, "min_albedo"), (MIN_ALBEDO, "uni_min_albedo"),
(MIN_BRIGHT, "min_bright"), (MIN_BRIGHT, "uni_min_bright"),
(TEXTURE, "texture"), (TEXTURE, "uni_texture"),
] ]
}; };

View File

@ -136,22 +136,19 @@ impl ShaderProgram {
} }
pub fn get_uniform_locations <'a, I> (&self, names: I) pub fn get_uniform_locations <'a, I> (&self, names: I)
-> HashMap <String, i32> -> Vec <i32>
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 ("uni_"); 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::GetUniformLocation (self.id, c_str.as_ptr ()) gl::GetUniformLocation (self.id, c_str.as_ptr ())
}; };
(String::from (name), loc) loc
}) })
.collect () .collect ()
} }