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);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use rand::prelude::*;
|
||||
use rand::Rng;
|
||||
|
||||
// Constants
|
||||
pub const INFINITY: f64 = std::f64::INFINITY;
|
||||
|
||||
22
src/vec3.rs
22
src/vec3.rs
@@ -1,5 +1,7 @@
|
||||
use std::{ops::{Add, Sub, AddAssign, SubAssign, Mul, Div, MulAssign, DivAssign}};
|
||||
|
||||
use crate::rtweekend::{random_f64, random_range_f64};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Vec3 {
|
||||
pub elements: [f64; 3],
|
||||
@@ -49,6 +51,14 @@ impl Vec3 {
|
||||
pub fn unit_vector(self) -> Self {
|
||||
self / self.length()
|
||||
}
|
||||
|
||||
pub fn random() -> Self {
|
||||
Vec3::new(random_f64(), random_f64(), random_f64())
|
||||
}
|
||||
|
||||
pub fn random_range(min: f64, max: f64) -> Self {
|
||||
Vec3::new(random_range_f64(min, max), random_range_f64(min, max), random_range_f64(min, max))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vec3 {
|
||||
@@ -165,4 +175,14 @@ impl DivAssign<f64> for Vec3 {
|
||||
}
|
||||
|
||||
pub type Color = Vec3;
|
||||
pub type Point3 = Vec3;
|
||||
pub type Point3 = Vec3;
|
||||
|
||||
pub fn random_in_unit_sphere() -> Vec3 {
|
||||
loop {
|
||||
let p = Vec3::random_range(-1.0, 1.0);
|
||||
if p.length_squared() >= 1.0 {
|
||||
continue;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user