-
Notifications
You must be signed in to change notification settings - Fork 10
/
DynamicLight.cpp
297 lines (246 loc) · 8.13 KB
/
DynamicLight.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
290
291
292
293
294
295
296
297
//
// DynamicLight.cpp
// pixelgame
//
// Created by Jochen Heizmann on 30.07.13.
//
//
#include "DynamicLight.h"
DynamicLight::DynamicLight() :
softShadows{true}, lightSize{256},
upScale{1.0f}, finalSize(lightSize * upScale),
dirty{false}, shadowRenderShader{nullptr},
shadowMapShader{nullptr}, occlusionMap{nullptr}, shadowMap1D{nullptr},
finalShadowMap{nullptr}, shadowCasters{nullptr}, bakedShadowMap{nullptr},
accuracy{1.0}, bakedMapIsValid{false}, additive{true}
{
}
DynamicLight::~DynamicLight()
{
CC_SAFE_RELEASE(shadowRenderShader);
CC_SAFE_RELEASE(shadowMapShader);
CC_SAFE_RELEASE(occlusionMap);
CC_SAFE_RELEASE(shadowMap1D);
CC_SAFE_RELEASE(finalShadowMap);
CC_SAFE_RELEASE(shadowCasters);
CC_SAFE_RELEASE(bakedShadowMap);
}
bool DynamicLight::init()
{
if (!CCNode::init())
return false;
shadowMapShader = this->loadShader("shader/pass.vsh", "shader/shadowMap.fsh");
shadowRenderShader = this->loadShader("shader/pass.vsh", "shader/shadowRender.fsh");
this->initOcclusionMap();
this->initShadowMap1D();
this->initFinalShadowMap();
this->initBakedShadowMap();
color = cocos2d::ccc4(255, 255, 255, 255);
this->updateUniforms();
return true;
}
void DynamicLight::initOcclusionMap()
{
if (occlusionMap)
occlusionMap->release();
occlusionMap = cocos2d::CCRenderTexture::create(lightSize, lightSize);
occlusionMap->retain();
}
void DynamicLight::initShadowMap1D()
{
if (shadowMap1D)
shadowMap1D->release();
// seems like 16 pixel is the minimum height of a texture (on ios)
shadowMap1D = cocos2d::CCRenderTexture::create(lightSize, 16);
shadowMap1D->retain();
}
void DynamicLight::initFinalShadowMap()
{
if (finalShadowMap)
finalShadowMap->release();
finalSize = lightSize * upScale;
finalShadowMap = cocos2d::CCRenderTexture::create(finalSize, finalSize);
finalShadowMap->retain();
}
void DynamicLight::initBakedShadowMap()
{
if (bakedShadowMap)
bakedShadowMap->release();
finalSize = lightSize * upScale;
bakedShadowMap = cocos2d::CCRenderTexture::create(finalSize, finalSize);
bakedShadowMap->retain();
}
void DynamicLight::setShadowCasters(CCNode *casters)
{
this->shadowCasters = casters;
this->shadowCasters->retain();
}
void DynamicLight::updateShadowMap()
{
this->createOcclusionMap();
this->createShadowMap();
}
void DynamicLight::setPosition(const cocos2d::CCPoint &position)
{
CCNode::setPosition(position);
this->invalidateBakedMap();
}
void DynamicLight::draw()
{
if (!this->bakedMapIsValid)
{
if (dirty)
{
this->updateUniforms();
dirty = false;
}
this->updateUniforms();
this->updateShadowMap();
finalShadowMap->getSprite()->setColor(cocos2d::ccc3(color.r, color.g, color.b));
finalShadowMap->getSprite()->setShaderProgram(shadowRenderShader);
finalShadowMap->getSprite()->setAnchorPoint(ccp(0,0));
finalShadowMap->getSprite()->setPosition(ccp(0,0));
finalShadowMap->setScaleY(-1);
bakedShadowMap->beginWithClear(0.0, 0.0, 0.0, 0.0);
finalShadowMap->setAnchorPoint(ccp(0.5,0.5));
finalShadowMap->setPosition(ccp(0,0));
finalShadowMap->visit();
bakedShadowMap->end();
bakedShadowMap->setPosition(ccp(0,0));
if (additive)
bakedShadowMap->getSprite()->setBlendFunc({GL_SRC_ALPHA, GL_ONE});
this->bakedMapIsValid = true;
}
bakedShadowMap->visit();
#if DEBUG_DRAW
this->debugDraw();
#endif
}
void DynamicLight::invalidateBakedMap()
{
++updateCount;
if (updateCount > updateFrequency)
{
updateCount = 0;
this->bakedMapIsValid = false;
}
}
void DynamicLight::debugDraw()
{
//debug rendering
occlusionMap->getSprite()->setColor(cocos2d::ccc3(255, 0, 0));
occlusionMap->setAnchorPoint(ccp(0,0));
occlusionMap->setPosition((ccp(800 - lightSize/2 - this->getPositionX(), 600 - lightSize/2 - this->getPositionY())));
occlusionMap->visit();
occlusionMap->getSprite()->setColor(cocos2d::ccc3(255,255,255));
shadowMap1D->setAnchorPoint(ccp(0,0));
shadowMap1D->setPosition((ccp(800 - lightSize/2 - this->getPositionX(), 600 - lightSize - 15 - this->getPositionY())));
shadowMap1D->visit();
}
cocos2d::CCGLProgram* DynamicLight::loadShader(const char* vertexShader, const char* fragmentShader)
{
cocos2d::CCGLProgram* shader = cocos2d::CCShaderCache::sharedShaderCache()->programForKey(fragmentShader);
if (!shader)
{
shader = new cocos2d::CCGLProgram();
shader->retain();
shader->initWithVertexShaderFilename(vertexShader, fragmentShader);
shader->addAttribute(kCCAttributeNamePosition, cocos2d::kCCVertexAttrib_Position);
shader->addAttribute(kCCAttributeNameTexCoord, cocos2d::kCCVertexAttrib_TexCoords);
shader->addAttribute(kCCAttributeNameColor, cocos2d::kCCVertexAttrib_Color);
shader->link();
shader->updateUniforms();
shader->use();
}
return shader;
}
void DynamicLight::updateUniforms()
{
shadowMapShader->use();
GLint res = shadowMapShader->getUniformLocationForName("resolution");
shadowMapShader->setUniformLocationWith2f(res, lightSize, lightSize);
GLint glUpscale = shadowMapShader->getUniformLocationForName("upScale");
shadowMapShader->setUniformLocationWith1f(glUpscale, upScale);
GLint glAccuracy = shadowMapShader->getUniformLocationForName("accuracy");
shadowMapShader->setUniformLocationWith1f(glAccuracy, accuracy);
shadowRenderShader->use();
GLint glResolution2 = shadowRenderShader->getUniformLocationForName("resolution");
GLint glSoftShadows = shadowRenderShader->getUniformLocationForName("softShadows");
shadowRenderShader->setUniformLocationWith2f(glResolution2, lightSize, lightSize);
shadowRenderShader->setUniformLocationWith1f(glSoftShadows, softShadows ? 1.0f : 0.0f);
glUniform1i(glGetUniformLocation(shadowRenderShader->getProgram(), "u_texture2"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, shadowMap1D->getSprite()->getTexture()->getName());
glActiveTexture(GL_TEXTURE0);
}
void DynamicLight::createOcclusionMap()
{
if (!shadowCasters)
{
occlusionMap->beginWithClear(0.0f,0.0f,0.0f,0.0f);
occlusionMap->end();
return;
}
cocos2d::CCPoint p1 = shadowCasters->getAnchorPoint();
cocos2d::CCPoint p2 = shadowCasters->getPosition();
// Render light region to occluder FBO
occlusionMap->beginWithClear(0.0f,0.0f,0.0f,0.0f);
shadowCasters->setAnchorPoint(ccp(0,0));
//ugly....
shadowCasters->setPosition(ccp(-getPositionX() + lightSize/2 + shadowCasters->getPositionX(),-getPositionY() + lightSize/2 + shadowCasters->getPositionY()));
shadowCasters->visit();
occlusionMap->end();
shadowCasters->setAnchorPoint(p1);
shadowCasters->setPosition(p2);
}
void DynamicLight::createShadowMap()
{
//STEP 2. build a 1D shadow map from occlude FBO
occlusionMap->getSprite()->setShaderProgram(shadowMapShader);
shadowMap1D->beginWithClear(0.f, 0.f, 0.f, 0.f);
occlusionMap->setAnchorPoint(ccp(0.5,0.5));
occlusionMap->setPosition(ccp(lightSize / 2.0, lightSize / 2.0));
occlusionMap->visit();
shadowMap1D->end();
// occlusionMap->getSprite()->setShaderProgram(cocos2d::CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
}
void DynamicLight::setSoftShadows(bool shadows)
{
if (softShadows != shadows)
{
dirty = true;
softShadows = shadows;
}
}
void DynamicLight::setLightSize(int size)
{
if (lightSize != size)
{
dirty = true;
lightSize = size;
}
}
void DynamicLight::setUpScale(float upScale)
{
if (this->upScale != upScale)
{
dirty = true;
this->upScale = upScale;
}
}
void DynamicLight::setAccuracy(float accuracy)
{
if (this->accuracy != accuracy)
{
dirty = true;
this->accuracy = accuracy;
}
}
void DynamicLight::setAdditive(bool additive)
{
this->additive = additive;
}
void DynamicLight::setColor(cocos2d::ccColor4B c)
{
this->color = c;
}