Add Primitive trait
This commit is contained in:
parent
a0de9c18ba
commit
8c527d34fc
14
src/main.rs
14
src/main.rs
|
|
@ -19,7 +19,7 @@ use vanrijn::colour::{ColourRgbF, NamedColour};
|
||||||
use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper};
|
use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper};
|
||||||
use vanrijn::materials::{LambertianMaterial, PhongMaterial, ReflectiveMaterial};
|
use vanrijn::materials::{LambertianMaterial, PhongMaterial, ReflectiveMaterial};
|
||||||
use vanrijn::mesh::load_obj;
|
use vanrijn::mesh::load_obj;
|
||||||
use vanrijn::raycasting::{Intersect, Plane, Sphere};
|
use vanrijn::raycasting::{Primitive, Plane, Sphere};
|
||||||
use vanrijn::scene::Scene;
|
use vanrijn::scene::Scene;
|
||||||
|
|
||||||
fn update_texture(image: &ImageRgbU8, texture: &mut Texture) {
|
fn update_texture(image: &ImageRgbU8, texture: &mut Texture) {
|
||||||
|
|
@ -75,17 +75,17 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|triangle| Box::new(triangle) as Box<dyn Intersect<f64>>)
|
.map(|triangle| Arc::new(triangle) as Arc<dyn Primitive<f64>>)
|
||||||
.chain(vec![
|
.chain(vec![
|
||||||
Box::new(Plane::new(
|
Arc::new(Plane::new(
|
||||||
Vector3::new(0.0, 1.0, 0.0),
|
Vector3::new(0.0, 1.0, 0.0),
|
||||||
-2.0,
|
-2.0,
|
||||||
Arc::new(LambertianMaterial {
|
Arc::new(LambertianMaterial {
|
||||||
colour: ColourRgbF::new(0.55, 0.27, 0.04),
|
colour: ColourRgbF::new(0.55, 0.27, 0.04),
|
||||||
diffuse_strength: 0.1,
|
diffuse_strength: 0.1,
|
||||||
}),
|
}),
|
||||||
)) as Box<dyn Intersect<f64>>,
|
)) as Arc<dyn Primitive<f64>>,
|
||||||
Box::new(Sphere::new(
|
Arc::new(Sphere::new(
|
||||||
Point3::new(-6.25, -0.5, 1.0),
|
Point3::new(-6.25, -0.5, 1.0),
|
||||||
1.0,
|
1.0,
|
||||||
Arc::new(LambertianMaterial {
|
Arc::new(LambertianMaterial {
|
||||||
|
|
@ -93,7 +93,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
diffuse_strength: 0.1,
|
diffuse_strength: 0.1,
|
||||||
}),
|
}),
|
||||||
)),
|
)),
|
||||||
Box::new(Sphere::new(
|
Arc::new(Sphere::new(
|
||||||
Point3::new(-4.25, -0.5, 2.0),
|
Point3::new(-4.25, -0.5, 2.0),
|
||||||
1.0,
|
1.0,
|
||||||
Arc::new(ReflectiveMaterial {
|
Arc::new(ReflectiveMaterial {
|
||||||
|
|
@ -102,7 +102,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
reflection_strength: 0.99,
|
reflection_strength: 0.99,
|
||||||
}),
|
}),
|
||||||
)),
|
)),
|
||||||
Box::new(Sphere::new(
|
Arc::new(Sphere::new(
|
||||||
Point3::new(-5.0, 1.5, 1.0),
|
Point3::new(-5.0, 1.5, 1.0),
|
||||||
1.0,
|
1.0,
|
||||||
Arc::new(PhongMaterial {
|
Arc::new(PhongMaterial {
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,8 @@ pub trait HasBoundingBox<T: RealField>: Send + Sync {
|
||||||
fn bounding_box(&self) -> BoundingBox<T>;
|
fn bounding_box(&self) -> BoundingBox<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Primitive<T: RealField>: Intersect<T> + HasBoundingBox<T> {}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use quickcheck_macros::quickcheck;
|
use quickcheck_macros::quickcheck;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3};
|
||||||
|
|
||||||
use crate::materials::Material;
|
use crate::materials::Material;
|
||||||
|
|
||||||
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
|
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
@ -87,6 +87,8 @@ impl<T: RealField> HasBoundingBox<T> for Plane<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: RealField> Primitive<T> for Plane<T> {}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use nalgebra::Point3;
|
use nalgebra::Point3;
|
||||||
|
|
@ -274,6 +276,4 @@ mod tests {
|
||||||
assert!(bb.contains_point(Point3::new(-2000.0, 2.0, 2.0)));
|
assert!(bb.contains_point(Point3::new(-2000.0, 2.0, 2.0)));
|
||||||
assert!(bb.contains_point(Point3::new(3.0, 2.0, 3.0)));
|
assert!(bb.contains_point(Point3::new(3.0, 2.0, 3.0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3};
|
||||||
|
|
||||||
use crate::materials::Material;
|
use crate::materials::Material;
|
||||||
|
|
||||||
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
|
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
@ -84,6 +84,8 @@ impl<T: RealField> HasBoundingBox<T> for Sphere<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: RealField> Primitive<T> for Sphere<T> {}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use quickcheck::TestResult;
|
use quickcheck::TestResult;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::materials::Material;
|
use crate::materials::Material;
|
||||||
|
|
||||||
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
|
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
|
||||||
use nalgebra::{Point3, RealField, Vector2, Vector3};
|
use nalgebra::{Point3, RealField, Vector2, Vector3};
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
@ -83,6 +83,8 @@ impl<T: RealField> HasBoundingBox<T> for Triangle<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: RealField> Primitive<T> for Triangle<T> {}
|
||||||
|
|
||||||
fn indices_with_index_of_largest_element_last<T: RealField>(v: &Vector3<T>) -> [usize; 3] {
|
fn indices_with_index_of_largest_element_last<T: RealField>(v: &Vector3<T>) -> [usize; 3] {
|
||||||
if v.x > v.y {
|
if v.x > v.y {
|
||||||
if v.z > v.x {
|
if v.z > v.x {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
use nalgebra::{Point3, RealField};
|
use nalgebra::{Point3, RealField};
|
||||||
|
|
||||||
use crate::raycasting::Intersect;
|
use crate::raycasting::Primitive;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct Scene<T: RealField> {
|
pub struct Scene<T: RealField> {
|
||||||
pub camera_location: Point3<T>,
|
pub camera_location: Point3<T>,
|
||||||
pub objects: Vec<Box<dyn Intersect<T>>>,
|
pub objects: Vec<Arc<dyn Primitive<T>>>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue