-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support rendering of multiple objects
- Loading branch information
1 parent
57d27e4
commit 7ab282d
Showing
7 changed files
with
61 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub mod camera; | ||
pub mod intersect; | ||
pub mod ray; | ||
pub mod scene; | ||
|
||
pub use camera::Camera; | ||
pub use intersect::{Intersect, RayIntersection}; | ||
pub use ray::Ray; | ||
pub use scene::Scene; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
use crate::core::Ray; | ||
use nalgebra::Vector3; | ||
use std::ops::Range; | ||
|
||
pub struct RayIntersection { | ||
pub t: f64, | ||
pub normal: Vector3<f64>, | ||
} | ||
|
||
pub trait Intersect { | ||
fn intersect(&self, _ray: &Ray) -> Option<RayIntersection> { | ||
fn intersect(&self, _ray: &Ray, _range: &Range<f64>) -> Option<RayIntersection> { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::core::{Intersect, Ray, RayIntersection}; | ||
use std::ops::Range; | ||
|
||
pub struct Scene { | ||
objects: Vec<Box<dyn Intersect>>, | ||
} | ||
|
||
impl Scene { | ||
pub fn new() -> Self { | ||
Self { | ||
objects: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn add(&mut self, object: Box<dyn Intersect>) { | ||
self.objects.push(object); | ||
} | ||
} | ||
|
||
impl Intersect for Scene { | ||
fn intersect(&self, ray: &Ray, range: &Range<f64>) -> Option<RayIntersection> { | ||
let mut range = range.clone(); | ||
let mut closest_intersection = None; | ||
for object in &self.objects { | ||
if let Some(intersection) = object.intersect(ray, &range) { | ||
range.end = intersection.t; | ||
closest_intersection = Some(intersection); | ||
} | ||
} | ||
closest_intersection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use nalgebra::Vector3; | ||
use palette::LinSrgb; | ||
use palette::Srgb; | ||
|
||
pub fn unit_vec_to_rgb(vec: Vector3<f64>) -> LinSrgb<f64> { | ||
pub fn unit_vec_to_rgb(vec: Vector3<f64>) -> Srgb<f64> { | ||
let vec = 0.5 * (vec + Vector3::new(1.0, 1.0, 1.0)); | ||
LinSrgb::new(vec.x, vec.y, vec.z) | ||
Srgb::new(vec.x, vec.y, vec.z) | ||
} |