-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightMap.cpp
executable file
·289 lines (241 loc) · 7.74 KB
/
LightMap.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "LightMap.h"
#include <algorithm>
//Based on http://journal.stuffwithstuff.com/2015/09/07/what-the-hero-sees/
class Shadow {
public:
float start;
float end;
int strength;
Shadow(float _start, float _end, int _strength) {
start = _start;
end = _end;
strength = _strength;
}
/// Returns `true` if [other] is completely covered by this shadow.
bool contains(Shadow other) {
return start <= other.start && end >= other.end;
}
};
class ShadowLine {
private:
std::vector<Shadow> shadows;
public:
float visibility(Shadow projection) {
int strength = 0;
for(Shadow shadow : shadows)
if(strength < 100 && shadow.contains(projection))
strength += shadow.strength;
return (100 - std::min(strength, 100)) / 100.0;
}
bool isFullShadow() {
return shadows.size() == 1 &&
shadows[0].start == 0 &&
shadows[0].end == 1 &&
shadows[0].strength == 100;
}
void add(Shadow shadow) {
// Figure out where to slot the new shadow in the list.
long unsigned int index = 0;
while(index < shadows.size()) {
// Stop when we hit the insertion point.
if(shadows[index].start >= shadow.start)
break;
index++;
}
// The new shadow is going here. See if it overlaps the
// previous or next.
if(shadow.strength == 100) {
Shadow *overlappingPrevious = NULL;
if(index > 0 && shadows[index - 1].end > shadow.start && shadows[index - 1].strength == 100)
overlappingPrevious = &(shadows[index - 1]);
Shadow *overlappingNext = NULL;
if(index < shadows.size() && shadows[index].start < shadow.end && shadows[index].strength == 100)
overlappingNext = &(shadows[index]);
// Insert and unify with overlapping shadows.
if(overlappingNext != NULL) {
if(overlappingPrevious != NULL) {
// Overlaps both, so unify one and delete the other.
overlappingPrevious->end = overlappingNext->end;
shadows.erase(shadows.begin() + index);
} else {
// Overlaps the next one, so unify it with that.
overlappingNext->start = shadow.start;
}
} else {
if(overlappingPrevious != NULL) {
// Overlaps the previous one, so unify it with that.
overlappingPrevious->end = shadow.end;
} else {
// Does not overlap anything, so insert.
shadows.insert(shadows.begin() + index, shadow);
}
}
}
}
};
// Creates a [Shadow] that corresponds to the projected
// silhouette of the tile at [row], [col].
Shadow projectTile(float row, float col, int _absorb) {
float topLeft = col / (row + 2);
float bottomRight = (col + 1) / (row + 1);
return Shadow(topLeft, bottomRight, _absorb);
}
sf::Color LightMap::applyIntensity(unsigned int x, unsigned int y) {
float intensity = ambientIntensity;
if(x < width && y < height)
intensity = tiles[x][y];
sf::Color result;
result.a = 255;
result.r = (char)(lightColor.r * intensity);
result.g = (char)(lightColor.g * intensity);
result.b = (char)(lightColor.b * intensity);
return result;
}
sf::Vector2f LightMap::getTilePos(unsigned int x, unsigned int y) {
sf::Vector2f pos(tileX / 2.0f + tileX * x, tileY / 2.0f + tileY * y);
if(x > width + 1)
pos.x = 0;
if(y > height + 1)
pos.y = 0;
return pos;
}
sf::Vector2f LightMap::transformOctant(int row, int col, int octant) {
switch(octant) {
case 0: return sf::Vector2f( col, -row);
case 1: return sf::Vector2f( row, -col);
case 2: return sf::Vector2f( row, col);
case 3: return sf::Vector2f( col, row);
case 4: return sf::Vector2f(-col, row);
case 5: return sf::Vector2f(-row, col);
case 6: return sf::Vector2f(-row, -col);
case 7: return sf::Vector2f(-col, -row);
}
return sf::Vector2f(0, 0);
}
void LightMap::lightOctant(sf::Vector2f light, int octant, float maxIntensity) {
ShadowLine line;
int row = 1;
while(true) {
// Stop once we go out of bounds.
sf::Vector2f pos = light + transformOctant(row, 0, octant);
if(!indexes.inBounds(pos + offset) || maxIntensity < ambientIntensity)
break;
float intensity = maxIntensity;
for(int col = 0; col <= row; col++) {
pos = light + transformOctant(row, col, octant);
// If we've traversed out of bounds, bail on this row.
if(!indexes.inBounds(pos + offset) || intensity < ambientIntensity)
break;
Shadow projection = projectTile(row, col, 0);
// Set the visibility of this tile.
float visible = line.visibility(projection);
float tileIntensity = std::max(visible * intensity, ambientIntensity);
if(tileIntensity > tiles[(int)pos.x][(int)pos.y])
tiles[(int)pos.x][(int)pos.y] = tileIntensity;
int tileValue = indexes.getTile(pos + offset);
if(tileValue / 100.0 > tiles[(int)pos.x][(int)pos.y])
tiles[(int)pos.x][(int)pos.y] = tileValue / 100.0;
// Add any opaque tiles to the shadow map.
if(visible > 0 && tileValue < 0) {
projection.strength = -tileValue;
line.add(projection);
if(line.isFullShadow())
return;
}
intensity -= absorb;
}
++row;
maxIntensity -= absorb;
}
}
LightMap::LightMap(int _tileX, int _tileY, float _ambient, float _absorb, Indexer _indexes,
Layer layer, bool indexLights, sf::Color _lightColor)
: Node(layer), indexes(_indexes) {
//Set arguments
tileX = _tileX;
tileY = _tileY;
ambientIntensity = _ambient;
absorb = _absorb;
lightColor = _lightColor;
//Set sizing
width = indexes.getSize().x * indexes.getScale().x + 1;
height = indexes.getSize().y * indexes.getScale().y + 1;
setSize(sf::Vector2i(tileX * width, tileY * height));
setOrigin(0, 0);
vertices.setPrimitiveType(sf::Quads);
vertices.resize((width + 1) * (height + 1) * 4);
//Build array
tiles = new float*[width];
for(unsigned int x = 0; x < width; ++x) {
tiles[x] = new float[height];
for(unsigned int y = 0; y < height; ++y) {
tiles[x][y] = ambientIntensity;
//Add static lights
if(indexLights) {
sf::Vector2f pos(x, y);
int tileValue = indexes.getTile(pos + offset);
if(tileValue > 0)
addSource(pos, tileValue / 100.0, false);
}
}
}
reload();
}
void LightMap::reload() {
//Clear existing lights
for(unsigned int x = 0; x < width; ++x)
for(unsigned int y = 0; y < height; ++y)
tiles[x][y] = ambientIntensity;
//Propogate Sources
for(long unsigned int i = 0; i < sourcePosition.size(); i++) {
sf::Vector2f light = sourcePosition[i];
tiles[(int)light.x][(int)light.y] = sourceIntensity[i];
for(int octant = 0; octant < 8; octant++)
lightOctant(light, octant, sourceIntensity[i]);
}
//Draw lighting
for(unsigned int x = 0; x < width + 1; ++x)
for(unsigned int y = 0; y < height + 1; ++y) {
sf::Vertex* quad = &vertices[(x + y * width) * 4];
quad[0].position = getTilePos(x, y);
quad[1].position = getTilePos(x-1, y);
quad[2].position = getTilePos(x-1, y-1);
quad[3].position = getTilePos(x, y-1);
quad[0].color = applyIntensity(x, y);
quad[1].color = applyIntensity(x-1, y);
quad[2].color = applyIntensity(x-1, y-1);
quad[3].color = applyIntensity(x, y-1);
}
}
sf::VertexArray *LightMap::getVertices() {
return &vertices;
}
sf::Vector2f LightMap::scalePosition(sf::Vector2f pos) {
return sf::Vector2f((int)(pos.x / tileX) - offset.x, (int)(pos.y / tileY) - offset.y);
}
int LightMap::addSource(sf::Vector2f light, float intensity, bool scale) {
if(scale)
light = scalePosition(light);
int lastIndex = nextIndex;
if(nextIndex < sourcePosition.size()) {
sourcePosition[nextIndex] = light;
sourceIntensity[nextIndex] = intensity;
while(nextIndex < sourcePosition.size() && sourceIntensity[nextIndex] > 0)
++nextIndex;
} else {
sourcePosition.push_back(light);
sourceIntensity.push_back(intensity);
nextIndex = sourcePosition.size();
}
return lastIndex;
}
void LightMap::moveSource(int i, sf::Vector2f light) {
sourcePosition[i] = scalePosition(light);
}
void LightMap::deleteSource(int i) {
sourceIntensity[i] = 0;
}
void LightMap::markCollection() {
singular = false;
setHidden(true);
}