main
_ 2020-03-08 05:06:19 +00:00
parent 0db0df9e0e
commit e751288dee
1 changed files with 34 additions and 30 deletions

View File

@ -140,13 +140,31 @@ struct BorrowedShaderVars <'a> {
attrs: &'a renderable_model::AttrMap, attrs: &'a renderable_model::AttrMap,
} }
fn get_shader_attrs (
shader: &ShaderProgram,
attr_lookup: &Vec <(usize, &str)>
)
-> renderable_model::AttrMap
{
let mut v = vec! [None; attr_lookup.iter ().map (|(i, _)| i).max ().unwrap () + 1];
let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ());
for ((i, _), loc) in attr_lookup.iter ().zip (attrs.iter ()) {
v [*i] = *loc;
}
v
}
impl ShaderClosure { impl ShaderClosure {
pub fn new ( pub fn new (
program: ShaderProgram, program: ShaderProgram,
uniforms: HashMap <u32, i32>, uniforms: HashMap <u32, i32>,
attributes: renderable_model::AttrMap attr_lookup: &Vec <(usize, &str)>
) -> Self ) -> Self
{ {
let attributes = get_shader_attrs (&program, attr_lookup);
Self { Self {
program, program,
uniforms, uniforms,
@ -208,15 +226,6 @@ fn main () {
ShaderProgram::new (&vert_shader, &frag_shader).unwrap () ShaderProgram::new (&vert_shader, &frag_shader).unwrap ()
}; };
let attr_lookup: Vec <(_, &str)> = {
use renderable_model::attributes::*;
vec! [
(POS, "attr_pos"),
(UV, "attr_uv"),
(NORMAL, "attr_normal"),
]
};
let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({ let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({
use uniforms::*; use uniforms::*;
vec! [ vec! [
@ -230,21 +239,6 @@ fn main () {
].into_iter () ].into_iter ()
}); });
let get_shader_attrs = |shader: &ShaderProgram| {
let mut v = vec! [None; attr_lookup.iter ().map (|(i, _)| i).max ().unwrap () + 1];
let attrs = shader.get_attribute_locations (attr_lookup.iter ().map (|(_, v)| v).copied ());
for ((i, _), loc) in attr_lookup.iter ().zip (attrs.iter ()) {
v [*i] = *loc;
}
v
};
let attrs = get_shader_attrs (&shader_program);
let shadow_attrs = get_shader_attrs (&shadow_shader);
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 ());
@ -265,15 +259,25 @@ fn main () {
unis unis
}; };
{ let attr_lookup: Vec <(_, &str)> = {
use renderable_model::attributes::*;
vec! [
(POS, "attr_pos"),
(UV, "attr_uv"),
(NORMAL, "attr_normal"),
]
};
let shader_diffuse = ShaderClosure::new (shader_program, unis, &attr_lookup);
let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, &attr_lookup);
shader_diffuse.with (|shader_vars| {
let attrs = shader_vars.attrs;
use renderable_model::attributes::*; use renderable_model::attributes::*;
glezz::enable_vertex_attrib_array (attrs [POS]); glezz::enable_vertex_attrib_array (attrs [POS]);
glezz::enable_vertex_attrib_array (attrs [UV]); glezz::enable_vertex_attrib_array (attrs [UV]);
glezz::enable_vertex_attrib_array (attrs [NORMAL]); glezz::enable_vertex_attrib_array (attrs [NORMAL]);
} });
let shader_diffuse = ShaderClosure::new (shader_program, unis, attrs);
let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, shadow_attrs);
let texture = Texture::from_file ("sky.png"); let texture = Texture::from_file ("sky.png");
texture.bind (); texture.bind ();