diff --git a/rtiow/src/perlin.rs b/rtiow/src/perlin.rs index 2c3a393..7e99cb6 100644 --- a/rtiow/src/perlin.rs +++ b/rtiow/src/perlin.rs @@ -31,21 +31,11 @@ fn perlin_generate() -> Vec { }).collect() } -fn permute(p: &mut Vec, n: usize) { +fn perlin_generate_perm() -> Vec { //let mut rng: XorShiftRng = SeedableRng::from_seed(SEED); let mut rng = rand::thread_rng(); - let mut random = || rng.gen_range::(0., 1.); - for i in (0..n).rev() { - let target = (random() * (i + 1) as f32) as usize; - let tmp = p[i]; - p[i] = p[target]; - p[target] = tmp; - } -} - -fn perlin_generate_perm() -> Vec { let mut p: Vec = (0..256).map(|i| i).collect(); - permute(p.as_mut(), 256); + rng.shuffle(&mut p); p } diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index ed3ef68..d20b689 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -28,6 +28,7 @@ pub enum Model { Test, CornellBox, CornellSmoke, + PerlinDebug, } impl Model { @@ -40,6 +41,7 @@ impl Model { Model::Test => scenes::test::new(&opt), Model::CornellBox => scenes::cornell_box::new(&opt), Model::CornellSmoke => scenes::cornell_smoke::new(&opt), + Model::PerlinDebug => scenes::perlin_debug::new(&opt), } } } @@ -64,6 +66,7 @@ impl str::FromStr for Model { "test" => Ok(Model::Test), "cornell_box" => Ok(Model::CornellBox), "cornell_smoke" => Ok(Model::CornellSmoke), + "perlin_debug" => Ok(Model::PerlinDebug), _ => Err(ModelParseError(s.to_owned())), } } @@ -82,8 +85,8 @@ pub struct Opt { #[structopt(short = "s", long = "subsample", default_value = "8")] pub subsamples: usize, /// Select scene to render, one of: "bench", "book", "tutorial", "bvh", "test", "cornell_box", - /// "cornell_smoke" - #[structopt(long = "model", default_value = "cornell_smoke")] + /// "cornell_smoke", "perlin_debug" + #[structopt(long = "model", default_value = "perlin_debug")] pub model: Model, /// Path to store pprof profile data, i.e. /tmp/cpuprofile.pprof #[structopt(long = "pprof", parse(from_os_str))] diff --git a/rtiow/src/scenes/mod.rs b/rtiow/src/scenes/mod.rs index f6f2b2c..ae0cd14 100644 --- a/rtiow/src/scenes/mod.rs +++ b/rtiow/src/scenes/mod.rs @@ -3,5 +3,6 @@ pub mod book; pub mod bvh; pub mod cornell_box; pub mod cornell_smoke; +pub mod perlin_debug; pub mod test; pub mod tutorial; diff --git a/rtiow/src/scenes/perlin_debug.rs b/rtiow/src/scenes/perlin_debug.rs new file mode 100644 index 0000000..4edd561 --- /dev/null +++ b/rtiow/src/scenes/perlin_debug.rs @@ -0,0 +1,60 @@ +use std::sync::Arc; + +use camera::Camera; +use hitable::Hit; +use hitable_list::HitableList; +use kdtree::KDTree; +use material::Lambertian; +use renderer::Opt; +use renderer::Scene; +use sphere::Sphere; +use texture::NoiseTexture; +use texture::Texture; +use vec3::Vec3; + +pub fn new(opt: &Opt) -> Scene { + let lookfrom = Vec3::new(13., 2., 3.); + let lookat = Vec3::new(0., 0., 0.); + let dist_to_focus = 10.0; + let aperture = 0.0; + let time_min = 0.; + let time_max = 1.; + let camera = Camera::new( + lookfrom, + lookat, + Vec3::new(0., 1., 0.), + 20., + opt.width as f32 / opt.height as f32, + aperture, + dist_to_focus, + time_min, + time_max, + ); + let pertext: Arc = Arc::new(NoiseTexture::with_scale(10.)); + + let objects: Vec> = vec![ + Box::new(Sphere::new( + Vec3::new(0., -1000., 0.), + 1000., + Lambertian::new(Arc::clone(&pertext)), + )), + Box::new(Sphere::new( + Vec3::new(0., 2., 0.), + 2.0, + Lambertian::new(Arc::clone(&pertext)), + )), + ]; + let world: Box = if opt.use_accel { + Box::new(KDTree::new(objects, time_min, time_max)) + } else { + Box::new(HitableList::new(objects)) + }; + Scene { + camera, + world, + subsamples: opt.subsamples, + width: opt.width, + height: opt.height, + global_illumination: true, + } +} diff --git a/rtiow/src/texture.rs b/rtiow/src/texture.rs index 255f768..ad5cfcb 100644 --- a/rtiow/src/texture.rs +++ b/rtiow/src/texture.rs @@ -1,12 +1,21 @@ +use std::sync::Arc; + use image::RgbImage; use perlin::turb; +use perlin::GENERATOR; use vec3::Vec3; pub trait Texture: Send + Sync { fn value(&self, u: f32, v: f32, p: Vec3) -> Vec3; } +impl Texture for Arc { + fn value(&self, u: f32, v: f32, p: Vec3) -> Vec3 { + (**self).value(u, v, p) + } +} + #[derive(Debug, PartialEq)] pub struct ConstantTexture { color: Vec3, @@ -75,9 +84,11 @@ impl NoiseTexture { impl Texture for NoiseTexture { fn value(&self, _u: f32, _v: f32, p: Vec3) -> Vec3 { - Vec3::new(1., 1., 1.) * turb(self.scale * p, 7) + let _ = GENERATOR; + let _ = turb; + //Vec3::new(1., 1., 1.) * turb(self.scale * p, 7) //Vec3::new(1., 1., 1.) * 0.5 * (1. + turb(self.scale * p, 7)) - //Vec3::new(1., 1., 1.) * 0.5 * (1. + (self.scale * p.x + 5. * turb(p, 7)).sin()) + Vec3::new(1., 1., 1.) * 0.5 * (1. + (self.scale * p.z + 10. * turb(p, 7)).sin()) //Vec3::new(1., 1., 1.) * 0.5 * (1. + GENERATOR.noise(self.scale * p)) } }