From 813f3ffb7347b87a86b169297ae4be3fa3ef8065 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 30 Jan 2023 01:24:53 +0100 Subject: [PATCH] add chunk borders --- shaders/colored.frag | 6 +- src/main.rs | 12 ++-- src/rendering/selection_box.rs | 1 - src/rendering/world.rs | 103 ++++++++++++++++++++++++--------- src/settings.rs | 4 +- 5 files changed, 86 insertions(+), 40 deletions(-) diff --git a/shaders/colored.frag b/shaders/colored.frag index 9b2ac94..3e09c7a 100644 --- a/shaders/colored.frag +++ b/shaders/colored.frag @@ -1,8 +1,8 @@ #version 150 core -out vec4 color; -uniform vec4 u_color; +out vec4 out_color; +uniform vec4 color; void main() { - color = u_color; + out_color = color; } diff --git a/src/main.rs b/src/main.rs index 33d15ce..68a3e31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,12 +32,6 @@ pub(crate) mod delta_time; pub(crate) mod cursor_lock; pub(crate) mod control_flow; -use rendering::{ - Renderer, - RenderTarget, - BackgroundColor, - clear_background -}; use world::{ init_game_world, loading::update_loaded_world_around_player, @@ -54,9 +48,14 @@ use events::{ use input::{init_input, process_inputs}; use fly_controller::update_controllers; use rendering::{ + Renderer, + RenderTarget, + BackgroundColor, + clear_background, primitives::init_simple_box_buffers, selection_box::render_selection_box, world::draw_world, + world::draw_current_chunk_border, }; use block_placement::block_placement_system; use delta_time::{DeltaTime, init_delta_time}; @@ -94,6 +93,7 @@ fn render() -> Workload { ( clear_background, draw_world, + draw_current_chunk_border, render_selection_box, ).into_sequential_workload() } diff --git a/src/rendering/selection_box.rs b/src/rendering/selection_box.rs index 1a895db..b04f75c 100644 --- a/src/rendering/selection_box.rs +++ b/src/rendering/selection_box.rs @@ -1,4 +1,3 @@ -use glam::Mat4; use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView}; use glium::{ Surface, diff --git a/src/rendering/world.rs b/src/rendering/world.rs index d0de7b3..f4770ec 100644 --- a/src/rendering/world.rs +++ b/src/rendering/world.rs @@ -1,4 +1,4 @@ -use glam::Vec3; +use glam::{Vec3, Mat4, Quat, ivec3}; use shipyard::{NonSendSync, UniqueView, UniqueViewMut, View, IntoIter}; use glium::{ implement_vertex, uniform, @@ -15,10 +15,12 @@ use glium::{ DepthTest, PolygonMode, BackfaceCullingMode, - } + }, Blend }; use crate::{ - camera::Camera, + camera::Camera, + player::MainPlayer, + transform::Transform, prefabs::{ ChunkShaderPrefab, BlockTexturesPrefab, @@ -28,7 +30,7 @@ use crate::{ ChunkStorage, ChunkMeshStorage, chunk::CHUNK_SIZE, - }, + }, settings::GameSettings, }; use super::{RenderTarget, primitives::SimpleBoxBuffers}; @@ -107,27 +109,72 @@ pub fn draw_world( } } -// this doesn't use culling! -// pub fn draw_chunk_borders( -// mut target: NonSendSync>, -// chunks: UniqueView, -// buffers: NonSendSync>, -// program: NonSendSync>, -// camera: View, -// ) { -// for (&position, chunk) in &chunks.chunks { -// let world_position = position.as_vec3() * CHUNK_SIZE as f32; -// target.0.draw( -// &buffers.0, -// &buffers.1, -// &program.0, -// &uniform! { -// position_offset: world_position.to_array(), -// view: view, -// perspective: perspective, -// tex: texture_sampler, -// }, -// &draw_parameters -// ).unwrap(); -// } -// } +//this doesn't use culling! +pub fn draw_current_chunk_border( + mut target: NonSendSync>, + player: View, + transforms: View, + buffers: NonSendSync>, + program: NonSendSync>, + camera: View, + settings: UniqueView, +) { + if !settings.debug_draw_current_chunk_border { + return + } + let camera = camera.iter().next().expect("No cameras in the scene"); + let view = camera.view_matrix.to_cols_array_2d(); + let perspective = camera.perspective_matrix.to_cols_array_2d(); + let (_, &player_transform) = (&player, &transforms).iter().next().expect("No player"); + let (_, _, player_position) = player_transform.0.to_scale_rotation_translation(); + let player_in_chunk = ivec3( + (player_position.x as i32).div_euclid(CHUNK_SIZE as i32), + (player_position.y as i32).div_euclid(CHUNK_SIZE as i32), + (player_position.z as i32).div_euclid(CHUNK_SIZE as i32), + ); + let world_position = player_in_chunk.as_vec3() * CHUNK_SIZE as f32; + target.0.draw( + &buffers.0, + &buffers.1, + &program.0, + &uniform! { + model: Mat4::from_scale_rotation_translation( + Vec3::splat(CHUNK_SIZE as f32), + Quat::default(), + world_position + ).to_cols_array_2d(), + color: [0.25f32; 4], + view: view, + perspective: perspective, + }, + &DrawParameters { + depth: Depth { + test: DepthTest::IfLess, + ..Default::default() + }, + blend: Blend::alpha_blending(), + ..Default::default() + } + ).unwrap(); + target.0.draw( + &buffers.0, + &buffers.1, + &program.0, + &uniform! { + model: Mat4::from_scale_rotation_translation( + Vec3::splat(CHUNK_SIZE as f32), + Quat::default(), + world_position + ).to_cols_array_2d(), + color: [0.0f32; 4], + view: view, + perspective: perspective, + }, + &DrawParameters { + polygon_mode: PolygonMode::Point, + line_width: Some(2.), + point_size: Some(5.), + ..Default::default() + } + ).unwrap(); +} diff --git a/src/settings.rs b/src/settings.rs index dee58eb..97cdde9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,14 +5,14 @@ pub struct GameSettings { /// there's a 1 chunk border of loaded but invisible around this pub render_distance: u8, pub mouse_sensitivity: f32, - pub debug_draw_chunk_border: bool, + pub debug_draw_current_chunk_border: bool, } impl Default for GameSettings { fn default() -> Self { Self { render_distance: 5, mouse_sensitivity: 1., - debug_draw_chunk_border: cfg!(debug_assertions), + debug_draw_current_chunk_border: cfg!(debug_assertions), } } }