Draw first real image
This commit is contained in:
parent
62b9fdcb56
commit
50d3874467
16 changed files with 565 additions and 56 deletions
49
ray-tracing-scene/src/basic_scene.rs
Normal file
49
ray-tracing-scene/src/basic_scene.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
use rand::Rng;
|
||||
use ray_tracing_core::material::DefaultMaterial;
|
||||
use ray_tracing_core::prelude::*;
|
||||
use ray_tracing_core::scene::{Intersection, Scene};
|
||||
|
||||
pub struct BasicScene {
|
||||
pub(crate) spheres: Vec<(Pos3, Float)>,
|
||||
}
|
||||
|
||||
impl BasicScene {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
spheres: vec![(Pos3::zero(), 1.0), (Pos3::new(0.0, 0.0, 2.5), 2.0)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Rng> Scene<R> for BasicScene {
|
||||
fn intersect(&self, ray: Ray, min: Float, max: Float) -> Option<Intersection<'_, R>> {
|
||||
let mut intersection: Option<Intersection<'_, R>> = None;
|
||||
|
||||
for &(c, r) in &self.spheres {
|
||||
let offset = ray.start() - c;
|
||||
let p = Dir3::dot(ray.dir(), offset);
|
||||
let delta = p * p - (offset.length_squared() - r * r);
|
||||
|
||||
if delta >= 0.0 {
|
||||
let d = -p - Float::sqrt(delta);
|
||||
|
||||
let int = Intersection::new(
|
||||
d,
|
||||
((ray.start() + d * ray.dir()) - c).normalize(),
|
||||
&DefaultMaterial {},
|
||||
);
|
||||
if d >= min && d <= max {
|
||||
if let Some(i) = intersection.as_ref() {
|
||||
if i.t() > d {
|
||||
intersection.replace(int);
|
||||
}
|
||||
} else {
|
||||
intersection = Some(int)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
intersection
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue