From 5b1b59db5e990d07fc92d17baf36426036c4f954 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 8 Mar 2020 05:52:26 +0000 Subject: [PATCH] Beautiful badly-lit debug arrows --- src/bin/pumpkin.rs | 85 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index bcb95a3..8c975a3 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -211,6 +211,11 @@ impl ShaderClosure { } } +struct Arrow { + origin: Vec3, + direction: Vec3, +} + fn main () { let sdl_context = sdl2::init ().unwrap (); let video_subsystem = sdl_context.video ().unwrap (); @@ -272,6 +277,7 @@ fn main () { let mesh_pitch = renderable_from_iqm_file ("pitch.iqm"); let mesh_arrow = renderable_from_iqm_file ("arrow.iqm"); + let magenta = color_from_255 ((255.0, 0.0, 255.0)); let orange = color_from_255 ((210.0, 125.0, 44.0)); let green = color_from_255 ((52.0, 101.0, 36.0)); let white = color_from_255 ((255.0, 255.0, 255.0)); @@ -360,7 +366,7 @@ fn main () { let shadow_mat = { let mut mat = Mat4::identity (); - mat.set_z_axis ((0.25, 0.125, 0.0, 0.0).into ()); + mat.set_z_axis ((-0.25, 0.125, 0.0, 0.0).into ()); mat }; @@ -373,7 +379,7 @@ fn main () { let sky_mvp_mat = view_mat * Mat4::from_scale ((16.0, 16.0, 16.0).into ()); - let light = Vec3::from ((2.0, 0.0, 5.0)).normalize (); + let light = Vec3::from ((0.25, -0.125, 1.0)).normalize (); 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); @@ -387,6 +393,50 @@ fn main () { let world_model_mat = Mat4::identity (); + let arrows = vec![ + Arrow { + origin: (0.0, 0.0, 1.0).into (), + direction: (0.0, 0.0, 1.0).into (), + }, + Arrow { + origin: (1.0, 0.0, 1.0).into (), + direction: (1.0, 0.0, 0.0).into (), + }, + Arrow { + origin: (0.0, 1.0, 1.0).into (), + direction: (0.0, 1.0, 0.0).into (), + }, + ]; + + let arrow_mats: Vec <_> = arrows.iter ().map (|arrow| { + let d = arrow.direction; + + let up: Vec3 = if d.z () > 0.5 { + (-1.0, 0.0, 0.0) + } + else if d.z () < -0.5 { + (-1.0, 0.0, 0.0) + } + else { + (0.0, 0.0, 1.0) + }.into (); + + // These are probably all fucked + + let left = d.cross (up); + let up = d.cross (left); + + let mut dir_mat = Mat4::identity (); + + dir_mat.set_x_axis ((left.x (), left.y (), left.z (), 0.0).into ()); + dir_mat.set_y_axis ((up.x (), up.y (), up.z (), 0.0).into ()); + dir_mat.set_z_axis ((d.x (), d.y (), d.z (), 0.0).into ()); + + Mat4::from_translation (arrow.origin) * + Mat4::from_scale ((0.125, 0.125, 0.125).into ()) * + dir_mat + }).collect (); + use uniforms::*; shader_diffuse.with (|shader_vars| { @@ -423,6 +473,14 @@ fn main () { i != grass_index }); + glezz::uniform_3fv (unis [&ALBEDO], &magenta); + for arrow_mat in &arrow_mats { + let mvp = view_mat * *arrow_mat; + + glezz::uniform_matrix_4fv (unis [&MVP], &mvp); + mesh_arrow.draw_all (attrs, |_| true); + } + let draw_sky = true; if draw_sky { glezz::front_face (gl::CCW); @@ -434,6 +492,11 @@ fn main () { mesh_sky.draw_all (attrs, |_| true); } + }); + + shader_shadow.with (|shader_vars| { + let unis = shader_vars.unis; + let attrs = shader_vars.attrs; glezz::enable (gl::STENCIL_TEST); unsafe { @@ -444,11 +507,6 @@ fn main () { gl::DepthMask (0); gl::StencilMask (1); } - }); - - shader_shadow.with (|shader_vars| { - let unis = shader_vars.unis; - let attrs = shader_vars.attrs; let view_mat = view_mat * shadow_mat; @@ -462,9 +520,11 @@ fn main () { mesh_pitch.draw_all (attrs, |i| i != grass_index); - unsafe { - gl::ColorMask (255, 255, 255, 255); - gl::DepthMask (1); + for arrow_mat in &arrow_mats { + let mvp = view_mat * *arrow_mat; + + glezz::uniform_matrix_4fv (unis [&MVP], &mvp); + mesh_arrow.draw_all (attrs, |_| true); } }); @@ -472,6 +532,11 @@ fn main () { let unis = shader_vars.unis; let attrs = shader_vars.attrs; + unsafe { + gl::ColorMask (255, 255, 255, 255); + gl::DepthMask (1); + } + glezz::front_face (gl::CW); let inverse_pumpkin = pumpkin_model_mat.inverse ();