Change Scene trait to account for material data

This commit is contained in:
hal8174 2025-08-22 21:48:07 +02:00
parent 76448ed442
commit 2bc5ec93fe
Signed by: hal8174
SSH key fingerprint: SHA256:NN98ZYwnrreQLSOV/g+amY7C3yL/mS1heD7bi5t6PPw
19 changed files with 306 additions and 124 deletions

View file

@ -73,12 +73,17 @@ impl<A, R: Rng> AccelerationStructureScene<A, R> {
}
impl<A: AccelerationStructure<u32>, R: Rng> Scene<R> for AccelerationStructureScene<A, R> {
type Mat<'a>
= &'a dyn Material<R>
where
A: 'a,
R: 'a;
fn intersect(
&self,
ray: Ray,
min: Float,
max: Float,
) -> Option<ray_tracing_core::scene::Intersection<'_, R>> {
) -> Option<ray_tracing_core::scene::Intersection<'_, R, Self::Mat<'_>>> {
let (t, n, i) = self.acceleration_structure.intersect(ray, min, max)?;
let material = &self.materials[i as usize];
@ -98,12 +103,15 @@ impl<A: AccelerationStructure<u32>, R: Rng> Scene<R> for AccelerationStructureSc
))
}
fn sample_light(
fn sample_light<'b>(
&self,
_w_in: Dir3,
_intersection: &ray_tracing_core::scene::Intersection<'_, R>,
_intersection: &ray_tracing_core::scene::Intersection<'_, R, Self::Mat<'b>>,
rng: &mut R,
) -> Option<ray_tracing_core::scene::LightSample<'_, R>> {
) -> Option<ray_tracing_core::scene::LightSample<'_, R>>
where
Self: 'b,
{
let t = self.lights.choose(rng);
if let Some(&(t, v)) = t {

View file

@ -26,9 +26,24 @@ impl Default for BasicScene<DefaultMaterial> {
}
}
impl<R: Rng, M: Material<R>> Scene<R> for BasicScene<M> {
fn intersect(&self, ray: Ray, min: Float, max: Float) -> Option<Intersection<'_, R>> {
let mut intersection: Option<Intersection<'_, R>> = None;
impl<R, M> Scene<R> for BasicScene<M>
where
R: Rng,
M: Material<R>,
{
type Mat<'a>
= &'a dyn Material<R>
where
M: 'a,
R: 'a;
fn intersect(
&self,
ray: Ray,
min: Float,
max: Float,
) -> Option<Intersection<'_, R, Self::Mat<'_>>> {
let mut intersection: Option<Intersection<'_, R, Self::Mat<'_>>> = None;
for &(c, r) in &self.spheres {
let offset = ray.start() - c;
@ -41,7 +56,7 @@ impl<R: Rng, M: Material<R>> Scene<R> for BasicScene<M> {
let int = Intersection::new(
d,
((ray.start() + d * ray.dir()) - c).normalize(),
Some(&self.material),
Some(&self.material as &dyn Material<R>),
None,
0.0,
);
@ -66,12 +81,15 @@ impl<R: Rng, M: Material<R>> Scene<R> for BasicScene<M> {
)))
}
fn sample_light(
fn sample_light<'b>(
&self,
_w_in: Dir3,
_intersection: &Intersection<'_, R>,
_intersection: &Intersection<'_, R, Self::Mat<'b>>,
_rng: &mut R,
) -> Option<ray_tracing_core::scene::LightSample<'_, R>> {
) -> Option<ray_tracing_core::scene::LightSample<'_, R>>
where
Self: 'b,
{
None
}
}

View file

@ -1,6 +1,9 @@
use std::cell::OnceCell;
use crate::triangle_bvh::{BVHMaterial, Triangle, TriangleBVH};
use crate::{
examples::ExampleSceneEnum,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
};
use super::ExampleScene;
@ -26,7 +29,7 @@ impl<R: Rng> Default for BasicCornell<R> {
}
impl<R: Rng + 'static> ExampleScene<R> for BasicCornell<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(|| {
let side_length = 1.5;
let light_size = 0.5;
@ -95,7 +98,7 @@ impl<R: Rng + 'static> ExampleScene<R> for BasicCornell<R> {
)
});
Box::new(s.clone()) as Box<dyn Scene<R> + Sync>
ExampleSceneEnum::TriangleBVH(s.clone())
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -4,6 +4,7 @@ use ray_tracing_material::{diffuse::DiffuseMaterial, mirror::Mirror};
use std::cell::OnceCell;
use crate::{
examples::ExampleSceneEnum,
parse_obj::ObjData,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
};
@ -29,7 +30,7 @@ impl<R: Rng> Default for Cornell2<R> {
}
impl<R: Rng + 'static> ExampleScene<R> for Cornell2<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(|| {
let obj = ObjData::new("ray-tracing-scene/obj/cornell_box.obj").unwrap();
@ -61,7 +62,7 @@ impl<R: Rng + 'static> ExampleScene<R> for Cornell2<R> {
TriangleBVH::new(obj.vertices, triangles, materials)
});
Box::new(s.clone())
ExampleSceneEnum::TriangleBVH(s.clone())
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -1,5 +1,8 @@
use super::ExampleScene;
use crate::triangle_bvh::{BVHMaterial, Triangle, TriangleBVH};
use crate::{
examples::ExampleSceneEnum,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
};
use ray_tracing_core::{light::AreaLight, prelude::*, scene::Scene};
use ray_tracing_material::{
microfacet::{BeckmannDistribution, Microfacet},
@ -70,7 +73,7 @@ impl<R: Rng> Default for MISTest<R> {
}
impl<R: Rng + 'static> ExampleScene<R> for MISTest<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(move || {
let mut vertices = Vec::new();
let mut triangles = Vec::new();
@ -155,7 +158,7 @@ impl<R: Rng + 'static> ExampleScene<R> for MISTest<R> {
TriangleBVH::new(vertices, triangles, materials)
});
Box::new(s.clone()) as Box<dyn Scene<R> + Sync>
ExampleSceneEnum::TriangleBVH(s.clone())
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -5,8 +5,17 @@ use ray_tracing_material::mirror::Mirror;
use std::collections::HashMap;
use std::fmt::Debug;
pub trait ExampleScene<R: Rng + 'static> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync>;
use crate::acceleration_structure::triangle_bvh::TriangleBVH;
use crate::acceleration_structure::AccelerationStructureScene;
use crate::basic_scene::BasicScene;
pub enum ExampleSceneEnum<R: Rng> {
AccelerationStructureScene(AccelerationStructureScene<TriangleBVH<u32>, R>),
TriangleBVH(crate::triangle_bvh::TriangleBVH<R>),
}
pub trait ExampleScene<R: Rng> {
fn get_scene(&self) -> ExampleSceneEnum<R>;
fn get_camera_pos(&self) -> Pos3;
@ -50,8 +59,8 @@ pub fn example_scenes<R: Rng + Debug + 'static>() -> HashMap<&'static str, Box<d
map.insert("mis_test", Box::new(mis_test::MISTest::new()));
// let material = Iridescent::new(250.0, 250.0, 1.0, 1.3, 20);
let material = Iridescent::new(0.9 * 988.0, 0.1 * 988.0, 1.0, 1.5, 20);
map.insert("sphere", Box::new(sphere::SphereScene::new(material)));
// let material = Iridescent::new(0.9 * 988.0, 0.1 * 988.0, 1.0, 1.5, 20);
// map.insert("sphere", Box::new(sphere::SphereScene::new(material)));
map.insert(
"presentation",

View file

@ -1,4 +1,5 @@
use crate::{
examples::ExampleSceneEnum,
parse_obj::ObjData,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
};
@ -7,7 +8,7 @@ use ray_tracing_core::{
color::Color,
light::AreaLight,
math::{Dir3, Pos3},
prelude::Float,
prelude::{Float, Material},
};
use ray_tracing_material::{iridescent::Iridescent, oren_nayar::OrenNayar};
use std::cell::OnceCell;
@ -28,7 +29,7 @@ impl<R: Rng> Presentation<R> {
}
impl<R: Rng + 'static> ExampleScene<R> for Presentation<R> {
fn get_scene(&self) -> Box<dyn ray_tracing_core::scene::Scene<R> + Sync> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(|| {
let obj = ObjData::new("ray-tracing-scene/obj/stanford_dragon.obj").unwrap();
@ -129,7 +130,7 @@ impl<R: Rng + 'static> ExampleScene<R> for Presentation<R> {
TriangleBVH::new(vertices, triangles, materials)
});
Box::new(s.clone())
ExampleSceneEnum::TriangleBVH(s.clone())
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -1,5 +1,5 @@
use super::ExampleScene;
use crate::basic_scene::BasicScene;
use crate::{basic_scene::BasicScene, examples::ExampleSceneEnum};
use ray_tracing_core::prelude::*;
use std::cell::{Cell, OnceCell};
@ -17,28 +17,33 @@ impl<M> SphereScene<M> {
}
}
impl<R: Rng + 'static, M: Material<R> + Clone + 'static> ExampleScene<R> for SphereScene<M> {
fn get_scene(&self) -> Box<dyn ray_tracing_core::scene::Scene<R> + Sync> {
let s = self
.scene
.get_or_init(|| BasicScene::new(self.material.take().unwrap()));
// impl<R, M> ExampleScene<R> for SphereScene<M>
// where
// R: Rng + 'static,
// M: Material<R> + Clone + 'static,
// M: Send + Sync,
// {
// fn get_scene(&self) -> ExampleSceneEnum<R> {
// let s = self
// .scene
// .get_or_init(|| BasicScene::new(self.material.take().unwrap()));
Box::new(s.clone())
}
// ExampleSceneEnum::BasicScene(s.clone())
// }
fn get_camera_pos(&self) -> Pos3 {
Pos3::new(5.0, 1.0, 0.0)
}
// fn get_camera_pos(&self) -> Pos3 {
// Pos3::new(5.0, 1.0, 0.0)
// }
fn get_camera_look_at(&self) -> Pos3 {
Pos3::new(0.0, 0.0, 0.0)
}
// fn get_camera_look_at(&self) -> Pos3 {
// Pos3::new(0.0, 0.0, 0.0)
// }
fn get_camera_up(&self) -> Dir3 {
Dir3::up()
}
// fn get_camera_up(&self) -> Dir3 {
// Dir3::up()
// }
fn get_horizontal_fov(&self) -> Float {
Float::to_radians(90.0)
}
}
// fn get_horizontal_fov(&self) -> Float {
// Float::to_radians(90.0)
// }
// }

View file

@ -1,4 +1,5 @@
use crate::{
examples::ExampleSceneEnum,
parse_obj::ObjData,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
};
@ -24,7 +25,7 @@ impl<R: Rng, M: Material<R>> StanfordDragon<R, M> {
}
impl<R: Rng + 'static, M: Material<R> + 'static> ExampleScene<R> for StanfordDragon<R, M> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(|| {
let obj = ObjData::new("ray-tracing-scene/obj/stanford_dragon.obj").unwrap();
@ -83,7 +84,7 @@ impl<R: Rng + 'static, M: Material<R> + 'static> ExampleScene<R> for StanfordDra
TriangleBVH::new(vertices, triangles, materials)
});
Box::new(s.clone())
ExampleSceneEnum::TriangleBVH(s.clone())
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -11,6 +11,7 @@ use crate::{
triangle_bvh::{Triangle, TriangleBVH},
ASMaterial, AccelerationStructureScene,
},
examples::ExampleSceneEnum,
parse_obj::ObjData,
};
@ -34,8 +35,8 @@ impl<R: Rng> Default for StanfordDragon<R> {
}
}
impl<R: Rng + 'static> ExampleScene<R> for StanfordDragon<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
impl<R: Rng> ExampleScene<R> for StanfordDragon<R> {
fn get_scene(&self) -> ExampleSceneEnum<R> {
let s = self.scene.get_or_init(|| {
let obj = ObjData::new("ray-tracing-scene/obj/stanford_dragon.obj").unwrap();
@ -111,7 +112,7 @@ impl<R: Rng + 'static> ExampleScene<R> for StanfordDragon<R> {
});
let n: AccelerationStructureScene<_, _> = s.clone();
Box::new(n)
ExampleSceneEnum::AccelerationStructureScene(n)
}
fn get_camera_pos(&self) -> Pos3 {

View file

@ -318,12 +318,17 @@ impl<R: Rng> TriangleBVH<R> {
}
impl<R: Rng> Scene<R> for TriangleBVH<R> {
type Mat<'a>
= &'a dyn Material<R>
where
R: 'a;
fn intersect(
&self,
ray: Ray,
min: Float,
max: Float,
) -> Option<ray_tracing_core::scene::Intersection<'_, R>> {
) -> Option<ray_tracing_core::scene::Intersection<'_, R, Self::Mat<'_>>> {
let (i, t) = self.intersect_bvh(0, ray, min, max)?;
let triangle = self.triangles[i as usize];
@ -342,12 +347,15 @@ impl<R: Rng> Scene<R> for TriangleBVH<R> {
))
}
fn sample_light(
fn sample_light<'b>(
&self,
_w_in: Dir3,
_intersection: &Intersection<'_, R>,
_intersection: &Intersection<'_, R, Self::Mat<'b>>,
rng: &mut R,
) -> Option<ray_tracing_core::scene::LightSample<'_, R>> {
) -> Option<ray_tracing_core::scene::LightSample<'_, R>>
where
Self: 'b,
{
let t = self.lights.choose(rng);
if let Some(t) = t {