Use the passes system

main
_ 2020-03-08 18:33:57 +00:00
parent 7b140301c3
commit 9e74609eb1
1 changed files with 44 additions and 24 deletions

View File

@ -323,6 +323,27 @@ impl Pass <'_> {
}
}
}
pub fn with <F> (&self, callback: F)
where F: Fn ()
{
self.apply_slow ();
callback ();
}
pub fn with_shader <F> (&self, callback: F)
where F: Fn (BorrowedShaderVars)
{
if let Some (s) = self.shader {
s.with (|shader_vars| {
self.apply_slow ();
callback (shader_vars);
});
}
else {
panic! ("Called with_shader on a pass with no shader");
}
}
}
struct GameGraphics {
@ -497,6 +518,7 @@ impl GameGraphics {
shader: Some (&self.shader_diffuse),
required_state: GlState {
flags: hashmap! {
gl::CULL_FACE => true,
gl::DEPTH_TEST => true,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => false,
@ -513,6 +535,7 @@ impl GameGraphics {
shader: Some (&self.shader_shadow),
required_state: GlState {
flags: hashmap! {
gl::CULL_FACE => true,
gl::DEPTH_TEST => true,
gl::STENCIL_TEST => true,
},
@ -539,6 +562,7 @@ impl GameGraphics {
shader: Some (&self.shader_diffuse),
required_state: GlState {
flags: hashmap! {
gl::CULL_FACE => true,
gl::DEPTH_TEST => true,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => true,
@ -566,6 +590,7 @@ impl GameGraphics {
shader: Some (&self.shader_diffuse),
required_state: GlState {
flags: hashmap! {
gl::CULL_FACE => true,
gl::DEPTH_TEST => true,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => true,
@ -590,11 +615,10 @@ impl GameGraphics {
},
];
passes [0].apply_slow ();
glezz::clear_color (1.0f32, 0.0f32, 1.0f32, 1.0f32);
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
glezz::enable (gl::CULL_FACE);
passes [0].with (|| {
glezz::clear_color (1.0f32, 0.0f32, 1.0f32, 1.0f32);
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
});
let pumpkin_model_mat =
Mat4::from_translation ((0.0, 0.0, 2.7 * 0.5).into ()) *
@ -603,21 +627,18 @@ impl GameGraphics {
;
let world_model_mat = Mat4::identity ();
let inverse_pumpkin = pumpkin_model_mat.inverse ();
use uniforms::*;
self.shader_diffuse.with (|shader_vars| {
// Draw the world except the ground plane
passes [1].with_shader (|shader_vars| {
let unis = shader_vars.unis;
let attrs = shader_vars.attrs;
// Pass 0 - Draw the world except the ground plane
passes [1].apply_slow ();
let mvp = view_mat * pumpkin_model_mat;
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
let inverse_pumpkin = pumpkin_model_mat.inverse ();
let object_space_light = make_object_space_vec (&inverse_pumpkin, &light);
let object_space_sky = make_object_space_vec (&inverse_pumpkin, &Vec3::from ((0.0, 0.0, 1.0)));
@ -666,13 +687,11 @@ impl GameGraphics {
}
});
self.shader_shadow.with (|shader_vars| {
// Draw shadows into stencil buffer
passes [2].with_shader (|shader_vars| {
let unis = shader_vars.unis;
let attrs = shader_vars.attrs;
// Draw shadows into stencil buffer
passes [2].apply_slow ();
let view_mat = view_mat * shadow_mat;
let mvp = view_mat * pumpkin_model_mat;
@ -693,17 +712,15 @@ impl GameGraphics {
}
});
self.shader_diffuse.with (|shader_vars| {
let object_space_light = make_object_space_vec (&inverse_pumpkin, &light);
// Draw unlit ground
passes [3].with_shader (|shader_vars| {
let unis = shader_vars.unis;
let attrs = shader_vars.attrs;
// Draw lit ground
passes [3].apply_slow ();
let inverse_pumpkin = pumpkin_model_mat.inverse ();
let object_space_light = make_object_space_vec (&inverse_pumpkin, &light);
let object_space_sky = make_object_space_vec (&inverse_pumpkin, &Vec3::from ((0.0, 0.0, 1.0)));
glezz::uniform_3fv (unis [&MIN_BRIGHT], &black);
@ -717,9 +734,12 @@ impl GameGraphics {
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0)));
self.mesh_pitch.draw (attrs, self.grass_index);
});
// Draw shadowed ground
passes [4].apply_slow ();
// Draw lit ground
passes [4].with_shader (|shader_vars| {
let unis = shader_vars.unis;
let attrs = shader_vars.attrs;
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
self.mesh_pitch.draw (attrs, self.grass_index);