Use floats for vertex values

This commit is contained in:
Carter Anderson 2019-12-09 23:12:50 -08:00
parent 5c1ce0597f
commit aa2f7e9789
6 changed files with 76 additions and 46 deletions

View file

@ -167,7 +167,7 @@ fn main() {
let mut scheduler = SystemScheduler::<ApplicationStage>::new();
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane{ size: 25 });
let plane = Mesh::load(MeshType::Plane{ size: 24.5 });
let mut mesh_storage = AssetStorage::<Mesh, MeshType>::new();
let _cube_handle = mesh_storage.add(cube, "cube");

View file

@ -62,13 +62,13 @@ impl Application {
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Char4,
format: wgpu::VertexFormat::Float4,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Char4,
offset: 4 * 1,
format: wgpu::VertexFormat::Float4,
offset: 4 * 4,
shader_location: 1,
},
],

View file

@ -1,7 +1,7 @@
#version 450
layout(location = 0) in ivec4 a_Pos;
layout(location = 1) in ivec4 a_Normal;
layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec4 a_Normal;
layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position;

View file

@ -5,7 +5,7 @@ use zerocopy::AsBytes;
pub enum MeshType {
Cube,
Plane {
size: i8
size: f32
}
}
@ -44,45 +44,38 @@ impl Asset<MeshType> for Mesh {
}
}
pub fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex {
Vertex {
pos: [pos[0], pos[1], pos[2], 1],
normal: [nor[0], nor[1], nor[2], 0],
}
}
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
let vertex_data = [
// top (0, 0, 1)
vertex([-1, -1, 1], [0, 0, 1]),
vertex([1, -1, 1], [0, 0, 1]),
vertex([1, 1, 1], [0, 0, 1]),
vertex([-1, 1, 1], [0, 0, 1]),
Vertex::from(([-1, -1, 1], [0, 0, 1])),
Vertex::from(([1, -1, 1], [0, 0, 1])),
Vertex::from(([1, 1, 1], [0, 0, 1])),
Vertex::from(([-1, 1, 1], [0, 0, 1])),
// bottom (0, 0, -1)
vertex([-1, 1, -1], [0, 0, -1]),
vertex([1, 1, -1], [0, 0, -1]),
vertex([1, -1, -1], [0, 0, -1]),
vertex([-1, -1, -1], [0, 0, -1]),
Vertex::from(([-1, 1, -1], [0, 0, -1])),
Vertex::from(([1, 1, -1], [0, 0, -1])),
Vertex::from(([1, -1, -1], [0, 0, -1])),
Vertex::from(([-1, -1, -1], [0, 0, -1])),
// right (1, 0, 0)
vertex([1, -1, -1], [1, 0, 0]),
vertex([1, 1, -1], [1, 0, 0]),
vertex([1, 1, 1], [1, 0, 0]),
vertex([1, -1, 1], [1, 0, 0]),
Vertex::from(([1, -1, -1], [1, 0, 0])),
Vertex::from(([1, 1, -1], [1, 0, 0])),
Vertex::from(([1, 1, 1], [1, 0, 0])),
Vertex::from(([1, -1, 1], [1, 0, 0])),
// left (-1, 0, 0)
vertex([-1, -1, 1], [-1, 0, 0]),
vertex([-1, 1, 1], [-1, 0, 0]),
vertex([-1, 1, -1], [-1, 0, 0]),
vertex([-1, -1, -1], [-1, 0, 0]),
Vertex::from(([-1, -1, 1], [-1, 0, 0])),
Vertex::from(([-1, 1, 1], [-1, 0, 0])),
Vertex::from(([-1, 1, -1], [-1, 0, 0])),
Vertex::from(([-1, -1, -1], [-1, 0, 0])),
// front (0, 1, 0)
vertex([1, 1, -1], [0, 1, 0]),
vertex([-1, 1, -1], [0, 1, 0]),
vertex([-1, 1, 1], [0, 1, 0]),
vertex([1, 1, 1], [0, 1, 0]),
Vertex::from(([1, 1, -1], [0, 1, 0])),
Vertex::from(([-1, 1, -1], [0, 1, 0])),
Vertex::from(([-1, 1, 1], [0, 1, 0])),
Vertex::from(([1, 1, 1], [0, 1, 0])),
// back (0, -1, 0)
vertex([1, -1, 1], [0, -1, 0]),
vertex([-1, -1, 1], [0, -1, 0]),
vertex([-1, -1, -1], [0, -1, 0]),
vertex([1, -1, -1], [0, -1, 0]),
Vertex::from(([1, -1, 1], [0, -1, 0])),
Vertex::from(([-1, -1, 1], [0, -1, 0])),
Vertex::from(([-1, -1, -1], [0, -1, 0])),
Vertex::from(([1, -1, -1], [0, -1, 0])),
];
let index_data: &[u16] = &[
@ -97,12 +90,12 @@ pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
(vertex_data.to_vec(), index_data.to_vec())
}
pub fn create_plane(size: i8) -> (Vec<Vertex>, Vec<u16>) {
pub fn create_plane(size: f32) -> (Vec<Vertex>, Vec<u16>) {
let vertex_data = [
vertex([size, -size, 0], [0, 0, 1]),
vertex([size, size, 0], [0, 0, 1]),
vertex([-size, -size, 0], [0, 0, 1]),
vertex([-size, size, 0], [0, 0, 1]),
Vertex::from(([size, -size, 0.0], [0.0, 0.0, 1.0])),
Vertex::from(([size, size, 0.0], [0.0, 0.0, 1.0])),
Vertex::from(([-size, -size, 0.0], [0.0, 0.0, 1.0])),
Vertex::from(([-size, size, 0.0], [0.0, 0.0, 1.0])),
];
let index_data: &[u16] = &[0, 1, 2, 2, 1, 3];

View file

@ -1,6 +1,6 @@
#version 450
layout(location = 0) in ivec4 a_Pos;
layout(location = 0) in vec4 a_Pos;
layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;

View file

@ -1,8 +1,45 @@
use zerocopy::{AsBytes, FromBytes};
use std::convert::From;
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct Vertex {
pub pos: [i8; 4],
pub normal: [i8; 4],
pub position: [f32; 4],
pub normal: [f32; 4],
}
impl From<([f32; 4], [f32; 4])> for Vertex {
fn from((position, normal): ([f32; 4], [f32; 4])) -> Self {
Vertex {
position: position,
normal: normal,
}
}
}
impl From<([f32; 3], [f32; 3])> for Vertex {
fn from((position, normal): ([f32; 3], [f32; 3])) -> Self {
Vertex {
position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0],
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
}
}
}
impl From<([i8; 4], [i8; 4])> for Vertex {
fn from((position, normal): ([i8; 4], [i8; 4])) -> Self {
Vertex {
position: [position[0] as f32, position[1] as f32, position[2] as f32, position[3] as f32],
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, normal[3] as f32],
}
}
}
impl From<([i8; 3], [i8; 3])> for Vertex {
fn from((position, normal): ([i8; 3], [i8; 3])) -> Self {
Vertex {
position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0],
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
}
}
}