Add perlin debugging. Still not right.

This commit is contained in:
Bill Thiede 2018-10-05 21:10:40 -07:00
parent aeaf4994fe
commit 7898e7022c
5 changed files with 81 additions and 16 deletions

View File

@ -31,21 +31,11 @@ fn perlin_generate() -> Vec<Vec3> {
}).collect()
}
fn permute(p: &mut Vec<usize>, n: usize) {
fn perlin_generate_perm() -> Vec<usize> {
//let mut rng: XorShiftRng = SeedableRng::from_seed(SEED);
let mut rng = rand::thread_rng();
let mut random = || rng.gen_range::<f32>(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<usize> {
let mut p: Vec<usize> = (0..256).map(|i| i).collect();
permute(p.as_mut(), 256);
rng.shuffle(&mut p);
p
}

View File

@ -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))]

View File

@ -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;

View File

@ -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<Texture> = Arc::new(NoiseTexture::with_scale(10.));
let objects: Vec<Box<Hit>> = 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<Hit> = 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,
}
}

View File

@ -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<Texture> {
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))
}
}