Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping #7

Merged
merged 7 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
958 changes: 622 additions & 336 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
name = "rusttracer"
version = "0.1.0"
authors = ["Hugo Tunius <h@tunius.se>", "Andrew Aylett <andrew@aylett.co.uk>"]
edition = "2021"

[dependencies]
image = "*"
rayon="1.3.0"
tobj="1.0.0"
serde = "1.0.12"
serde_derive = "1.0.12"
image = "0.25"
rayon = "1"
tobj = "4.0.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
getopts = "0.2.4"
clippy={version = "*", optional = true}
toml="0.5"

[features]
default = []
Expand Down
6 changes: 3 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use config;
use math::{Matrix4, Point3, Vector3};
use ray::Ray;
use crate::config;
use crate::math::{Matrix4, Point3, Vector3};
use crate::ray::Ray;

#[derive(Debug)]
pub struct Camera {
Expand Down
2 changes: 1 addition & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Color {
let x = chroma * (1.0 - ((hue_d % 2.0) - 1.0).abs());

let color = match hue_d {
v if v >= 0.0 && v <= 1.0 => (chroma, x, 0.0),
v if (0.0..=1.0).contains(&v) => (chroma, x, 0.0),
v if v > 1.0 && v <= 2.0 => (x, chroma, 0.0),
v if v > 2.0 && v <= 3.0 => (0.0, chroma, x),
v if v > 3.0 && v <= 4.0 => (0.0, x, chroma),
Expand Down
2 changes: 2 additions & 0 deletions src/config/camera.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct Camera {
pub fov: f32,
Expand Down
4 changes: 3 additions & 1 deletion src/config/light.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use light::Falloff;
use serde::Deserialize;

use crate::light::Falloff;

#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
Expand Down
4 changes: 3 additions & 1 deletion src/config/material.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use material::IllumninationModel;
use serde::Deserialize;

use crate::material::IllumninationModel;

#[derive(Deserialize, Debug, Clone)]
pub enum Texture {
Expand Down
6 changes: 3 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod object;
mod scene;
mod transform;

use serde::Deserialize;

pub use self::camera::Camera;
pub use self::light::Light;
pub use self::material::Material;
Expand All @@ -19,9 +21,7 @@ use std::fs::File;
use std::io;
use std::io::Read;

use serde_json;

use renderer::SuperSampling;
use crate::renderer::SuperSampling;

#[derive(Debug)]
pub struct ConfigError {
Expand Down
4 changes: 3 additions & 1 deletion src/config/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use config::Transform;
use serde::Deserialize;

use super::Transform;

#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
Expand Down
2 changes: 2 additions & 0 deletions src/config/scene.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::Deserialize;

use super::light::Light;
use super::object::Object;

Expand Down
6 changes: 4 additions & 2 deletions src/config/transform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use geometry::Transformable;
use math;
use serde::Deserialize;

use crate::geometry::Transformable;
use crate::math;

#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
Expand Down
18 changes: 9 additions & 9 deletions src/config_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;

use camera;
use color::Color;
use config;
use material;
use mesh_loader::MeshLoader;
use renderer;
use scene;
use texture;
use crate::camera;
use crate::color::Color;
use crate::config;
use crate::material;
use crate::mesh_loader::MeshLoader;
use crate::renderer;
use crate::scene;
use crate::texture;

pub struct ConfigLoader {
fallback_material: Rc<material::Material>,
Expand Down Expand Up @@ -63,7 +63,7 @@ impl ConfigLoader {
.collect();

let scene = scene::Scene::new_from_config(
&parsed_config.scenes.first().unwrap(),
parsed_config.scenes.first().unwrap(),
&materials,
&mut mesh_loader,
Rc::clone(&self.fallback_material),
Expand Down
16 changes: 8 additions & 8 deletions src/geometry/aabb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{BoundingVolume, Triangle};
use math::{Point3, Vector3};
use ray::Ray;
use crate::math::{Point3, Vector3};
use crate::ray::Ray;

#[derive(Debug, Clone)]
pub struct AABB {
Expand Down Expand Up @@ -79,12 +79,12 @@ impl AABB {
let b_min = bounding_box.min();
let b_max = bounding_box.max();

return min.x <= b_max.x
min.x <= b_max.x
&& max.x >= b_min.x
&& min.y <= b_max.y
&& max.y >= b_min.y
&& min.z <= b_max.z
&& max.z >= b_min.z;
&& max.z >= b_min.z
}
}

Expand Down Expand Up @@ -153,10 +153,10 @@ mod tests {
use std::rc::Rc;

use super::AABB;
use color::Color;
use geometry::triangle::{Normal, Triangle};
use material::{IllumninationModel, Material};
use math::{Point3, Vector3};
use crate::color::Color;
use crate::geometry::triangle::{Normal, Triangle};
use crate::material::{IllumninationModel, Material};
use crate::math::{Point3, Vector3};

fn make_material() -> Material {
Material::new(
Expand Down
12 changes: 6 additions & 6 deletions src/geometry/extent_volume.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::mem;

use super::{BoundingVolume, Ray, Triangle};
use math::Vector3;
use crate::math::Vector3;

// This approach is based on Ray Tracing Complex Scenes, Kay and Kajiya, 1986[0].
//
// 0: https://dl.acm.org/doi/pdf/10.1145/15886.15916

const NUM_PLANE_SET_NORMALS: usize = 7;
const PLAN_SET_NORMALS: &'static [Vector3; NUM_PLANE_SET_NORMALS] = &[
const PLAN_SET_NORMALS: &[Vector3; NUM_PLANE_SET_NORMALS] = &[
Vector3::new(1.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
Vector3::new(0.0, 0.0, 1.0),
// 0.577350269 = 3.0_f32.sqrt() / 3.0 but alas `sqrt` isn't a const fn
// so it cannot be used in this context.
Vector3::new(0.577350269, 0.577350269, 0.577350269),
Vector3::new(-0.577350269, 0.577350269, 0.577350269),
Vector3::new(-0.577350269, -0.577350269, 0.577350269),
Vector3::new(0.577350269, -0.577350269, 0.577350269),
Vector3::new(0.577_350_26, 0.577_350_26, 0.577_350_26),
Vector3::new(-0.577_350_26, 0.577_350_26, 0.577_350_26),
Vector3::new(-0.577_350_26, -0.577_350_26, 0.577_350_26),
Vector3::new(0.577_350_26, -0.577_350_26, 0.577_350_26),
];

pub struct ExtentVolume {
Expand Down
12 changes: 6 additions & 6 deletions src/geometry/instance.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::rc::Rc;

use geometry::mesh::Mesh;
use geometry::{BoundingVolume, Intersectable, Shape, Transformable, TriangleStorage};
use intersection::Intersection;
use material::Material;
use math::{Matrix4, Transform};
use ray::Ray;
use crate::geometry::mesh::Mesh;
use crate::geometry::{BoundingVolume, Intersectable, Shape, Transformable, TriangleStorage};
use crate::intersection::Intersection;
use crate::material::Material;
use crate::math::{Matrix4, Transform};
use crate::ray::Ray;

pub struct Instance<V, S> {
mesh: Rc<Mesh<V, S>>,
Expand Down
29 changes: 12 additions & 17 deletions src/geometry/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::rc::Rc;

use geometry::triangle::Normal;
use geometry::{BoundingVolume, Intersectable, Material, Transformable, Triangle, TriangleStorage};
use intersection::Intersection;
use math::{Point3, Transform};
use ray::Ray;
use super::triangle::Normal;
use super::{BoundingVolume, Intersectable, Material, Transformable, Triangle, TriangleStorage};
use crate::intersection::Intersection;
use crate::math::{Point3, Transform};
use crate::ray::Ray;

#[derive(Debug)]
pub struct Mesh<V, S> {
Expand Down Expand Up @@ -133,18 +133,13 @@ impl<V: BoundingVolume, S: for<'a> TriangleStorage<'a>> Intersectable for Mesh<V
let mut nearest_intersection: Option<Intersection> = None;

for triangle in self.storage.intersect(ray, cull) {
let potential_intersection = triangle.intersect(ray, cull);

match potential_intersection {
Some(intersection) => match nearest_intersection {
Some(nearest) => {
if intersection.t < nearest.t {
nearest_intersection = Some(intersection)
}
}
None => nearest_intersection = potential_intersection,
},
None => (),
let Some(intersection) = triangle.intersect(ray, cull) else {
continue;
};

let nearest = nearest_intersection.get_or_insert(intersection);
if intersection.t < nearest.t {
*nearest = intersection;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/geometry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub use self::triangle::Triangle;
pub use self::octtree::Octree;
pub use self::simple_triangle_storage::SimpleTriangleStorage;

use intersection::Intersection;
use material::Material;
use math::Transform;
use ray::Ray;
use crate::intersection::Intersection;
use crate::material::Material;
use crate::math::Transform;
use crate::ray::Ray;

pub trait Intersectable {
fn intersect(&self, ray: Ray, cull: bool) -> Option<Intersection>;
Expand Down
26 changes: 9 additions & 17 deletions src/geometry/octtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::{HashSet, VecDeque};
use std::ops::{Index, IndexMut};

use super::{BoundingVolume, Transformable, Triangle, TriangleStorage, AABB};
use math::Point3;
use math::Transform;
use ray::Ray;
use crate::math::Point3;
use crate::math::Transform;
use crate::ray::Ray;

#[derive(Debug, Clone, Copy)]
struct NodeId(usize);
Expand Down Expand Up @@ -42,12 +42,6 @@ struct Arena<M, T> {
}

impl<M: Default, T> Arena<M, T> {
fn new() -> Self {
Self {
nodes: Vec::default(),
}
}

fn with_capacity(capacity: usize) -> Self {
Self {
nodes: Vec::with_capacity(capacity),
Expand Down Expand Up @@ -162,7 +156,7 @@ pub struct Octree {
impl Octree {
fn visit_nodes<F>(&self, mut callback: F)
where
F: FnMut(NodeId) -> (),
F: FnMut(NodeId),
{
let mut to_visit = VecDeque::new();
to_visit.push_back(self.root);
Expand Down Expand Up @@ -248,15 +242,15 @@ impl Octree {
for i in 0..8 {
let triangle = &self.triangles[triangle_id.value()];

if child_bounding_volumes[i].intersects_triangle_aabb(&triangle) {
if child_bounding_volumes[i].intersects_triangle_aabb(triangle) {
to_delete.insert(*triangle_id);
child_nodes[i].insert(*triangle_id);
}
}
}

node.data = node.data.difference(&to_delete).cloned().collect();
assert!(node.data.len() == 0);
assert!(node.data.is_empty());
(child_nodes, child_bounding_volumes)
};

Expand Down Expand Up @@ -313,7 +307,7 @@ impl Octree {
impl Transformable for Octree {
fn transform(&mut self, transform: &Transform) {
for triangle in self.all_mut() {
triangle.transform(&transform);
triangle.transform(transform);
}

self.rebuild();
Expand All @@ -322,7 +316,7 @@ impl Transformable for Octree {
fn apply_transforms(&mut self, transforms: &[Transform]) {
for transform in transforms {
for triangle in self.all_mut() {
triangle.transform(&transform);
triangle.transform(transform);
}
}

Expand Down Expand Up @@ -387,9 +381,7 @@ impl<'a> TriangleStorage<'a> for Octree {

#[cfg(test)]
mod tests {
use super::Octree;
use geometry::AABB;
use math::Point3;
use super::*;

#[test]
fn test_build_octants() {
Expand Down
12 changes: 6 additions & 6 deletions src/geometry/plane.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::rc::Rc;

use geometry::{Intersectable, Shape, Transformable};
use intersection::Intersection;
use material::Material;
use math::EPSILON;
use math::{Point3, Transform, Vector3};
use ray::Ray;
use super::{Intersectable, Shape, Transformable};
use crate::intersection::Intersection;
use crate::material::Material;
use crate::math::EPSILON;
use crate::math::{Point3, Transform, Vector3};
use crate::ray::Ray;

pub struct Plane {
pub origin: Point3,
Expand Down
Loading
Loading