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 9b0ae10 commit 5a9e2ae
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 46 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Simple Raytracer for simple 3D scenes, made in Rust. Objective is to learn Rust,
- ...

## todo's
- rename BHV to BVH...
- jittering sampling

## Credits / references
Expand Down
8 changes: 4 additions & 4 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::bvh::aabb::Aabb;
use raytracer::bvh::Aabb;

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

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
4 changes: 2 additions & 2 deletions src/bvh/aabb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::elements::Hittable;
use crate::interval::Interval;
use crate::render::Interval;
use crate::linalg::Vec3;
use crate::render::Ray;

Expand Down Expand Up @@ -171,7 +171,7 @@ impl Index<usize> for Aabb {
#[cfg(test)]
mod tests {
use crate::bvh::aabb::Aabb;
use crate::interval::Interval;
use crate::render::Interval;
use crate::linalg::Vec3;
use crate::render::Ray;
use assert_approx_eq::assert_approx_eq;
Expand Down
2 changes: 1 addition & 1 deletion src/bvh/bvh_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;
use super::aabb::Aabb;

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

Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::elements::*;
use crate::interval::Interval;
use crate::render::Interval;
use crate::render::camera::Camera;
use crate::render::Ray;
use crate::{color::Color, render::camera::JSONCamera};
Expand Down
5 changes: 2 additions & 3 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use serde::{Deserialize, Serialize};
extern crate wavefront_obj;
use wavefront_obj::obj;

use crate::interval::Interval;
use crate::render::Interval;
use crate::linalg::{
mat4::{Mat4, Vec4},
Onb, Vec3,
Onb, Vec3, Mat4, Vec4,
};
use crate::render::Ray;
use crate::{bvh::Aabb, materials::*};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod bvh;
pub mod color;
pub mod config;
pub mod elements;
pub mod interval;
pub mod linalg;
pub mod materials;
pub mod render;
30 changes: 1 addition & 29 deletions src/linalg/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign};

use super::vec3::Vec3;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Vec4 {
pub x: f64,
pub y: f64,
pub z: f64,
pub w: f64,
}

impl Vec4 {
/// create a new Vec3 with x, y, z, w axis
pub fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
Vec4 { x, y, z, w }
}

pub fn new_from_vec3(v: Vec3, w: f64) -> Self {
Vec4 {
x: v.x(),
y: v.y(),
z: v.z(),
w,
}
}

pub fn to_vec3(self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
}
}
use super::Vec4;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Mat4 {
Expand Down
8 changes: 6 additions & 2 deletions src/linalg/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
mod vec3;
pub use vec3::Vec3;

pub mod mat4;

mod onb;
pub use onb::Onb;

mod mat4;
pub use mat4::Mat4;

mod vec4;
pub use vec4::Vec4;
31 changes: 31 additions & 0 deletions src/linalg/vec4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::{Deserialize, Serialize};

use super::Vec3;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Vec4 {
pub x: f64,
pub y: f64,
pub z: f64,
pub w: f64,
}

impl Vec4 {
/// create a new Vec3 with x, y, z, w axis
pub fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
Vec4 { x, y, z, w }
}

pub fn new_from_vec3(v: Vec3, w: f64) -> Self {
Vec4 {
x: v.x(),
y: v.y(),
z: v.z(),
w,
}
}

pub fn to_vec3(self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
}
}
2 changes: 1 addition & 1 deletion src/render/integrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
color::Color,
config::{Config, JSONScene},
elements::{Element, Hittable, JSONElement},
interval::Interval,
render::Interval,
materials::{Emmits, Reflects, Refracts, Scatterable},
render::camera::Camera,
render::pdf::{HittablePDF, MixedPDF, PDFTrait, Pdf},
Expand Down
2 changes: 1 addition & 1 deletion src/interval.rs → src/render/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Interval {

#[cfg(test)]
mod tests {
use crate::interval::Interval;
use crate::render::Interval;
use assert_approx_eq::assert_approx_eq;

// Test creating a new interval, taking floats as an argument
Expand Down
3 changes: 3 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub use pdf::{CosinePDF, HittablePDF, MixedPDF, PDFTrait, Pdf};

mod ray;
pub use ray::Ray;

mod interval;
pub use interval::Interval;

0 comments on commit 5a9e2ae

Please sign in to comment.