Fixed directional shadows.

This commit is contained in:
Faerbit 2015-02-10 00:27:13 +01:00
parent 49b7cb34fe
commit d1173ba903
7 changed files with 42 additions and 18 deletions

View File

@ -1,16 +1,7 @@
#version 150 #version 150
in vec4 fragPosition;
uniform float farPlane;
out float gl_FragDepth; out float gl_FragDepth;
void main() { void main() {
float nearPlane = 0.1; gl_FragDepth = gl_FragCoord.z;
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;
} }

View File

@ -5,11 +5,7 @@ in vec3 aNormal;
in vec3 aTexcoord; in vec3 aTexcoord;
uniform mat4 modelViewProjectionMatrix; uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelViewMatrix;
out vec4 fragPosition;
void main() { void main() {
fragPosition = modelViewMatrix * vec4(aPosition, 1.0);
gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0);
} }

16
Shader/depth_cube.fsh Normal file
View File

@ -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;
}

15
Shader/depth_cube.vsh Normal file
View File

@ -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);
}

View File

@ -101,7 +101,8 @@ float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) {
float A = -(farPlane+nearPlane)/(farPlane-nearPlane); float A = -(farPlane+nearPlane)/(farPlane-nearPlane);
float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane);
float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; 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 , length(lightDirection)/farPlane - bias));
return texture(shadowMap, vec4(lightDirection , compValue - bias)); return texture(shadowMap, vec4(lightDirection , compValue - bias));
} }

View File

@ -42,6 +42,9 @@ void Graphics::init(Level* level) {
depthShader = ShaderProgramCreator("depth") depthShader = ShaderProgramCreator("depth")
.attributeLocations(vao->getAttributeLocations()).create(); .attributeLocations(vao->getAttributeLocations()).create();
depthCubeShader = ShaderProgramCreator("depth_cube")
.attributeLocations(vao->getAttributeLocations()).create();
depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24));
depthTexture->setMinFilter(GL_NEAREST); depthTexture->setMinFilter(GL_NEAREST);
depthTexture->setMagFilter(GL_NEAREST); depthTexture->setMagFilter(GL_NEAREST);
@ -88,8 +91,8 @@ glm::uvec2 Graphics::getWindowSize() {
void Graphics::render(double time) void Graphics::render(double time)
{ {
// At first render shadows // At first render shadows
depthShader->use(); depthCubeShader->use();
depthShader->setUniform("farPlane", farPlane); depthCubeShader->setUniform("farPlane", farPlane);
// render depth textures for point lights // render depth textures for point lights
glViewport(0, 0, cube_size, cube_size); 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::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; glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(); std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
viewMatrixVector.push_back(viewMatrix); viewMatrixVector.push_back(viewMatrix);
level->render(depthShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); level->render(depthCubeShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector);
if (!framebuffer_cube->isFrameBufferObjectComplete()) { if (!framebuffer_cube->isFrameBufferObjectComplete()) {
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
} }
} }
} }
// render depth texture for sun // render depth texture for sun
depthShader->use();
glViewport(0, 0, windowSize.x, windowSize.y); glViewport(0, 0, windowSize.x, windowSize.y);
// far pass // far pass

View File

@ -26,6 +26,7 @@ class Graphics {
float nearPlane; float nearPlane;
float farPlane; float farPlane;
ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthCubeShader;
ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedShaderProgram depthShader;
ACGL::OpenGL::SharedTexture2D depthTexture; ACGL::OpenGL::SharedTexture2D depthTexture;
ACGL::OpenGL::SharedFrameBufferObject framebuffer; ACGL::OpenGL::SharedFrameBufferObject framebuffer;