first diffused sphere
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -8,7 +8,6 @@ pub mod rtweekend;
|
||||
pub mod camera;
|
||||
|
||||
use std::{io::{BufWriter, Write}, fs::File};
|
||||
use camera::Camera;
|
||||
|
||||
use crate::{
|
||||
color::*,
|
||||
@@ -18,12 +17,20 @@ use crate::{
|
||||
vec3::*,
|
||||
ray::*,
|
||||
hittable::*,
|
||||
camera::*,
|
||||
};
|
||||
|
||||
fn ray_color(r: &Ray, world: &mut dyn Hittable) -> Color {
|
||||
fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
let mut rec = HitRecord::new_empty();
|
||||
|
||||
// Limit the bounces
|
||||
if depth <= 0 {
|
||||
return Color::new(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if world.hit(r, 0.0, INFINITY, &mut rec) {
|
||||
return 0.5 * (rec.normal + Color::new(1.0, 1.0, 1.0))
|
||||
let target = rec.p + rec.normal + random_in_unit_sphere();
|
||||
return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth - 1)
|
||||
}
|
||||
|
||||
let unit_direction: Vec3 = r.direction().unit_vector();
|
||||
@@ -37,7 +44,8 @@ fn main() {
|
||||
let aspect_ratio: f64 = 1.0 / 1.0;
|
||||
let image_width: i32 = 512;
|
||||
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
|
||||
let samples_per_pixel = 100;
|
||||
let samples_per_pixel: i32 = 100;
|
||||
let max_depth: i32 = 4;
|
||||
|
||||
// World
|
||||
let mut world = HittableList::new_empty();
|
||||
@@ -45,25 +53,25 @@ fn main() {
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
|
||||
|
||||
// Camera
|
||||
let mut cam = Camera::new();
|
||||
let cam = Camera::new();
|
||||
|
||||
// Render
|
||||
|
||||
//let mut rng = rand::thread_rng();
|
||||
let mut file = BufWriter::new(File::create("img.ppm").expect("File creation failed"));
|
||||
|
||||
write!(file, "P3\n{image_width} {image_height}\n255").expect("Error while writing Magic Byte");
|
||||
|
||||
for j in (0..image_width).rev() {
|
||||
write!(file, "\n").expect("grr");
|
||||
write!(file, "\n").expect("Error writing to File");
|
||||
|
||||
for i in 0..image_height {
|
||||
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
|
||||
|
||||
for s in 0..samples_per_pixel {
|
||||
for _s in 0..samples_per_pixel {
|
||||
let u = (i as f64 + random_f64()) / (image_width - 1) as f64;
|
||||
let v = (j as f64 + random_f64()) / (image_height - 1) as f64;
|
||||
let r: Ray = cam.get_ray(u, v);
|
||||
pixel_color += ray_color(&r, &mut world)
|
||||
pixel_color += ray_color(&r, &mut world, max_depth)
|
||||
}
|
||||
write_color(&mut file, pixel_color, samples_per_pixel);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user