Skip to content

Commit

Permalink
get basic bladed apertures working
Browse files Browse the repository at this point in the history
  • Loading branch information
gillett-hernandez committed Jun 23, 2024
1 parent 703944c commit 93b1354
Show file tree
Hide file tree
Showing 30 changed files with 231 additions and 107 deletions.
4 changes: 2 additions & 2 deletions data/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ tile_size = [32, 32]
[[render_settings]]
threads = 20
filename = "beauty"
min_samples = 32
min_samples = 256
min_bounces = 1
max_bounces = 12
max_bounces = 2
hwss = false
camera_id = "main"
russian_roulette = true
Expand Down
2 changes: 0 additions & 2 deletions data/lib_textures.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ curves = ["srgb_r", "srgb_g", "srgb_b", "flat_zero"]
[[low_res_hdri]]
type = "EXR"
filename = "data/hdri/kloofendal_43d_clear_puresky_1k.exr"
alpha_fill = 0.0
curves = ["srgb_r", "srgb_g", "srgb_b", "flat_zero"]

[[kiara_dawn_8k]]
Expand Down Expand Up @@ -65,7 +64,6 @@ curve = "simple_yellow"
[[test_texture]]
type = "Texture4"
filename = "data/textures/test.png"
alpha_fill = 0.0
curves = ["srgb_r", "srgb_g", "srgb_b", "flat_one"]


Expand Down
8 changes: 5 additions & 3 deletions data/scenes/bokeh_test.toml → data/scenes/test_bokeh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,10 @@ origin = [0.5, 20.0, 0.0]
[[cameras]]
type = "SimpleCamera"
name = "main"
look_from = [-5.0, 0.0, 0.5]
look_at = [0.0, 0.0, 0.4]
aperture_size = 0.1
look_from = [0.0, -5.0, 0.5]
look_at = [0.0, 0.0, 0.2]
lens_diameter = 0.1
aperture_diameter = 0.1
aperture = {type = "Bladed", blades = 3, sharpness = 0.5}
focal_distance = 5.0
vfov = 45.0
46 changes: 37 additions & 9 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ extern crate tracing;

use std::fs;
use std::path::PathBuf;
use std::thread::JoinHandle;
use std::{fs::File, sync::Arc};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::prelude::*;

// TODO: switch to clap
use structopt::StructOpt;

#[cfg(all(target_os = "windows", feature = "notification"))]
Expand All @@ -32,18 +34,29 @@ use win32_notification::NotificationBuilder;
struct Opt {
#[structopt(long)]
pub scene: Option<String>,

#[structopt(long, default_value = "data/config.toml")]
pub config: String,

#[structopt(short = "n", long)]
pub dry_run: bool,
#[structopt(short = "pll", long, default_value = "warn")]

#[structopt(long, default_value = "warn")]
pub stdout_log_level: String,
#[structopt(short = "wll", long, default_value = "info")]

#[structopt(long, default_value = "info")]
pub write_log_level: String,
}

fn construct_scene(config: &mut Config) -> anyhow::Result<World> {
construct_world(config, PathBuf::from(config.scene_file.clone()))
fn construct_scene(
config: &mut Config,
mut handles: &mut Vec<JoinHandle<()>>,
) -> anyhow::Result<World> {
construct_world(
config,
PathBuf::from(config.scene_file.clone()),
&mut handles,
)
}

fn construct_renderer(config: &Config) -> Box<dyn Renderer> {
Expand Down Expand Up @@ -128,12 +141,21 @@ fn main() {
toml_config.default_scene_file = opts.scene.unwrap_or(toml_config.default_scene_file);

let mut config = Config::from(toml_config);
let world = construct_scene(&mut config);

let mut handles = Vec::new();
let world = construct_scene(&mut config, &mut handles);
if world.is_err() {
error!(
"fatal error parsing world, aborting. error is {:?}",
world.err().unwrap()
);
let as_error = world.unwrap_err();
// error!(
// "fatal error parsing world, aborting. error is {:?}",
// as_error
// );
for frame in as_error.chain() {
error!("{}", frame);
}
for handle in handles {
let _ = handle.join();
}
return;
}

Expand All @@ -156,6 +178,9 @@ fn main() {
{
if opts.dry_run {
// don't send notification if it's a dry run, since no rendering occurred
for handle in handles {
let _ = handle.join();
}
return;
}
let notification = NotificationBuilder::new()
Expand All @@ -170,4 +195,7 @@ fn main() {
.delete()
.expect("Failed to delete notification");
}
for handle in handles {
let _ = handle.join();
}
}
5 changes: 3 additions & 2 deletions src/bin/raymarch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ fn main() {
// )

let scene_file_path = config.scene_file.clone();
let world = construct_world(&mut config, PathBuf::from(scene_file_path)).unwrap();
let mut handles = Vec::new();
let world = construct_world(&mut config, PathBuf::from(scene_file_path), &mut handles).unwrap();

let camera = world.cameras[0]
.clone()
Expand Down Expand Up @@ -500,7 +501,7 @@ fn main() {
primitives: scene_sdf,

environment: env_map,
materials: world.materials,
materials: (*world.materials).clone(),
material_map,
// max_depth: 20,
// world aabb needs to encompass camera
Expand Down
72 changes: 46 additions & 26 deletions src/camera/projective_camera.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use optics::aperture::ApertureSample;
use optics::ApertureEnum;

use crate::geometry::*;
use crate::prelude::*;

#[derive(Debug, Clone)]
pub struct ProjectiveCamera {
pub origin: Point3,
pub direction: Vec3,
// half_height: f32,
// half_width: f32,
focal_distance: f32,
lower_left_corner: Point3,
vfov: f32,
focal_distance: f32, // in meters
pub lens: Instance,
pub horizontal: Vec3,
pub vertical: Vec3,
aspect_ratio: f32,
u: Vec3,
v: Vec3,
w: Vec3,
// TODO: change this to aperture from rust_optics crate
aperture_radius: f32,
pub aperture_diameter: f32,
pub aperture: ApertureEnum,
lower_left_corner: Point3,
vertical: Vec3,
horizontal: Vec3,
}

impl ProjectiveCamera {
Expand All @@ -27,10 +30,11 @@ impl ProjectiveCamera {
v_up: Vec3,
vertical_fov: f32, // vertical_fov should be given in degrees, since it is converted to radians
focal_distance: f32,
aperture: f32,
lens_diameter: f32,
aperture_diameter: f32,
aperture: ApertureEnum,
) -> ProjectiveCamera {
let direction = (look_at - look_from).normalized();
let lens_radius = aperture / 2.0;
let theta: f32 = vertical_fov.to_radians();
let half_height = (theta / 2.0).tan();
let half_width = 1.0 * half_height;
Expand All @@ -44,10 +48,10 @@ impl ProjectiveCamera {
let w = -direction;
let u = -v_up.cross(w).normalized();
let v = w.cross(u).normalized();
// println!(
// "constructing camera with point, direction, and uvw = {:?} {:?} {:?} {:?} {:?}",
// look_from, direction, u, v, w
// );
info!(
"constructing camera with point, direction, and uvw = {:?} {:?} {:?} {:?} {:?}",
look_from, direction, u, v, w
);

let transform = Transform3::from_stack(
None,
Expand All @@ -56,33 +60,36 @@ impl ProjectiveCamera {
)
.inverse();

let lens_radius = lens_diameter / 2.0;
if lens_radius == 0.0 {
warn!("Warn: lens radius is 0.0");
}

ProjectiveCamera {
origin: look_from,
direction,
// half_height,
// half_width,
focal_distance,
lower_left_corner: look_from
- u * half_width * focal_distance
- v * half_height * focal_distance
- w * focal_distance,
vfov: vertical_fov,
aspect_ratio,
lens: Instance::new(
Aggregate::from(Disk::new(lens_radius, Point3::ORIGIN, true)),
Some(transform),
// use some placeholder ids, to be overwritten later when the world is constructed
Some(MaterialId::Camera(0)),
0,
),
horizontal: u * 2.0 * half_width * focal_distance,
vertical: v * 2.0 * half_height * focal_distance,
u,
v,
w,
aperture_radius: aperture / 2.0,
lower_left_corner: look_from
- u * half_width * focal_distance
- v * half_height * focal_distance
- w * focal_distance,
horizontal: u * 2.0 * half_width * focal_distance,
vertical: v * 2.0 * half_height * focal_distance,

aperture_diameter,
aperture,
focal_distance,
}
}
pub fn get_surface(&self) -> Option<&Instance> {
Expand All @@ -91,9 +98,15 @@ impl ProjectiveCamera {
}

impl Camera<f32, f32> for ProjectiveCamera {
fn get_ray(&self, sampler: &mut Box<dyn Sampler>, _lambda: f32, u: f32, v: f32) -> (Ray, f32) {
// circular aperture/lens
let rd: Vec3 = self.aperture_radius * random_in_unit_disk(sampler.draw_2d());
fn get_ray(&self, sampler: &mut Box<dyn Sampler>, lambda: f32, u: f32, v: f32) -> (Ray, f32) {
let vec: Vec3 = loop {
if let Ok(p) = self.aperture.sample(sampler.draw_2d()) {
break p;
}
}
.into();
let rd = self.aperture_diameter * vec;

let offset = self.u * rd.x() + self.v * rd.y();
let ray_origin: Point3 = self.origin + offset;

Expand Down Expand Up @@ -195,6 +208,7 @@ unsafe impl Sync for ProjectiveCamera {}
mod tests {
use super::*;
use math::prelude::*;
use optics::CircularAperture;

#[test]
fn test_camera() {
Expand All @@ -205,6 +219,8 @@ mod tests {
35.2,
5.0,
0.08,
0.08,
ApertureEnum::CircularAperture(CircularAperture::default()),
)
.with_aspect_ratio(0.6);
let s = debug_random();
Expand All @@ -231,6 +247,8 @@ mod tests {
35.2,
5.0,
0.08,
0.08,
ApertureEnum::CircularAperture(CircularAperture::default()),
)
.with_aspect_ratio(width as f32 / height as f32);
let px = (0.99 * width) as usize;
Expand Down Expand Up @@ -265,6 +283,8 @@ mod tests {
27.0,
5.0,
0.08,
0.08,
ApertureEnum::CircularAperture(CircularAperture::default()),
)
.with_aspect_ratio(0.6);

Expand Down
2 changes: 1 addition & 1 deletion src/camera/realistic_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Camera<f32, f32> for RealisticCamera {
self.lens_zoom,
Input::new(ray, lambda / 1000.0),
1.0,
|e| (self.aperture.intersects(self.aperture_radius, e), false),
|e| (self.aperture.is_rejected(self.aperture_radius, e.origin), false),
drop,
);
if let Some(Output {
Expand Down
2 changes: 1 addition & 1 deletion src/materials/lambertian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{prelude::*, texture::EvalAt};

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Lambertian {
pub texture: TexStack,
}
Expand Down
21 changes: 18 additions & 3 deletions src/materials/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::prelude::*;

use std::marker::{Send, Sync};
use std::{
marker::{Send, Sync},
ops::{Deref, DerefMut},
};

mod diffuse_light;
mod ggx;
Expand Down Expand Up @@ -236,7 +239,7 @@ macro_rules! generate_enum {
}
};
( $name:ident, $( $s:ident),+) => {
#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum $name {
$(
$s($s),
Expand Down Expand Up @@ -290,7 +293,19 @@ generate_enum!(
// PassthroughFilter
// );

pub type MaterialTable = Vec<MaterialEnum>;
#[derive(Debug, Clone)]
pub struct MaterialTable(pub Vec<MaterialEnum>);
impl Deref for MaterialTable {
type Target = Vec<MaterialEnum>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for MaterialTable {
fn deref_mut(&mut self) -> &mut Vec<MaterialEnum> {
&mut self.0
}
}

// #[cfg(test)]
// mod tests {
Expand Down
2 changes: 1 addition & 1 deletion src/materials/passthrough.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct PassthroughFilter {
pub color: Curve,
pub outer_medium_id: MediumId,
Expand Down
2 changes: 1 addition & 1 deletion src/mediums/hg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn phase_hg(cos_theta: f32, g: f32) -> f32 {
);
(1.0 - g * g) / (denom * denom.sqrt() * 2.0 * std::f32::consts::TAU)
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct HenyeyGreensteinHomogeneous {
// domain: visible range
// range: 0..2
Expand Down
Loading

0 comments on commit 93b1354

Please sign in to comment.