added metals and dielectrics

This commit is contained in:
2023-02-02 09:41:11 +01:00
parent 72fb4ce3fd
commit 32cb8b4554
7 changed files with 145 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
use std::ops::{Add, Sub, AddAssign, SubAssign, Mul, Div, MulAssign, DivAssign, Neg};
use std::{ops::{Add, Sub, AddAssign, SubAssign, Mul, Div, MulAssign, DivAssign, Neg}};
use crate::rtweekend::{random_f64, random_range_f64};
@@ -8,7 +8,7 @@ pub struct Vec3 {
}
impl Vec3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Vec3 { elements: [x, y, z] }
}
@@ -126,6 +126,20 @@ impl Mul<f64> for Vec3 {
}
}
impl Mul<Self> for Vec3 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
elements: [
self.x() * rhs.x(),
self.y() * rhs.y(),
self.z() * rhs.z()
]
}
}
}
impl Mul<Vec3> for f64 {
type Output = Vec3;
@@ -225,4 +239,12 @@ pub fn random_in_hemisphere(normal: &Vec3) -> Vec3 {
pub fn reflect(v: &Vec3, n: &Vec3) -> Vec3 {
*v - 2.0 * v.dot(n) * *n
}
pub fn refract(uv: &Vec3, n: &Vec3, etai_over_etat: f64) -> Vec3 {
let cos_theta = f64::min(-uv.dot(&n), 1.0);
let r_out_perp = etai_over_etat * (*uv + cos_theta * *n);
let r_out_parallel = -f64::sqrt(f64::abs(1.0 - r_out_perp.length_squared())) * *n;
r_out_perp + r_out_parallel
}