feo_oop_engine/components/
mod.rs

1//! Components used by game objects.
2//! 
3//! TODO
4//! 
5pub 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// all in one TODO
13// #[derive(Default, Debug, Copy, Clone)]
14// pub struct VertexV2 {
15//     pub position: [f32; 3],
16//     pub normal: [f32; 3],
17//     pub texture_index: [f32; 2],
18// }
19// vulkano::impl_vertex!(VertexV2, position, normal, texture_index);
20
21#[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 { // rmb w
35        // rmb color  find out what thats about
36        // maybe vertex paint?
37        Vertex{
38            position: [x, y, z]
39        }
40    }
41   
42    // In my math library I sometimes use vectors as points but thats technically incorrect although it works and gets the job done 
43    pub fn into_vector3(from: &Self, to: &Self) -> Vector3<f32> { // rmb w
44        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    // possible due to counterclockwise ordering
70    pub fn calculate_normal(a: &Vertex, b: &Vertex, c: &Vertex) -> Self { // TODO: use & everywhere
71        Normal::from(Vector3::<f32>::cross_product(
72            Vertex::into_vector3(a, b), 
73            Vertex::into_vector3(a, c))
74        ) // TODO: right hand rule comment
75    }
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 { // double check
91    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 { // actually use parts r g and b and add a to arr method
105    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(())) // formatting error
128        }
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}