-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLight.cpp
114 lines (91 loc) · 2.03 KB
/
Light.cpp
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
#include "Constants.h"
#include "ShadeRec.h"
#include "Light.h"
#include "World.h"
Light::Light():shadows(true) {
}
bool Light::castShadows() const {
return shadows;
}
// Ambient
Ambient::Ambient():
Light(),
ls(RT_ONE),
color(COLOR_WHITE){
}
Vector Ambient::getDirection(ShadeRec &sr) {
return Vector(RT_ZERO);
}
RGBColor Ambient::L(ShadeRec &sr) {
return ls * color;
}
void Ambient::scaleRadience(const RTdouble &_ls) {
ls = _ls;
}
bool Ambient::inShadow(const Ray &ray, const ShadeRec &sr) const {
return false; // fake
}
// Point Light
PointLight::PointLight():
Light(),
ls(RT_ONE),
color(COLOR_WHITE),
location(Point(RT_ZERO)){
}
Vector PointLight::getDirection(ShadeRec &sr) {
return (location - sr.hit_point).unit();
}
RGBColor PointLight::L(ShadeRec &sr) {
//return ls * color;
RTdouble r = location.distance(sr.hit_point);
if( r < 1) {
return COLOR_BLACK;
}
return ls * color / (r * r * coeff);
}
Point PointLight::getLocation() const {
return location;
}
void PointLight::setLocation(const Point &p) {
location = p;
}
void PointLight::setCoeff(const RTdouble &c) {
coeff = c;
}
void PointLight::setColor(const RGBColor &c) {
color = c;
}
void PointLight::scaleRadience(const RTdouble &_ls) {
ls = _ls;
}
// shadow ray
bool PointLight::inShadow(const Ray &ray, const ShadeRec &sr) const {
RTdouble t;
int numObjects = sr.w.objects.size();
RTdouble d = location.distance(ray.o);
for(int j = 0; j < numObjects; j++) {
if(sr.w.objects[j]->shadowHit(ray, t) && t < d) {
return true;
}
}
return false;
}
// Directional
Directional::Directional():
Light(),
ls(RT_ONE),
color(COLOR_WHITE),
dir(Vector(RT_ZERO)){
}
Vector Directional::getDirection(ShadeRec &sr) {
return dir;
}
RGBColor Directional::L(ShadeRec &sr) {
return ls * color;
}
void Directional::setDir(const Vector &d) {
dir = d;
}
bool Directional::inShadow(const Ray &ray, const ShadeRec &sr) const {
return false; // fake
}