added metals and dielectrics
This commit is contained in:
28
src/main.rs
28
src/main.rs
@@ -20,22 +20,23 @@ use crate::{
|
||||
vec3::*,
|
||||
ray::*,
|
||||
hittable::*,
|
||||
camera::*,
|
||||
camera::*, material::{Lambertian, Dielectric},
|
||||
};
|
||||
|
||||
fn ray_color(r: &Ray, world: &dyn Hittable, depth: i32) -> Color {
|
||||
let mut rec = HitRecord::new_empty(&Metal{ albedo: Color::new_empty() });
|
||||
|
||||
fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
// Limit the bounces
|
||||
if depth <= 0 {
|
||||
return Color::new(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if let Some(rec) = world.hit(r, 0.001, INFINITY) {
|
||||
if let Some(rec) = world.hit(r, 0.001, INFINITY) {
|
||||
let mut scattered = Ray::new_empty();
|
||||
let mut attenuation = Color::new_empty();
|
||||
|
||||
//if rec.mat_ptr
|
||||
if rec.mat_ptr.scatter(r, rec, &mut attenuation, &mut scattered) {
|
||||
return attenuation * ray_color(&scattered, world, depth - 1);
|
||||
}
|
||||
return Color::new_empty();
|
||||
}
|
||||
|
||||
let unit_direction: Vec3 = r.direction().unit_vector();
|
||||
@@ -54,11 +55,18 @@ fn main() {
|
||||
|
||||
// World
|
||||
let mut world = HittableList::new_empty();
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
|
||||
static MATERIAL_GROUND: Lambertian = Lambertian::new(Color::new(0.3, 0.8, 0.0));
|
||||
static MATERIAL_CENTER: Lambertian = Lambertian::new(Color::new(0.1, 0.2, 0.5));
|
||||
static MATERIAL_LEFT: Dielectric = Dielectric::new(1.5);
|
||||
static MATERIAL_RIGHT: Metal = Metal::new(Color::new(0.8, 0.6, 0.2), 0.0);
|
||||
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, &MATERIAL_GROUND)));
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5, &MATERIAL_CENTER)));
|
||||
world.add(Box::new(Sphere::new(Point3::new(-1.0, 0.0, -1.0), -0.5, &MATERIAL_LEFT)));
|
||||
world.add(Box::new(Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, &MATERIAL_RIGHT)));
|
||||
|
||||
// Camera
|
||||
let cam = Camera::new();
|
||||
let cam = Camera::new(120.0, aspect_ratio);
|
||||
|
||||
// Render
|
||||
//let mut rng = rand::thread_rng();
|
||||
@@ -90,4 +98,4 @@ fn main() {
|
||||
println!("Took {:.6}s", now.elapsed().as_secs_f64());
|
||||
println!("Took {:.6}m", now.elapsed().as_secs_f64() / 60.0);
|
||||
println!("Took {:.6}h", now.elapsed().as_secs_f64() / 3600.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user