Added additional directional shadow maps for better looking sun shadows.

This commit is contained in:
Faerbit 2015-05-31 17:36:21 +02:00
parent 1f1741effb
commit 52a36c9b18
3 changed files with 66 additions and 25 deletions

View File

@ -6,6 +6,8 @@ in vec4 fragPosition;
in vec4 shadowCoord0; in vec4 shadowCoord0;
in vec4 shadowCoord1; in vec4 shadowCoord1;
in vec4 shadowCoord2; in vec4 shadowCoord2;
in vec4 shadowCoord3;
in vec4 shadowCoord4;
out vec4 oColor; out vec4 oColor;
@ -13,6 +15,8 @@ uniform sampler2D uTexture;
uniform sampler2DShadow shadowMap_directional0; uniform sampler2DShadow shadowMap_directional0;
uniform sampler2DShadow shadowMap_directional1; uniform sampler2DShadow shadowMap_directional1;
uniform sampler2DShadow shadowMap_directional2; uniform sampler2DShadow shadowMap_directional2;
uniform sampler2DShadow shadowMap_directional3;
uniform sampler2DShadow shadowMap_directional4;
uniform samplerCubeShadow shadowMap_cube0; uniform samplerCubeShadow shadowMap_cube0;
uniform samplerCubeShadow shadowMap_cube1; uniform samplerCubeShadow shadowMap_cube1;
uniform samplerCubeShadow shadowMap_cube2; uniform samplerCubeShadow shadowMap_cube2;
@ -210,26 +214,46 @@ void main()
sunAngle = dot(vec3(0.0, 1.0, 0.0), directionalVector); sunAngle = dot(vec3(0.0, 1.0, 0.0), directionalVector);
float directionalVisibility = 1.0f; float directionalVisibility = 1.0f;
float directionalIntensity = sunIntensity(sunAngle); float directionalIntensity = sunIntensity(sunAngle);
if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.2) { if (distanceToBorder(shadowCoord3.xy) <= 0.5 && distanceToBorder(shadowCoord3.xy) > 0.2) {
if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.2) { if (distanceToBorder(shadowCoord2.xy) <= 0.5 && distanceToBorder(shadowCoord2.xy) > 0.2) {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001, directionalIntensity); if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.2) {
if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.2) {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001, directionalIntensity);
}
else if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.0) {
float directionalVisibility0 = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001, directionalIntensity);
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.001, directionalIntensity);
directionalVisibility = mix(directionalVisibility0, directionalVisibility1, distanceToBorder(shadowCoord0.xy) * 5);
}
else {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.001, directionalIntensity);
}
}
else if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.0) {
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.001, directionalIntensity);
float directionalVisibility2 = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.002, directionalIntensity);
directionalVisibility = mix(directionalVisibility1, directionalVisibility2, distanceToBorder(shadowCoord1.xy) * 5);
}
else {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.002, directionalIntensity);
}
} }
else if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.0) { else if (distanceToBorder(shadowCoord2.xy) <= 0.5 && distanceToBorder(shadowCoord2.xy) > 0.0) {
float directionalVisibility0 = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001, directionalIntensity); float directionalVisibility2 = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.002, directionalIntensity);
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.002, directionalIntensity); float directionalVisibility3 = sampleDirectionalShadow(shadowMap_directional3, shadowCoord3, 0.005, directionalIntensity);
directionalVisibility = mix(directionalVisibility0, directionalVisibility1, distanceToBorder(shadowCoord0.xy) * 5); directionalVisibility = mix(directionalVisibility2, directionalVisibility3, distanceToBorder(shadowCoord2.xy) * 5);
} }
else { else {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.002, directionalIntensity); directionalVisibility = sampleDirectionalShadow(shadowMap_directional3, shadowCoord3, 0.005, directionalIntensity);
} }
} }
else if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.0) { else if (distanceToBorder(shadowCoord3.xy) <= 0.5 && distanceToBorder(shadowCoord3.xy) > 0.0) {
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.002, directionalIntensity); float directionalVisibility3 = sampleDirectionalShadow(shadowMap_directional3, shadowCoord3, 0.005, directionalIntensity);
float directionalVisibility2 = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.01, directionalIntensity); float directionalVisibility4 = sampleDirectionalShadow(shadowMap_directional4, shadowCoord4, 0.01, directionalIntensity);
directionalVisibility = mix(directionalVisibility1, directionalVisibility2, distanceToBorder(shadowCoord1.xy) * 5); directionalVisibility = mix(directionalVisibility3, directionalVisibility4, distanceToBorder(shadowCoord3.xy) * 5);
} }
else { else {
directionalVisibility = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.01, directionalIntensity); directionalVisibility = sampleDirectionalShadow(shadowMap_directional4, shadowCoord4, 0.01, directionalIntensity);
} }
diffuseColor += clamp(dot(normalize(vNormal), directionalVector) diffuseColor += clamp(dot(normalize(vNormal), directionalVector)
*diffuseFactor*directionalIntensity*sunColor(sunAngle), 0.0, 1.0)*directionalVisibility; *diffuseFactor*directionalIntensity*sunColor(sunAngle), 0.0, 1.0)*directionalVisibility;
@ -242,7 +266,14 @@ void main()
// point lights // point lights
float visibility = 1.0; float visibility = 1.0;
for(int i = 0; i<lightCount; i++) { for(int i = 0; i<lightCount; i++) {
vec3 lightDirection = vec3(fragPosition) - lightSources[i]; vec3 lightPos = vec3(0.0, 0.0, 0.0);
if (isFlame[i] == true) {
lightPos = vec3(lightSources[i].x + movement.y , lightSources[i].y, lightSources[i].z + movement.x);
}
else {
lightPos = lightSources[i];
}
vec3 lightDirection = vec3(fragPosition) - lightPos;
float distance = length(lightDirection); float distance = length(lightDirection);
float pointVisibility = 1.0f; float pointVisibility = 1.0f;
// only take lights into account with meaningful contribution // only take lights into account with meaningful contribution
@ -284,7 +315,7 @@ void main()
if (i == 9 && i<maxShadowRenderCount) { if (i == 9 && i<maxShadowRenderCount) {
pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection, intensity); pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection, intensity);
} }
vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); vec3 lightVector = normalize(lightPos-vec3(fragPosition));
diffuseColor += clamp(dot(normalize(vNormal), lightVector) diffuseColor += clamp(dot(normalize(vNormal), lightVector)
*diffuseFactor*intensity*lightColors[i]*pointVisibility, 0.0, 1.0); *diffuseFactor*intensity*lightColors[i]*pointVisibility, 0.0, 1.0);
vec3 cameraVector = normalize(camera - vec3(fragPosition)); vec3 cameraVector = normalize(camera - vec3(fragPosition));

View File

@ -14,6 +14,8 @@ out vec4 fragPosition;
out vec4 shadowCoord0; out vec4 shadowCoord0;
out vec4 shadowCoord1; out vec4 shadowCoord1;
out vec4 shadowCoord2; out vec4 shadowCoord2;
out vec4 shadowCoord3;
out vec4 shadowCoord4;
void main() void main()
{ {
@ -23,5 +25,7 @@ void main()
shadowCoord0 = shadowMVPs[0] * vec4(aPosition, 1.0); shadowCoord0 = shadowMVPs[0] * vec4(aPosition, 1.0);
shadowCoord1 = shadowMVPs[1] * vec4(aPosition, 1.0); shadowCoord1 = shadowMVPs[1] * vec4(aPosition, 1.0);
shadowCoord2 = shadowMVPs[2] * vec4(aPosition, 1.0); shadowCoord2 = shadowMVPs[2] * vec4(aPosition, 1.0);
shadowCoord3 = shadowMVPs[3] * vec4(aPosition, 1.0);
shadowCoord4 = shadowMVPs[4] * vec4(aPosition, 1.0);
gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0);
} }

View File

@ -125,8 +125,8 @@ void Graphics::init(Level* level) {
level->getPhysics()->getWorld()->setDebugDrawer(&debugDrawer); level->getPhysics()->getWorld()->setDebugDrawer(&debugDrawer);
depth_directionalMaps = std::vector<SharedTexture2D>(3); depth_directionalMaps = std::vector<SharedTexture2D>(5);
framebuffer_directional = std::vector<SharedFrameBufferObject>(3); framebuffer_directional = std::vector<SharedFrameBufferObject>(5);
for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) { for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) {
depth_directionalMaps.at(i) = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); depth_directionalMaps.at(i) = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24));
depth_directionalMaps.at(i)->setMinFilter(GL_NEAREST); depth_directionalMaps.at(i)->setMinFilter(GL_NEAREST);
@ -235,20 +235,20 @@ void Graphics::bindTextureUnits(){
if (level->getLights()->size() > 0) { if (level->getLights()->size() > 0) {
for(unsigned int i = 0; i<depth_cubeMaps.size(); i++){ for(unsigned int i = 0; i<depth_cubeMaps.size(); i++){
lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), textureCount + i + 5); lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), textureCount + i + 7);
} }
} }
flamePostShader->use(); flamePostShader->use();
flamePostShader->setTexture("light_fbo", light_fbo_color_texture, textureCount + 15); flamePostShader->setTexture("light_fbo", light_fbo_color_texture, textureCount + 17);
skydomeShader->use(); skydomeShader->use();
skydomeShader->setTexture("dayTexture", level->getSkydome()->getDayTexture(), textureCount + 16); skydomeShader->setTexture("dayTexture", level->getSkydome()->getDayTexture(), textureCount + 18);
skydomeShader->setTexture("nightTexture", level->getSkydome()->getNightTexture(), textureCount + 17); skydomeShader->setTexture("nightTexture", level->getSkydome()->getNightTexture(), textureCount + 19);
loadingShader->use(); loadingShader->use();
loadingShader->setTexture("screen", loadingScreen, textureCount + 18); loadingShader->setTexture("screen", loadingScreen, textureCount + 20);
loadingShader->setTexture("screenContinue", loadingContinueScreen, textureCount + 19); loadingShader->setTexture("screenContinue", loadingContinueScreen, textureCount + 21);
printf("This application used %d texture units.\n", textureCount + 19); printf("This application used %d texture units.\n", textureCount + 21);
} }
void Graphics::renderLoadingScreen() { void Graphics::renderLoadingScreen() {
@ -385,9 +385,15 @@ void Graphics::render(double time)
projection_size = 10.0f; projection_size = 10.0f;
break; break;
case 1: case 1:
projection_size = 30.0f; projection_size = 20.0f;
break; break;
case 2: case 2:
projection_size = 40.0f;
break;
case 3:
projection_size = 60.0f;
break;
case 4:
projection_size = farPlane/1.5f; projection_size = farPlane/1.5f;
break; break;
} }