From e922027210393ddac516a6285e872ea6032cb3a6 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 14:27:41 +0100 Subject: [PATCH] Got Point Light Shadows working. YEEHAW! --- Shader/depth.fsh | 6 ++++-- Shader/phong.fsh | 21 ++++++--------------- application.cc | 2 +- graphics.cc | 37 ++++++++++++++++++------------------- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index 5c868ce..0fcb690 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -2,9 +2,11 @@ in vec4 fragPosition; +uniform float farPlane; + out float gl_FragDepth; void main() { - gl_FragDepth = fragPosition.z; - //gl_FragDepth = 0.0f; + gl_FragDepth = length(fragPosition)/farPlane; + //gl_FragDepth = 1.5f; } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 93f00cb..09a8bb8 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -9,7 +9,7 @@ out vec4 oColor; uniform sampler2D uTexture; uniform sampler2DShadow shadowMap; -uniform samplerCube shadowMap_cube; +uniform samplerCubeShadow shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -65,15 +65,13 @@ float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { return visibility; } -float samplePointShadow(samplerCube shadowMap, vec3 lightDirection) { +float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float nearPlane = 0.1; float A = -(farPlane+nearPlane)/(farPlane-nearPlane); float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; float bias = 0.005; - //return texture(shadowMap, vec4(lightDirection , compValue - bias)); - float value1 = texture(shadowMap, vec3(lightDirection)); - return value1 - length(lightDirection); + return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); } float distanceToBorder(vec2 vector) { @@ -114,14 +112,7 @@ void main() specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; if (i == 0) { - //visibility = samplePointShadow(shadowMap_cube, lightDirection); - float value = samplePointShadow(shadowMap_cube, lightDirection); - if (abs(value) < 0.1f) { - oColor = vec4(0, 255, 0, 255); - } - else { - oColor = vec4(255, 0, 0, 255); - } + visibility = samplePointShadow(shadowMap_cube, lightDirection); } } /*float value = texture(shadowMap_cube, lightDirection); @@ -140,6 +131,6 @@ void main() fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0); vec4 texture = texture(uTexture, vTexCoord).rgba; - //oColor = vec4(finalColor, 1.0f)*texture; - //oColor = mix(oColor, fogColor, fogFactor); + oColor = vec4(finalColor, 1.0f)*texture; + oColor = mix(oColor, fogColor, fogFactor); } diff --git a/application.cc b/application.cc index b2b00f9..ff80cac 100644 --- a/application.cc +++ b/application.cc @@ -2,7 +2,7 @@ Application::Application() { - graphics = Graphics(glm::uvec2(1024, 1024), 0.1f, 150.0f); + graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f); } void Application::init() diff --git a/graphics.cc b/graphics.cc index 2c36483..941b27c 100644 --- a/graphics.cc +++ b/graphics.cc @@ -62,7 +62,7 @@ void Graphics::init(Level* level) { depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); depth_cubeMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE); depth_cubeMaps.at(i)->setWrapT(GL_CLAMP_TO_EDGE); - //depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); } framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); @@ -72,7 +72,7 @@ void Graphics::init(Level* level) { depthTexture_cube->setMagFilter(GL_NEAREST); depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE); depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); - //depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); framebuffer_cube_mirror = SharedFrameBufferObject(new FrameBufferObject()); framebuffer_cube_mirror->setDepthTexture(depthTexture_cube); @@ -92,11 +92,14 @@ void Graphics::render(double time) { // At first render shadows depthShader->use(); + depthShader->setUniform("farPlane", farPlane); // render depth textures for point lights glViewport(0, 0, cube_size, cube_size); glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, farPlane); - glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0001f, 1.0f, 0.0f), - glm::vec3(0.0001f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; + glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)), + glm::normalize(glm::vec3(0.0f, -1.0f, 0.0f)), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; + glm::vec3 upvectors[6] = {glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, 0.0f, -1.0f), + glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)}; framebuffer_cube->bind(); static bool printed = false; @@ -111,28 +114,24 @@ void Graphics::render(double time) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0); glClear(GL_DEPTH_BUFFER_BIT); glm::mat4 viewMatrix = glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), - level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f)); + level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; if (i_face == 0) { reproduceMatrix = depthViewProjectionMatrix_face; } if (!printed) { - printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", - viewMatrix[0][0], viewMatrix[0][1], viewMatrix[0][2], viewMatrix[0][3], - viewMatrix[1][0], viewMatrix[1][1], viewMatrix[1][2], viewMatrix[1][3], - viewMatrix[2][0], viewMatrix[2][1], viewMatrix[2][2], viewMatrix[2][3], - viewMatrix[3][0], viewMatrix[3][1], viewMatrix[3][2], viewMatrix[3][3] + printf("\n\nView matrix:\n %2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n\n\n", + viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0], viewMatrix[3][0], + viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1], viewMatrix[3][1], + viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2], viewMatrix[3][2], + viewMatrix[0][3], viewMatrix[1][3], viewMatrix[2][3], viewMatrix[3][3] ); + + //std::cout<<"View matrix:" < foovector = std::vector(); foovector.push_back(viewMatrix); level->render(depthShader, false, &depthViewProjectionMatrix_face, &foovector); - glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube->getObjectName()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); - glBlitFramebuffer(0, 0, cube_size, cube_size, cube_size, cube_size, 0, 0, GL_DEPTH_BUFFER_BIT, GL_NEAREST); - glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube->getObjectName()); - glBlitFramebuffer(0, 0, cube_size, cube_size, 0, 0, cube_size, cube_size, GL_DEPTH_BUFFER_BIT, GL_NEAREST); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -146,7 +145,7 @@ void Graphics::render(double time) } } // render depth texture for sun - //glViewport(0, 0, windowSize.x, windowSize.y); + glViewport(0, 0, windowSize.x, windowSize.y); // far pass framebuffer->bind(); @@ -208,8 +207,8 @@ void Graphics::render(double time) shadowVPs.push_back(depthBiasVP); // render the level - //level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); - level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); + level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); + //level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); } void Graphics::updateLights() {