use crate::color;
use crate::geom::{Point2, Point3, Vec3};
use crate::mesh::vertex::{WithColor, WithTexCoords};
pub type Point = Point3;
pub type Color = color::LinSrgba;
pub type TexCoords = Point2;
pub type Normal = Vec3;
pub type ColoredPoint = WithColor<Point, Color>;
pub type ColoredPoint2 = WithColor<Point2, Color>;
pub type Vertex = WithTexCoords<WithColor<Point, Color>, TexCoords>;
pub const COLOR_CHANNEL_COUNT: usize = 4;
pub const DEFAULT_VERTEX_COLOR: Color = color::Alpha {
color: color::rgb::Rgb {
red: 1.0,
green: 1.0,
blue: 1.0,
standard: std::marker::PhantomData,
},
alpha: 1.0,
};
pub fn new(point: Point, color: Color, tex_coords: TexCoords) -> Vertex {
WithTexCoords {
tex_coords,
vertex: WithColor {
color,
vertex: point,
},
}
}
pub fn default_tex_coords() -> TexCoords {
[0.0; 2].into()
}
#[derive(Clone, Debug)]
pub struct IterFromColoredPoints<I> {
colored_points: I,
}
impl<I> IterFromColoredPoints<I> {
pub fn new<P>(colored_points: P) -> Self
where
P: IntoIterator<IntoIter = I, Item = WithColor<Point, Color>>,
I: Iterator<Item = WithColor<Point, Color>>,
{
let colored_points = colored_points.into_iter();
IterFromColoredPoints { colored_points }
}
}
impl<I> Iterator for IterFromColoredPoints<I>
where
I: Iterator<Item = WithColor<Point, Color>>,
{
type Item = Vertex;
fn next(&mut self) -> Option<Self::Item> {
self.colored_points.next().map(|vertex| {
let tex_coords = default_tex_coords();
let vertex = WithTexCoords { tex_coords, vertex };
vertex
})
}
}
#[derive(Clone, Debug)]
pub struct IterFromPoints<I> {
points: I,
default_color: Color,
}
#[derive(Clone, Debug)]
pub struct IterFromPoint2s<I> {
points: I,
default_color: Color,
}
impl<I> IterFromPoints<I> {
pub fn new<P>(points: P, default_color: Color) -> Self
where
P: IntoIterator<IntoIter = I, Item = Point>,
I: Iterator<Item = Point3>,
{
let points = points.into_iter();
IterFromPoints {
points,
default_color,
}
}
}
impl<I> IterFromPoint2s<I> {
pub fn new<P>(points: P, default_color: Color) -> Self
where
P: IntoIterator<IntoIter = I, Item = Point2>,
I: Iterator<Item = Point2>,
{
let points = points.into_iter();
IterFromPoint2s {
points,
default_color,
}
}
}
impl<I> Iterator for IterFromPoints<I>
where
I: Iterator<Item = Point>,
{
type Item = Vertex;
fn next(&mut self) -> Option<Self::Item> {
self.points.next().map(|vertex| {
let color = self.default_color;
let vertex = WithColor { vertex, color };
let tex_coords = default_tex_coords();
let vertex = WithTexCoords { vertex, tex_coords };
vertex
})
}
}
impl<I> Iterator for IterFromPoint2s<I>
where
I: Iterator<Item = Point2>,
{
type Item = Vertex;
fn next(&mut self) -> Option<Self::Item> {
self.points.next().map(|p| {
let vertex = p.extend(0.0);
let color = self.default_color;
let vertex = WithColor { vertex, color };
let tex_coords = default_tex_coords();
let vertex = WithTexCoords { vertex, tex_coords };
vertex
})
}
}