Skip to content

Commit

Permalink
Add cornell_flips with the flipped faces just in case. Refer to RayTr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Jun 27, 2020
1 parent 388bd2f commit 54dba1a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use color::Color;
use hitable::{BVHNode, HitRecord, Hitable};
#[allow(unused)] // Scene imports, only using one at a time
use scenes::{
cornell, glass_spheres, metal_spheres, random_scene, simple_light_lambertian,
cornell, cornell_flips, glass_spheres, metal_spheres, random_scene, simple_light_lambertian,
simple_light_perlin, two_perlin_spheres, two_spheres,
};
mod perlin;
Expand Down
14 changes: 11 additions & 3 deletions src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ pub struct Lambertian {

impl Material for Lambertian {
fn scatter(&self, ray: &Ray, hit_record: &HitRecord, rng: ThreadRng) -> Option<(Ray, Color)> {
let target = hit_record.position + hit_record.normal + random_unit_vector(rng);
let scattered: Ray = Ray::new(hit_record.position, target - hit_record.position, ray.time);
let attenuation: Color = self
let scatter_direction: Vec3 = hit_record.normal + random_unit_vector(rng);
let scattered = Ray::new(hit_record.position, scatter_direction, ray.time);
let attenuation = self
.albedo
.color(hit_record.u, hit_record.v, hit_record.position);
Some((scattered, attenuation))

// old backup
// let target = hit_record.position + hit_record.normal + random_unit_vector(rng);
// let scattered: Ray = Ray::new(hit_record.position, target - hit_record.position, ray.time);
// let attenuation: Color = self
// .albedo
// .color(hit_record.u, hit_record.v, hit_record.position);
// Some((scattered, attenuation))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use rand::prelude::*;

pub mod cornell;
pub mod cornell_flips;
pub mod glass_spheres;
pub mod metal_spheres;
pub mod random_scene;
Expand Down
102 changes: 102 additions & 0 deletions src/scenes/cornell_flips.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use super::Scene;
use crate::{
camera::Camera,
color::Color,
hitable::{FlipFace, HitableList},
material::{DiffuseLight, Lambertian, Material},
rect::{XYRect, XZRect, YZRect},
texture::SolidColor,
Float, Vec3, HEIGHT, WIDTH,
};
use rand::prelude::*;
use std::sync::Arc;

pub fn load(rng: ThreadRng) -> Scene {
let time_0: Float = 0.0;
let time_1: Float = 1.0;
let mut world: HitableList = HitableList::new();

let red = Lambertian::new(Arc::new(SolidColor::new(Color::new(0.65, 0.05, 0.05))));
let white: Arc<dyn Material> = Arc::new(Lambertian::new(Arc::new(SolidColor::new(
Color::new(0.73, 0.73, 0.73),
))));
let green = Lambertian::new(Arc::new(SolidColor::new(Color::new(0.12, 0.45, 0.15))));
let light = DiffuseLight::new(Arc::new(SolidColor::new(Color::new(15.0, 15.0, 15.0))));

world
.hitables
.push(Arc::new(FlipFace::new(Arc::new(YZRect::new(
0.0,
555.0,
0.0,
555.0,
555.0,
Arc::new(green),
)))));
world.hitables.push(Arc::new(YZRect::new(
0.0,
555.0,
0.0,
555.0,
0.0,
Arc::new(red),
)));
world.hitables.push(Arc::new(XZRect::new(
213.0,
343.0,
227.0,
332.0,
554.0,
Arc::new(light),
)));
world
.hitables
.push(Arc::new(FlipFace::new(Arc::new(XZRect::new(
0.0,
555.0,
0.0,
555.0,
0.0,
Arc::clone(&white),
)))));
world.hitables.push(Arc::new(XZRect::new(
0.0,
555.0,
0.0,
555.0,
555.0,
Arc::clone(&white),
)));
world
.hitables
.push(Arc::new(FlipFace::new(Arc::new(XYRect::new(
0.0,
555.0,
0.0,
555.0,
555.0,
Arc::clone(&white),
)))));

let camera_position: Vec3 = Vec3::new(278.0, 278.0, -800.0);
let camera_target: Vec3 = Vec3::new(278.0, 278.0, 0.0);
let camera_up: Vec3 = Vec3::new(0.0, 1.0, 0.0);
let fov: Float = 40.0;
let aspect_ratio: Float = WIDTH as Float / HEIGHT as Float;
let aperture: Float = 0.0;
let focus_distance: Float = 10.0;
let camera = Camera::new(
camera_position,
camera_target,
camera_up,
fov,
aspect_ratio,
aperture,
focus_distance,
time_0,
time_1,
);

let background: Color = Color::new(0.0, 0.0, 0.0); // Black background = only lit by the light, no ambient
Scene::new(world, camera, time_0, time_1, background, rng)
}

0 comments on commit 54dba1a

Please sign in to comment.