diff --git a/.gitignore b/.gitignore index 693699042b1a8..3f0dd1c706aa5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /target **/*.rs.bk Cargo.lock + +tiny-town/target +**/*.rs.bk +tiny-town/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 4ad3a922f49e0..8b9a1102f0791 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,10 @@ edition = "2018" [dependencies] legion = { git = "https://github.com/TomGillen/legion.git" } -nalgebra = "0.18" +nalgebra-glm = "0.5.0" wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "44fa1bc2fa208fa92f80944253e0da56cb7ac1fe"} winit = "0.20.0-alpha4" glsl-to-spirv = "0.1" -cgmath = "0.17" zerocopy = "0.2" log = "0.4" env_logger = "0.7" \ No newline at end of file diff --git a/examples/simple.rs b/examples/simple.rs index df72902b34a86..fc4b0f001dc5f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -13,7 +13,6 @@ fn main() { // Create a query which finds all `Position` and `Velocity` components let mut query = Read::::query(); - // // Iterate through all entities that match the query in the world for mut trans in query.iter(&mut world) { // println!("{} hi", trans.global); diff --git a/src/application.rs b/src/application.rs index c25cef7fb0bd3..d68e4fd01a54f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -4,7 +4,8 @@ use winit::{ event_loop::{ControlFlow, EventLoop}, }; -use zerocopy::{AsBytes, FromBytes}; +use zerocopy::AsBytes; +use nalgebra_glm as glm; use std::rc::Rc; use std::mem; @@ -12,23 +13,6 @@ use std::mem; use crate::temp::*; use crate::vertex::*; -#[cfg_attr(rustfmt, rustfmt_skip)] -#[allow(unused)] -pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( - 1.0, 0.0, 0.0, 0.0, - 0.0, -1.0, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.0, 0.0, 0.5, 1.0, -); - -#[allow(dead_code)] -pub fn cast_slice(data: &[T]) -> &[u8] { - use std::mem::size_of; - use std::slice::from_raw_parts; - - unsafe { from_raw_parts(data.as_ptr() as *const u8, data.len() * size_of::()) } -} - pub struct Application { entities: Vec, @@ -38,6 +22,8 @@ pub struct Application forward_pass: Pass, forward_depth: wgpu::TextureView, light_uniform_buf: wgpu::Buffer, + camera_position: glm::Vec3, + camera_fov: f32, } impl Application { @@ -88,8 +74,6 @@ impl Application { }); let mut entities = vec![{ - use cgmath::SquareMatrix; - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &local_bind_group_layout, bindings: &[wgpu::Binding { @@ -101,7 +85,7 @@ impl Application { }], }); Entity { - mx_world: cgmath::Matrix4::identity(), + mx_world: glm::identity(), rotation_speed: 0.0, color: wgpu::Color::WHITE, vertex_buf: Rc::new(plane_vertex_buf), @@ -112,53 +96,39 @@ impl Application { } }]; + let camera_position = glm::vec3(3.0f32, -10.0, 6.0); + let camera_fov = glm::quarter_pi(); + struct CubeDesc { - offset: cgmath::Vector3, - angle: f32, - scale: f32, + offset: glm::Vec3, rotation: f32, } let cube_descs = [ CubeDesc { - offset: cgmath::vec3(-2.0, -2.0, 2.0), - angle: 10.0, - scale: 0.7, + offset: glm::vec3(-2.0, -2.0, 2.0), rotation: 0.1, }, CubeDesc { - offset: cgmath::vec3(2.0, -2.0, 2.0), - angle: 50.0, - scale: 1.3, + offset: glm::vec3(2.0, -2.0, 2.0), rotation: 0.2, }, CubeDesc { - offset: cgmath::vec3(-2.0, 2.0, 2.0), - angle: 140.0, - scale: 1.1, + offset: glm::vec3(-2.0, 2.0, 2.0), rotation: 0.3, }, CubeDesc { - offset: cgmath::vec3(2.0, 2.0, 2.0), - angle: 210.0, - scale: 0.9, + offset: glm::vec3(2.0, 2.0, 2.0), rotation: 0.4, }, ]; for cube in &cube_descs { - use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3}; - - 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 { size: entity_uniform_size, usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, }); entities.push(Entity { - mx_world: cgmath::Matrix4::from(transform), + mx_world: glm::translation(&cube.offset), rotation_speed: cube.rotation, color: wgpu::Color::GREEN, vertex_buf: Rc::clone(&cube_vertex_buf), @@ -217,26 +187,26 @@ impl Application { .collect::>(); let lights = vec![ Light { - pos: cgmath::Point3::new(7.0, -5.0, 10.0), + pos: glm::vec3(7.0, -5.0, 10.0), color: wgpu::Color { r: 0.5, g: 1.0, b: 0.5, a: 1.0, }, - fov: 60.0, + fov: f32::to_radians(60.0), depth: 1.0 .. 20.0, target_view: shadow_target_views[0].take().unwrap(), }, Light { - pos: cgmath::Point3::new(-5.0, 7.0, 10.0), + pos: glm::vec3(-5.0, 7.0, 10.0), color: wgpu::Color { r: 1.0, g: 0.5, b: 0.5, a: 1.0, }, - fov: 45.0, + fov: f32::to_radians(45.0), depth: 1.0 .. 20.0, target_view: shadow_target_views[1].take().unwrap(), }, @@ -383,7 +353,7 @@ impl Application { bind_group_layouts: &[&bind_group_layout, &local_bind_group_layout], }); - let mx_total = generate_matrix(sc_desc.width as f32 / sc_desc.height as f32); + let mx_total = generate_matrix(&camera_position, camera_fov, sc_desc.width as f32 / sc_desc.height as f32, 1.0, 20.0); let forward_uniforms = ForwardUniforms { proj: *mx_total.as_ref(), num_lights: [lights.len() as u32, 0, 0, 0], @@ -501,6 +471,8 @@ impl Application { forward_pass, forward_depth: depth_texture.create_default_view(), light_uniform_buf, + camera_position, + camera_fov }; (this, None) } @@ -512,8 +484,8 @@ impl Application { ) -> Option { let command_buf = { - let mx_total = generate_matrix(sc_desc.width as f32 / sc_desc.height as f32); - let mx_ref: &[f32; 16] = mx_total.as_ref(); + let mx_total = generate_matrix(&self.camera_position, self.camera_fov, sc_desc.width as f32 / sc_desc.height as f32, 1.0, 20.0); + let mx_ref: [[f32; 4]; 4] = mx_total.into(); let temp_buf = device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC); @@ -541,7 +513,7 @@ impl Application { Some(command_buf) } - fn update(&mut self, event: WindowEvent) + fn update(&mut self, _: WindowEvent) { } @@ -559,7 +531,6 @@ impl Application { let temp_buf_data = device .create_buffer_mapped(self.entities.len() * size, wgpu::BufferUsage::COPY_SRC); - // FIXME: Align and use `LayoutVerified` for (entity, slot) in self .entities .iter_mut() @@ -567,7 +538,7 @@ impl Application { { if entity.rotation_speed != 0.0 { let rotation = - cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed)); + glm::rotation(entity.rotation_speed, &glm::vec3(0.0, 1.0, 0.0)); entity.mx_world = entity.mx_world * rotation; } slot.copy_from_slice( @@ -603,7 +574,6 @@ impl Application { let total_size = size * self.lights.len(); let temp_buf_data = device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC); - // FIXME: Align and use `LayoutVerified` for (light, slot) in self .lights .iter() diff --git a/src/lib.rs b/src/lib.rs index 90d6d3e58c380..7b424913ad4cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,5 @@ mod vertex; mod temp; pub use transform::Transform; -pub use application::Application; \ No newline at end of file +pub use application::Application; +pub use legion; \ No newline at end of file diff --git a/src/temp.rs b/src/temp.rs index e5527a9a75611..00766e3c3e9b2 100644 --- a/src/temp.rs +++ b/src/temp.rs @@ -1,28 +1,18 @@ pub use std::rc::Rc; pub use std::ops::Range; use zerocopy::{AsBytes, FromBytes}; +use nalgebra_glm as glm; -pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( - 1.0, - 0.0, - 0.0, - 0.0, - 0.0, - -1.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.5, - 0.0, - 0.0, - 0.0, - 0.5, - 1.0, -); - +pub fn opengl_to_wgpu_matrix() -> glm::Mat4 { + glm::mat4( + 1.0, 0.0, 0.0, 0.0, + 0.0, -1.0, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.0, 0.0, 0.5, 1.0, + ) +} pub struct Entity { - pub mx_world: cgmath::Matrix4, + pub mx_world: glm::Mat4, pub rotation_speed: f32, pub color: wgpu::Color, pub vertex_buf: Rc, @@ -33,7 +23,7 @@ pub struct Entity { } pub struct Light { - pub pos: cgmath::Point3, + pub pos: glm::Vec3, pub color: wgpu::Color, pub fov: f32, pub depth: Range, @@ -50,19 +40,8 @@ pub struct LightRaw { impl Light { pub fn to_raw(&self) -> LightRaw { - use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3}; - - let mx_view = Matrix4::look_at(self.pos, Point3::origin(), Vector3::unit_z()); - let projection = PerspectiveFov { - fovy: Deg(self.fov).into(), - aspect: 1.0, - near: self.depth.start, - far: self.depth.end, - }; - let mx_view_proj = OPENGL_TO_WGPU_MATRIX * - cgmath::Matrix4::from(projection.to_perspective()) * mx_view; LightRaw { - proj: *mx_view_proj.as_ref(), + proj: generate_matrix(&self.pos, self.fov, 1.0, self.depth.start, self.depth.end).into(), pos: [self.pos.x, self.pos.y, self.pos.z, 1.0], color: [ self.color.r as f32, @@ -99,6 +78,7 @@ pub struct Pass { pub uniform_buf: wgpu::Buffer, } +#[allow(dead_code)] pub enum ShaderStage { Vertex, Fragment, @@ -115,12 +95,14 @@ pub fn load_glsl(code: &str, stage: ShaderStage) -> Vec { wgpu::read_spirv(glsl_to_spirv::compile(&code, ty).unwrap()).unwrap() } -pub fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0); - let mx_view = cgmath::Matrix4::look_at( - cgmath::Point3::new(3.0f32, -10.0, 6.0), - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_z(), +pub fn generate_matrix(eye: &glm::Vec3, fov: f32, aspect_ratio: f32, near: f32, far: f32) -> glm::Mat4 { + let projection = glm::perspective(aspect_ratio, fov, near, far); + + let view = glm::look_at_rh::( + &eye, + &glm::vec3(0.0, 0.0, 0.0), + &glm::vec3(0.0, 0.0, 1.0), ); - OPENGL_TO_WGPU_MATRIX * mx_projection * mx_view + + opengl_to_wgpu_matrix() * projection * view } diff --git a/src/transform.rs b/src/transform.rs index 45bb6d2f598cf..4d1abcee27d67 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -1,16 +1,16 @@ -use nalgebra::Matrix4; +use nalgebra_glm as glm; #[derive(Clone, Copy, Debug, PartialEq)] pub struct Transform { - pub local: Matrix4, - pub global: Matrix4, + pub local: glm::Mat4, + pub global: glm::Mat4, } impl Transform { pub fn new() -> Transform { Transform { - local: Matrix4::::identity(), - global: Matrix4::::identity(), + local: glm::identity(), + global: glm::identity(), } } } \ No newline at end of file diff --git a/src/vertex.rs b/src/vertex.rs index 80e4435d7d162..bad5a92f8dac2 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -73,6 +73,7 @@ pub fn create_plane(size: i8) -> (Vec, Vec) { (vertex_data.to_vec(), index_data.to_vec()) } +#[allow(dead_code)] pub fn create_texels(size: usize) -> Vec { use std::iter; diff --git a/tiny-town/Cargo.toml b/tiny-town/Cargo.toml new file mode 100644 index 0000000000000..499dc41f5904b --- /dev/null +++ b/tiny-town/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "tiny-town" +version = "0.1.0" +authors = ["Carter Anderson "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bevy = { path = "../" } \ No newline at end of file diff --git a/tiny-town/README.md b/tiny-town/README.md new file mode 100644 index 0000000000000..0d9d87bdc82b6 --- /dev/null +++ b/tiny-town/README.md @@ -0,0 +1,6 @@ + + +```bash +# run using this command +env RUSTFLAGS="-C link-arg=-fuse-ld=lld" cargo run --release +``` \ No newline at end of file diff --git a/tiny-town/src/main.rs b/tiny-town/src/main.rs new file mode 100644 index 0000000000000..4cabc14e0f8ba --- /dev/null +++ b/tiny-town/src/main.rs @@ -0,0 +1,12 @@ +use bevy::*; +use bevy::legion::prelude::*; + +fn main() { + let universe = Universe::new(); + let mut world = universe.create_world(); + world.insert((), vec![(Transform::new(),)]); + + // Create a query which finds all `Position` and `Velocity` components + // let mut query = Read::::query(); + Application::run(); +} \ No newline at end of file