added hemispherical scattering, switched to ppm version 6
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -6,8 +6,9 @@ pub mod hittable;
|
||||
pub mod hittable_list;
|
||||
pub mod rtweekend;
|
||||
pub mod camera;
|
||||
pub mod material;
|
||||
|
||||
use std::{io::{BufWriter, Write}, fs::File, time::Instant};
|
||||
use std::{io::{BufWriter, Write, stdout}, fs::File, time::Instant};
|
||||
|
||||
use crate::{
|
||||
color::*,
|
||||
@@ -28,8 +29,8 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
return Color::new(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if world.hit(r, 0.0, INFINITY, &mut rec) {
|
||||
let target = rec.p + rec.normal + random_in_unit_sphere();
|
||||
if world.hit(r, 0.001, INFINITY, &mut rec) {
|
||||
let target = rec.p + random_in_hemisphere(&rec.normal);
|
||||
return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth - 1)
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
fn main() {
|
||||
// Image
|
||||
let aspect_ratio: f64 = 1.0 / 1.0;
|
||||
let image_width: i32 = 512;
|
||||
let image_width: i32 = 1024;
|
||||
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
|
||||
let samples_per_pixel: i32 = 100;
|
||||
let max_depth: i32 = 4;
|
||||
@@ -60,24 +61,26 @@ fn main() {
|
||||
let now = Instant::now();
|
||||
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");
|
||||
write!(file, "P6\n{image_width} {image_height}\n255\n").expect("Error while writing Magic Byte");
|
||||
|
||||
for j in (0..image_width).rev() {
|
||||
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 _ 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, max_depth)
|
||||
}
|
||||
write_color(&mut file, pixel_color, samples_per_pixel);
|
||||
write_color_bin(&mut file, pixel_color, samples_per_pixel);
|
||||
}
|
||||
|
||||
println!("[ {:.2} % ]", (1.0 - ((j + 1) as f64 / image_width as f64)) * 100.0);
|
||||
print!(
|
||||
"[ {:.2} % ]\r",
|
||||
(1.0 - ((j + 1) as f64 / image_width as f64)) * 100.0
|
||||
);
|
||||
stdout().flush().expect("Can flush stdout");
|
||||
}
|
||||
println!("Took {:.6}ms", now.elapsed().as_millis());
|
||||
println!("Took {:.6}s", now.elapsed().as_secs_f64());
|
||||
|
||||
Reference in New Issue
Block a user