-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLights.cpp
executable file
·121 lines (90 loc) · 3.01 KB
/
Lights.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
115
116
117
118
119
120
121
#include <cfloat>
#include "Lights.h"
#include <time.h>
// Default Constructor
Light::Light() {}
Light::~Light() {}
Light::Light(rgb color) {
illumination = color;
}
const rgb Light::getColor() {
return illumination;
}
PointLight::PointLight(double x, double y, double z, const rgb color):
Light(color) {
position = vec3(x,y,z);
}
// Returns a normalized incidence vector.
vec3 PointLight::getIncidence(const vec3 &point) {
//vec3 lightVector = position - origin;
//vec3 incidence = lightVector - (point - origin);
vec3 incidence = position - point;
incidence.normalize();
return incidence;
}
Ray PointLight::getShadowRay(const vec3& startPoint, double bias, Ray& viewRay) {
return Ray(startPoint, bias, 1, getIncidence(startPoint), viewRay.getSample(), NULL);
}
DirectionalLight::DirectionalLight(double x, double y, double z, const rgb color):
Light(color) {
direction = vec3(x,y,z);
direction.normalize();
}
// Returns a normalized incidence vector.
vec3 DirectionalLight::getIncidence(const vec3 &point) {
//vec3 incidence = position - origin;
//incidence.normalize();
return -direction;
}
Ray DirectionalLight::getShadowRay(const vec3& startPoint, double bias, Ray& viewRay) {
return Ray(startPoint, bias, DBL_MAX, getIncidence(startPoint), viewRay.getSample(), viewRay.getLastHitPrim());
}
AreaLight::AreaLight(const vec3& position, const vec3& edge1, const vec3& edge2, const rgb color, int sampleRate) : Light(color) {
this->position = position;
this->edge1 = edge1;
this->edge2 = edge2;
randGen = new CRandomMersenne((uint32)time(NULL));
// Initialize point grid.
for (int i = 0; i < sampleRate; i++) {
jitteredPoints.push_back(vector<vec3>());
}
// Generate jittered points.
double stepSize = 1.0 / sampleRate;
vector<vec3> shuffledPoints;
for (int i = 0; i < sampleRate; i++) {
for (int j = 0; j < sampleRate; j++) {
vec3 point = position + (i*stepSize + randGen->Random()*stepSize)*edge1 +
(j*stepSize + randGen->Random()*stepSize)*edge2;
shuffledPoints.push_back(point);
}
}
// Shuffle points
int size = (int)shuffledPoints.size();
for (int i = size - 1; i >= 0; i--) {
int randomIndex = randGen->IRandom(0, i);
vec3 temp = shuffledPoints[i];
shuffledPoints[i] = shuffledPoints[randomIndex];
shuffledPoints[randomIndex] = temp;
}
// Fill point grid with shuffled points
unsigned int shuffledIndex = 0;
for (int i = 0; i < sampleRate; i++) {
for (int j = 0; j < sampleRate; j++) {
jitteredPoints[i].push_back(shuffledPoints[shuffledIndex]);
shuffledIndex++;
}
}
}
AreaLight::~AreaLight() {
delete randGen;
}
vec3 AreaLight::getIncidence(const vec3 &point, Ray& viewRay) {
int p = viewRay.getSample().p;
int q = viewRay.getSample().q;
vec3 incidence = jitteredPoints[p][q] - point;
incidence.normalize();
return incidence;
}
Ray AreaLight::getShadowRay(const vec3& startPoint, double bias, Ray& viewRay) {
return Ray(startPoint, bias, 1, getIncidence(startPoint, viewRay), viewRay.getSample(), viewRay.getLastHitPrim());
}