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

Book two [WIP] #1

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
Binary file added renders/bvh_bug.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/three_spheres_on_a_ checkered_plain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions src/aabb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::vector3;
use crate::ray;
use crate::hittable::HittableObj;
use crate::hittable::Hittable;
use std::cmp;
#[derive(Copy, Clone)]
pub struct AABB
{
pub minimum : vector3::Point,
pub maximum : vector3::Point,
}

impl AABB{
pub fn new(minimum : vector3::Point, maximum : vector3::Point) -> AABB {
//println!("AABB created with ({},{},{}) and ({},{},{})", minimum.x, minimum.y, minimum.z, maximum.x, maximum.y, maximum.z);
AABB {
minimum,
maximum,
}
}
pub fn hit(&self, r: &ray::Ray, mut t_min: f64, mut t_max: f64) -> bool {
let tx_0 = ((self.minimum.x - r.origin.x) / r.dir.x).min((self.maximum.x - r.origin.x) / r.dir.x);
let tx_1 = ((self.minimum.x - r.origin.x) / r.dir.x).max((self.maximum.x - r.origin.x) / r.dir.x);
t_min = t_min.max(tx_0);
t_max = t_max.min(tx_1);
if t_max <= t_min {
return false;
}
let ty_0 = ((self.minimum.y - r.origin.y) / r.dir.y).min((self.maximum.y - r.origin.y) / r.dir.y);
let ty_1 = ((self.minimum.y - r.origin.y) / r.dir.y).max((self.maximum.y - r.origin.y) / r.dir.y);
t_min = t_min.max(ty_0);
t_max = t_max.min(ty_1);
if t_max <= t_min {
return false;
}
let tz_0 = ((self.minimum.z - r.origin.z) / r.dir.z).min((self.maximum.z - r.origin.z) / r.dir.z);
let tz_1 = ((self.minimum.z - r.origin.z) / r.dir.z).max((self.maximum.z - r.origin.z) / r.dir.z);
t_min = t_min.max(tz_0);
t_max = t_max.min(tz_1);
if t_max <= t_min {
return false;
}
true
}
}

pub fn surrounding_box(box0 : AABB, box1 : AABB) -> AABB {
let smol_point = vector3::Point::new(
box0.minimum.x.min(box1.minimum.x),
box0.minimum.y.min(box1.minimum.y),
box0.minimum.z.min(box1.minimum.z)
);
let big_point = vector3::Point::new(
box0.maximum.x.max(box1.maximum.x),
box0.maximum.y.max(box1.maximum.y),
box0.maximum.z.max(box1.maximum.z)
);
AABB::new(smol_point, big_point)
}

pub fn box_compare(a : HittableObj, b : HittableObj, axis : i32) -> cmp::Ordering{
let box_a = a.bounding_box(0.0, 0.0);
let box_b = b.bounding_box(0.0, 0.0);
let x:f64;
let y:f64;
if axis == 0
{
x = box_a.unwrap().minimum.x;
y = box_b.unwrap().minimum.x;
}
else if axis == 1
{
x = box_a.unwrap().minimum.y;
y = box_b.unwrap().minimum.y;
}
else
{
x = box_a.unwrap().minimum.z;
y = box_b.unwrap().minimum.z;
}
if x < y
{
return cmp::Ordering::Less;
}
if x == y
{
return cmp::Ordering::Equal;
}
else
{
return cmp::Ordering::Greater;
}
}
207 changes: 207 additions & 0 deletions src/aarect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
use crate::aabb;
use crate::hittable;
use crate::material;
use crate::ray;
use crate::vector3;
use std::sync::Arc;

// --------------------------- XY ---------------------------------------

#[derive(Clone)]
pub struct XYRect {
material: Arc<material::Material>,
x0: f64,
x1: f64,
y0: f64,
y1: f64,
k: f64,
}

impl XYRect {
pub fn new(
x0: f64,
x1: f64,
y0: f64,
y1: f64,
k: f64,
material: Arc<material::Material>,
) -> XYRect {
XYRect {
x0,
x1,
y0,
y1,
k,
material,
}
}
}

impl hittable::Hittable for XYRect {
fn hit(&self, r: &ray::Ray, t_min: f64, t_max: f64) -> Option<hittable::HitRecord> {
let t = (self.k - r.origin.z()) / r.dir.z();
if t < t_min || t > t_max {
return None;
}

let x = r.origin.x() + t * r.dir.x();
let y = r.origin.y() + t * r.dir.y();

if x < self.x0 || x > self.x1 || y < self.y0 || y > self.y1 {
return None;
}

let mut hit_record = hittable::HitRecord {
p: r.at(t),
t: t,
normal: vector3::Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: self.material.clone(),
u: (x - self.x0) / (self.x1 - self.x0),
v: (y - self.y0) / (self.y1 - self.y0),
};

hit_record.set_face_normal(r, &vector3::Vec3::new(0.0, 0.0, 1.0));
Some(hit_record)
}
fn bounding_box(&self, _time_0: f64, _time_1: f64) -> Option<aabb::AABB> {
Some(aabb::AABB::new(
vector3::Point::new(self.x0, self.y0, self.k - 0.0001),
vector3::Point::new(self.x1, self.y1, self.k + 0.0001),
))
}
}

// --------------------------- XZ ---------------------------------------

#[derive(Clone)]
pub struct XZRect {
material: Arc<material::Material>,
x0: f64,
x1: f64,
z0: f64,
z1: f64,
k: f64,
}

impl XZRect {
pub fn new(
x0: f64,
x1: f64,
z0: f64,
z1: f64,
k: f64,
material: Arc<material::Material>,
) -> XZRect {
XZRect {
x0,
x1,
z0,
z1,
k,
material,
}
}
}

impl hittable::Hittable for XZRect {
fn hit(&self, r: &ray::Ray, t_min: f64, t_max: f64) -> Option<hittable::HitRecord> {
let t = (self.k - r.origin.y()) / r.dir.y();
if t < t_min || t > t_max {
return None;
}

let x = r.origin.x() + t * r.dir.x();
let z = r.origin.z() + t * r.dir.z();

if x < self.x0 || x > self.x1 || z < self.z0 || z > self.z1 {
return None;
}

let mut hit_record = hittable::HitRecord {
p: r.at(t),
t: t,
normal: vector3::Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: self.material.clone(),
u: (x - self.x0) / (self.x1 - self.x0),
v: (z - self.z0) / (self.z1 - self.z0),
};

hit_record.set_face_normal(r, &vector3::Vec3::new(0.0, 0.0, 1.0));
Some(hit_record)
}
fn bounding_box(&self, _time_0: f64, _time_1: f64) -> Option<aabb::AABB> {
Some(aabb::AABB::new(
vector3::Point::new(self.x0, self.k - 0.0001, self.z0),
vector3::Point::new(self.x1, self.k + 0.0001, self.z1),
))
}
}

// --------------------------- YZ ---------------------------------------

#[derive(Clone)]
pub struct YZRect {
material: Arc<material::Material>,
y0: f64,
y1: f64,
z0: f64,
z1: f64,
k: f64,
}

impl YZRect {
pub fn new(
y0: f64,
y1: f64,
z0: f64,
z1: f64,
k: f64,
material: Arc<material::Material>,
) -> YZRect {
YZRect {
y0,
y1,
z0,
z1,
k,
material,
}
}
}

impl hittable::Hittable for YZRect {
fn hit(&self, r: &ray::Ray, t_min: f64, t_max: f64) -> Option<hittable::HitRecord> {
let t = (self.k - r.origin.x()) / r.dir.x();
if t < t_min || t > t_max {
return None;
}

let y = r.origin.y() + t * r.dir.y();
let z = r.origin.z() + t * r.dir.z();

if y < self.y0 || y > self.y1 || z < self.z0 || z > self.z1 {
return None;
}

let mut hit_record = hittable::HitRecord {
p: r.at(t),
t: t,
normal: vector3::Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: self.material.clone(),
u: (y - self.y0) / (self.y1 - self.y0),
v: (z - self.z0) / (self.z1 - self.z0),
};

hit_record.set_face_normal(r, &vector3::Vec3::new(0.0, 0.0, 1.0));
Some(hit_record)
}
fn bounding_box(&self, _time_0: f64, _time_1: f64) -> Option<aabb::AABB> {
Some(aabb::AABB::new(
vector3::Point::new(self.k - 0.0001, self.y0, self.z0),
vector3::Point::new(self.k + 0.0001, self.y1, self.z1),
))
}
}
Loading