main
_ 2020-03-08 04:17:57 +00:00
parent 61988a61ce
commit 07d925dc1e
2 changed files with 65 additions and 35 deletions

View File

@ -101,13 +101,14 @@ impl WorldState {
mod uniforms { mod uniforms {
use iota::iota; use iota::iota;
iota! { iota! {
pub const MVP: u32 = iota; pub const
, OBJECT_SPACE_LIGHT MVP: u32 = iota;
, OBJECT_SPACE_SKY , OBJECT_SPACE_LIGHT
, ALBEDO , OBJECT_SPACE_SKY
, MIN_ALBEDO , ALBEDO
, MIN_BRIGHT , MIN_ALBEDO
, TEXTURE , MIN_BRIGHT
, TEXTURE
} }
} }
@ -131,19 +132,19 @@ where P: AsRef <std::path::Path>
struct ShaderClosure { struct ShaderClosure {
program: ShaderProgram, program: ShaderProgram,
uniforms: HashMap <u32, i32>, uniforms: HashMap <u32, i32>,
attributes: HashMap <String, Option <u32>>, attributes: HashMap <usize, Option <u32>>,
} }
struct BorrowedShaderVars <'a> { struct BorrowedShaderVars <'a> {
unis: &'a HashMap <u32, i32>, unis: &'a HashMap <u32, i32>,
attrs: &'a HashMap <String, Option <u32>>, attrs: &'a HashMap <usize, Option <u32>>,
} }
impl ShaderClosure { impl ShaderClosure {
pub fn new ( pub fn new (
program: ShaderProgram, program: ShaderProgram,
uniforms: HashMap <u32, i32>, uniforms: HashMap <u32, i32>,
attributes: HashMap <String, Option <u32>> attributes: HashMap <usize, Option <u32>>
) -> Self ) -> Self
{ {
Self { Self {
@ -207,17 +208,14 @@ fn main () {
ShaderProgram::new (&vert_shader, &frag_shader).unwrap () ShaderProgram::new (&vert_shader, &frag_shader).unwrap ()
}; };
let attrs = shader_program.get_attribute_locations (vec! [ let attr_lookup: HashMap <_, &str> = HashMap::from_iter ({
"pos", use renderable_model::attributes::*;
"uv", vec! [
"normal", (POS, "pos"),
].into_iter ()); (UV, "uv"),
(NORMAL, "normal"),
let shadow_attrs = shadow_shader.get_attribute_locations (vec! [ ].into_iter ()
"pos", });
"uv",
"normal",
].into_iter ());
let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({ let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({
use uniforms::*; use uniforms::*;
@ -232,6 +230,24 @@ fn main () {
].into_iter () ].into_iter ()
}); });
let attrs = {
let attrs = shader_program.get_attribute_locations (attr_lookup.values ().copied ());
HashMap::from_iter (
attr_lookup.iter ()
.map (|(key, name)| (*key, *attrs.get (*name).unwrap ()))
)
};
let shadow_attrs = {
let attrs = shadow_shader.get_attribute_locations (attr_lookup.values ().copied ());
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 ());
@ -252,9 +268,12 @@ fn main () {
unis unis
}; };
glezz::enable_vertex_attrib_array (attrs ["pos"]); {
glezz::enable_vertex_attrib_array (attrs ["uv"]); use renderable_model::attributes::*;
glezz::enable_vertex_attrib_array (attrs ["normal"]); glezz::enable_vertex_attrib_array (attrs [&POS]);
glezz::enable_vertex_attrib_array (attrs [&UV]);
glezz::enable_vertex_attrib_array (attrs [&NORMAL]);
}
let shader_diffuse = ShaderClosure::new (shader_program, unis, attrs); let shader_diffuse = ShaderClosure::new (shader_program, unis, attrs);
let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, shadow_attrs); let shader_shadow = ShaderClosure::new (shadow_shader, shadow_unis, shadow_attrs);
@ -405,7 +424,7 @@ fn main () {
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
glezz::uniform_3fv (unis [&OBJECT_SPACE_SKY], &object_space_sky); glezz::uniform_3fv (unis [&OBJECT_SPACE_SKY], &object_space_sky);
mesh_pumpkin.draw_all (&attrs, |i| { mesh_pumpkin.draw_all (attrs, |i| {
glezz::uniform_3fv (unis [&ALBEDO], &pumpkin_colors [i]); glezz::uniform_3fv (unis [&ALBEDO], &pumpkin_colors [i]);
true true
}); });
@ -413,7 +432,7 @@ fn main () {
let mvp = view_mat * world_model_mat; let mvp = view_mat * world_model_mat;
glezz::uniform_matrix_4fv (unis [&MVP], &mvp); glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
mesh_pitch.draw_all (&attrs, |i| { mesh_pitch.draw_all (attrs, |i| {
glezz::uniform_3fv (unis [&ALBEDO], &pitch_colors [i]); glezz::uniform_3fv (unis [&ALBEDO], &pitch_colors [i]);
i != grass_index i != grass_index
}); });
@ -427,7 +446,7 @@ fn main () {
glezz::uniform_3fv (unis [&MIN_ALBEDO], &black); glezz::uniform_3fv (unis [&MIN_ALBEDO], &black);
glezz::uniform_1i (unis [&TEXTURE], 0); glezz::uniform_1i (unis [&TEXTURE], 0);
mesh_sky.draw_all (&attrs, |_| true); mesh_sky.draw_all (attrs, |_| true);
} }
glezz::enable (gl::STENCIL_TEST); glezz::enable (gl::STENCIL_TEST);
@ -450,12 +469,12 @@ fn main () {
let mvp = view_mat * pumpkin_model_mat; let mvp = view_mat * pumpkin_model_mat;
glezz::uniform_matrix_4fv (unis [&MVP], &mvp); glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
mesh_pumpkin.draw_all (&attrs, |_| true); mesh_pumpkin.draw_all (attrs, |_| true);
let mvp = view_mat * world_model_mat; let mvp = view_mat * world_model_mat;
glezz::uniform_matrix_4fv (unis [&MVP], &mvp); glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
mesh_pitch.draw_all (&attrs, |i| i != grass_index); mesh_pitch.draw_all (attrs, |i| i != grass_index);
unsafe { unsafe {
gl::ColorMask (255, 255, 255, 255); gl::ColorMask (255, 255, 255, 255);
@ -489,14 +508,14 @@ fn main () {
gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP); gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP);
} }
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0))); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0)));
mesh_pitch.draw (&attrs, grass_index); mesh_pitch.draw (attrs, grass_index);
unsafe { unsafe {
gl::StencilFunc (gl::EQUAL, 0, 1); gl::StencilFunc (gl::EQUAL, 0, 1);
gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP); gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP);
} }
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
mesh_pitch.draw (&attrs, grass_index); mesh_pitch.draw (attrs, grass_index);
}); });
window.gl_swap_window (); window.gl_swap_window ();

View File

@ -37,7 +37,17 @@ unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, float_offset
} }
} }
pub type AttrMap = HashMap <String, Option <u32>>; pub type AttrMap = HashMap <usize, Option <u32>>;
pub mod attributes {
use iota::iota;
iota! {
pub const
POS: usize = iota;
, UV
, NORMAL
}
}
impl RenderableModel { impl RenderableModel {
pub fn from_iqm (model: &iqm::Model) -> RenderableModel { pub fn from_iqm (model: &iqm::Model) -> RenderableModel {
@ -99,9 +109,10 @@ impl RenderableModel {
self.indexes.bind (); self.indexes.bind ();
unsafe { unsafe {
vertex_attrib_pointer (attrs ["pos"], 3, 0); use attributes::*;
vertex_attrib_pointer (attrs ["uv"], 2, self.num_pos); vertex_attrib_pointer (attrs [&POS], 3, 0);
vertex_attrib_pointer (attrs ["normal"], 3, self.num_pos + self.num_uv); vertex_attrib_pointer (attrs [&UV], 2, self.num_pos);
vertex_attrib_pointer (attrs [&NORMAL], 3, self.num_pos + self.num_uv);
} }
for mesh_num in start..end { for mesh_num in start..end {