WIP: add Mesh, TriangleTexture, TriangleMaterial

This commit is contained in:
Bruno BELANYI 2020-03-23 22:49:49 +01:00
parent e65a2a1f48
commit 0f6b81e40c
7 changed files with 82 additions and 0 deletions

View file

@ -13,6 +13,7 @@ use serde::Deserialize;
pub enum MaterialEnum {
#[serde(rename = "uniform")]
UniformMaterial,
TriangleMaterial,
}
/// Represent the physical light properties of an object in the scene;
@ -22,5 +23,8 @@ pub trait Material: std::fmt::Debug {
fn properties(&self, point: Point2D) -> LightProperties;
}
mod triangle;
pub use triangle::*;
mod uniform;
pub use uniform::*;

View file

@ -0,0 +1,31 @@
use super::Material;
use crate::core::{LightProperties, LinearColor, ReflTransEnum};
use crate::Point2D;
use serde::Deserialize;
/// Represent a material which interpolates between three points.
#[derive(Debug, PartialEq, Deserialize)]
pub struct TriangleMaterial {
/// The diffuse components.
diffuse: [LinearColor; 3],
/// The specular components.
specular: [LinearColor; 3],
/// The transparency or reflectivity properties, this is not interpolated.
#[serde(flatten)]
pub refl_trans: Option<ReflTransEnum>,
}
impl Material for TriangleMaterial {
fn properties(&self, point: Point2D) -> LightProperties {
let (u, v) = (point.x, point.y);
let diffuse = self.diffuse[0].clone() * (1. - u - v)
+ self.diffuse[1].clone() * u
+ self.diffuse[2].clone() * v;
let specular = self.specular[0].clone() * (1. - u - v)
+ self.specular[1].clone() * u
+ self.specular[2].clone() * v;
LightProperties::new(diffuse, specular, self.refl_trans.clone())
}
}
// FIXME: tests

View file

@ -0,0 +1,8 @@
use super::Object;
/// Represent a mesh of objects.
pub struct Mesh {
/// The shapes composing the mesh
#[allow(unused)] // FIXME: remove when used
shapes: Vec<Object>,
}

View file

@ -3,6 +3,9 @@
pub mod light_aggregate;
pub use light_aggregate::*;
mod mesh;
pub use mesh::*;
pub mod object;
pub use object::*;

View file

@ -0,0 +1,9 @@
use super::{InterpolatedTriangle, Shape, Triangle};
use crate::material::{Material, TriangleMaterial, UniformMaterial};
use crate::texture::{Texture, TriangleTexture, UniformTexture};
use crate::Point;
use beevee::{
aabb::{Bounded, AABB},
bvh::Intersected,
ray::Ray,
};

View file

@ -13,6 +13,7 @@ use serde::Deserialize;
pub enum TextureEnum {
#[serde(rename = "uniform")]
UniformTexture,
TriangleTexture,
}
/// Represent an object's texture.
@ -22,5 +23,8 @@ pub trait Texture: std::fmt::Debug {
fn texel_color(&self, point: Point2D) -> LinearColor;
}
mod triangle;
pub use triangle::*;
mod uniform;
pub use uniform::*;

View file

@ -0,0 +1,23 @@
use super::{uniform::UniformTexture, Texture};
use crate::core::LinearColor;
use crate::Point2D;
use serde::Deserialize;
/// Represent a texture which interpolates between three points.
#[derive(Debug, PartialEq, Deserialize)]
pub struct TriangleTexture {
/// The texture at each point
textures: [UniformTexture; 3],
}
impl Texture for TriangleTexture {
fn texel_color(&self, point: Point2D) -> LinearColor {
let (u, v) = (point.x, point.y);
let sum = self.textures[0].texel_color(point) * (1. - u - v)
+ self.textures[1].texel_color(point) * u
+ self.textures[2].texel_color(point) * v;
sum / 3.
}
}
// FIXME: tests