use astrelis_render::wgpu;
use bytemuck::{Pod, Zeroable};
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct FillVertex {
pub position: [f32; 2],
}
impl FillVertex {
pub fn new(x: f32, y: f32) -> Self {
Self { position: [x, y] }
}
pub fn vertex_layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x2,
}],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct StrokeVertex {
pub position: [f32; 2],
pub normal: [f32; 2],
pub distance: f32,
pub side: f32,
}
impl StrokeVertex {
pub fn new(x: f32, y: f32, nx: f32, ny: f32, distance: f32, side: f32) -> Self {
Self {
position: [x, y],
normal: [nx, ny],
distance,
side,
}
}
pub fn vertex_layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: 8,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: 16,
shader_location: 2,
format: wgpu::VertexFormat::Float32,
},
wgpu::VertexAttribute {
offset: 20,
shader_location: 3,
format: wgpu::VertexFormat::Float32,
},
],
}
}
}
#[derive(Debug, Clone, Default)]
pub struct TessellatedMesh<V> {
pub vertices: Vec<V>,
pub indices: Vec<u32>,
}
impl<V> TessellatedMesh<V> {
pub fn new() -> Self {
Self {
vertices: Vec::new(),
indices: Vec::new(),
}
}
pub fn from_data(vertices: Vec<V>, indices: Vec<u32>) -> Self {
Self { vertices, indices }
}
pub fn is_empty(&self) -> bool {
self.vertices.is_empty() || self.indices.is_empty()
}
pub fn vertex_count(&self) -> usize {
self.vertices.len()
}
pub fn index_count(&self) -> usize {
self.indices.len()
}
pub fn triangle_count(&self) -> usize {
self.indices.len() / 3
}
pub fn clear(&mut self) {
self.vertices.clear();
self.indices.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fill_vertex_size() {
assert_eq!(std::mem::size_of::<FillVertex>(), 8);
}
#[test]
fn test_stroke_vertex_size() {
assert_eq!(std::mem::size_of::<StrokeVertex>(), 24);
}
#[test]
fn test_empty_mesh() {
let mesh: TessellatedMesh<FillVertex> = TessellatedMesh::new();
assert!(mesh.is_empty());
assert_eq!(mesh.vertex_count(), 0);
assert_eq!(mesh.triangle_count(), 0);
}
}