bezier/src/vertex.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2024-06-06 22:40:28 +07:00
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
position: [f32; 2],
}
impl Vertex {
2024-06-10 20:54:56 +07:00
pub fn new(position: [f32; 2]) -> Vertex {
Vertex { position }
2024-06-06 22:40:28 +07:00
}
2024-06-10 20:54:56 +07:00
pub fn new_f64(position: [f64; 2]) -> Vertex {
Vertex {
position: position.map(|x| x as f32),
}
2024-06-06 22:40:28 +07:00
}
2024-06-10 20:54:56 +07:00
const ATTRIBS: [wgpu::VertexAttribute; 2] =
wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x3];
2024-06-06 22:40:28 +07:00
pub const fn desc() -> wgpu::VertexBufferLayout<'static> {
use std::mem;
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &Self::ATTRIBS,
}
}
}
2024-06-10 20:54:56 +07:00
pub struct RenderData {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
}
impl RenderData {
pub fn new() -> RenderData {
RenderData {
vertices: vec![],
indices: vec![],
}
}
pub fn merge(self: RenderData, other: RenderData) -> RenderData {
let vertices_len = self.vertices.len() as u32;
RenderData {
vertices: self.vertices.into_iter().chain(other.vertices).collect(),
indices: self
.indices
.into_iter()
.chain(other.indices.into_iter().map(|i| i + vertices_len))
.collect(),
}
}
}