Performance optimization for deciding when the directional shadow gets rendered.

This commit is contained in:
Faerbit 2015-05-28 15:03:08 +02:00
parent a8ce6916e7
commit c8ba61ca56
3 changed files with 63 additions and 52 deletions

View File

@ -47,6 +47,7 @@ uniform bool movingTexture;
uniform vec2 movement;
uniform vec2 movingTextureOffset;
uniform float time;
uniform bool sampleDirectionalShadowSwitch;
vec2 poissonDisk[16] = vec2[](
vec2( -0.94201624, -0.39906216 ),
@ -171,10 +172,9 @@ void main()
// direction lighting
float sunAngle = -1.0;
if(length(directionalLightVector)>0.0f) {
if(sampleDirectionalShadowSwitch) {
vec3 directionalVector = normalize(directionalLightVector);
sunAngle = dot(vec3(0.0, 1.0, 0.0), directionalVector);
if ( sunAngle > 0.0) {
float directionalVisibility = 1.0f;
float directionalIntensity = sunIntensity(sunAngle);
if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.2) {
@ -205,7 +205,6 @@ void main()
(length(cameraVector+directionalVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0)
*specularFactor*directionalIntensity*sunColor(sunAngle)*directionalVisibility;
}
}
// point lights
float visibility = 1.0;

View File

@ -30,6 +30,7 @@ Graphics::Graphics(glm::uvec2 windowSize, float nearPlane,
renderFlames = true;
renderWorld = true;
renderDebug = false;
directionalShadowSwitch = false;
}
Graphics::Graphics() {
@ -196,6 +197,7 @@ void Graphics::init(Level* level) {
lightingShader->setUniform("fogColorRise", level->getFogColourRise());
lightingShader->setUniform("fogColorNight", level->getFogColourNight());
lightingShader->setUniform("ambientColor", level->getAmbientLight());
lightingShader->setUniform("sampleDirectionalShadowSwitch", false);
if(level->getDirectionalLight()) {
lightingShader->setUniform("directionalLightVector",
level->getDirectionalLight()->getPosition());
@ -361,18 +363,22 @@ void Graphics::render(double time)
}
}
// render depth textures for sun
depthShader->use();
glViewport(0, 0, windowSize.x, windowSize.y);
// render depth textures for sun
float sunAngle = glm::dot(glm::vec3(0.0f, 1.0f, 0.0f), glm::normalize(level->getDirectionalLight()->getPosition()));
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
if (sunAngle > 0.0f) {
if (!directionalShadowSwitch) {
lightingShader->use();
lightingShader->setUniform("sampleDirectionalShadowSwitch", true);
directionalShadowSwitch = true;
}
depthShader->use();
for (unsigned int i = 0; i<framebuffer_directional.size(); i++) {
framebuffer_directional.at(i)->bind();
glClear(GL_DEPTH_BUFFER_BIT);
if (sunAngle > 0.0f) {
float projection_size = 0.0f;
switch(i) {
case 0:
@ -393,6 +399,11 @@ void Graphics::render(double time)
}
}
}
else if (directionalShadowSwitch) {
lightingShader->use();
lightingShader->setUniform("sampleDirectionalShadowSwitch", false);
directionalShadowSwitch = false;
}
}
// lighting render pass

View File

@ -84,6 +84,7 @@ class Graphics {
bool renderFlames;
bool renderDebug;
bool renderWorld;
bool directionalShadowSwitch;
DebugDraw debugDrawer;
SharedArrayBuffer debug_ab;
SharedVertexArrayObject debug_vao;