Implement initial area light
This commit is contained in:
parent
95092b1571
commit
333acab099
3 changed files with 102 additions and 22 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{
|
||||
either::Either,
|
||||
scene::PbrtScene,
|
||||
shape::{Shape, ShapeAlpha, ShapeType},
|
||||
tokenizer::{Token, Tokenizer},
|
||||
|
|
@ -8,6 +9,7 @@ use miette::{IntoDiagnostic, Result, bail, miette};
|
|||
use ray_tracing_core::{
|
||||
affine_transform::AffineTransform,
|
||||
color::Color,
|
||||
light::Light,
|
||||
math::{Dir3, Pos3},
|
||||
prelude::{Float, Rng},
|
||||
};
|
||||
|
|
@ -83,6 +85,22 @@ enum Statement<R> {
|
|||
Material(Arc<dyn PbrtMaterial<R>>),
|
||||
MakeNamedMaterial(String, Arc<dyn PbrtMaterial<R>>),
|
||||
NamedMaterial(String),
|
||||
AreaLight(AreaLight),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AreaLight {
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
impl<R: Rng> Light<R> for AreaLight {
|
||||
fn emit(&self, w_in: Dir3, _rng: &mut R) -> Color {
|
||||
if w_in.y() > 0.0 {
|
||||
self.color
|
||||
} else {
|
||||
Color::black()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_look_at<R>(iter: &mut Tokenizer) -> Result<Statement<R>> {
|
||||
|
|
@ -417,7 +435,9 @@ impl Lexer {
|
|||
material::parse_make_named_material(&mut self.input, context)
|
||||
.map(|(name, material)| Statement::MakeNamedMaterial(name, material)),
|
||||
),
|
||||
"NamedMaterial" => Some(self.input.parse_parameter().map(Statement::NamedMaterial)),
|
||||
"NamedMaterial" => {
|
||||
Some(self.input.next_string_value().map(Statement::NamedMaterial))
|
||||
}
|
||||
"ConcatTransform" => {
|
||||
Some(parse_transform(&mut self.input).map(Statement::ConcatTransform))
|
||||
}
|
||||
|
|
@ -429,6 +449,8 @@ impl Lexer {
|
|||
Ok(s) => Ok(Statement::CoordSysTransform(s)),
|
||||
Err(e) => Err(e),
|
||||
}),
|
||||
|
||||
"AreaLightSource" => Some(parse_area_light(&mut self.input)),
|
||||
"WorldBegin" => Some(Ok(Statement::WorldBegin)),
|
||||
_ => {
|
||||
if s.chars().any(|c| !c.is_ascii_alphabetic()) {
|
||||
|
|
@ -459,6 +481,22 @@ impl Lexer {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_area_light<R>(input: &mut Tokenizer) -> Result<Statement<R>> {
|
||||
let s = input.next_string_value()?;
|
||||
|
||||
if s.as_str() != "diffuse" {
|
||||
return Err(miette!(
|
||||
labels = vec![input.last_span_labeled(Some("here"))],
|
||||
"Only diffuse area light supported."
|
||||
)
|
||||
.with_source_code(input.get_src()));
|
||||
}
|
||||
|
||||
Ok(Statement::AreaLight(parse_dict2!(input, AreaLight;
|
||||
color, Color::white(), ["rgb L", texture::parse_rgb(input)?]
|
||||
)))
|
||||
}
|
||||
|
||||
fn parse_transform(input: &mut Tokenizer) -> Result<AffineTransform> {
|
||||
input.next_expect_bracket_open()?;
|
||||
let mut v = [0.0; 16];
|
||||
|
|
@ -615,6 +653,7 @@ pub struct PbrtContext<R> {
|
|||
ctm: Vec<AffineTransform>,
|
||||
textures: HashMap<String, Arc<dyn PbrtTexture>>,
|
||||
material: Vec<Arc<dyn PbrtMaterial<R>>>,
|
||||
area_light: Vec<AreaLight>,
|
||||
materials: HashMap<String, Arc<dyn PbrtMaterial<R>>>,
|
||||
}
|
||||
|
||||
|
|
@ -623,6 +662,7 @@ impl<R> PbrtContext<R> {
|
|||
Self {
|
||||
ctm: vec![AffineTransform::identity()],
|
||||
textures: HashMap::new(),
|
||||
area_light: Vec::new(),
|
||||
material: Vec::new(),
|
||||
materials: HashMap::new(),
|
||||
}
|
||||
|
|
@ -651,6 +691,11 @@ impl<R> PbrtContext<R> {
|
|||
self.material
|
||||
.push(Arc::clone(self.material.last().unwrap()));
|
||||
}
|
||||
|
||||
if !self.area_light.is_empty() {
|
||||
self.area_light
|
||||
.push(self.area_light.last().unwrap().clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> Result<()> {
|
||||
|
|
@ -661,6 +706,7 @@ impl<R> PbrtContext<R> {
|
|||
}
|
||||
|
||||
self.material.pop();
|
||||
self.area_light.pop();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -747,17 +793,26 @@ fn inner_parse_pbrt<R: Rng + std::fmt::Debug>(
|
|||
}
|
||||
Statement::Shape(shape_type, shape_alpha) => {
|
||||
dbg!(&context);
|
||||
pbrt.scene.shapes.push(Shape {
|
||||
ctm: context.get_ctm(),
|
||||
material: Arc::clone(
|
||||
context
|
||||
.material
|
||||
.last()
|
||||
.ok_or_else(|| miette!("No material specified"))?,
|
||||
),
|
||||
obj: shape_type,
|
||||
alpha: shape_alpha,
|
||||
});
|
||||
if context.area_light.is_empty() {
|
||||
pbrt.scene.shapes.push(Shape {
|
||||
ctm: context.get_ctm(),
|
||||
material: Either::A(Arc::clone(
|
||||
context
|
||||
.material
|
||||
.last()
|
||||
.ok_or_else(|| miette!("No material specified"))?,
|
||||
)),
|
||||
obj: shape_type,
|
||||
alpha: shape_alpha,
|
||||
});
|
||||
} else {
|
||||
pbrt.scene.shapes.push(Shape {
|
||||
ctm: context.get_ctm(),
|
||||
material: Either::B(context.area_light.last().unwrap().clone()),
|
||||
obj: shape_type,
|
||||
alpha: shape_alpha,
|
||||
});
|
||||
}
|
||||
}
|
||||
Statement::CoordinateSystem(s) => {
|
||||
named_transforms.insert(s, context.get_ctm());
|
||||
|
|
@ -774,6 +829,13 @@ fn inner_parse_pbrt<R: Rng + std::fmt::Debug>(
|
|||
*context.material.last_mut().unwrap() = m;
|
||||
}
|
||||
}
|
||||
Statement::AreaLight(l) => {
|
||||
if context.area_light.is_empty() {
|
||||
context.area_light.push(l);
|
||||
} else {
|
||||
*context.area_light.last_mut().unwrap() = l;
|
||||
}
|
||||
}
|
||||
Statement::MakeNamedMaterial(n, m) => {
|
||||
context.materials.insert(n, m);
|
||||
}
|
||||
|
|
@ -801,7 +863,7 @@ fn inner_parse_pbrt<R: Rng + std::fmt::Debug>(
|
|||
|
||||
// dbg!(context);
|
||||
|
||||
Ok(pbrt)
|
||||
Ok(dbg!(pbrt))
|
||||
}
|
||||
|
||||
pub fn parse_pbrt_v4<R: Rng + std::fmt::Debug>(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue