diff --git a/src/main.rs b/src/main.rs index aa72591..7bfbd13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,6 +72,7 @@ fn render() -> Workload { ( clear_background, draw_world, + render_selection_box, ).into_sequential_workload() } fn after_frame_end() -> Workload { diff --git a/src/player.rs b/src/player.rs index 4a2fbc1..a6afaed 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,3 @@ -use glam::Mat4; use shipyard::{Component, AllStoragesViewMut}; use crate::{ transform::Transform, diff --git a/src/rendering/primitives.rs b/src/rendering/primitives.rs index 998ad16..7efa2e5 100644 --- a/src/rendering/primitives.rs +++ b/src/rendering/primitives.rs @@ -1,16 +1,16 @@ -const CUBE_VERTICES: &[f32] = &[ +pub const CUBE_VERTICES: &[f32] = &[ // front - -1.0, -1.0, 1.0, - 1.0, -1.0, 1.0, - 1.0, 1.0, 1.0, - -1.0, 1.0, 1.0, + 0.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 1.0, 1.0, + 0.0, 1.0, 1.0, // back - -1.0, -1.0, -1.0, - 1.0, -1.0, -1.0, - 1.0, 1.0, -1.0, - -1.0, 1.0, -1.0 + 0.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 1.0, 0.0, + 0.0, 1.0, 0.0 ]; -const CUBE_INDICES: &[u16] = &[ +pub const CUBE_INDICES: &[u16] = &[ // front 0, 1, 2, 2, 3, 0, diff --git a/src/rendering/selection_box.rs b/src/rendering/selection_box.rs index 6301a0d..a359163 100644 --- a/src/rendering/selection_box.rs +++ b/src/rendering/selection_box.rs @@ -1,35 +1,76 @@ -use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut}; -use glium::{Surface, implement_vertex}; +use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView}; +use glium::{Surface, implement_vertex, IndexBuffer, index::PrimitiveType, VertexBuffer, uniform, DrawParameters, Blend, BackfaceCullingMode}; use crate::{ world::raycast::LookingAtBlock, - camera::Camera + camera::Camera, prefabs::SelBoxShaderPrefab +}; +use super::{ + RenderTarget, + primitives::{CUBE_INDICES, CUBE_VERTICES}, Renderer }; -use super::RenderTarget; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)] pub struct SelBoxVertex { pub position: [f32; 3], } implement_vertex!(SelBoxVertex, position); +const fn box_vertices() -> [SelBoxVertex; CUBE_VERTICES.len() / 3] { + let mut arr = [SelBoxVertex { position: [0., 0., 0.] }; CUBE_VERTICES.len() / 3]; + let mut ptr = 0; + loop { + arr[ptr] = SelBoxVertex { + position: [ + CUBE_VERTICES[ptr * 3], + CUBE_VERTICES[(ptr * 3) + 1], + CUBE_VERTICES[(ptr * 3) + 2] + ] + }; + ptr += 1; + if ptr >= CUBE_VERTICES.len() / 3 { + return arr + } + } +} +const BOX_VERTICES: &[SelBoxVertex] = &box_vertices(); + //wip pub fn render_selection_box( lookat: View, camera: View, mut target: NonSendSync>, + display: NonSendSync>, + program: NonSendSync>, ) { - for lookat in lookat.iter() { - // target.0.draw( - // &mesh.vertex_buffer, - // &mesh.index_buffer, - // &program.0, - // &uniform! { - // position_offset: world_position.to_array(), - // view: view, - // perspective: perspective, - // tex: texture_sampler, - // }, - // &draw_parameters - // ).unwrap(); - } + let camera = camera.iter().next().unwrap(); + let Some(lookat) = lookat.iter().next() else { return }; + let Some(lookat) = lookat.0 else { return }; + + //this may be slow but the amount of vertices is very low + let vert = VertexBuffer::new( + &display.display, + BOX_VERTICES + ).unwrap(); + let index = IndexBuffer::new( + &display.display, + PrimitiveType::TrianglesList, + CUBE_INDICES + ).unwrap(); + + target.0.draw( + &vert, + &index, + &program.0, + &uniform! { + color: [0., 0., 0., 0.5_f32], + u_position: lookat.block_position.as_vec3().to_array(), + perspective: camera.perspective_matrix.to_cols_array_2d(), + view: camera.view_matrix.to_cols_array_2d(), + }, + &DrawParameters { + blend: Blend::alpha_blending(), + backface_culling: BackfaceCullingMode::CullClockwise, + ..Default::default() + } + ).unwrap(); }