rust_pathtracer/
globals.rs

1use crate::prelude::*;
2
3// State
4
5#[derive(Clone, Debug)]
6pub struct State {
7    pub depth               : u16,
8    pub eta                 : F,
9
10    pub hit_dist            : F,
11
12    pub fhp                 : F3,
13    pub normal              : F3,
14    pub ffnormal            : F3,
15
16    pub is_emitter          : bool,
17
18    pub material            : Material,
19    pub medium              : Medium,
20}
21
22impl State {
23    pub fn new() -> Self {
24        Self {
25            depth           : 4,
26            eta             : 0.0,
27
28            hit_dist        : -1.0,
29
30            fhp             : F3::zeros(),
31            normal          : F3::zeros(),
32            ffnormal        : F3::zeros(),
33
34            is_emitter      : false,
35
36            material        : Material::new(),
37            medium          : Medium::new(),
38        }
39    }
40
41    /// Calculate tangent and bitangent
42    pub fn onb(&mut self, n: F3, t: &mut F3, b: &mut F3) {
43        let up = if n.z.abs() < 0.999 { F3::new(0.0, 0.0, 1.0) } else { F3::new(1.0, 0.0, 0.0) };
44
45        *t = up.cross(&n).normalize();
46        *b = n.cross(&t);
47    }
48
49    /// State post-processing, called by the tracer after calling Scene::closest_hit()
50    pub fn finalize(&mut self, ray: &Ray) {
51
52        self.fhp = ray.at(&self.hit_dist);
53
54        if dot(&self.normal, &ray.direction) <= 0.0 {
55            self.ffnormal = self.normal;
56        } else {
57            self.ffnormal = -self.normal;
58        }
59
60        self.material.finalize();
61        self.eta = if dot(&ray.direction, &self.normal) < 0.0 { 1.0 / self.material.ior } else { self.material.ior };
62    }
63
64}
65
66// Light
67
68#[derive(PartialEq, Clone, Debug)]
69pub enum LightType {
70    Rectangular,
71    Spherical,
72    Distant,
73}
74
75#[derive(PartialEq, Clone, Debug)]
76pub struct Light {
77    pub light_type          : LightType,
78    pub position            : F3,
79    pub emission            : F3,
80    pub u                   : F3,
81    pub v                   : F3,
82    pub radius              : F,
83    pub area                : F,
84}
85
86// ScatterSampleRec
87
88#[derive(PartialEq, Clone, Debug)]
89pub struct ScatterSampleRec {
90
91    pub l                   : F3,
92    pub f                   : F3,
93    pub pdf                 : F,
94}
95
96impl ScatterSampleRec {
97    pub fn new() -> Self {
98        Self {
99            l               : F3::zeros(),
100            f               : F3::zeros(),
101            pdf             : 0.0,
102        }
103    }
104}
105
106// LightSampleRec
107
108#[derive(PartialEq, Clone, Debug)]
109pub struct LightSampleRec {
110
111    pub normal              : F3,
112    pub emission            : F3,
113    pub direction           : F3,
114
115    pub dist                : F,
116    pub pdf                 : F,
117}
118
119impl LightSampleRec {
120    pub fn new() -> Self {
121        Self {
122            normal          : F3::zeros(),
123            emission        : F3::zeros(),
124            direction       : F3::zeros(),
125
126            dist            : 0.0,
127            pdf             : 0.0,
128        }
129    }
130}
131