feo_oop_engine/components/
mod.rs1pub mod texture;
6pub mod material;
7pub mod triangle_mesh;
8use std::{iter::FromIterator, num::ParseFloatError};
9
10use feo_math::linear_algebra::vector3::Vector3;
11
12#[derive(Default, Debug, Copy, Clone)]
22pub struct ScreenPos {
23 pub position: [f32; 2],
24}
25vulkano::impl_vertex!(ScreenPos, position);
26
27#[derive(Default, Debug, Copy, Clone)]
28pub struct Vertex{
29 pub position: [f32; 3],
30}
31vulkano::impl_vertex!(Vertex, position);
32
33impl Vertex {
34 pub fn new(x: f32, y: f32, z: f32) -> Self { Vertex{
38 position: [x, y, z]
39 }
40 }
41
42 pub fn into_vector3(from: &Self, to: &Self) -> Vector3<f32> { let to: Vector3<f32> = (*to).into();
45 let from: Vector3<f32> = (*from).into();
46 to - from
47 }
48}
49
50impl From<Vertex> for Vector3<f32> {
51 fn from(other: Vertex) -> Vector3<f32> {
52 Vector3::from(other.position)
53 }
54}
55
56#[derive(Default, Debug, Copy, Clone)]
57pub struct Normal {
58 pub normal: (f32, f32, f32)
59}
60vulkano::impl_vertex!(Normal, normal);
61
62impl Normal {
63 pub fn new(x: f32, y: f32, z: f32) -> Self {
64 Normal{
65 normal: (x, y, z)
66 }
67 }
68
69 pub fn calculate_normal(a: &Vertex, b: &Vertex, c: &Vertex) -> Self { Normal::from(Vector3::<f32>::cross_product(
72 Vertex::into_vector3(a, b),
73 Vertex::into_vector3(a, c))
74 ) }
76}
77
78impl From<Vector3<f32>> for Normal {
79 fn from(other: Vector3<f32>) -> Self {
80 Normal{ normal: (other.0, other.1, other.2)}
81 }
82}
83
84#[derive(Default, Debug, Copy, Clone)]
85pub struct TextureIndex{
86 pub texture_index: [f32; 2],
87}
88vulkano::impl_vertex!(TextureIndex, texture_index);
89
90impl TextureIndex { pub fn new(x: f32, y: f32) -> Self {
92 TextureIndex{
93 texture_index: [x, y]
94 }
95 }
96 pub fn default() -> Self {
97 TextureIndex{
98 texture_index: [0.0, 0.0]
99 }
100 }
101}
102
103#[derive(Default, Debug, Copy, Clone)]
104pub struct RGB { pub r: f32,
106 pub g: f32,
107 pub b: f32,
108}
109struct CollectRGB(pub Result<RGB, ()>);
110
111impl RGB {
112 pub fn from_parts<'a, I>(parts_iter: I) -> Result<Self, ()>
113 where I: IntoIterator<Item = &'a str> {
114 parts_iter.into_iter().map(|str| str.parse::<f32>()).collect::<CollectRGB>().0
115 }
116 pub fn new(r: f32, g: f32, b: f32) -> Self {
117 RGB { r, g, b }
118 }
119}
120
121impl FromIterator<Result<f32, ParseFloatError>> for CollectRGB{
122 fn from_iter<T: IntoIterator<Item = Result<f32, ParseFloatError>>>(iter: T) -> Self {
123 let mut iter = iter.into_iter();
124 if let (Some(Ok(r)), Some(Ok(g)), Some(Ok(b)), None) = (iter.next(), iter.next(), iter.next(), iter.next()) {
125 CollectRGB(Ok(RGB::new(r, g, b)))
126 } else {
127 CollectRGB(Err(())) }
129 }
130}
131
132impl From<RGB> for [f32; 3]{
133 fn from(other: RGB) -> [f32; 3] {
134 [other.r, other.g, other.b]
135 }
136}