Skip to content

Commit

Permalink
perlin noise!
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBiscardi committed Sep 1, 2023
1 parent 0b6ee8a commit c1548fa
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 9 deletions.
95 changes: 87 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -12,5 +12,6 @@ glam = { version = "0.24.1", features = ["rand"] }
image = "0.24.7"
indicatif = { version = "0.17.6", features = ["rayon"] }
itertools = "0.11.0"
noise = "0.8.2"
rand = "0.8.5"
rayon = "1.7.0"
49 changes: 49 additions & 0 deletions examples/perlin-noise.rs
@@ -0,0 +1,49 @@
use glam::DVec3;
use noise::{NoiseFn, Perlin, Seedable};
use raytracer::{
camera::Camera, material::Material,
shapes::sphere::Sphere, textures::Texture,
};
use std::io;

fn main() -> io::Result<()> {
let mut world = vec![];

// let perlin = Perlin::new(1);
// let noise_texture = Texture::PerlinNoise(perlin, 10.);

let perlin = Perlin::new(1);
let noise_texture = Texture::Turbulence(perlin);

world.push(Sphere::new(
DVec3::new(0., -1000., 0.),
1000.,
Material::Lambertian {
albedo: noise_texture.clone(),
},
));
world.push(Sphere::new(
DVec3::new(0., 2., 0.),
2.,
Material::Lambertian {
albedo: noise_texture,
},
));

let camera = Camera::init()
.image_width(800)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(12., 2., 3.))
.look_at(DVec3::ZERO)
.vup(DVec3::Y)
.focus_dist(10.0)
.defocus_angle(0.0)
.samples_per_pixel(100)
.max_depth(50)
.vfov(20.)
.build();

camera.render_to_disk("perlin-noise", world)?;

Ok(())
}
18 changes: 17 additions & 1 deletion src/textures.rs
@@ -1,13 +1,16 @@
use std::{io, path::Path};

use glam::DVec3;
use glam::{DVec3, Vec3Swizzles};
use image::{DynamicImage, GenericImageView};
use noise::{NoiseFn, Perlin, Turbulence};

#[derive(Clone)]
pub enum Texture {
SolidColor(DVec3),
Checkered { even: DVec3, odd: DVec3, scale: f64 },
Image(DynamicImage),
PerlinNoise(Perlin, f64),
Turbulence(Perlin),
}
impl Texture {
pub fn load_image<P>(path: P) -> io::Result<Self>
Expand Down Expand Up @@ -73,6 +76,19 @@ impl Texture {
color_scale * pixel[2] as f64,
);
}
Texture::PerlinNoise(noise, freq) => {
DVec3::ONE
* noise.get(
(point * *freq).xyz().to_array(),
)
}
Texture::Turbulence(perlin) => {
let noise =
Turbulence::<_, Perlin>::new(perlin);
DVec3::ONE
* noise.get((point).xyz().to_array())
+ 1.0 / 2.0
}
}
}
}
Expand Down

0 comments on commit c1548fa

Please sign in to comment.