Skip to content

Commit

Permalink
refactored into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
scott223 committed Nov 12, 2023
1 parent 4bfbcb2 commit 1e91f40
Show file tree
Hide file tree
Showing 27 changed files with 716 additions and 609 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env_logger = "0.10.0"
rand = { version = "0.8.5", features = ["small_rng"] }
rand_distr = "0.4.2"
indicatif = "0.17.5"
rayon = "1.7.0"
rayon = "1.8.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.105"
png = "0.17.10"
Expand Down
6 changes: 3 additions & 3 deletions benches/default_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use criterion::{criterion_group, criterion_main, Criterion};
use rand::seq::SliceRandom;
use rand_distr::Distribution;
use rand_distr::Uniform;
use raytracer::aabb::Aabb;
use raytracer::bvh::aabb::Aabb;

use raytracer::interval::Interval;
use raytracer::ray::Ray;
use raytracer::vec3::Vec3;
use raytracer::render::ray::Ray;
use raytracer::linalg::vec3::Vec3;

pub fn criterion_benchmark(c: &mut Criterion) {
let bbox: Aabb = Aabb::new_from_points(Vec3::new(1.0, 1.0, 1.0), Vec3::new(2.0, 2.0, 2.0));
Expand Down
Binary file added renders/render_a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/render_dragon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 14 additions & 5 deletions src/aabb.rs → src/bvh/aabb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::elements::Hittable;
use crate::interval::Interval;
use crate::ray::Ray;
use crate::vec3::Vec3;
use crate::render::Ray;
use crate::linalg::Vec3;

use std::fmt;
use std::ops::Index;

Expand Down Expand Up @@ -114,6 +116,8 @@ impl Aabb {
}

// checks if we have a hit with the aabb, in a given interval
// source: https://docs.rs/bvh/latest/src/bvh/ray.rs.html#168-188

pub fn hit(&self, ray: &Ray, _ray_t: &mut Interval) -> bool {
let mut ray_min = (self[ray.sign_x].x() - ray.origin.x()) * ray.inv_direction.x();
let mut ray_max = (self[1 - ray.sign_x].x() - ray.origin.x()) * ray.inv_direction.x();
Expand All @@ -132,6 +136,11 @@ impl Aabb {

ray_min.max(0.0) <= ray_max
}
pub fn grow<T: Hittable>(mut self, b: T) {
self.x = Interval::new_from_intervals(self.x, b.bounding_box().x);
self.y = Interval::new_from_intervals(self.y, b.bounding_box().y);
self.z = Interval::new_from_intervals(self.z, b.bounding_box().z);
}
}

/// Display trait for Aabb
Expand Down Expand Up @@ -161,10 +170,10 @@ impl Index<usize> for Aabb {

#[cfg(test)]
mod tests {
use crate::aabb::Aabb;
use crate::bvh::aabb::Aabb;
use crate::interval::Interval;
use crate::ray::Ray;
use crate::vec3::Vec3;
use crate::render::Ray;
use crate::linalg::Vec3;
use assert_approx_eq::assert_approx_eq;

// Test creating a new aabb, taking intervals as an argument
Expand Down
27 changes: 14 additions & 13 deletions src/bhv.rs → src/bvh/bvh_node.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use std::fmt::Debug;

use crate::aabb::Aabb;
use super::aabb::Aabb;

use crate::elements::*;
use crate::interval::Interval;
use crate::ray::Ray;
use crate::vec3::Vec3;
use crate::render::Ray;
use crate::linalg::Vec3;

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

// this is a node for the BHV tree, and it consists of objects with a hittable trait (either another node, or an element)
// it will only have a left or a right object
pub struct BHVNode {
pub struct BVHNode {
left: Box<dyn Hittable + Sync>,
right: Box<dyn Hittable + Sync>,
bbox: Aabb, // this gets calculated and set when constructing the tree, so we can use this to speed up the hit calculations
}

impl BHVNode {
impl BVHNode {
pub fn new(objects: &mut Vec<Element>, start: usize, end: usize) -> Box<dyn Hittable + Sync> {
// see how many elements we still have left
let object_span: usize = end - start;
Expand All @@ -27,8 +28,8 @@ impl BHVNode {
Box::new(objects[start])
} else if object_span == 2 {
// we have two items, lets see which one comes first and asign in the right order to the end node
if BHVNode::box_compare(&objects[start], &objects[start + 1]) {
let node: BHVNode = BHVNode {
if BVHNode::box_compare(&objects[start], &objects[start + 1]) {
let node: BVHNode = BVHNode {
left: Box::new(objects[start]),
right: Box::new(objects[start + 1]),
bbox: Aabb::new_from_aabbs(
Expand All @@ -39,7 +40,7 @@ impl BHVNode {

Box::new(node)
} else {
let node: BHVNode = BHVNode {
let node: BVHNode = BVHNode {
left: Box::new(objects[start + 1]),
right: Box::new(objects[start]),
bbox: Aabb::new_from_aabbs(
Expand Down Expand Up @@ -68,14 +69,14 @@ impl BHVNode {
let mid: usize = start + object_span / 2;

// we create two sub nodes, that get the same object list, but a will look a at a smaller subsection
let left = BHVNode::new(objects, start, mid);
let right = BHVNode::new(objects, mid, end);
let left = BVHNode::new(objects, start, mid);
let right = BVHNode::new(objects, mid, end);

// get the bounding box
let bbox = Aabb::new_from_aabbs(left.bounding_box(), right.bounding_box());

// create the node
let node: BHVNode = BHVNode { left, right, bbox };
let node: BVHNode = BVHNode { left, right, bbox };

Box::new(node)
}
Expand All @@ -87,7 +88,7 @@ impl BHVNode {
}
}

impl Debug for BHVNode {
impl Debug for BVHNode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
Expand All @@ -105,7 +106,7 @@ impl Debug for dyn Hittable + Sync {
}
}

impl Hittable for BHVNode {
impl Hittable for BVHNode {
// recursive check for hits through the BHV nodes
fn hit(&self, ray: &Ray, ray_t: &mut Interval) -> Option<HitRecord> {
// check if we hit the bounding box of this node, because if we dont, we can stop right here
Expand Down
5 changes: 5 additions & 0 deletions src/bvh/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod aabb;
pub use aabb::Aabb;

mod bvh_node;
pub use bvh_node::BVHNode;
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::camera::Camera;
use crate::render::camera::Camera;
use crate::elements::*;
use crate::interval::Interval;
use crate::ray::Ray;
use crate::{camera::JSONCamera, color::Color};
use crate::render::Ray;
use crate::{render::camera::JSONCamera, color::Color};

use serde::{Deserialize, Serialize};

Expand Down
16 changes: 7 additions & 9 deletions src/elements.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use std::f64::consts::PI;
use std::fmt::Debug;
use std::fs::File;
use std::io::Read;
use std::fmt::Debug;

use rand::Rng;
use rand::rngs::SmallRng;
use rand::Rng;

use serde::{Deserialize, Serialize};

extern crate wavefront_obj;
use wavefront_obj::obj;

use crate::linalg::{Vec3, Onb, mat4::{Mat4, Vec4}};
use crate::{bvh::Aabb, materials::*};
use crate::render::Ray;
use crate::interval::Interval;
use crate::mat4::{Mat4, Vec4};
use crate::onb::Onb;
use crate::ray::Ray;
use crate::vec3::Vec3;
use crate::{aabb::Aabb, materials::*};

// hitrecord gets returned on a hit, containting the point on the ray, the point in the global coordinate system, the normal and the material for the hit
#[derive(Debug)]
Expand Down Expand Up @@ -824,8 +822,8 @@ impl JSONObj {
mod tests {
use crate::color::Color;
use crate::elements::*;
use crate::ray::Ray;
use crate::vec3::Vec3;
use crate::render::Ray;
use crate::linalg::Vec3;
use assert_approx_eq::assert_approx_eq;

#[test_log::test]
Expand Down
Loading

0 comments on commit 1e91f40

Please sign in to comment.