Add next event estimation

This commit is contained in:
hal8174 2024-10-20 19:35:02 +02:00
parent 534a7d7097
commit 7d69122e8c
13 changed files with 317 additions and 21 deletions

View file

@ -1,4 +1,4 @@
pub mod default;
pub mod diffuse;
pub mod microfacet;
pub mod mirror;

View file

@ -0,0 +1,64 @@
use ray_tracing_core::{material::Material, prelude::*};
use std::fmt::Debug;
#[derive(Debug)]
struct Microfacet<D: MicrofacetDistribution + Debug> {
dist: D,
color: Color,
}
fn fresnel(w_in: Dir3, n: Dir3, nu_rel: Float) -> Float {
todo!()
}
impl<R: Rng, D: MicrofacetDistribution + Debug + Sync> Material<R> for Microfacet<D> {
fn eval(
&self,
w_in: ray_tracing_core::prelude::Dir3,
w_out: ray_tracing_core::prelude::Dir3,
_rng: &mut R,
) -> ray_tracing_core::prelude::Color {
let w_h = (w_in + w_out).normalize();
let g = self.dist.g1(w_in, w_h) * self.dist.g1(w_out, w_h);
self.color * fresnel(w_in, w_h, 1.0) * g * self.dist.d(w_h) / (4.0 * w_in.y() * w_out.y())
}
}
struct BeckmannDistribution {
alpha: Float,
}
impl MicrofacetDistribution for BeckmannDistribution {
fn g1(&self, w: Dir3, w_h: Dir3) -> Float {
if w.y() > 0.0 && Dir3::dot(w, w_h) > 0.0 {
let cos_theta = w.y();
let a = self.alpha * (Float::sqrt(1.0 - cos_theta * cos_theta) / cos_theta);
(3.535 * a + 2.181 * a * a) / (1.0 + 2.276 * a + 2.577 * a * a)
} else {
1.0
}
}
fn d(&self, w_h: Dir3) -> Float {
if w_h.y() > 0.0 {
let cos_theta_squared = w_h.y() * w_h.y();
1.0 / (FloatConsts::PI
* self.alpha
* self.alpha
* cos_theta_squared
* cos_theta_squared)
} else {
0.0
}
}
}
trait MicrofacetDistribution {
fn g1(&self, w: Dir3, w_h: Dir3) -> Float;
fn d(&self, w_h: Dir3) -> Float;
// fn sample_d(&self) -> Dir3;
}