add generic reflector

This commit is contained in:
numzero 2025-11-20 13:05:02 +03:00
parent cbe0839cd6
commit 713fd25c9c

View File

@ -1,7 +1,7 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use glam::{Mat4, Vec2, Vec3, vec3}; use glam::{Mat4, Vec2, Vec3, vec3};
use rand_distr::Distribution; use rand_distr::{Distribution, UnitSphere};
use crate::{camera::OrbitalCamera, ray::Ray}; use crate::{camera::OrbitalCamera, ray::Ray};
@ -134,3 +134,27 @@ impl Scene {
}) })
} }
} }
pub trait Reflector {
fn brdf(&self, normal: Vec3, incident: Vec3, reflected: Vec3) -> f32 /* 1/sr */;
fn reflect(&self, rgen: &mut impl rand::Rng, normal: Vec3, incident: Vec3) -> Vec3;
}
pub struct Lambertian;
impl Reflector for Lambertian {
fn brdf(&self, _normal: Vec3, _incident: Vec3, _reflected: Vec3) -> f32 {
1. / PI
}
fn reflect(&self, rgen: &mut impl rand::Rng, normal: Vec3, _incident: Vec3) -> Vec3 {
let sphere: Vec3 = UnitSphere.sample(rgen).into();
let sphere_n = normal.dot(sphere); // uniform on [-1, 1]!
let sphere_t = sphere - sphere_n * normal;
let out_n_len2 = sphere_n.abs();
let out_t = (1. + out_n_len2).recip().sqrt() * sphere_t;
let out_n = out_n_len2.sqrt() * normal;
out_t + out_n
}
}