diff --git a/Shader/depth.fsh b/Shader/depth.fsh index d7beeb7..4e98194 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -1,16 +1,7 @@ #version 150 -in vec4 fragPosition; - -uniform float farPlane; - out float gl_FragDepth; void main() { - float nearPlane = 0.1; - float A = -(farPlane+nearPlane)/(farPlane-nearPlane); - float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); - float value = 0.5*(-A*length(fragPosition) + B)/length(fragPosition) + 0.5; - gl_FragDepth = value; - //gl_FragDepth = length(fragPosition)/farPlane; + gl_FragDepth = gl_FragCoord.z; } diff --git a/Shader/depth.vsh b/Shader/depth.vsh index 2618bfa..f84c3cf 100644 --- a/Shader/depth.vsh +++ b/Shader/depth.vsh @@ -5,11 +5,7 @@ in vec3 aNormal; in vec3 aTexcoord; uniform mat4 modelViewProjectionMatrix; -uniform mat4 modelViewMatrix; - -out vec4 fragPosition; void main() { - fragPosition = modelViewMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/Shader/depth_cube.fsh b/Shader/depth_cube.fsh new file mode 100644 index 0000000..d7beeb7 --- /dev/null +++ b/Shader/depth_cube.fsh @@ -0,0 +1,16 @@ +#version 150 + +in vec4 fragPosition; + +uniform float farPlane; + +out float gl_FragDepth; + +void main() { + float nearPlane = 0.1; + float A = -(farPlane+nearPlane)/(farPlane-nearPlane); + float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); + float value = 0.5*(-A*length(fragPosition) + B)/length(fragPosition) + 0.5; + gl_FragDepth = value; + //gl_FragDepth = length(fragPosition)/farPlane; +} diff --git a/Shader/depth_cube.vsh b/Shader/depth_cube.vsh new file mode 100644 index 0000000..2618bfa --- /dev/null +++ b/Shader/depth_cube.vsh @@ -0,0 +1,15 @@ +#version 150 + +in vec3 aPosition; +in vec3 aNormal; +in vec3 aTexcoord; + +uniform mat4 modelViewProjectionMatrix; +uniform mat4 modelViewMatrix; + +out vec4 fragPosition; + +void main() { + fragPosition = modelViewMatrix * vec4(aPosition, 1.0); + gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); +} diff --git a/Shader/phong.fsh b/Shader/phong.fsh index d43e311..5c29256 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -101,7 +101,8 @@ float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { 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; + float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0))); + bias = clamp(bias, 0.0, 0.01); //return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); return texture(shadowMap, vec4(lightDirection , compValue - bias)); } diff --git a/graphics.cc b/graphics.cc index a2be832..e4a0b72 100644 --- a/graphics.cc +++ b/graphics.cc @@ -42,6 +42,9 @@ void Graphics::init(Level* level) { depthShader = ShaderProgramCreator("depth") .attributeLocations(vao->getAttributeLocations()).create(); + depthCubeShader = ShaderProgramCreator("depth_cube") + .attributeLocations(vao->getAttributeLocations()).create(); + depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); depthTexture->setMinFilter(GL_NEAREST); depthTexture->setMagFilter(GL_NEAREST); @@ -88,8 +91,8 @@ glm::uvec2 Graphics::getWindowSize() { void Graphics::render(double time) { // At first render shadows - depthShader->use(); - depthShader->setUniform("farPlane", farPlane); + depthCubeShader->use(); + depthCubeShader->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); @@ -109,13 +112,14 @@ void Graphics::render(double time) glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); - level->render(depthShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); + level->render(depthCubeShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } } } // render depth texture for sun + depthShader->use(); glViewport(0, 0, windowSize.x, windowSize.y); // far pass diff --git a/graphics.hh b/graphics.hh index 6c3d7e2..e512636 100644 --- a/graphics.hh +++ b/graphics.hh @@ -26,6 +26,7 @@ class Graphics { float nearPlane; float farPlane; ACGL::OpenGL::SharedShaderProgram lightingShader; + ACGL::OpenGL::SharedShaderProgram depthCubeShader; ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedTexture2D depthTexture; ACGL::OpenGL::SharedFrameBufferObject framebuffer;