diff --git a/resources/light.mtl b/resources/light.mtl new file mode 100644 index 0000000..70d3ba1 --- /dev/null +++ b/resources/light.mtl @@ -0,0 +1,10 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl None +Ns 0 +Ka 0.000000 0.000000 0.000000 +Kd 0.8 0.8 0.8 +Ks 0.8 0.8 0.8 +d 1 +illum 2 diff --git a/resources/light.obj b/resources/light.obj new file mode 100644 index 0000000..1ad05f4 --- /dev/null +++ b/resources/light.obj @@ -0,0 +1,163 @@ +# Blender v2.79 (sub 0) OBJ File: '' +# www.blender.org +mtllib light.mtl +o Cone +v -0.739722 -0.000000 -0.429722 +v -0.739722 -0.083835 -0.421465 +v -0.739722 -0.164448 -0.397011 +v -0.739722 -0.238741 -0.357301 +v -0.739722 -0.303859 -0.303859 +v -0.739722 -0.357301 -0.238741 +v -0.739722 -0.397011 -0.164448 +v -0.739722 -0.421465 -0.083835 +v -0.739722 -0.429722 -0.000000 +v -0.739722 -0.421465 0.083835 +v -0.739722 -0.397011 0.164447 +v -0.739722 -0.357301 0.238741 +v -0.739722 -0.303859 0.303859 +v -0.739722 -0.238741 0.357301 +v -0.739722 -0.164447 0.397011 +v -0.739722 -0.083835 0.421465 +v -0.739722 0.000000 0.429722 +v -0.739722 0.083835 0.421465 +v -0.739722 0.164448 0.397011 +v -0.739722 0.238741 0.357301 +v -0.739722 0.303860 0.303859 +v -0.739722 0.357301 0.238741 +v -0.739722 0.397012 0.164447 +v 0.119722 0.000000 0.000000 +v -0.739722 0.421465 0.083834 +v -0.739722 0.429722 -0.000000 +v -0.739722 0.421465 -0.083835 +v -0.739722 0.397011 -0.164448 +v -0.739722 0.357301 -0.238741 +v -0.739722 0.303859 -0.303860 +v -0.739722 0.238740 -0.357301 +v -0.739722 0.164447 -0.397012 +v -0.739722 0.083834 -0.421465 +vn 0.4455 -0.0878 -0.8910 +vn 0.4455 -0.2599 -0.8567 +vn 0.4455 -0.4220 -0.7896 +vn 0.4455 -0.5680 -0.6921 +vn 0.4455 -0.6921 -0.5680 +vn 0.4455 -0.7896 -0.4220 +vn 0.4455 -0.8567 -0.2599 +vn 0.4455 -0.8910 -0.0878 +vn 0.4455 -0.8910 0.0878 +vn 0.4455 -0.8567 0.2599 +vn 0.4455 -0.7896 0.4220 +vn 0.4455 -0.6921 0.5680 +vn 0.4455 -0.5680 0.6921 +vn 0.4455 -0.4220 0.7896 +vn 0.4455 -0.2599 0.8567 +vn 0.4455 -0.0878 0.8910 +vn 0.4455 0.0878 0.8910 +vn 0.4455 0.2599 0.8567 +vn 0.4455 0.4220 0.7896 +vn 0.4455 0.5680 0.6921 +vn 0.4455 0.6921 0.5680 +vn 0.4455 0.7896 0.4220 +vn 0.4455 0.8567 0.2599 +vn 0.4455 0.8910 0.0878 +vn 0.4455 0.8910 -0.0878 +vn 0.4455 0.8567 -0.2599 +vn 0.4455 0.7896 -0.4220 +vn 0.4455 0.6921 -0.5680 +vn 0.4455 0.5680 -0.6921 +vn 0.4455 0.4220 -0.7896 +vn 0.4455 0.2599 -0.8567 +vn 0.4455 0.0878 -0.8910 +vn -1.0000 -0.0000 -0.0000 +usemtl None +s off +f 1//1 24//1 2//1 +f 2//2 24//2 3//2 +f 3//3 24//3 4//3 +f 4//4 24//4 5//4 +f 5//5 24//5 6//5 +f 6//6 24//6 7//6 +f 7//7 24//7 8//7 +f 8//8 24//8 9//8 +f 9//9 24//9 10//9 +f 10//10 24//10 11//10 +f 11//11 24//11 12//11 +f 12//12 24//12 13//12 +f 13//13 24//13 14//13 +f 14//14 24//14 15//14 +f 15//15 24//15 16//15 +f 16//16 24//16 17//16 +f 17//17 24//17 18//17 +f 18//18 24//18 19//18 +f 19//19 24//19 20//19 +f 20//20 24//20 21//20 +f 21//21 24//21 22//21 +f 22//22 24//22 23//22 +f 23//23 24//23 25//23 +f 25//24 24//24 26//24 +f 26//25 24//25 27//25 +f 27//26 24//26 28//26 +f 28//27 24//27 29//27 +f 29//28 24//28 30//28 +f 30//29 24//29 31//29 +f 31//30 24//30 32//30 +f 32//31 24//31 33//31 +f 33//32 24//32 1//32 +f 8//33 16//33 25//33 +f 33//33 1//33 2//33 +f 2//33 3//33 4//33 +f 4//33 5//33 6//33 +f 6//33 7//33 4//33 +f 7//33 8//33 4//33 +f 8//33 9//33 10//33 +f 10//33 11//33 8//33 +f 11//33 12//33 8//33 +f 12//33 13//33 16//33 +f 13//33 14//33 16//33 +f 14//33 15//33 16//33 +f 16//33 17//33 18//33 +f 18//33 19//33 20//33 +f 20//33 21//33 22//33 +f 22//33 23//33 25//33 +f 25//33 26//33 27//33 +f 27//33 28//33 29//33 +f 29//33 30//33 33//33 +f 30//33 31//33 33//33 +f 31//33 32//33 33//33 +f 33//33 2//33 4//33 +f 16//33 18//33 25//33 +f 18//33 20//33 25//33 +f 20//33 22//33 25//33 +f 25//33 27//33 33//33 +f 27//33 29//33 33//33 +f 33//33 4//33 8//33 +f 8//33 12//33 16//33 +f 33//33 8//33 25//33 +o Cube_Cube.001 +v -0.377020 -0.377020 0.377020 +v -0.377020 0.377020 0.377020 +v -0.377020 -0.377020 -0.377020 +v -0.377020 0.377020 -0.377020 +v 0.377020 -0.377020 0.377020 +v 0.377020 0.377020 0.377020 +v 0.377020 -0.377020 -0.377020 +v 0.377020 0.377020 -0.377020 +vn -1.0000 0.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +vn 1.0000 0.0000 0.0000 +vn 0.0000 0.0000 1.0000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 0.0000 +usemtl None +s off +f 35//34 36//34 34//34 +f 37//35 40//35 36//35 +f 41//36 38//36 40//36 +f 39//37 34//37 38//37 +f 40//38 34//38 36//38 +f 37//39 39//39 41//39 +f 35//34 37//34 36//34 +f 37//35 41//35 40//35 +f 41//36 39//36 38//36 +f 39//37 35//37 34//37 +f 40//38 38//38 34//38 +f 37//39 35//39 39//39 diff --git a/src/camera.rs b/src/camera.rs index 419bfea..194c3e6 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -157,15 +157,16 @@ impl CameraController { (1.0 * camera.pitch.0.sin() * camera.yaw.0.cos()), ); - let yaw_offset = camera.yaw.0 - Rad(PI).0; - let right_vector = Vector3::new( - (1.0 * camera.pitch.0.sin() * yaw_offset.sin()), - (1.0 * camera.pitch.0.cos()), - (1.0 * camera.pitch.0.sin() * yaw_offset.cos()), + let offset = camera.yaw.0 + PI/2.0; + let pitch = PI/2.0; + let left_vector = Vector3::new( + (1.0 * pitch.sin() * offset.sin()), + (1.0 * pitch.cos()), + (1.0 * pitch.sin() * offset.cos()), ); camera.position += view_vector * (self.amount_forward - self.amount_backward) * self.speed * dt; - camera.position += right_vector * (self.amount_right - self.amount_left) * self.speed * dt; + camera.position += left_vector * (self.amount_left - self.amount_right) * self.speed * dt; // I'm not a huge fan of this // // Move in/out (aka. "zoom") diff --git a/src/components.rs b/src/components.rs index bfbb436..6bc1358 100644 --- a/src/components.rs +++ b/src/components.rs @@ -44,13 +44,7 @@ pub struct RangeCopy { pub end: Idx, } -#[derive(Clone, Debug)] -pub struct DirectionalLight { - pub color: wgpu::Color, - pub fov: f32, - pub depth: RangeCopy, - pub target_view: Arc, -} + #[derive(Clone, Debug)] pub struct Mesh { diff --git a/src/light.rs b/src/light.rs index 71d3105..b6e9081 100644 --- a/src/light.rs +++ b/src/light.rs @@ -2,7 +2,9 @@ use bytemuck::__core::ops::Range; use bytemuck::{Zeroable, Pod}; use cgmath::Point3; use crate::render::OPENGL_TO_WGPU_MATRIX; -use crate::components::DirectionalLight; +use std::sync::Arc; +use wgpu::TextureView; +use crate::components::RangeCopy; #[repr(C)] @@ -17,6 +19,14 @@ unsafe impl Pod for LightRaw {} unsafe impl Zeroable for LightRaw {} +#[derive(Clone, Debug)] +pub struct DirectionalLight { + pub color: wgpu::Color, + pub fov: f32, + pub depth: RangeCopy, + pub target_view: Arc, +} + impl DirectionalLight { pub fn to_raw(&self, pos: Point3) -> LightRaw { use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3}; diff --git a/src/main.rs b/src/main.rs index ad1f3c8..5bc6f71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,7 +131,7 @@ fn main() { resources.insert(LoopState { delta_time: Default::default(), start_time: Instant::now(), - step_size: 0.005, + step_size: 0.01666, // 60hz }); @@ -181,11 +181,8 @@ fn main() { let mut current_time: f32 = elapsed_time; event_loop.run(move |event, _, control_flow| { - // Artificially slows the loop rate to 10 millis - // This is called after redraw events cleared - //*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10)); - *control_flow = ControlFlow::Poll; + *control_flow = ControlFlow::Poll; match event { event::Event::NewEvents(cause) => { event_schedule.execute(&mut world, &mut resources); @@ -256,15 +253,30 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) { yaw: Rad(-PI), pitch: Rad(PI/2.0) }, + Color { + r: 1.0, + g: 1.0, + b: 0.5, + a: 1.0, + }, CameraController::new(3.0, 1.0), )); + let light_mesh = renderer.load_mesh_to_buffer("./resources/light.obj"); + let light_entity: Entity = world.push(( cgmath::Point3 { - x: 7.0 as f32, - y: -5.0 as f32, - z: 10.0 as f32, + x: 5.0 as f32, + y: 5.0 as f32, + z: 5.0 as f32, + }, + Color { + r: 1.0, + g: 0.0, + b: 1.0, + a: 1.0, }, + light_mesh.clone(), renderer.create_light(), )); @@ -274,6 +286,7 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) { y: 7.0 as f32, z: 10.0 as f32, }, + light_mesh, renderer.create_light(), )); @@ -297,9 +310,9 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) { }, monkey_mesh, Color { - r: 1.0, - g: 0.5, - b: 0.5, + r: 0.0, + g: 1.0, + b: 0.0, a: 1.0, }, )); diff --git a/src/render.rs b/src/render.rs index 13297f2..dd4692c 100644 --- a/src/render.rs +++ b/src/render.rs @@ -21,9 +21,9 @@ use winit::platform::unix::x11::ffi::Time; use winit::window::Window; use crate::camera::{Camera, CameraController}; -use crate::components::{Color, DirectionalLight, Mesh, Position, RangeCopy}; +use crate::components::{Color, Mesh, Position, RangeCopy}; use crate::geometry::{create_plane, import_mesh, vertex, Vertex}; -use crate::light::LightRaw; +use crate::light::{LightRaw, DirectionalLight}; #[cfg_attr(rustfmt, rustfmt_skip)] #[allow(unused)] @@ -267,8 +267,8 @@ impl Renderer { cgmath::perspective( cgmath::Deg(75f32), self.size.width as f32 / self.size.height as f32, - 1.0, - 20.0, + 0.1, + 100.0, ) } @@ -340,46 +340,6 @@ impl Renderer { } } - /// I don't know why the hell this is still in here - pub fn create_plane(&self, size: f32) -> Mesh { - let vertices = [ - vertex([size, -size, 0.0], [0.0, 0.0, 1.0]), - vertex([size, size, 0.0], [0.0, 0.0, 1.0]), - vertex([-size, -size, 0.0], [0.0, 0.0, 1.0]), - vertex([-size, size, 0.0], [0.0, 0.0, 1.0]), - ]; - - let indices: &[u32] = &[0, 1, 2, 2, 1, 3]; - - let index_count = indices.len(); - let (vertex_buf, index_buf) = - Renderer::create_buffer(&self.device, indices.to_vec(), vertices.to_vec()); - - let uniform_buf = Arc::new(self.device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Plane Uniform Buf"), - size: mem::size_of::() as wgpu::BufferAddress, - usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, - mapped_at_creation: false, - })); - - let bind_group = Arc::new(self.device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &self.entity_bind_group_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)), - }], - label: Some("Plane Bind Group"), - })); - - Mesh { - index_buffer: index_buf, - index_count: index_count, - vertex_buffer: vertex_buf, - uniform_buffer: uniform_buf, - bind_group: bind_group, - } - } - pub fn load_mesh_to_buffer(&self, filepath: &str) -> Mesh { let (vertices, indices) = import_mesh(filepath); let index_count = indices.len(); @@ -411,7 +371,6 @@ impl Renderer { } pub fn init(window: &Window) -> Renderer { - log::info!("Initializing the surface..."); // Grab the GPU instance, and query its features let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); @@ -428,11 +387,9 @@ impl Renderer { }); let adapter = futures::executor::block_on(adapter).unwrap(); - let optional_features = Renderer::optional_features(); let required_features = Renderer::required_features(); let adapter_features = adapter.features(); - let needed_limits = wgpu::Limits::default(); //Renderer::required_limits(); // Maybe for debug tracing??? @@ -453,39 +410,6 @@ impl Renderer { let queue = Arc::new(queue); let device = Arc::new(device); - // This is some gross-ass web shit - /*#[cfg(target_arch = "wasm32")] - let spawner = { - use futures::{future::LocalFutureObj, task::SpawnError}; - use winit::platform::web::WindowExtWebSys; - - struct WebSpawner {} - impl LocalSpawn for WebSpawner { - fn spawn_local_obj( - &self, - future: LocalFutureObj<'static, ()>, - ) -> Result<(), SpawnError> { - Ok(wasm_bindgen_futures::spawn_local(future)) - } - } - - std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - - // On wasm, append the canvas to the document body - web_sys::window() - .and_then(|win| win.document()) - .and_then(|doc| doc.body()) - .and_then(|body| { - body.append_child(&web_sys::Element::from(window.canvas())) - .ok() - }) - .expect("couldn't append canvas to document body"); - - WebSpawner {} - };*/ - - log::info!("Done doing the loading part..."); - let mut sc_desc = (wgpu::SwapChainDescriptor { // Allows a texture to be a output attachment of a renderpass. usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,