-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight.cpp
More file actions
89 lines (79 loc) · 3.27 KB
/
light.cpp
File metadata and controls
89 lines (79 loc) · 3.27 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
#include "light.h"
#include "surface.h"
Light::Light(LightType type, nlohmann::json config) {
switch (type) {
case LightType::POINT_LIGHT:
this->position = Vector3f(config["location"][0], config["location"][1], config["location"][2]);
break;
case LightType::DIRECTIONAL_LIGHT:
this->direction = Vector3f(config["direction"][0], config["direction"][1], config["direction"][2]);
break;
case LightType::AREA_LIGHT:
this->center = Vector3f(config["center"][0], config["center"][1], config["center"][2]);
this->vx = Vector3f(config["vx"][0], config["vx"][1], config["vx"][2]);
this->vy = Vector3f(config["vy"][0], config["vy"][1], config["vy"][2]);
this->normal = Vector3f(config["normal"][0], config["normal"][1], config["normal"][2]);
break;
default:
std::cout << "WARNING: Invalid light type detected";
break;
}
this->radiance = Vector3f(config["radiance"][0], config["radiance"][1], config["radiance"][2]);
this->type = type;
}
std::pair<Vector3f, LightSample> Light::sample(Interaction& si) {
LightSample ls;
memset(&ls, 0, sizeof(ls));
Vector3f radiance;
switch (type) {
case LightType::POINT_LIGHT:
ls.wo = (position - si.p);
ls.d = ls.wo.Length();
ls.wo = Normalize(ls.wo);
radiance = (1.f / (ls.d * ls.d)) * this->radiance;
break;
case LightType::DIRECTIONAL_LIGHT:
ls.wo = Normalize(direction);
ls.d = 1e10;
radiance = this->radiance;
break;
case LightType::AREA_LIGHT:
Vector3f pt = rectangle_sample(this->center, this->vx, this->vy);
ls.wo = pt - si.p;
ls.d = ls.wo.Length();
ls.wo = Normalize(ls.wo);
if (Dot(ls.wo, this->normal) < 0)
radiance = Cross(this->vx*2, this->vy*2).Length()*this->radiance*std::abs(Dot(this->normal, ls.wo))/(ls.d*ls.d);
break;
}
return { radiance, ls };
}
Interaction Light::intersectLight(Ray& ray) {
Interaction si;
memset(&si, 0, sizeof(si));
if (type == LightType::AREA_LIGHT) {
if (Dot(ray.d, this->normal) >= 0)
return si;
Interaction li = Surface::rayTriangleIntersect(ray,
this->center + this->vx + this->vy,
this->center + this->vx - this->vy,
this->center - this->vx + this->vy,
this->normal);
if (li.didIntersect)
{
li.emissiveColor = this->radiance;
return li;
}
li = Surface::rayTriangleIntersect(ray,
this->center - this->vx - this->vy,
this->center + this->vx - this->vy,
this->center - this->vx + this->vy,
this->normal);
if (li.didIntersect)
{
li.emissiveColor = this->radiance;
return li;
}
}
return si;
}