2019-01-07 21:10:31 +00:00
|
|
|
use super::Pipeline;
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// A `Vec`-based mesh structure used to store mesh data on the CPU.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub struct Mesh<P: Pipeline> {
|
|
|
|
verts: Vec<P::Vertex>,
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:30:39 +00:00
|
|
|
impl<P: Pipeline> Clone for Mesh<P>
|
|
|
|
where
|
|
|
|
P::Vertex: Clone,
|
|
|
|
{
|
2020-04-19 19:46:04 +00:00
|
|
|
fn clone(&self) -> Self {
|
2020-04-20 16:30:39 +00:00
|
|
|
Self {
|
|
|
|
verts: self.verts.clone(),
|
|
|
|
}
|
2020-04-19 19:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 21:10:31 +00:00
|
|
|
impl<P: Pipeline> Mesh<P> {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new `Mesh`.
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::new_without_default)] // TODO: Pending review in #587
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn new() -> Self { Self { verts: vec![] } }
|
2019-01-07 21:10:31 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Clear vertices, allows reusing allocated memory of the underlying Vec.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn clear(&mut self) { self.verts.clear(); }
|
2019-02-23 02:41:52 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Get a slice referencing the vertices of this mesh.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn vertices(&self) -> &[P::Vertex] { &self.verts }
|
2019-01-07 21:10:31 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Push a new vertex onto the end of this mesh.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn push(&mut self, vert: P::Vertex) { self.verts.push(vert); }
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Push a new polygon onto the end of this mesh.
|
|
|
|
pub fn push_tri(&mut self, tri: Tri<P>) {
|
|
|
|
self.verts.push(tri.a);
|
|
|
|
self.verts.push(tri.b);
|
|
|
|
self.verts.push(tri.c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push a new quad onto the end of this mesh.
|
2019-01-11 20:14:37 +00:00
|
|
|
pub fn push_quad(&mut self, quad: Quad<P>) {
|
2020-02-01 20:39:39 +00:00
|
|
|
// A quad is composed of two triangles. The code below converts the former to
|
|
|
|
// the latter.
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-01-11 20:14:37 +00:00
|
|
|
// Tri 1
|
|
|
|
self.verts.push(quad.a.clone());
|
|
|
|
self.verts.push(quad.b);
|
|
|
|
self.verts.push(quad.c.clone());
|
|
|
|
|
|
|
|
// Tri 2
|
|
|
|
self.verts.push(quad.c);
|
|
|
|
self.verts.push(quad.d);
|
|
|
|
self.verts.push(quad.a);
|
|
|
|
}
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Push the vertices of another mesh onto the end of this mesh.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn push_mesh(&mut self, other: &Mesh<P>) { self.verts.extend_from_slice(other.vertices()); }
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Map and push the vertices of another mesh onto the end of this mesh.
|
2019-01-13 20:53:55 +00:00
|
|
|
pub fn push_mesh_map<F: FnMut(P::Vertex) -> P::Vertex>(&mut self, other: &Mesh<P>, mut f: F) {
|
2020-02-01 20:39:39 +00:00
|
|
|
// Reserve enough space in our Vec. This isn't necessary, but it tends to reduce
|
|
|
|
// the number of required (re)allocations.
|
2019-01-13 20:53:55 +00:00
|
|
|
self.verts.reserve(other.vertices().len());
|
|
|
|
|
|
|
|
for vert in other.vertices() {
|
|
|
|
self.verts.push(f(vert.clone()));
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 19:46:04 +00:00
|
|
|
|
2020-04-20 16:30:39 +00:00
|
|
|
pub fn iter(&self) -> std::slice::Iter<P::Vertex> { self.verts.iter() }
|
2020-04-19 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pipeline> IntoIterator for Mesh<P> {
|
|
|
|
type IntoIter = std::vec::IntoIter<P::Vertex>;
|
2020-04-20 16:30:39 +00:00
|
|
|
type Item = P::Vertex;
|
2020-04-19 19:46:04 +00:00
|
|
|
|
2020-04-20 16:30:39 +00:00
|
|
|
fn into_iter(self) -> Self::IntoIter { self.verts.into_iter() }
|
2019-01-11 20:14:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Represents a triangle stored on the CPU.
|
|
|
|
pub struct Tri<P: Pipeline> {
|
|
|
|
a: P::Vertex,
|
|
|
|
b: P::Vertex,
|
|
|
|
c: P::Vertex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pipeline> Tri<P> {
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn new(a: P::Vertex, b: P::Vertex, c: P::Vertex) -> Self { Self { a, b, c } }
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a quad stored on the CPU.
|
2019-01-11 20:14:37 +00:00
|
|
|
pub struct Quad<P: Pipeline> {
|
|
|
|
a: P::Vertex,
|
|
|
|
b: P::Vertex,
|
|
|
|
c: P::Vertex,
|
|
|
|
d: P::Vertex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pipeline> Quad<P> {
|
2019-04-29 20:37:19 +00:00
|
|
|
pub fn new(a: P::Vertex, b: P::Vertex, c: P::Vertex, d: P::Vertex) -> Self {
|
2019-01-11 20:14:37 +00:00
|
|
|
Self { a, b, c, d }
|
|
|
|
}
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|