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

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 {
pub fn from_iqm (model: &iqm::Model) -> RenderableModel {
@ -99,9 +109,10 @@ impl RenderableModel {
self.indexes.bind ();
unsafe {
vertex_attrib_pointer (attrs ["pos"], 3, 0);
vertex_attrib_pointer (attrs ["uv"], 2, self.num_pos);
vertex_attrib_pointer (attrs ["normal"], 3, self.num_pos + self.num_uv);
use attributes::*;
vertex_attrib_pointer (attrs [&POS], 3, 0);
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 {