-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlighting.cpp
More file actions
177 lines (148 loc) · 4.93 KB
/
lighting.cpp
File metadata and controls
177 lines (148 loc) · 4.93 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
#include "main.h"
#include "lighting.h"
#include <math.h>
sf::ConvexShape shadowList[512];
Light::Light(float xx, float yy,int rad, sf::Color col){
type = "point";
x = xx;
y = yy;
radius = rad;
hitBox.left = x-(radius);
hitBox.top = y-(radius);
hitBox.width = radius*2;
hitBox.height = radius*2;
lightColour = col;
}
void Light::update(){
hitBox.left = x-(radius);
hitBox.top = y-(radius);
}
LightManager::LightManager(){
lightLayer.create(800,600);
lightMask.create(800,600);
allLights.create(800,600);
sLightsLayer.setTexture(lightLayer.getTexture());
sLightMask.setTexture(lightMask.getTexture());
sAllLights.setTexture(allLights.getTexture());
lightTexture.loadFromFile(lightFile);
lights.setTexture(lightTexture);
}
void LightManager::drawLights(sf::RenderWindow *screen,int screenx,int screeny){
lightLayer.clear();
lightMask.clear(sf::Color(255,255,255));
allLights.clear();
int shadowSize = 0;
for(int i=0;i<lightList.size();i++){
//cout << "Drawing light " << i << endl;
vector<sf::FloatRect> targetBoxes;
//Get list of objects that collide with light
targetBoxes = whatIntersectsBox(lightList[i]->hitBox);
shadowSize = 0;
for(int j=0;j<targetBoxes.size();j++){
//find middle of box
sf::Vector2f middle(targetBoxes[j].left+(targetBoxes[j].width/2),
targetBoxes[j].top+(targetBoxes[j].height/2));
//Get my middle
sf::Vector2f me(lightList[i]->x,lightList[i]->y);
middle = middle-me;
float len = sqrt(pow(middle.x,2)+pow(middle.y,2));
float angle = atan2(middle.y,middle.x)*180/PI;
//Find right point
while(targetBoxes[j].contains(getCirclePoint(len,angle,me))){
angle-=1;
}
angle += 1;
sf::Vector2f p1 = getCirclePoint(len,angle,me);
sf::Vector2f p2 = getCirclePoint(lightList[i]->radius,angle-1,me);
//Find left point
while(targetBoxes[j].contains(getCirclePoint(len,angle,me))){
angle+=1;
}
angle -= 1;
sf::Vector2f p3 = getCirclePoint(lightList[i]->radius,angle+1,me);
sf::Vector2f p4 = getCirclePoint(len,angle,me);
sf::ConvexShape shadow;
shadow.setPointCount(4);
shadow.setPoint(0,p1);
shadow.setPoint(1,p2);
shadow.setPoint(2,p3);
shadow.setPoint(3,p4);
shadowList[j] = shadow;
shadowSize++;
}
lightMask.clear(sf::Color::White);
lightLayer.clear();
for(int j=0;j<shadowSize;j++){
shadowList[j].move(-screenx,-screeny);
shadowList[j].setFillColor(sf::Color(10,10,10));
shadowList[j].setOutlineThickness(0.5);
shadowList[j].setOutlineColor(sf::Color(10,10,10));
//lightLayer.draw(shadowList[j]);
lightMask.draw(shadowList[j]);
}
for(int j=0;j<targetBoxes.size();j++){
sf::RectangleShape rect(sf::Vector2f(targetBoxes[j].width,targetBoxes[j].height));
rect.setPosition(targetBoxes[j].left-screenx,targetBoxes[j].top-screeny);
rect.setFillColor(sf::Color(100,100,100));
lightMask.draw(rect);
}
lights.setScale(lightList[i]->radius/32,lightList[i]->radius/32);
sf::Vector2f fakePos(lightList[i]->x-screenx-lightList[i]->radius/2,
lightList[i]->y-screeny-lightList[i]->radius/2);
lights.setPosition(fakePos);
lights.setColor(lightList[i]->lightColour);
lightLayer.draw(lights);
lightMask.display();
lightLayer.draw(sLightMask,sf::RenderStates(sf::BlendMultiply));
if(lightList[i]->type == "spot"){
sf::Vector2f me(lightList[i]->x,lightList[i]->y);
sf::Vector2f p1 = me;
sf::Vector2f p2 = getCirclePoint(lightList[i]->radius,lightList[i]->rot-35,me);
sf::Vector2f p3 = getCirclePoint(lightList[i]->radius,lightList[i]->rot+35,me);
sf::CircleShape rect(16);
rect.setFillColor(sf::Color(200,200,200));
rect.setPosition((me.x-16)-screenx,(me.y-16)-screeny);
sf::ConvexShape shadow;
shadow.setPointCount(3);
shadow.setPoint(0,p1);
shadow.setPoint(1,p2);
shadow.setPoint(2,p3);
shadow.setFillColor(sf::Color::White);
shadow.setOutlineThickness(5);
shadow.setOutlineColor(sf::Color::White);
shadow.move(-screenx,-screeny);
lightMask.clear();
lightMask.draw(shadow);
lightMask.draw(rect);
lightMask.display();
lightLayer.draw(sLightMask,sf::RenderStates(sf::BlendMultiply));
}
lightLayer.display();
allLights.draw(sLightsLayer,sf::RenderStates(sf::BlendAdd));
}
//lightMask.display();
//lightLayer.draw(sLightMask,sf::RenderStates(sf::BlendMultiply));
//lightLayer.draw(sLightMask);
//lightLayer.display();
allLights.display();
screen->draw(sAllLights,sf::RenderStates(sf::BlendMultiply));
//screen->draw(sLightsLayer,sf::RenderStates(sf::BlendMultiply));
//screen->draw(sLightMask);
//screen->draw(sLightsLayer);
}
sf::Vector2f LightManager::getCirclePoint(float radius, float angle,sf::Vector2f relPoint){
sf::Vector2f out;
float radians = angle*(PI/180);
out.x = cos(radians)*radius;
out.y = sin(radians)*radius;
out += relPoint;
return out;
}
bool LightManager::checkBoxes(vector<sf::FloatRect> boxes, sf::Vector2f point){
for(int i=0;i<boxes.size();i++){
if(boxes[i].contains(point)){
return true;
}
}
return false;
}