Draw first image
This commit is contained in:
parent
4c872f9f91
commit
62b9fdcb56
19 changed files with 1566 additions and 76 deletions
8
ray-tracing-scene/Cargo.toml
Normal file
8
ray-tracing-scene/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "ray-tracing-scene"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
ray-tracing-core = { path = "../ray-tracing-core" }
|
||||
51
ray-tracing-scene/src/lib.rs
Normal file
51
ray-tracing-scene/src/lib.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use rand::Rng;
|
||||
use ray_tracing_core::material::DefaultMaterial;
|
||||
use ray_tracing_core::prelude::*;
|
||||
use ray_tracing_core::scene::{Intersection, Scene};
|
||||
|
||||
pub mod sphere;
|
||||
|
||||
pub struct BasicScene {
|
||||
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
|
||||
}
|
||||
}
|
||||
0
ray-tracing-scene/src/sphere.rs
Normal file
0
ray-tracing-scene/src/sphere.rs
Normal file
Loading…
Add table
Add a link
Reference in a new issue