use bytemuck::{Pod, Zeroable};
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct VertexPNUC {
pub position: [f32; 3],
pub normal: [f32; 3],
pub uv: [f32; 2],
pub color: [f32; 4],
}
impl VertexPNUC {
pub const fn new(position: [f32; 3], normal: [f32; 3], uv: [f32; 2], color: [f32; 4]) -> Self {
Self {
position,
normal,
uv,
color,
}
}
pub const fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<VertexPNUC>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 6]>() as wgpu::BufferAddress,
shader_location: 2,
format: wgpu::VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 8]>() as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32x4,
},
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct VertexPN {
pub position: [f32; 3],
pub normal: [f32; 3],
}
impl VertexPN {
pub const fn new(position: [f32; 3], normal: [f32; 3]) -> Self {
Self { position, normal }
}
pub const fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<VertexPN>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
},
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct VertexPC {
pub position: [f32; 3],
pub color: [f32; 4],
}
impl VertexPC {
pub const fn new(position: [f32; 3], color: [f32; 4]) -> Self {
Self { position, color }
}
pub const fn from_rgb(position: [f32; 3], color: [f32; 3]) -> Self {
Self {
position,
color: [color[0], color[1], color[2], 1.0],
}
}
pub const fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<VertexPC>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x4,
},
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct VertexP {
pub position: [f32; 3],
}
impl VertexP {
pub const fn new(position: [f32; 3]) -> Self {
Self { position }
}
pub const fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<VertexP>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vertex_sizes() {
assert_eq!(std::mem::size_of::<VertexPNUC>(), 48); assert_eq!(std::mem::size_of::<VertexPN>(), 24); assert_eq!(std::mem::size_of::<VertexPC>(), 28); assert_eq!(std::mem::size_of::<VertexP>(), 12); }
#[test]
fn test_vertex_pc_from_rgb() {
let v = VertexPC::from_rgb([1.0, 2.0, 3.0], [0.5, 0.6, 0.7]);
assert_eq!(v.color[3], 1.0);
}
}