added metals and dielectrics
This commit is contained in:
26
src/vec3.rs
26
src/vec3.rs
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user