transition math to nalgebra and add tiny-town.

This commit is contained in:
Carter Anderson 2019-11-30 21:21:45 -08:00
parent cd60778d46
commit e204538ad7
11 changed files with 88 additions and 104 deletions

4
.gitignore vendored
View file

@ -1,3 +1,7 @@
/target
**/*.rs.bk
Cargo.lock
tiny-town/target
**/*.rs.bk
tiny-town/Cargo.lock

View file

@ -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"

View file

@ -13,7 +13,6 @@ fn main() {
// Create a query which finds all `Position` and `Velocity` components
let mut query = Read::<Transform>::query();
// // Iterate through all entities that match the query in the world
for mut trans in query.iter(&mut world) {
// println!("{} hi", trans.global);

View file

@ -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<f32> = 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<T>(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::<T>()) }
}
pub struct Application
{
entities: Vec<Entity>,
@ -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<f32>,
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::<Vec<_>>();
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<wgpu::CommandBuffer>
{
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()

View file

@ -5,3 +5,4 @@ mod temp;
pub use transform::Transform;
pub use application::Application;
pub use legion;

View file

@ -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<f32> = 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<f32>,
pub mx_world: glm::Mat4,
pub rotation_speed: f32,
pub color: wgpu::Color,
pub vertex_buf: Rc<wgpu::Buffer>,
@ -33,7 +23,7 @@ pub struct Entity {
}
pub struct Light {
pub pos: cgmath::Point3<f32>,
pub pos: glm::Vec3,
pub color: wgpu::Color,
pub fov: f32,
pub depth: Range<f32>,
@ -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<u32> {
wgpu::read_spirv(glsl_to_spirv::compile(&code, ty).unwrap()).unwrap()
}
pub fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
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::<f32>(
&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
}

View file

@ -1,16 +1,16 @@
use nalgebra::Matrix4;
use nalgebra_glm as glm;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform {
pub local: Matrix4<f32>,
pub global: Matrix4<f32>,
pub local: glm::Mat4,
pub global: glm::Mat4,
}
impl Transform {
pub fn new() -> Transform {
Transform {
local: Matrix4::<f32>::identity(),
global: Matrix4::<f32>::identity(),
local: glm::identity(),
global: glm::identity(),
}
}
}

View file

@ -73,6 +73,7 @@ pub fn create_plane(size: i8) -> (Vec<Vertex>, Vec<u16>) {
(vertex_data.to_vec(), index_data.to_vec())
}
#[allow(dead_code)]
pub fn create_texels(size: usize) -> Vec<u8> {
use std::iter;

10
tiny-town/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "tiny-town"
version = "0.1.0"
authors = ["Carter Anderson <mcanders1@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { path = "../" }

6
tiny-town/README.md Normal file
View file

@ -0,0 +1,6 @@
```bash
# run using this command
env RUSTFLAGS="-C link-arg=-fuse-ld=lld" cargo run --release
```

12
tiny-town/src/main.rs Normal file
View file

@ -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::<Transform>::query();
Application::run();
}