Skip to content

Commit

Permalink
Material, Medium and State structures
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Nov 20, 2022
1 parent ba781d2 commit 6e1b885
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
13 changes: 8 additions & 5 deletions renderer/src/analytical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ impl Scene for AnalyticalScene {
}
}

fn hit(&self, ray: &Ray) -> bool {
fn hit(&self, ray: &Ray) -> Option<State> {

if let Some(dist) = self.sphere(ray, PTF3::new(0.0, 0.0, 0.0), 1.0) {
return true;

let mut state = State::new();
state.hit_dist = dist;

return Some(state);
}

false
None
}

}

// Analytical Intersections
// Based on https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection

impl AnalyticalIntersections for AnalyticalScene {

// Based on https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
fn sphere(&self, ray: &Ray, center: PTF3, radius: PTF) -> Option<PTF> {
let l = center - ray[0];
let tca = l.dot(&ray[1]);
Expand Down
45 changes: 45 additions & 0 deletions rust-pathtracer/src/globals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::prelude::*;

// State

#[derive(PartialEq, Clone, Debug)]
pub struct State {

pub depth : u16,
pub eta : PTF,

pub hit_dist : PTF,

pub fhp : PTF3,
pub normal : PTF3,
pub ffnormal : PTF3,
pub tangent : PTF3,
pub bitangent : PTF3,

pub is_emitter : bool,

pub mat : Material,
pub medium : Medium,
}

impl State {
pub fn new() -> Self {
Self {
depth : 0,
eta : 0.0,

hit_dist : -1.0,

fhp : PTF3::new(0.0, 0.0, 0.0),
normal : PTF3::new(0.0, 0.0, 0.0),
ffnormal : PTF3::new(0.0, 0.0, 0.0),
tangent : PTF3::new(0.0, 0.0, 0.0),
bitangent : PTF3::new(0.0, 0.0, 0.0),

is_emitter : false,

mat : Material::new(PTF3::new(1.0, 1.0, 1.0)),
medium : Medium::new(),
}
}
}
5 changes: 5 additions & 0 deletions rust-pathtracer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub type Ray = [PTF3; 2];
pub mod tracer;
pub mod camera;
pub mod scene;
pub mod material;
pub mod globals;

pub mod prelude {
pub use crate::PTF;
Expand All @@ -30,6 +32,9 @@ pub mod prelude {
pub use crate::camera::Camera3D;
pub use crate::camera::pinhole::Pinhole;

pub use crate::material::*;
pub use crate::globals::*;

pub use crate::scene::Scene;
pub use crate::tracer::Tracer;
}
98 changes: 98 additions & 0 deletions rust-pathtracer/src/material.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use crate::prelude::*;

// Medium

#[derive(PartialEq, Clone, Debug)]
pub enum MediumType {
None,
Absorb,
Scatter,
Emissive
}

#[derive(PartialEq, Clone, Debug)]
pub struct Medium {
pub medium_type : MediumType,
pub density : PTF,
pub color : PTF3,
pub anisotropy : PTF,
}

impl Medium {

pub fn new() -> Self {
Self {
medium_type : MediumType::None,
density : 0.0,
color : PTF3::new(0.0, 0.0, 0.0),
anisotropy : 0.0,
}
}
}

// Material

#[derive(PartialEq, Clone, Debug)]
pub enum AlphaMode
{
Opaque,
Blend,
Mask
}

#[derive(PartialEq, Clone, Debug)]
pub struct Material {
pub base_color : PTF3,
pub anisotropic : PTF,
pub emission : PTF3,

pub metallic : PTF,
pub roughness : PTF,
pub subsurface : PTF,
pub specular_tint : PTF,

pub sheen : PTF,
pub sheen_tint : PTF,
pub clearcoat : PTF,
pub clearcoat_gloss : PTF,

pub spec_trans : PTF,
pub ior : PTF,

pub opacity : PTF,
pub alpha_mode : AlphaMode,
pub alpha_cutoff : PTF,

pub medium : Medium
}

impl Material {

pub fn new(base_color: PTF3) -> Self {
Self {
base_color,
emission : PTF3::new(0.0, 0.0, 0.0),

anisotropic : 0.0,
metallic : 0.0,
roughness : 0.5,
subsurface : 0.0,
specular_tint : 0.0,

sheen : 0.0,
sheen_tint : 0.0,

clearcoat : 0.0,
clearcoat_gloss : 0.0,
spec_trans : 0.0,
ior : 1.5,

opacity : 1.0,
alpha_mode : AlphaMode::Opaque,
alpha_cutoff : 0.0,

medium : Medium::new()
}
}

}
2 changes: 1 addition & 1 deletion rust-pathtracer/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub trait Scene : Sync + Send {

fn new() -> Self where Self: Sized;

fn hit(&self, ray: &Ray) -> bool;
fn hit(&self, ray: &Ray) -> Option<State>;
}
2 changes: 1 addition & 1 deletion rust-pathtracer/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Tracer {

let mut color = [0.0, 0.0, 0.0, 0.0];

if self.scene.hit(&ray) {
if self.scene.hit(&ray).is_some() {
color[0] = 1.0;
color[3] = 1.0;
}
Expand Down

0 comments on commit 6e1b885

Please sign in to comment.