1
0

Added checkered texture

This commit is contained in:
Ishan Jain 2021-03-02 18:29:01 +05:30
parent a278969bbf
commit 6037fe2802
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
5 changed files with 38 additions and 6 deletions

View File

@ -1,7 +1,7 @@
use crate::{
materials::{Dielectric, Lambertian, Metal},
shapes::{MovingSphere, Sphere},
texture::Solid,
texture::{Checker, Solid},
types::{Ray, Vec3},
BvhNode, Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION,
};
@ -55,7 +55,10 @@ impl Demo {
world.push(Arc::new(Sphere::new(
Vec3::new(0.0, -1000.0, 0.0),
1000.0,
Lambertian::new(Solid::new(Vec3::new(0.5, 0.5, 0.5))),
Lambertian::new(Checker::new(
Solid::new(Vec3::new(0.2, 0.3, 0.1)),
Solid::new(Vec3::new(0.9, 0.9, 0.9)),
)),
)));
let radius = 0.2;

View File

@ -21,6 +21,9 @@ impl<T: Texture + Send + Sync> Material for Lambertian<T> {
let scatter_direction = hit_rec.normal + random_point_in_unit_sphere(rng);
let scattered_ray = Ray::new(hit_rec.p, scatter_direction, ray.time());
(self.albedo.value(hit_rec.u, hit_rec.v), Some(scattered_ray))
let mut p = hit_rec.p;
self.albedo.value(hit_rec.u, hit_rec.v, &mut p);
(p, Some(scattered_ray))
}
}

24
src/texture/checker.rs Normal file
View File

@ -0,0 +1,24 @@
use crate::{types::Vec3, Texture};
pub struct Checker<T: Texture> {
odd: T,
even: T,
}
impl<T: Texture> Checker<T> {
pub fn new(even: T, odd: T) -> Self {
Self { odd, even }
}
}
impl<T: Texture> Texture for Checker<T> {
fn value(&self, u: f64, v: f64, p: &mut Vec3) {
let sine_wave = f64::sin(10.0 * p.x()) * f64::sin(10.0 * p.y()) * f64::sin(10.0 * p.z());
if sine_wave < 0.0 {
self.odd.value(u, v, p)
} else {
self.even.value(u, v, p)
}
}
}

View File

@ -1,9 +1,11 @@
mod checker;
mod solid;
pub use checker::Checker;
pub use solid::Solid;
use crate::types::Vec3;
pub trait Texture {
fn value(&self, u: f64, v: f64) -> Vec3;
fn value(&self, u: f64, v: f64, p: &mut Vec3);
}

View File

@ -11,7 +11,7 @@ impl Solid {
}
impl Texture for Solid {
fn value(&self, _u: f64, _v: f64) -> Vec3 {
self.color
fn value(&self, _u: f64, _v: f64, p: &mut Vec3) {
*p = self.color
}
}