Compare commits
4
Commits
ff4132edff
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3343133e9d | ||
|
|
27905c888f | ||
|
|
ff7cde8758 | ||
|
|
0e62a20775 |
Generated
+52
@@ -14,6 +14,37 @@ version = "1.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
@@ -99,6 +130,27 @@ name = "ray-trace-w1"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rand",
|
"rand",
|
||||||
|
"rayon",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.9.2" # 使用最新稳定版
|
rand = "0.9.2" # 使用最新稳定版
|
||||||
|
rayon = "1.11.0"
|
||||||
|
|||||||
+113
-14
@@ -1,5 +1,7 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufWriter};
|
use std::io::{BufWriter};
|
||||||
|
use std::sync::{RwLock};
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
use crate::hittable::HittableList;
|
use crate::hittable::HittableList;
|
||||||
use crate::math_utils::{clamp, degrees_to_radians, random_in_unit_disk};
|
use crate::math_utils::{clamp, degrees_to_radians, random_in_unit_disk};
|
||||||
@@ -7,6 +9,7 @@ use crate::ppm_writer::PPMWriter;
|
|||||||
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
|
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
|
||||||
use rand::rngs::ThreadRng;
|
use rand::rngs::ThreadRng;
|
||||||
use rand::{Rng, rng};
|
use rand::{Rng, rng};
|
||||||
|
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||||
use crate::material::{Material, MaterialKind};
|
use crate::material::{Material, MaterialKind};
|
||||||
|
|
||||||
impl<'a> Camera<'a> {
|
impl<'a> Camera<'a> {
|
||||||
@@ -70,8 +73,8 @@ impl<'a> Camera<'a> {
|
|||||||
// viewport_width,
|
// viewport_width,
|
||||||
// viewport_height,
|
// viewport_height,
|
||||||
center,
|
center,
|
||||||
look_at,
|
// look_at,
|
||||||
v_up,
|
// v_up,
|
||||||
// focal_length,
|
// focal_length,
|
||||||
// viewport_u,
|
// viewport_u,
|
||||||
// viewport_v,
|
// viewport_v,
|
||||||
@@ -90,6 +93,10 @@ impl<'a> Camera<'a> {
|
|||||||
|
|
||||||
pub fn render(&mut self, world: &HittableList) {
|
pub fn render(&mut self, world: &HittableList) {
|
||||||
// let mut img_content = format!("P3\n{} {}\n255\n", self.image_width, self.image_height);
|
// let mut img_content = format!("P3\n{} {}\n255\n", self.image_width, self.image_height);
|
||||||
|
let start = Instant::now();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for j in 0..self.image_height {
|
for j in 0..self.image_height {
|
||||||
// if j % 10 == 0 {
|
// if j % 10 == 0 {
|
||||||
@@ -97,16 +104,25 @@ impl<'a> Camera<'a> {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
for i in 0..self.image_width {
|
for i in 0..self.image_width {
|
||||||
// color
|
|
||||||
let mut color = Color::new(0.0, 0.0, 0.0);
|
let sample_color: Vec3 = (0..self.sample_times)
|
||||||
let rng = &mut rng();
|
.into_par_iter()
|
||||||
for _ in 0..self.sample_times {
|
.map(|_| {
|
||||||
let r = self.get_ray(i, j);
|
let r = self.get_ray(i, j);
|
||||||
let sample_color = self.ray_color(&r, self.reflect_depth, world);
|
return self.ray_color_sync(&r, self.reflect_depth, RwLock::new(world));
|
||||||
color = color + sample_color;
|
}).sum();
|
||||||
}
|
let color: Vec3 = sample_color * (1.0 / self.sample_times as f32);
|
||||||
// color * each sample color
|
|
||||||
color = color * (1.0 / self.sample_times as f32);
|
|
||||||
|
// // color
|
||||||
|
// let mut color = Color::new(0.0, 0.0, 0.0);
|
||||||
|
// for _ in 0..self.sample_times {
|
||||||
|
// let r = self.get_ray(i, j);
|
||||||
|
// let sample_color = self.ray_color(&r, self.reflect_depth, world);
|
||||||
|
// color = color + sample_color;
|
||||||
|
// }
|
||||||
|
// // color * each sample color
|
||||||
|
// color = color * (1.0 / self.sample_times as f32);
|
||||||
|
|
||||||
// clamp color rgb
|
// clamp color rgb
|
||||||
let color_clamped = Color::new(
|
let color_clamped = Color::new(
|
||||||
@@ -120,7 +136,8 @@ impl<'a> Camera<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _ = self.ppm_file_writer.finish();
|
let _ = self.ppm_file_writer.finish();
|
||||||
|
let end = Instant::now();
|
||||||
|
println!("================[{}mils]=====================", end.duration_since(start).as_millis())
|
||||||
// img_content
|
// img_content
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,9 +215,64 @@ impl<'a> Camera<'a> {
|
|||||||
(1.0 - a) * Color::new(1.0, 1.0, 1.0) + a * Color::new(0.5, 0.7, 1.0)
|
(1.0 - a) * Color::new(1.0, 1.0, 1.0) + a * Color::new(0.5, 0.7, 1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn ray_color_sync(&self, ray: &Ray, depth: i8, world: RwLock<&HittableList>) -> Vec3 {
|
||||||
|
|
||||||
|
// 反射次数
|
||||||
|
if depth <= 0 {
|
||||||
|
return Vec3::new(0.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 限制出射光线的角度(0.001经验值)
|
||||||
|
let hit_record = &mut HitRecord{
|
||||||
|
t: 0.0,
|
||||||
|
p: Vec3::new(0.0, 0.0, 0.0),
|
||||||
|
normal: Vec3::new(0.0, 0.0, 0.0),
|
||||||
|
front_face: false,
|
||||||
|
material: None,
|
||||||
|
};
|
||||||
|
let hit = world.read().unwrap().hit(&ray, 0.001, f32::MAX, hit_record);
|
||||||
|
if hit {
|
||||||
|
let hit_m = &hit_record.material;
|
||||||
|
let hc = &mut HitRecord{
|
||||||
|
t: hit_record.t,
|
||||||
|
p: hit_record.p,
|
||||||
|
normal: hit_record.normal,
|
||||||
|
front_face: hit_record.front_face,
|
||||||
|
material: None,
|
||||||
|
};
|
||||||
|
let hit_c = match hit_m {
|
||||||
|
Some(mk) => {
|
||||||
|
match mk {
|
||||||
|
// MaterialKind::Lambertian(l) => l.albedo,
|
||||||
|
MaterialKind::Lambertian(l) => self.scatter_color_sync(ray, hc, l, depth, world),
|
||||||
|
MaterialKind::Metal(m) => self.scatter_color_sync(ray, hc, m, depth, world),
|
||||||
|
MaterialKind::Dielectric(d) => self.scatter_color_sync(ray, hc, d, depth, world)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
if hit_record.t >= 0.0 {
|
||||||
|
let diffuse_vec = Vec3::random_unit();
|
||||||
|
let lambertian_vec = diffuse_vec + hit_record.normal;
|
||||||
|
0.5 * self.ray_color_sync(&Ray::new(ray.point, lambertian_vec), depth - 1, world)
|
||||||
|
} else {
|
||||||
|
Color::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return hit_c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// v / |v|
|
||||||
|
let unit_direction = ray.direction / ray.direction.length();
|
||||||
|
let a = 0.5 * (unit_direction.y + 1.0);
|
||||||
|
// return background color.
|
||||||
|
(1.0 - a) * Color::new(1.0, 1.0, 1.0) + a * Color::new(0.5, 0.7, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
// -> [x, y, 0]
|
// -> [x, y, 0]
|
||||||
fn random_square(&self, rng: &mut ThreadRng) -> Vec3 {
|
fn random_square(&self, rng: &mut ThreadRng) -> Vec3 {
|
||||||
Vec3::new(rng.gen_range(-0.5..0.1), rng.gen_range(-0.5..0.1), 0.0)
|
Vec3::new(rng.random_range(-0.5..0.1), rng.random_range(-0.5..0.1), 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -231,6 +303,33 @@ impl<'a> Camera<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn scatter_color_sync(
|
||||||
|
&self,
|
||||||
|
ray: &Ray,
|
||||||
|
hc: &mut HitRecord,
|
||||||
|
material: &impl Material,
|
||||||
|
depth: i8,
|
||||||
|
world: RwLock<&HittableList>,
|
||||||
|
) -> Color {
|
||||||
|
let mut scatted = Ray::new(Point::new(0.0, 0.0, 0.0), Vec3::random());
|
||||||
|
let mut attenuation = Color::new(1.0, 1.0, 1.0);
|
||||||
|
if material.scatter(ray, hc, &mut attenuation, &mut scatted) {
|
||||||
|
let r_c = self.ray_color_sync(&scatted, depth - 1, world);
|
||||||
|
let sc_color = Vec3::new(
|
||||||
|
attenuation.x * r_c.x,
|
||||||
|
attenuation.y * r_c.y,
|
||||||
|
attenuation.z * r_c.z,
|
||||||
|
);
|
||||||
|
let color = Color::new(sc_color.x, sc_color.y, sc_color.z);
|
||||||
|
// if near_zero(color) {
|
||||||
|
// println!("near zero: {:?}", scatted.direction);
|
||||||
|
// }
|
||||||
|
color
|
||||||
|
} else {
|
||||||
|
Color::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ use crate::material::MaterialKind;
|
|||||||
use crate::math_utils::{front_face_normal, is_front_face};
|
use crate::math_utils::{front_face_normal, is_front_face};
|
||||||
use crate::types_defined::{HitRecord, Ray, Vec3};
|
use crate::types_defined::{HitRecord, Ray, Vec3};
|
||||||
|
|
||||||
pub trait Hittable {
|
pub trait Hittable: Send + Sync {
|
||||||
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool;
|
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-2
@@ -6,6 +6,7 @@ use rand::{rng, Rng};
|
|||||||
use crate::hittable::HittableList;
|
use crate::hittable::HittableList;
|
||||||
use crate::material::{Dielectric, Lambertian, MaterialKind, Metal};
|
use crate::material::{Dielectric, Lambertian, MaterialKind, Metal};
|
||||||
use crate::ppm_writer::PPMWriter;
|
use crate::ppm_writer::PPMWriter;
|
||||||
|
use crate::triangle::Triangle;
|
||||||
use crate::types_defined::{Camera, Color, Point, Sphere, Vec3};
|
use crate::types_defined::{Camera, Color, Point, Sphere, Vec3};
|
||||||
mod camera;
|
mod camera;
|
||||||
mod color;
|
mod color;
|
||||||
@@ -17,6 +18,7 @@ mod vec3;
|
|||||||
mod math_utils;
|
mod math_utils;
|
||||||
mod ppm_writer;
|
mod ppm_writer;
|
||||||
mod material;
|
mod material;
|
||||||
|
mod triangle;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
camera_render();
|
camera_render();
|
||||||
@@ -28,8 +30,8 @@ fn camera_render() {
|
|||||||
let scale = 1;
|
let scale = 1;
|
||||||
let width: i32 = 1920/scale;
|
let width: i32 = 1920/scale;
|
||||||
let height: i32 = 1080/scale;
|
let height: i32 = 1080/scale;
|
||||||
let sample_times = 50;
|
let sample_times = 20;
|
||||||
let reflect_depth = 64;
|
let reflect_depth = 20;
|
||||||
|
|
||||||
|
|
||||||
let pw_r = PPMWriter::new(
|
let pw_r = PPMWriter::new(
|
||||||
@@ -77,6 +79,19 @@ fn camera_render() {
|
|||||||
// world.put(Box::new(Sphere::new(Point::new(1.0, 0.0, -1.0), 0.5, right_m)));
|
// world.put(Box::new(Sphere::new(Point::new(1.0, 0.0, -1.0), 0.5, right_m)));
|
||||||
world.put(Box::new(Sphere::new(Point::new(0.0, -500.5, -1.0), 500.0, plane_m)));
|
world.put(Box::new(Sphere::new(Point::new(0.0, -500.5, -1.0), 500.0, plane_m)));
|
||||||
|
|
||||||
|
// world.put(Box::new(Triangle::new(
|
||||||
|
// Point::new(0.0, 0.5, 0.0),
|
||||||
|
// Point::new(5.5, 0.5, 0.0),
|
||||||
|
// Point::new(5.5, 5.5, 0.0),
|
||||||
|
// Some(MaterialKind::Metal(Metal { albedo: Color::new(0.8, 0.6, 0.2), fuzz: 0.00001 }))
|
||||||
|
// )));
|
||||||
|
world.put(Box::new(Triangle::new(
|
||||||
|
Point::new(0.0, 0.5, 5.0),
|
||||||
|
Point::new(0.0, 5.5, -5.0),
|
||||||
|
Point::new(5.5, 0.5, 0.0),
|
||||||
|
Some(MaterialKind::Dielectric(Dielectric { albedo: Color::new(1.0, 1.0, 1.0), refraction_index: 1./1.5 }))
|
||||||
|
)));
|
||||||
|
|
||||||
gen_spheres(&mut world);
|
gen_spheres(&mut world);
|
||||||
|
|
||||||
camera.render(&world);
|
camera.render(&world);
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ impl Material for Lambertian {
|
|||||||
|
|
||||||
let mut scatter_direction = r_in.direction + Vec3::random_unit_on_hemisphere(hit_record.normal);
|
let mut scatter_direction = r_in.direction + Vec3::random_unit_on_hemisphere(hit_record.normal);
|
||||||
|
|
||||||
if (near_zero(scatter_direction)) {
|
if near_zero(scatter_direction) {
|
||||||
scatter_direction = hit_record.normal;
|
scatter_direction = hit_record.normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ pub fn random_in_unit_disk() -> Vec3 {
|
|||||||
let rng = &mut rng();
|
let rng = &mut rng();
|
||||||
loop {
|
loop {
|
||||||
let p = Vec3::new(rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0);
|
let p = Vec3::new(rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0);
|
||||||
if (p.length_squared() < 1.0) {
|
if p.length_squared() < 1.0 {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
use crate::{hittable::Hittable, material::MaterialKind, math_utils::is_front_face, types_defined::{HitRecord, Point, Ray, Vec3}};
|
||||||
|
|
||||||
|
pub struct Triangle {
|
||||||
|
pub points: Vec<Vec3>,
|
||||||
|
pub material: Option<MaterialKind>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Triangle {
|
||||||
|
pub fn new(p1: Point, p2: Point, p3: Point, material: Option<MaterialKind>,) -> Self {
|
||||||
|
Triangle {
|
||||||
|
points: vec![
|
||||||
|
p1, p2, p3
|
||||||
|
],
|
||||||
|
material: material
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hittable for Triangle {
|
||||||
|
|
||||||
|
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
|
||||||
|
let p0 = self.points[0];
|
||||||
|
let p1 = self.points[1];
|
||||||
|
let p2 = self.points[2];
|
||||||
|
let u = Vec3 {
|
||||||
|
x: p1.x - p0.x,
|
||||||
|
y: p1.y - p0.y,
|
||||||
|
z: p1.z - p0.z,
|
||||||
|
};
|
||||||
|
let v = Vec3 {
|
||||||
|
x: p2.x - p0.x,
|
||||||
|
y: p2.y - p0.y,
|
||||||
|
z: p2.z - p0.z,
|
||||||
|
};
|
||||||
|
let normal = Vec3 {
|
||||||
|
x: u.y * v.z - u.z * v.y,
|
||||||
|
y: u.z * v.x - u.x * v.z,
|
||||||
|
z: u.x * v.y - u.y * v.x,
|
||||||
|
};
|
||||||
|
let denominator = r.direction.x * normal.x + r.direction.y * normal.y + r.direction.z * normal.z;
|
||||||
|
if denominator.abs() < std::f32::EPSILON {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let w0 = Vec3 {
|
||||||
|
x: r.point.x - p0.x,
|
||||||
|
y: r.point.y - p0.y,
|
||||||
|
z: r.point.z - p0.z,
|
||||||
|
};
|
||||||
|
let t = -(w0.x * normal.x + w0.y * normal.y + w0.z * normal.z) / denominator;
|
||||||
|
if t < t_min || t > t_max {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let P = Vec3 {
|
||||||
|
x: r.point.x + t * r.direction.x,
|
||||||
|
y: r.point.y + t * r.direction.y,
|
||||||
|
z: r.point.z + t * r.direction.z,
|
||||||
|
};
|
||||||
|
let w = Vec3 {
|
||||||
|
x: P.x - p0.x,
|
||||||
|
y: P.y - p0.y,
|
||||||
|
z: P.z - p0.z,
|
||||||
|
};
|
||||||
|
let uv_cross = Vec3 {
|
||||||
|
x: u.y * v.z - u.z * v.y,
|
||||||
|
y: u.z * v.x - u.x * v.z,
|
||||||
|
z: u.x * v.y - u.y * v.x,
|
||||||
|
};
|
||||||
|
let wv_cross = Vec3 {
|
||||||
|
x: w.y * v.z - w.z * v.y,
|
||||||
|
y: w.z * v.x - w.x * v.z,
|
||||||
|
z: w.x * v.y - w.y * v.x,
|
||||||
|
};
|
||||||
|
let uw_cross = Vec3 {
|
||||||
|
x: u.y * w.z - u.z * w.y,
|
||||||
|
y: u.z * w.x - u.x * w.z,
|
||||||
|
z: u.x * w.y - u.y * w.x,
|
||||||
|
};
|
||||||
|
let alpha = (wv_cross.x * normal.x + wv_cross.y * normal.y + wv_cross.z * normal.z) / (uv_cross.x * normal.x + uv_cross.y * normal.y + uv_cross.z * normal.z);
|
||||||
|
let beta = (uw_cross.x * normal.x + uw_cross.y * normal.y + uw_cross.z * normal.z) / (uv_cross.x * normal.x + uv_cross.y * normal.y + uv_cross.z * normal.z);
|
||||||
|
if alpha >= 0.0 && beta >= 0.0 && alpha + beta <= 1.0 {
|
||||||
|
|
||||||
|
hit_record.p = P;
|
||||||
|
hit_record.t = t;
|
||||||
|
hit_record.normal = normal;
|
||||||
|
hit_record.front_face = is_front_face(r, normal);
|
||||||
|
hit_record.material = if let Some(ref m) = self.material {
|
||||||
|
match m {
|
||||||
|
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
|
||||||
|
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
|
||||||
|
MaterialKind::Dielectric(d) => Some(MaterialKind::Dielectric(d.clone())),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::{fs::File, io::{BufWriter}};
|
use std::{fs::File, io::{BufWriter}};
|
||||||
use crate::{material::MaterialKind, vec3};
|
use crate::{material::MaterialKind};
|
||||||
use crate::ppm_writer::PPMWriter;
|
use crate::ppm_writer::PPMWriter;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -38,8 +38,8 @@ pub struct Camera<'a> {
|
|||||||
// pub viewport_width: f32,
|
// pub viewport_width: f32,
|
||||||
// pub viewport_height: f32,
|
// pub viewport_height: f32,
|
||||||
pub center: Point,
|
pub center: Point,
|
||||||
pub look_at: Point,
|
// pub look_at: Point,
|
||||||
pub v_up: Vec3,
|
// pub v_up: Vec3,
|
||||||
// pub focal_length: Vec3,
|
// pub focal_length: Vec3,
|
||||||
// pub viewport_u: Vec3,
|
// pub viewport_u: Vec3,
|
||||||
// pub viewport_v: Vec3,
|
// pub viewport_v: Vec3,
|
||||||
|
|||||||
+20
-6
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
|
use std::{fmt::Display, iter::Sum, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{rng, Rng};
|
||||||
use crate::types_defined::Vec3;
|
use crate::types_defined::Vec3;
|
||||||
|
|
||||||
impl Display for Vec3 {
|
impl Display for Vec3 {
|
||||||
@@ -50,13 +50,13 @@ impl Vec3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn random_range(min: f32, max: f32) -> Self {
|
pub fn random_range(min: f32, max: f32) -> Self {
|
||||||
let rng = &mut thread_rng();
|
let rng = &mut rng();
|
||||||
Vec3::new(rng.gen_range(min..max), rng.gen_range(min..max), rng.gen_range(min..max))
|
Vec3::new(rng.random_range(min..max), rng.random_range(min..max), rng.random_range(min..max))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random() -> Self {
|
pub fn random() -> Self {
|
||||||
let rng = &mut thread_rng();
|
let rng = &mut rng();
|
||||||
Vec3::new(rng.r#gen(), rng.r#gen(), rng.r#gen())
|
Vec3::new(rng.random(), rng.random(), rng.random())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random_unit() -> Self {
|
pub fn random_unit() -> Self {
|
||||||
@@ -159,3 +159,17 @@ impl MulAssign<f32> for Vec3 {
|
|||||||
*self = *self * scalar;
|
*self = *self * scalar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Sum for Vec3 {
|
||||||
|
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
|
||||||
|
let mut sum_x = 0.0;
|
||||||
|
let mut sum_y = 0.0;
|
||||||
|
let mut sum_z = 0.0;
|
||||||
|
for vec in iter {
|
||||||
|
sum_x += vec.x;
|
||||||
|
sum_y += vec.y;
|
||||||
|
sum_z += vec.z;
|
||||||
|
}
|
||||||
|
Vec3 { x: sum_x, y: sum_y, z: sum_z }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user