Fixed directional shadows.
This commit is contained in:
parent
49b7cb34fe
commit
d1173ba903
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
16
Shader/depth_cube.fsh
Normal file
16
Shader/depth_cube.fsh
Normal 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
15
Shader/depth_cube.vsh
Normal 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);
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
10
graphics.cc
10
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<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
|
||||
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
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user