-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight.hpp
More file actions
260 lines (177 loc) · 8.63 KB
/
Light.hpp
File metadata and controls
260 lines (177 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#pragma once
#include "Interaction.hpp"
#include "Shape.hpp"
#include <functional>
struct LightSample{
glm::vec3 L;
SurfaceInteraction interaction;
glm::vec3 dir;
bool isDeltaInteraction() const{
return interaction.n == glm::vec3 { 0,0,0 };
}
};
class Light{
public:
virtual ~Light() = default;
virtual bool isDelta() const = 0;
virtual glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const = 0;
virtual LightSample sample(const glm::vec2& uv, float time) const = 0;
virtual float PDF(const GeometricInteraction& interaction, float time) const = 0;
virtual float PDF(const GeometricInteraction& interaction, const Ray& ray) const = 0;
virtual float Power() const = 0;
virtual void PreProcess(const AABB& bbox){}
};
//infinite light has Le which doesnt need interaction!
//faraway / infinite directional
//uniform infinite
//texture infinite
//should remove InfiniteLight!
class InfiniteLight : public Light{
public:
virtual ~InfiniteLight() = default;
virtual glm::vec3 Le(const Ray& ray) const = 0;
bool isDelta() const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
void PreProcess(const AABB& bbox) override;
protected:
float sceneRadius;
};
class UniformInfiniteLight : public InfiniteLight{
public:
virtual ~UniformInfiniteLight() = default;
UniformInfiniteLight(const glm::vec3& light_color, const std::function<float(float)>& powerFunc = [](float r) -> float{ return std::sqrt(r); }) : color { light_color }, powerFunction { powerFunc }{}
glm::vec3 Le(const Ray& ray) const override;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
float Power() const override;
private:
glm::vec3 color;
std::function<float(float)> powerFunction;
};
class FunctionInfiniteLight : public InfiniteLight{
public:
virtual ~FunctionInfiniteLight() = default;
FunctionInfiniteLight(const std::function<glm::vec3(const Ray& ray)>& lightFunc, const std::function<float(float)>& powerFunc = [](float r) -> float{ return std::sqrt(r); }) : lightFunction(lightFunc), powerFunction { powerFunc }{}
glm::vec3 Le(const Ray& ray) const override;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
float Power() const override;
void PreProcess(const AABB& bbox) override;
private:
std::function<glm::vec3(const Ray& ray)> lightFunction;
std::function<float(float)> powerFunction;
float cachedPower;//need to implement
};
class TextureInfiniteLight : public InfiniteLight{
public:
virtual ~TextureInfiniteLight() = default;
TextureInfiniteLight(const std::shared_ptr<Texture>& tex, float LeScale = 1.0f, const std::function<float(float)>& powerFunc = [](float r) -> float{ return std::sqrt(r); }) : tex { tex }, LeScale(LeScale), powerFunction { powerFunc }{}
glm::vec3 Le(const Ray& ray) const override;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float Power() const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
void PreProcess(const AABB& bbox) override;
private:
std::shared_ptr<Texture> tex;
float LeScale;
float cachedPower;
std::vector<float> weights;
std::vector<float> accWeights;
double totalWeight;
std::function<float(float)> powerFunction;// no_unique_address ?
static constexpr int spp = 500;//HIGH MEMORY USAGE
static constexpr int xSamples = 1920;
static constexpr int ySamples = 1080;
};
//distantLight : InfiniteLIght?
class DistantLight : public Light{
public:
virtual ~DistantLight() = default;
DistantLight(const glm::vec3& light_dir, const glm::vec3& light_color, const std::function<float(float)>& powerFunc = [](float r) -> float{ return std::sqrt(r); }) : dir { light_dir }, color { light_color }, powerFunction { powerFunc }{}
bool isDelta() const final;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
float Power() const override;
void PreProcess(const AABB& bbox) override;
private:
glm::vec3 dir;
glm::vec3 color;
float sceneRadius;
std::function<float(float)> powerFunction;
};
class PointLight : public Light{
public:
virtual ~PointLight() = default;
PointLight(const glm::vec3& p, const glm::vec3& light_color, const std::function<float(float)>& powerFunc = [](float r) -> float{ return 4 * r; }) : p { p }, color { light_color }, powerFunction { powerFunc }{}
bool isDelta() const final;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
float Power() const override;
void PreProcess(const AABB& bbox) override;
private:
glm::vec3 p;
glm::vec3 color;
float sceneRadius;
std::function<float(float)> powerFunction;
};
class AreaLight : public Light{
public:
virtual ~AreaLight() = default;
AreaLight(const std::shared_ptr<Shape>& light_shape, const glm::vec3& light_color, bool oneSided = false) : shape { light_shape }, emissiveTexture { std::make_shared<SolidColor>(light_color) }, cachedPower { 0 }, oneSided { oneSided }{}
AreaLight(const std::shared_ptr<Shape>& light_shape, const std::shared_ptr<Texture>& emissiveTex, bool oneSided = false) : shape { light_shape }, emissiveTexture { emissiveTex }, cachedPower { 0 }, oneSided { oneSided }{}
bool isDelta() const final;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
float Power() const override;
void PreProcess(const AABB& bbox) override;
std::shared_ptr<Shape> getShape() const;
private:
std::shared_ptr<Shape> shape;
std::shared_ptr<Texture> emissiveTexture; //switch to texture/image
float cachedPower;
bool oneSided;
//add alpha mask
};
class TransformedLight : public Light{
public:
virtual ~TransformedLight() = default;
TransformedLight(const std::shared_ptr<Light>& light, const glm::mat4& transform) : light(light), transform(transform), normalMatrix(glm::transpose(glm::inverse(glm::mat3(transform)))), invTransform(glm::inverse(transform)){}
bool isDelta() const final;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
void PreProcess(const AABB& bbox) override;
float Power() const override;
private:
std::shared_ptr<Light> light;
glm::mat4 transform;
glm::mat3 normalMatrix;
glm::mat4 invTransform;
};
class AnimatedLight : public Light{
public:
virtual ~AnimatedLight() = default;
AnimatedLight(const std::shared_ptr<Light>& light, const glm::vec3& direction, const glm::vec2& timeBounds) : light(light), dir(direction), timeBounds(timeBounds){}
bool isDelta() const final;
glm::vec3 L(const SurfaceInteraction& interaction, const Ray& ray) const override;
LightSample sample(const glm::vec2& uv, float time) const override;
float PDF(const GeometricInteraction& interaction, float time) const override;
float PDF(const GeometricInteraction& interaction, const Ray& ray) const override;
void PreProcess(const AABB& bbox) override;
float Power() const override;
private:
std::shared_ptr<Light> light;
glm::vec3 dir;
glm::vec2 timeBounds;
};