diff --git a/src/graphics/vertices.rs b/src/graphics/vertices.rs index f2baafff5..795387e11 100644 --- a/src/graphics/vertices.rs +++ b/src/graphics/vertices.rs @@ -56,18 +56,19 @@ impl From<&Vec>> for Vertices { let [a, b, c] = triangle.points(); let normal = (b - a).cross(&(c - a)).normalize(); + let color = triangle.color(); - mesh.push((a, normal)); - mesh.push((b, normal)); - mesh.push((c, normal)); + mesh.push((a, normal, color)); + mesh.push((b, normal, color)); + mesh.push((c, normal, color)); } let vertices = mesh .vertices() - .map(|(vertex, normal)| Vertex { + .map(|(vertex, normal, color)| Vertex { position: vertex.into(), normal: normal.into(), - color: [1.0, 0.0, 0.0, 1.0], + color: color.map(|v| f32::from(v) / 255.0), }) .collect(); diff --git a/src/math/triangle.rs b/src/math/triangle.rs index f290ad9ef..c2b26b902 100644 --- a/src/math/triangle.rs +++ b/src/math/triangle.rs @@ -2,12 +2,25 @@ use super::{Point, Scalar}; /// A triangle #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct Triangle([Point; 3]); +pub struct Triangle { + points: [Point; 3], + color: [u8; 4], +} impl Triangle { /// Access the triangle's points pub fn points(&self) -> [Point; 3] { - self.0 + self.points + } + + /// Return the specified color of the triangle in RGBA + pub fn color(&self) -> [u8; 4] { + self.color + } + + /// Set a new color for the particular triangle + pub fn set_color(&mut self, color: [u8; 4]) { + self.color = color; } } @@ -27,7 +40,10 @@ impl From<[Point; 3]> for Triangle { // A triangle is not valid if it doesn't span any area if area != Scalar::from(0.0) { - Self(points) + Self { + points, + color: [255, 0, 0, 255], + } } else { panic!("Invalid Triangle specified"); } @@ -72,4 +88,23 @@ mod tests { let c = Point::from([2.0, 2.0, 2.0]); let _triangle = Triangle::from([a, b, c]); } + + #[test] + fn triangle_default_color() { + let a = Point::from([0.0, 0.0]); + let b = Point::from([1.0, 1.0]); + let c = Point::from([1.0, 2.0]); + let triangle = Triangle::from([a, b, c]); + assert_eq!(triangle.color(), [255, 0, 0, 255]); + } + + #[test] + fn triangle_set_color() { + let a = Point::from([0.0, 0.0]); + let b = Point::from([1.0, 1.0]); + let c = Point::from([1.0, 2.0]); + let mut triangle = Triangle::from([a, b, c]); + triangle.set_color([1, 2, 3, 4]); + assert_eq!(triangle.color(), [1, 2, 3, 4]); + } }