aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-08-06 12:39:51 +0200
committerJulian T <julian@jtle.dk>2021-08-06 12:39:51 +0200
commit8e3bc57bf03457c33088e62c9d30da0565730257 (patch)
tree6aba7020638f6060ff0bd033ef72ea557b6a2203
parent978580ff9464f7470c46f3084ee26021b71933c8 (diff)
Add box shape and replicate RTTNW boxHEADmaster
-rw-r--r--src/main.rs29
-rw-r--r--src/world/container/list.rs8
-rw-r--r--src/world/mod.rs7
-rw-r--r--src/world/shapes/box.rs40
-rw-r--r--src/world/shapes/mod.rs26
5 files changed, 67 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs
index a288689..aa39677 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
use rendering::camera::{Camera, Film, CameraSettings};
-use rendering::world::{Scene, Object, shapes::{Rect, Plane, Sphere}, Instancable};
+use rendering::world::{Scene, Object, shapes::{Rect, Plane, Sphere, BoxShp}, Instancable};
use rendering::trace::DefaultTracer;
use rendering::core::{Vector2i, Vector3f, Spectrum};
use rendering::render::{RenderContext, RenderCoord};
@@ -12,10 +12,10 @@ fn main() {
let res = Vector2i::new_xy(500, 500);
let cam = Camera::new(&CameraSettings {
- target: Vector3f::new_xyz(0.0, 0.0, 0.0),
- origin: Vector3f::new_xyz(0.0, 0.0, 5.0),
+ target: Vector3f::new_xyz(278.0, 278.0, 0.0),
+ origin: Vector3f::new_xyz(278.0, 278.0, -800.0),
up: Vector3f::new_xyz(0.0, 1.0, 0.0),
- fov: 85.0,
+ fov: 40.0,
filmsize: res,
focus: None,
aperture: None,
@@ -31,18 +31,19 @@ fn main() {
let mut scn = Scene::new();
scn.add_objects(vec![
- Object::new(white.clone(), Rect::new_with_offset(10.0, 10.0, -5.0, Plane::XY)),
- Object::new(white.clone(), Rect::new_with_offset(10.0, 10.0, 5.0, Plane::XY)),
- Object::new(white.clone(), Rect::new_with_offset(10.0, 10.0, -5.0, Plane::XZ)),
- Object::new(white.clone(), Rect::new_with_offset(10.0, 10.0, 5.0, Plane::XZ)),
- Object::new(red.clone(), Rect::new_with_offset(10.0, 10.0, -5.0, Plane::YZ)),
- Object::new(green.clone(), Rect::new_with_offset(10.0, 10.0, 5.0, Plane::YZ)),
- Object::new(sun, Rect::new(1.4, 1.0, Plane::XZ).translate(0.0, 4.99, -2.5)),
+ Object::new(green.clone(), Rect::new(555.0, 555.0, Plane::YZ).translate(555.0, 277.5, 277.5)),
+ Object::new(red.clone(), Rect::new(555.0, 555.0, Plane::YZ).translate(0.0, 277.5, 277.5)),
+ Object::new(white.clone(), Rect::new(555.0, 555.0, Plane::XZ).translate(277.5, 0.0, 277.5)),
+ Object::new(white.clone(), Rect::new(555.0, 555.0, Plane::XZ).translate(277.5, 555.0, 277.5)),
+ Object::new(white.clone(), Rect::new(555.0, 555.0, Plane::XY).translate(277.5, 277.5, 555.0)),
+ Object::new(sun, Rect::new(130.0, 105.0, Plane::XZ).translate(278.0, 554.0, 279.5)),
+ Object::new(white.clone(), BoxShp::new(Vector3f::new(165.0)).translate(212.5, 82.5, 147.5)),
+ Object::new(white.clone(), BoxShp::new(Vector3f::new_xyz(165.0, 330.0, 165.0)).translate(347.5, 165.0, 377.5)),
]);
let tracer = DefaultTracer::new(&scn, Some(5),
- Some(Box::new(SkyLight::new()))
- //None
+ //Some(Box::new(SkyLight::new()))
+ None
);
let mut sampler = UniformSampler::new();
@@ -51,7 +52,7 @@ fn main() {
let mut film = Film::new(res);
{
- let coord = RenderCoord::new(&mut film, Vector2i::new_xy(50, 50), 500);
+ let coord = RenderCoord::new(&mut film, Vector2i::new_xy(50, 50), 50);
coord.run_threaded(&ctx, &mut sampler, 8);
//coord.work(&ctx, &mut sampler);
diff --git a/src/world/container/list.rs b/src/world/container/list.rs
index 22b6d88..f1a658d 100644
--- a/src/world/container/list.rs
+++ b/src/world/container/list.rs
@@ -1,9 +1,9 @@
-use crate::world::{Object, Hittable, Intersection};
+use crate::world::{Object, Hittable, DynHittable, Intersection};
use crate::core::{Bound3f, Ray};
pub struct HittableList {
- elems: Vec<Object>,
+ elems: Vec<DynHittable>,
}
impl HittableList {
@@ -11,8 +11,8 @@ impl HittableList {
Self::default()
}
- pub fn add(&mut self, h: Object) {
- self.elems.push(h);
+ pub fn add<T: Into<DynHittable>>(&mut self, h: T) {
+ self.elems.push(h.into());
}
}
diff --git a/src/world/mod.rs b/src/world/mod.rs
index dd96b91..0394ffe 100644
--- a/src/world/mod.rs
+++ b/src/world/mod.rs
@@ -8,7 +8,6 @@ mod instancing;
pub use scene::*;
pub use hittable::{Intersection, Hittable, DynHittable};
-pub use shapes::Shape;
pub use instancing::{Instance, Instancable};
use std::sync::Arc;
@@ -29,6 +28,12 @@ impl Object {
}
}
+impl Into<DynHittable> for Object {
+ fn into(self) -> DynHittable {
+ DynHittable::new(Box::new(self))
+ }
+}
+
impl Hittable for Object {
fn intersect(&self, ray: &Ray) -> Option<Intersection> {
if let Some(mut inter) = self.inner.intersect(ray) {
diff --git a/src/world/shapes/box.rs b/src/world/shapes/box.rs
new file mode 100644
index 0000000..e07d5fe
--- /dev/null
+++ b/src/world/shapes/box.rs
@@ -0,0 +1,40 @@
+use crate::core::{Ray, Bound3f, Vector3f};
+use crate::world::shapes::{Plane, Rect};
+use crate::world::{Instancable, Intersection, Hittable, DynHittable};
+use crate::world::container::HittableList;
+
+pub struct BoxShp {
+ sides: HittableList,
+}
+
+impl BoxShp {
+ pub fn new(size: Vector3f) -> Self {
+ let mut cont = HittableList::new();
+ cont.add(Rect::new_with_offset(size.x, size.y, size.z / 2.0, Plane::XY));
+ cont.add(Rect::new_with_offset(size.x, size.y, -size.z / 2.0, Plane::XY));
+ cont.add(Rect::new_with_offset(size.x, size.z, size.y / 2.0, Plane::XZ));
+ cont.add(Rect::new_with_offset(size.x, size.z, -size.y / 2.0, Plane::XZ));
+ cont.add(Rect::new_with_offset(size.y, size.z, size.x / 2.0, Plane::YZ));
+ cont.add(Rect::new_with_offset(size.y, size.z, -size.x / 2.0, Plane::YZ));
+
+ Self {sides: cont}
+ }
+}
+
+impl Into<DynHittable> for BoxShp {
+ fn into(self) -> DynHittable {
+ DynHittable::new(Box::new(self))
+ }
+}
+
+impl Hittable for BoxShp {
+ fn intersect(&self, ray: &Ray) -> Option<Intersection> {
+ self.sides.intersect(ray)
+ }
+
+ fn bounding_box(&self) -> Bound3f {
+ self.sides.bounding_box()
+ }
+}
+
+impl Instancable for BoxShp {}
diff --git a/src/world/shapes/mod.rs b/src/world/shapes/mod.rs
index 6e4b2ce..d15840c 100644
--- a/src/world/shapes/mod.rs
+++ b/src/world/shapes/mod.rs
@@ -1,32 +1,10 @@
mod sphere;
mod rectangle;
+mod r#box;
pub use sphere::Sphere;
pub use rectangle::{Rect, Plane};
+pub use r#box::BoxShp;
use crate::world::{Hittable, Intersection};
use crate::core::{Bound3f, Ray};
-
-pub enum Shape {
- Sphere(Sphere),
-}
-
-impl Hittable for Shape {
- fn intersect(&self, ray: &Ray) -> Option<Intersection> {
- match self {
- Self::Sphere(sph) => sph.intersect(ray)
- }
- }
-
- fn bounding_box(&self) -> Bound3f {
- match self {
- Self::Sphere(sph) => sph.bounding_box()
- }
- }
-}
-
-impl From<Sphere> for Shape {
- fn from(s: Sphere) -> Self {
- Self::Sphere(s)
- }
-}