From e2c459872f77adf482527097f2f899dcfb049492 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 4 Feb 2021 20:04:52 -0800 Subject: [PATCH] cleanup. bringing in ncollider --- src/main.rs | 150 +++++++++++++++++-------------------- src/render.rs | 6 +- src/runtime.rs | 199 ------------------------------------------------- 3 files changed, 73 insertions(+), 282 deletions(-) delete mode 100644 src/runtime.rs diff --git a/src/main.rs b/src/main.rs index f5b24fc..26e9336 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,6 @@ mod framework; mod geometry; mod light; mod render; -mod runtime; /* @@ -78,17 +77,6 @@ pub enum ShaderStage { Compute, } -/* - window: winit::window::Window, - event_loop: EventLoop<()>, - instance: wgpu::Instance, - size: winit::dpi::PhysicalSize, - surface: wgpu::Surface, - adapter: wgpu::Adapter, - device: wgpu::Device, - queue: wgpu::Queue, -*/ - // a component is any type that is 'static, sized, send and sync #[derive(Clone, Copy, Debug, PartialEq)] pub struct Position { @@ -217,76 +205,9 @@ fn main() { // Load up the renderer (and the resources) let mut renderer = render::Renderer::init(window); - let untitled_mesh = - renderer.load_mesh_to_buffer("./resources/monkey.obj"); - - // This could be used for relationships between entities...??? - let light_entity: Entity = world.push(( - cgmath::Point3 { - x: 7.0 as f32, - y: -5.0 as f32, - z: 10.0 as f32, - }, - renderer.create_light(), - )); - - let light_entity: Entity = world.push(( - cgmath::Point3 { - x: -5.0 as f32, - y: 7.0 as f32, - z: 10.0 as f32, - }, - renderer.create_light(), - )); - - let offset = cgmath::vec3(2.0, 2.0, 2.0); - let transform = Decomposed { - disp: offset.clone(), - rot: Quaternion::from_axis_angle(offset.normalize(), Deg(50.0)), - scale: 1.0, - }; - - let mesh_entity: Entity = world.push(( - Position { - x: 17.0, - y: 15.0, - z: 10.0, - mx: cgmath::Matrix4::from(transform), - }, - untitled_mesh, - Color { - r: 1.0, - g: 0.5, - b: 0.5, - a: 1.0, - }, - )); - - let plane_mesh = - renderer.create_plane(7.0); - - // let plane_entity: Entity = world.push(( - // Position { - // x: 0.0, - // y: 0.0, - // z: 0.0, - // mx: cgmath::Matrix4::identity(), - // }, - // plane_mesh, - // Color { - // r: 1.0, - // g: 0.5, - // b: 0.5, - // a: 1.0, - // }, - // )); - - - // Init, this wants the references to the buffers... - let mut runtime = runtime::Runtime::init(); + entity_loading(&mut world, &mut renderer); let mut resources = Resources::default(); - resources.insert(runtime); resources.insert(renderer); // This is just an winit event loop @@ -372,3 +293,72 @@ fn main() { } }); } + + +pub fn entity_loading(world: &mut World, renderer: &mut Renderer) { + + let untitled_mesh = + renderer.load_mesh_to_buffer("./resources/monkey.obj"); + + // This could be used for relationships between entities...??? + let light_entity: Entity = world.push(( + cgmath::Point3 { + x: 7.0 as f32, + y: -5.0 as f32, + z: 10.0 as f32, + }, + renderer.create_light(), + )); + + let light_entity: Entity = world.push(( + cgmath::Point3 { + x: -5.0 as f32, + y: 7.0 as f32, + z: 10.0 as f32, + }, + renderer.create_light(), + )); + + let offset = cgmath::vec3(2.0, 2.0, 2.0); + let transform = Decomposed { + disp: offset.clone(), + rot: Quaternion::from_axis_angle(offset.normalize(), Deg(50.0)), + scale: 1.0, + }; + + let mesh_entity: Entity = world.push(( + Position { + x: 17.0, + y: 15.0, + z: 10.0, + mx: cgmath::Matrix4::from(transform), + }, + untitled_mesh, + Color { + r: 1.0, + g: 0.5, + b: 0.5, + a: 1.0, + }, + )); + + let plane_mesh = + renderer.create_plane(7.0); + + let plane_entity: Entity = world.push(( + Position { + x: 0.0, + y: 0.0, + z: 0.0, + mx: cgmath::Matrix4::identity(), + }, + plane_mesh, + Color { + r: 1.0, + g: 0.5, + b: 0.5, + a: 1.0, + }, + )); + +} \ No newline at end of file diff --git a/src/render.rs b/src/render.rs index 7864456..1d4e36b 100644 --- a/src/render.rs +++ b/src/render.rs @@ -118,8 +118,8 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { // Update the entity uniforms for (pos, mesh, color) in query.iter_mut(world) { - let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(2.0)); - pos.mx = pos.mx * rotation; + // let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(2.0)); + // pos.mx = pos.mx * rotation; let data = EntityUniforms { model: pos.mx.into(), @@ -138,7 +138,7 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) { } if renderer.lights_are_dirty { - //renderer.lights_are_dirty = false; + renderer.lights_are_dirty = false; let mut query = <(&mut DirectionalLight, &mut Point3)>::query(); for (i, (light, pos)) in query.iter_mut(world).enumerate() { renderer.queue.write_buffer( diff --git a/src/runtime.rs b/src/runtime.rs deleted file mode 100644 index f5b6c3b..0000000 --- a/src/runtime.rs +++ /dev/null @@ -1,199 +0,0 @@ -use std::rc::Rc; - -use bytemuck::__core::mem; -use bytemuck::__core::num::NonZeroU32; -use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3, SquareMatrix}; - -use crate::render::EntityUniforms; -use crate::DirectionalLight; - -/* - -This will eventually be within an ECS... - -So I will probably take the same approach that I did for tracer and have meta-data for rendering -held by the ECS, and a render system which will cycle through them and render - - */ - -struct Entity { - mx_world: cgmath::Matrix4, - rotation_speed: f32, - color: wgpu::Color, - - index_count: usize, - bind_group: wgpu::BindGroup, - // This is a little weird to have in the entity isn't it? - - // uniform buf is tough... - uniform_buf: wgpu::Buffer, - vertex_buf: Rc, - index_buf: Rc, -} - -pub struct Runtime { - entities: Vec, - // This is going to be ECS'd - lights: Vec, // ECS -} - -impl Runtime { - pub fn init( - // sc_desc: &wgpu::SwapChainDescriptor, - // device: &wgpu::Device, - // _queue: &wgpu::Queue, - ) -> Self - { - - // https://sotrh.github.io/learn-wgpu/beginner/tutorial5-textures/#the-bindgroup - // It appears like bindgroups are the shader input definitions - /* - But it is defined in multiples places... - - ` ` - - one of these in each pass - let bind_group_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - entries: &[ - wgpu::BindGroupLayoutEntry { - - - - The entities have one - - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &local_bind_group_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer(plane_uniform_buf.slice(..)), - }], - label: None, - }); - */ - - // Defines the Uniform buffer for the Vertex and Fragment shaders - // let local_bind_group_layout = - // device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - // entries: &[wgpu::BindGroupLayoutEntry { - // binding: 0, - // visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT, - // ty: wgpu::BindingType::UniformBuffer { - // dynamic: false, - // min_binding_size: wgpu::BufferSize::new( - // mem::size_of::() as _ - // ), - // }, - // count: None, - // }], - // label: None, - // }); - - - let mut entities = Vec::default(); - - // entities.push(Entity { - // mx_world: cgmath::Matrix4::identity(), - // rotation_speed: 0.0, - // color: wgpu::Color::WHITE, - // vertex_buf: Rc::new(plane_vertex_buf), - // index_buf: Rc::new(plane_index_buf), - // index_count: plane_index_data.len(), - // bind_group: device.create_bind_group(&wgpu::BindGroupDescriptor { - // layout: &local_bind_group_layout, - // entries: &[wgpu::BindGroupEntry { - // binding: 0, - // resource: wgpu::BindingResource::Buffer(plane_uniform_buf.slice(..)), - // }], - // label: None, - // }), - // uniform_buf: plane_uniform_buf, - // }); - - - struct CubeDesc { - offset: cgmath::Vector3, - angle: f32, - scale: f32, - rotation: f32, - } - - let cube_descs = [ - CubeDesc { - offset: cgmath::vec3(-2.0, -2.0, 2.0), - angle: 10.0, - scale: 0.7, - rotation: 1.5, - }, - ]; - - - // for cube in &cube_descs { - // let transform = Decomposed { - // disp: cube.offset.clone(), - // rot: Quaternion::from_axis_angle(cube.offset.normalize(), Deg(cube.angle)), - // scale: cube.scale, - // }; - // let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { - // label: None, - // size: entity_uniform_size, - // usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, - // mapped_at_creation: false, - // }); - // entities.push(Entity { - // mx_world: cgmath::Matrix4::from(transform), - // rotation_speed: cube.rotation, - // color: wgpu::Color::GREEN, - // vertex_buf: Rc::clone(&cube_vertex_buf), - // index_buf: Rc::clone(&cube_index_buf), - // index_count: cube_index_data.len(), - // bind_group: device.create_bind_group(&wgpu::BindGroupDescriptor { - // layout: &local_bind_group_layout, - // entries: &[wgpu::BindGroupEntry { - // binding: 0, - // resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)), - // }], - // label: None, - // }), - // uniform_buf, - // }); - //} - -// Create other resources - -// This is just metadata we hold for the lights. We can hold onto this - let lights = vec![ - // Light { - // pos: cgmath::Point3::new(7.0, -5.0, 10.0), - // color: wgpu::Color { - // r: 0.5, - // g: 1.0, - // b: 0.5, - // a: 1.0, - // }, - // fov: 60.0, - // depth: 1.0..20.0, - // }, - // Light { - // pos: cgmath::Point3::new(-5.0, 7.0, 10.0), - // color: wgpu::Color { - // r: 1.0, - // g: 0.5, - // b: 0.5, - // a: 1.0, - // }, - // fov: 45.0, - // depth: 1.0..20.0, - // }, - ]; - - Runtime { - entities, - lights, - } - } - - pub fn update(&mut self, _event: winit::event::WindowEvent) { - //empty - } -} \ No newline at end of file