pub mod texture;
pub mod material;
pub mod triangle_mesh;
use std::{iter::FromIterator, num::ParseFloatError};
use feo_math::linear_algebra::vector3::Vector3;
#[derive(Default, Debug, Copy, Clone)]
pub struct ScreenPos {
pub position: [f32; 2],
}
vulkano::impl_vertex!(ScreenPos, position);
#[derive(Default, Debug, Copy, Clone)]
pub struct Vertex{
pub position: [f32; 3],
}
vulkano::impl_vertex!(Vertex, position);
impl Vertex {
pub fn new(x: f32, y: f32, z: f32) -> Self { Vertex{
position: [x, y, z]
}
}
pub fn into_vector3(from: &Self, to: &Self) -> Vector3<f32> { let to: Vector3<f32> = (*to).into();
let from: Vector3<f32> = (*from).into();
to - from
}
}
impl From<Vertex> for Vector3<f32> {
fn from(other: Vertex) -> Vector3<f32> {
Vector3::from(other.position)
}
}
#[derive(Default, Debug, Copy, Clone)]
pub struct Normal {
pub normal: (f32, f32, f32)
}
vulkano::impl_vertex!(Normal, normal);
impl Normal {
pub fn new(x: f32, y: f32, z: f32) -> Self {
Normal{
normal: (x, y, z)
}
}
pub fn calculate_normal(a: &Vertex, b: &Vertex, c: &Vertex) -> Self { Normal::from(Vector3::<f32>::cross_product(
Vertex::into_vector3(a, b),
Vertex::into_vector3(a, c))
) }
}
impl From<Vector3<f32>> for Normal {
fn from(other: Vector3<f32>) -> Self {
Normal{ normal: (other.0, other.1, other.2)}
}
}
#[derive(Default, Debug, Copy, Clone)]
pub struct TextureIndex{
pub texture_index: [f32; 2],
}
vulkano::impl_vertex!(TextureIndex, texture_index);
impl TextureIndex { pub fn new(x: f32, y: f32) -> Self {
TextureIndex{
texture_index: [x, y]
}
}
pub fn default() -> Self {
TextureIndex{
texture_index: [0.0, 0.0]
}
}
}
#[derive(Default, Debug, Copy, Clone)]
pub struct RGB { pub r: f32,
pub g: f32,
pub b: f32,
}
struct CollectRGB(pub Result<RGB, ()>);
impl RGB {
pub fn from_parts<'a, I>(parts_iter: I) -> Result<Self, ()>
where I: IntoIterator<Item = &'a str> {
parts_iter.into_iter().map(|str| str.parse::<f32>()).collect::<CollectRGB>().0
}
pub fn new(r: f32, g: f32, b: f32) -> Self {
RGB { r, g, b }
}
}
impl FromIterator<Result<f32, ParseFloatError>> for CollectRGB{
fn from_iter<T: IntoIterator<Item = Result<f32, ParseFloatError>>>(iter: T) -> Self {
let mut iter = iter.into_iter();
if let (Some(Ok(r)), Some(Ok(g)), Some(Ok(b)), None) = (iter.next(), iter.next(), iter.next(), iter.next()) {
CollectRGB(Ok(RGB::new(r, g, b)))
} else {
CollectRGB(Err(())) }
}
}
impl From<RGB> for [f32; 3]{
fn from(other: RGB) -> [f32; 3] {
[other.r, other.g, other.b]
}
}