diff --git a/Shader/depth.fsh b/Shader/depth.fsh index 1e0039f..b236c6a 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -1,10 +1,7 @@ #version 150 -out vec4 fragmentDepth; +out float fragmentDepth; void main() { - fragmentDepth.r = gl_FragCoord.z; - fragmentDepth.g = gl_FragCoord.z; - fragmentDepth.b = gl_FragCoord.z; - fragmentDepth.a = 1.0; + fragmentDepth = gl_FragCoord.z; } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index d9fc0d5..d1b4031 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -8,7 +8,7 @@ in vec4 shadowCoord; out vec4 oColor; uniform sampler2D uTexture; -uniform sampler2D shadowMap; +uniform sampler2DShadow shadowMap; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -59,13 +59,16 @@ void main() } // shadows - float bias = 0.005; + float bias = 0.001; + vec3 biasedShadowCoord = vec3(shadowCoord); + biasedShadowCoord.z = shadowCoord.z - bias; float visibility = 1.0; if (shadowCoord.x > 0.0 && shadowCoord.x < 1.0) { if (shadowCoord.y > 0.0 && shadowCoord.y < 1.0) { - if (texture(shadowMap, shadowCoord.xy).z < shadowCoord.z-bias) { - visibility = 0.5; - } + visibility = texture(shadowMap, biasedShadowCoord); + //if (texture(shadowMap, vec3(shadowCoord), bias) < shadowCoord.z) { + //visibility = 0.5; + //} } } diff --git a/graphics.cc b/graphics.cc index 951f09f..75f33b0 100644 --- a/graphics.cc +++ b/graphics.cc @@ -29,20 +29,13 @@ void Graphics::init() { lightingShader = ShaderProgramCreator("phong").attributeLocations( vao->getAttributeLocations()).create(); - depthTexture_depth = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH24_STENCIL8)); - depthTexture_depth->setMinFilter(GL_NEAREST); - depthTexture_depth->setMagFilter(GL_NEAREST); - depthTexture_depth->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_depth->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_colour = SharedTexture2D( new Texture2D(windowSize)); - depthTexture_colour->setMinFilter(GL_LINEAR); - depthTexture_colour->setMagFilter(GL_LINEAR); - depthTexture_colour->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_colour->setWrapT(GL_CLAMP_TO_EDGE); + depthTexture = SharedTexture2D( new Texture2D(glm::vec2(windowSize.x, windowSize.y), GL_DEPTH24_STENCIL8)); + depthTexture->setMinFilter(GL_LINEAR); + depthTexture->setMagFilter(GL_LINEAR); + depthTexture->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); framebuffer = SharedFrameBufferObject(new FrameBufferObject()); - framebuffer->attachColorTexture("fragmentDepth", depthTexture_colour); - framebuffer->setDepthTexture(depthTexture_depth); + framebuffer->setDepthTexture(depthTexture); framebuffer->validate(); depthShader = ShaderProgramCreator("depth") @@ -63,7 +56,7 @@ void Graphics::render(Level* level) { // render depth texture for sun framebuffer->bind(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT); depthShader->use(); glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition()); glm::mat4 depthViewProjectionMatrix = glm::ortho(-20, 20, -20, 20, -20, 40) * @@ -95,7 +88,7 @@ void Graphics::render(Level* level) glm::mat4 depthBiasMVP = biasMatrix*depthViewProjectionMatrix; lightingShader->setUniform("shadowMVP", depthBiasMVP); - lightingShader->setTexture("shadowMap", depthTexture_colour, 2); + lightingShader->setTexture("shadowMap", depthTexture, 1); //set lighting parameters if (level->getLights().size() > 0) { @@ -149,8 +142,7 @@ void Graphics::render(Level* level) void Graphics::resize(glm::uvec2 windowSize) { this->windowSize = windowSize; - depthTexture_depth->resize(windowSize); - depthTexture_colour->resize(windowSize); + depthTexture->resize(glm::vec2(windowSize.x, windowSize.y)); } glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) { diff --git a/graphics.hh b/graphics.hh index 1a0a9b3..73aa112 100644 --- a/graphics.hh +++ b/graphics.hh @@ -30,8 +30,7 @@ class Graphics { GLFWwindow* window; ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram depthShader; - ACGL::OpenGL::SharedTexture2D depthTexture_depth; - ACGL::OpenGL::SharedTexture2D depthTexture_colour; + ACGL::OpenGL::SharedTexture2D depthTexture; ACGL::OpenGL::SharedFrameBufferObject framebuffer; };