add cube primitive

This commit is contained in:
griffi-gh 2024-05-07 01:11:05 +02:00
parent 196a7010b6
commit 7aa91b6414
5 changed files with 71 additions and 194 deletions

View file

@ -4,6 +4,7 @@ use glam::Vec3;
use crate::{events::WindowResizedEvent, state::is_ingame}; use crate::{events::WindowResizedEvent, state::is_ingame};
mod renderer; mod renderer;
mod primitives;
pub use renderer::Renderer; pub use renderer::Renderer;
pub mod background; pub mod background;
@ -27,11 +28,14 @@ pub struct RenderCtx<'a> {
pub surface_view: &'a wgpu::TextureView, pub surface_view: &'a wgpu::TextureView,
} }
//TODO run init_world_render_state only once ingame?
pub fn init_rendering() -> Workload { pub fn init_rendering() -> Workload {
( (
depth::init_depth_texture, depth::init_depth_texture,
camera::init_camera_uniform_buffer, camera::init_camera_uniform_buffer,
world::init_world_render_state, //TODO run only once ingame world::init_world_render_state, //requires depth and camera buffers
primitives::init_primitives,
).into_sequential_workload() ).into_sequential_workload()
} }
@ -82,30 +86,6 @@ pub fn resize_renderer(
} }
} }
//Deprecated WindowSize thingy // pub fn if_resized (resize: View<WindowResizedEvent>,) -> bool {
// #[derive(Unique, Clone, Copy)]
// #[repr(transparent)]
// #[deprecated = "use Renderer.size instead"]
// #[allow(deprecated)]
// pub struct WindowSize(pub UVec2);
// pub fn init_window_size(storages: AllStoragesView) {
// let size = storages.borrow::<View<WindowResizedEvent>>().unwrap().iter().next().unwrap().0;
// storages.add_unique(WindowSize(size))
// }
// pub fn update_window_size(
// mut win_size: UniqueViewMut<WindowSize>,
// resize: View<WindowResizedEvent>,
// ) {
// if let Some(resize) = resize.iter().next() {
// win_size.0 = resize.0;
// }
// }
// pub fn if_resized (
// resize: View<WindowResizedEvent>,
// ) -> bool {
// resize.len() > 0 // resize.len() > 0
// } // }

View file

@ -1,30 +1,24 @@
// use shipyard::{Workload, IntoWorkload}; use bytemuck::{Pod, Zeroable};
// use glium::implement_vertex; use shipyard::{IntoWorkload, Workload};
// pub mod cube; mod cube;
// pub mod rect;
// pub mod stri;
// use cube::init_cube_primitive; pub fn init_primitives() -> Workload {
// use rect::init_rect_primitive; (
// use stri::init_stri_primitive; cube::init_cube_primitive,
).into_workload()
}
// #[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default, Pod, Zeroable)]
// pub struct PositionOnlyVertex { #[repr(C, packed)]
// pub position: [f32; 3], pub struct PrimitiveVertex {
// } pub position: [f32; 3],
// implement_vertex!(PositionOnlyVertex, position); }
// #[derive(Clone, Copy, Default)] impl PrimitiveVertex {
// pub struct PositionOnlyVertex2d { pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
// pub position: [f32; 2], array_stride: std::mem::size_of::<PrimitiveVertex>() as wgpu::BufferAddress,
// } step_mode: wgpu::VertexStepMode::Vertex,
// implement_vertex!(PositionOnlyVertex2d, position); attributes: &wgpu::vertex_attr_array![0 => Float32x3],
};
// pub fn init_primitives() -> Workload { }
// (
// init_cube_primitive,
// init_rect_primitive,
// init_stri_primitive,
// ).into_workload()
// }

View file

@ -1,85 +1,49 @@
// use shipyard::{AllStoragesView, NonSendSync, UniqueView, Unique}; use shipyard::{AllStoragesView, Unique, UniqueView};
// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; use wgpu::util::DeviceExt;
// use crate::rendering::Renderer; use crate::rendering::{BufferPair, Renderer};
// use super::PositionOnlyVertex; use super::PrimitiveVertex;
// #[derive(Unique)] #[derive(Unique)]
// pub struct CubePrimitive(pub VertexBuffer<PositionOnlyVertex>, pub IndexBuffer<u16>); pub struct CubePrimitive(pub BufferPair);
// #[derive(Unique)] /// Vertices for a centered cube with a side length of 1
// pub struct CenteredCubePrimitive(pub VertexBuffer<PositionOnlyVertex>, pub IndexBuffer<u16>); const CUBE_VERTICES: &[PrimitiveVertex] = &[
// front
PrimitiveVertex { position: [-0.5, -0.5, 0.5] },
PrimitiveVertex { position: [ 0.5, -0.5, 0.5] },
PrimitiveVertex { position: [ 0.5, 0.5, 0.5] },
PrimitiveVertex { position: [-0.5, 0.5, 0.5] },
// back
PrimitiveVertex { position: [-0.5, -0.5, -0.5] },
PrimitiveVertex { position: [ 0.5, -0.5, -0.5] },
PrimitiveVertex { position: [ 0.5, 0.5, -0.5] },
PrimitiveVertex { position: [-0.5, 0.5, -0.5] },
];
// const CENTERED_CUBE_VERTICES: &[PositionOnlyVertex] = &[ /// Indices for a cube primitive
// // front const CUBE_INDICES: &[u16] = &[
// PositionOnlyVertex { position: [-0.5, -0.5, 0.5] }, 0, 1, 2, 2, 3, 0, // front
// PositionOnlyVertex { position: [ 0.5, -0.5, 0.5] }, 1, 5, 6, 6, 2, 1, // right
// PositionOnlyVertex { position: [ 0.5, 0.5, 0.5] }, 7, 6, 5, 5, 4, 7, // back
// PositionOnlyVertex { position: [-0.5, 0.5, 0.5] }, 4, 0, 3, 3, 7, 4, // left
// // back 4, 5, 1, 1, 0, 4, // bottom
// PositionOnlyVertex { position: [-0.5, -0.5, -0.5] }, 3, 2, 6, 6, 7, 3, // top
// PositionOnlyVertex { position: [ 0.5, -0.5, -0.5] }, ];
// PositionOnlyVertex { position: [ 0.5, 0.5, -0.5] },
// PositionOnlyVertex { position: [-0.5, 0.5, -0.5] },
// ];
// const CUBE_VERTICES: &[PositionOnlyVertex] = &[
// // front
// PositionOnlyVertex { position: [0.0, 0.0, 1.0] },
// PositionOnlyVertex { position: [1.0, 0.0, 1.0] },
// PositionOnlyVertex { position: [1.0, 1.0, 1.0] },
// PositionOnlyVertex { position: [0.0, 1.0, 1.0] },
// // back
// PositionOnlyVertex { position: [0.0, 0.0, 0.0] },
// PositionOnlyVertex { position: [1.0, 0.0, 0.0] },
// PositionOnlyVertex { position: [1.0, 1.0, 0.0] },
// PositionOnlyVertex { position: [0.0, 1.0, 0.0] },
// ];
// const CUBE_INDICES: &[u16] = &[
// // front
// 0, 1, 2,
// 2, 3, 0,
// // right
// 1, 5, 6,
// 6, 2, 1,
// // back
// 7, 6, 5,
// 5, 4, 7,
// // left
// 4, 0, 3,
// 3, 7, 4,
// // bottom
// 4, 5, 1,
// 1, 0, 4,
// // top
// 3, 2, 6,
// 6, 7, 3
// ];
// pub(super) fn init_cube_primitive( pub fn init_cube_primitive(storages: AllStoragesView) {
// storages: AllStoragesView, let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
// display: NonSendSync<UniqueView<Renderer>> storages.add_unique(CubePrimitive(BufferPair {
// ) { index: renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor {
// { label: Some("cube_index_buffer"),
// let vert = VertexBuffer::immutable( contents: bytemuck::cast_slice(CUBE_INDICES),
// &display.display, usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::INDEX,
// CUBE_VERTICES }),
// ).unwrap(); index_len: CUBE_INDICES.len() as u32,
// let index = IndexBuffer::immutable( vertex: renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor {
// &display.display, label: Some("cube_vertex_buffer"),
// PrimitiveType::TrianglesList, contents: bytemuck::cast_slice(CUBE_VERTICES),
// CUBE_INDICES usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::VERTEX,
// ).unwrap(); }),
// storages.add_unique_non_send_sync(CubePrimitive(vert, index)); vertex_len: CUBE_VERTICES.len() as u32,
// } }));
// { }
// let vert = VertexBuffer::immutable(
// &display.display,
// CENTERED_CUBE_VERTICES
// ).unwrap();
// let index = IndexBuffer::immutable(
// &display.display,
// PrimitiveType::TrianglesList,
// CUBE_INDICES
// ).unwrap();
// storages.add_unique_non_send_sync(CenteredCubePrimitive(vert, index));
// }
// }

View file

@ -1,31 +0,0 @@
// use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView};
// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
// use crate::rendering::Renderer;
// use super::PositionOnlyVertex2d;
// #[derive(Unique)]
// pub struct RectPrimitive(pub VertexBuffer<PositionOnlyVertex2d>, pub IndexBuffer<u16>);
// const RECT_VERTEX: &[PositionOnlyVertex2d] = &[
// PositionOnlyVertex2d { position: [0., 0.] },
// PositionOnlyVertex2d { position: [1., 0.] },
// PositionOnlyVertex2d { position: [0., 1.] },
// PositionOnlyVertex2d { position: [1., 1.] },
// ];
// const RECT_INDEX: &[u16] = &[0, 1, 2, 1, 3, 2];
// pub(super) fn init_rect_primitive(
// storages: AllStoragesView,
// display: NonSendSync<UniqueView<Renderer>>
// ) {
// let vert = VertexBuffer::immutable(
// &display.display,
// RECT_VERTEX
// ).unwrap();
// let index = IndexBuffer::immutable(
// &display.display,
// PrimitiveType::TrianglesList,
// RECT_INDEX
// ).unwrap();
// storages.add_unique_non_send_sync(RectPrimitive(vert, index));
// }

View file

@ -1,30 +0,0 @@
// use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView};
// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
// use crate::rendering::Renderer;
// use super::PositionOnlyVertex2d;
// #[derive(Unique)]
// pub struct STriPrimitive(pub VertexBuffer<PositionOnlyVertex2d>, pub IndexBuffer<u16>);
// const STRI_VERTEX: &[PositionOnlyVertex2d] = &[
// PositionOnlyVertex2d { position: [-1., -1.] },
// PositionOnlyVertex2d { position: [ 3., -1.] },
// PositionOnlyVertex2d { position: [-1., 3.] },
// ];
// const STRI_INDEX: &[u16] = &[0, 1, 2];
// pub(super) fn init_stri_primitive(
// storages: AllStoragesView,
// display: NonSendSync<UniqueView<Renderer>>
// ) {
// let vert = VertexBuffer::immutable(
// &display.display,
// STRI_VERTEX
// ).unwrap();
// let index = IndexBuffer::immutable(
// &display.display,
// PrimitiveType::TrianglesList,
// STRI_INDEX
// ).unwrap();
// storages.add_unique_non_send_sync(STriPrimitive(vert, index));
// }