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

@ -2,6 +2,7 @@ pub mod dir3;
pub mod frame;
pub mod mat3;
pub mod pos3;
pub mod sampling;
pub use dir3::Dir3;
pub use frame::Frame;

View file

@ -31,6 +31,20 @@ impl Pos3 {
z: 0.0,
}
}
pub fn from_barycentric(vertices: [Self; 3], coordinates: [Float; 3]) -> Self {
Self {
x: vertices[0].x() * coordinates[0]
+ vertices[1].x() * coordinates[1]
+ vertices[2].x() * coordinates[2],
y: vertices[0].y() * coordinates[0]
+ vertices[1].y() * coordinates[1]
+ vertices[2].y() * coordinates[2],
z: vertices[0].z() * coordinates[0]
+ vertices[1].z() * coordinates[1]
+ vertices[2].z() * coordinates[2],
}
}
}
impl Sub for Pos3 {

View file

@ -0,0 +1,8 @@
use crate::prelude::*;
/// Sample a triangle
pub fn sample_triangle(uv: [Float; 2]) -> [Float; 3] {
let s = 1.0 - Float::sqrt(1.0 - uv[0]);
let t = (1.0 - s) * uv[1];
[s, t, 1.0 - s - t]
}

View file

@ -2,6 +2,13 @@ use crate::prelude::*;
pub trait Scene<R: Rng> {
fn intersect(&self, ray: Ray, min: Float, max: Float) -> Option<Intersection<'_, R>>;
fn sample_light(
&self,
w_in: Dir3,
intersection: &Intersection<'_, R>,
rng: &mut R,
) -> Option<LightSample<'_, R>>;
}
pub struct Intersection<'sc, R: Rng> {
@ -46,3 +53,41 @@ impl<'sc, R: Rng> Intersection<'sc, R> {
Frame::from_normal(self.normal)
}
}
pub struct LightSample<'sc, R: Rng> {
pos: Pos3,
pdf: Float,
normal: Dir3,
light: &'sc dyn Light<R>,
}
impl<'sc, R: Rng> LightSample<'sc, R> {
pub fn new(pos: Pos3, pdf: Float, normal: Dir3, light: &'sc dyn Light<R>) -> Self {
Self {
pos,
pdf,
normal,
light,
}
}
pub fn pdf(&self) -> Float {
self.pdf
}
pub fn pos(&self) -> Pos3 {
self.pos
}
pub fn light(&self) -> &'sc dyn Light<R> {
self.light
}
pub fn normal(&self) -> Dir3 {
self.normal
}
pub fn tangent_frame(&self) -> Frame {
Frame::from_normal(self.normal)
}
}