Schwarze Oberfläche

This commit is contained in:
Jonathan Flueren 2022-05-18 17:49:46 +02:00
parent 48bbd5f2f0
commit 0632c60989
3 changed files with 29 additions and 6 deletions

View file

@ -3,7 +3,7 @@ use super::Ray;
use super::Vec3;
pub struct HitRecord {
p: Point3,
pub p: Point3,
pub normal: Vec3,
pub t: f64,
front_face: bool,

View file

@ -19,11 +19,16 @@ use image::{Rgb, RgbImage};
use ray::Ray;
use vec3::{Color, Point3, Vec3};
fn ray_color(r: &Ray, world: &HittableList) -> Color {
fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color {
let mut rec = HitRecord::empty();
if depth <= 0 {
return Color::new(0.0,0.0,0.0);
}
if world.hit(r, 0.0, f64::INFINITY, &mut rec) {
return 0.5 * (rec.normal + Color::new(1.0, 1.0, 1.0));
let target = rec.p + rec.normal + Vec3::random_in_unit_sphere();
return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth-1);
}
let unit_direction = r.direction();
let t = 0.5 * (unit_direction.y() + 1.0);
@ -38,7 +43,8 @@ fn main() {
let aspect_ratio = 16.0 / 9.0;
let image_width = 1000;
let image_height = (image_width as f64 / aspect_ratio) as u32;
let samples_per_pixel = 50_u32;
let samples_per_pixel = 10_u32;
let max_depth = 50;
// World
let mut world = HittableList::new();
@ -47,7 +53,7 @@ fn main() {
0.5,
)));
world.add(Box::<Sphere>::new(Sphere::new(
Point3::new(0.0, -100.5, -150.0),
Point3::new(0.0, -100.5, -1.0),
100.0,
)));
world.add(Box::<Sphere>::new(Sphere::new(
@ -93,7 +99,7 @@ fn main() {
let u = (i as f64 + utility::random_f64()) / (image_width - 1) as f64;
let v = (j as f64 + utility::random_f64()) / (image_height - 1) as f64;
let r = cam.get_ray(u, v);
pixel_color += ray_color(&r, &mut world);
pixel_color += ray_color(&r, &mut world, max_depth);
}
//color::write_color(&mut file, &pixel_color);

View file

@ -1,4 +1,5 @@
use std::ops::*;
use super::utility;
#[derive(Clone, Copy, Debug)]
pub struct Vec3 {
@ -37,6 +38,22 @@ impl Vec3 {
a / a.length()
}
pub fn random_f64() -> Vec3 {
Vec3::new(utility::random_f64(),utility::random_f64(),utility::random_f64())
}
pub fn random_rng(min: f64, max: f64) -> Self {
Self::new(utility::random_rng(min,max), utility::random_rng(min,max), utility::random_rng(min,max))
}
pub fn random_in_unit_sphere() -> Self {
loop {
let p = Vec3::random_rng(-1.0, 1.0);
if p.length_squared() >= 1.0 { continue };
return p;
}
}
pub fn x(&self) -> f64 {
self.e[0]
}