diff --git a/.gitignore b/.gitignore index 08272ef..9b27146 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /quic_server.crt /target **/*.rs.bk +/videos *.blend* *.trace diff --git a/crate.png b/crate.png new file mode 100644 index 0000000..6c19367 Binary files /dev/null and b/crate.png differ diff --git a/earth.png b/earth.png new file mode 100644 index 0000000..3abd9ad Binary files /dev/null and b/earth.png differ diff --git a/sphere.iqm b/sphere.iqm index 2a9c7a1..b137494 100644 Binary files a/sphere.iqm and b/sphere.iqm differ diff --git a/src/bin/platformer.rs b/src/bin/platformer.rs index ea0098e..1e69c37 100644 --- a/src/bin/platformer.rs +++ b/src/bin/platformer.rs @@ -73,7 +73,10 @@ struct GameGraphics { text_stream: TriangleStream, texture_crate: Texture, + texture_earth: Texture, texture_sky: Texture, + + frames: u64, } impl ShaderLookup for GameGraphics { @@ -92,8 +95,9 @@ impl GameGraphics { let white = color_from_255 ((255.0, 255.0, 255.0)); let black = color_from_255 ((0.0, 0.0, 0.0)); + let shadow_blue = color_from_255 ((0.0, 0.0, 0.25)); - let screen_size = (1280.0, 720.0); + let screen_size = (320.0, 240.0); let fov = 30.0f32; @@ -114,13 +118,14 @@ impl GameGraphics { let attrs = shader_vars.attrs; let unis = shader_vars.unis; - self.texture_sky.bind (); + self.texture_earth.bind (); { let mvp = view_mat * Mat4::from_translation (state.player.pos) * Mat4::from_scale ((0.5, 0.5, 0.5).into ()); glezz::uniform_matrix_4fv (unis [&u::MVP], &mvp); + glezz::uniform_3fv (unis [&u::ALBEDO], &white); self.mesh_sphere.draw_all (attrs, |_| { true @@ -128,6 +133,7 @@ impl GameGraphics { } self.texture_crate.bind (); + glezz::uniform_3fv (unis [&u::ALBEDO], &white); for aabb in &state.aabbs { let min = aabb.min; @@ -145,6 +151,55 @@ impl GameGraphics { }); } + glezz::uniform_3fv (unis [&u::ALBEDO], &shadow_blue); + + if true { + // Raycast for player shadow + let coll = opengl_rust::physics::get_candidate ( + &[], &state.aabbs, + state.player.pos, + state.player.pos + Vec3::new (0.0, 0.0, -100.0), + 0.0 + ); + + if coll.t <= 1.0 { + let mvp = view_mat * + Mat4::from_translation (coll.p_impact + Vec3::new (0.0, 0.0, 0.0625)) * + Mat4::from_scale ((0.5, 0.5, 0.0).into ()); + glezz::uniform_matrix_4fv (unis [&u::MVP], &mvp); + + self.mesh_sphere.draw_all (attrs, |_| { + true + }); + } + + for aabb in &state.aabbs { + // Raycast for box shadows + let min = aabb.min; + let max = aabb.max; + let center = (min + max) / 2.0; + let scale = (max - min) / 2.0; + + let coll = opengl_rust::physics::get_candidate ( + &[], &state.aabbs, + center, + center + Vec3::new (0.0, 0.0, -100.0), + 0.0 + ); + + if coll.t <= 1.0 { + let mvp = view_mat * + Mat4::from_translation (coll.p_impact + Vec3::new (0.0, 0.0, 0.0625)) * + Mat4::from_scale ((scale.x, scale.y, 0.0).into ()); + glezz::uniform_matrix_4fv (unis [&u::MVP], &mvp); + + self.mesh_cube.draw_all (attrs, |_| { + true + }); + } + } + } + { let sky_mvp_mat = view_mat * Mat4::from_scale ((64.0, 64.0, 64.0).into ()); @@ -170,7 +225,7 @@ async fn main () -> Result <()> { let sdl_context = sdl2::init ().map_err (|e| anyhow! ("Can't init SDL: {}", e))?; let video_subsystem = sdl_context.video ().map_err (|e| anyhow! ("Can't get SDL video subsystem: {}", e))?; - let window = video_subsystem.window ("3D platformer", 1280, 720) + let window = video_subsystem.window ("3D platformer", 320, 240) .position_centered () .opengl () .build () @@ -275,7 +330,7 @@ async fn main () -> Result <()> { glezz::enable_vertex_attrib_array (attrs [attributes::NORMAL]); }); - let graphics = GameGraphics { + let mut graphics = GameGraphics { mesh_cube, mesh_sky, mesh_sphere, @@ -284,7 +339,9 @@ async fn main () -> Result <()> { shaders, text_stream, texture_crate: Texture::from_file ("crate.png"), + texture_earth: Texture::from_file ("earth.png"), texture_sky: Texture::from_file ("sky.png"), + frames: 0, }; let mut gl_state = Default::default (); @@ -374,9 +431,9 @@ async fn main () -> Result <()> { window.gl_make_current (&gl_ctx).unwrap (); graphics.draw (&game_state, &mut gl_state); + graphics.frames += 1; window.gl_swap_window (); - graphics_frames += 1; tokio::time::sleep (Duration::from_millis (15)).await; } diff --git a/src/physics.rs b/src/physics.rs index 2487956..132d864 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -59,8 +59,8 @@ impl Triangle { #[derive (Clone, Copy, Debug, PartialEq)] pub struct Collision { - t: f32, - p_impact: Vec3, + pub t: f32, + pub p_impact: Vec3, normal: Vec3, i: usize, c_type: CollisionType, diff --git a/src/timestep.rs b/src/timestep.rs index c8e1f0c..447f6c7 100644 --- a/src/timestep.rs +++ b/src/timestep.rs @@ -12,7 +12,7 @@ impl TimeStep { pub fn new (fps_num: u16, fps_den: u16) -> Self { Self { last_frame_time: Instant::now (), - accum: 0, + accum: (fps_den / 2).into (), fps_num, fps_den, }