Changing shadow map sampler from sampler2D to sampler2Dshadow and removing now useless colour texture.

This commit is contained in:
Faerbit 2014-12-04 13:21:20 +01:00
parent 09fc76d26b
commit aa764cfa39
4 changed files with 19 additions and 28 deletions

View File

@ -1,10 +1,7 @@
#version 150 #version 150
out vec4 fragmentDepth; out float fragmentDepth;
void main() { void main() {
fragmentDepth.r = gl_FragCoord.z; fragmentDepth = gl_FragCoord.z;
fragmentDepth.g = gl_FragCoord.z;
fragmentDepth.b = gl_FragCoord.z;
fragmentDepth.a = 1.0;
} }

View File

@ -8,7 +8,7 @@ in vec4 shadowCoord;
out vec4 oColor; out vec4 oColor;
uniform sampler2D uTexture; uniform sampler2D uTexture;
uniform sampler2D shadowMap; uniform sampler2DShadow shadowMap;
uniform vec3 ambientColor; uniform vec3 ambientColor;
uniform float ambientFactor; uniform float ambientFactor;
uniform float diffuseFactor; uniform float diffuseFactor;
@ -59,13 +59,16 @@ void main()
} }
// shadows // shadows
float bias = 0.005; float bias = 0.001;
vec3 biasedShadowCoord = vec3(shadowCoord);
biasedShadowCoord.z = shadowCoord.z - bias;
float visibility = 1.0; float visibility = 1.0;
if (shadowCoord.x > 0.0 && shadowCoord.x < 1.0) { if (shadowCoord.x > 0.0 && shadowCoord.x < 1.0) {
if (shadowCoord.y > 0.0 && shadowCoord.y < 1.0) { if (shadowCoord.y > 0.0 && shadowCoord.y < 1.0) {
if (texture(shadowMap, shadowCoord.xy).z < shadowCoord.z-bias) { visibility = texture(shadowMap, biasedShadowCoord);
visibility = 0.5; //if (texture(shadowMap, vec3(shadowCoord), bias) < shadowCoord.z) {
} //visibility = 0.5;
//}
} }
} }

View File

@ -29,20 +29,13 @@ void Graphics::init() {
lightingShader = ShaderProgramCreator("phong").attributeLocations( lightingShader = ShaderProgramCreator("phong").attributeLocations(
vao->getAttributeLocations()).create(); vao->getAttributeLocations()).create();
depthTexture_depth = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH24_STENCIL8)); depthTexture = SharedTexture2D( new Texture2D(glm::vec2(windowSize.x, windowSize.y), GL_DEPTH24_STENCIL8));
depthTexture_depth->setMinFilter(GL_NEAREST); depthTexture->setMinFilter(GL_LINEAR);
depthTexture_depth->setMagFilter(GL_NEAREST); depthTexture->setMagFilter(GL_LINEAR);
depthTexture_depth->setWrapS(GL_CLAMP_TO_EDGE); depthTexture->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
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);
framebuffer = SharedFrameBufferObject(new FrameBufferObject()); framebuffer = SharedFrameBufferObject(new FrameBufferObject());
framebuffer->attachColorTexture("fragmentDepth", depthTexture_colour); framebuffer->setDepthTexture(depthTexture);
framebuffer->setDepthTexture(depthTexture_depth);
framebuffer->validate(); framebuffer->validate();
depthShader = ShaderProgramCreator("depth") depthShader = ShaderProgramCreator("depth")
@ -63,7 +56,7 @@ void Graphics::render(Level* level)
{ {
// render depth texture for sun // render depth texture for sun
framebuffer->bind(); framebuffer->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
depthShader->use(); depthShader->use();
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition()); glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
glm::mat4 depthViewProjectionMatrix = glm::ortho<float>(-20, 20, -20, 20, -20, 40) * glm::mat4 depthViewProjectionMatrix = glm::ortho<float>(-20, 20, -20, 20, -20, 40) *
@ -95,7 +88,7 @@ void Graphics::render(Level* level)
glm::mat4 depthBiasMVP = biasMatrix*depthViewProjectionMatrix; glm::mat4 depthBiasMVP = biasMatrix*depthViewProjectionMatrix;
lightingShader->setUniform("shadowMVP", depthBiasMVP); lightingShader->setUniform("shadowMVP", depthBiasMVP);
lightingShader->setTexture("shadowMap", depthTexture_colour, 2); lightingShader->setTexture("shadowMap", depthTexture, 1);
//set lighting parameters //set lighting parameters
if (level->getLights().size() > 0) { if (level->getLights().size() > 0) {
@ -149,8 +142,7 @@ void Graphics::render(Level* level)
void Graphics::resize(glm::uvec2 windowSize) { void Graphics::resize(glm::uvec2 windowSize) {
this->windowSize = windowSize; this->windowSize = windowSize;
depthTexture_depth->resize(windowSize); depthTexture->resize(glm::vec2(windowSize.x, windowSize.y));
depthTexture_colour->resize(windowSize);
} }
glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) { glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) {

View File

@ -30,8 +30,7 @@ class Graphics {
GLFWwindow* window; GLFWwindow* window;
ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedShaderProgram depthShader;
ACGL::OpenGL::SharedTexture2D depthTexture_depth; ACGL::OpenGL::SharedTexture2D depthTexture;
ACGL::OpenGL::SharedTexture2D depthTexture_colour;
ACGL::OpenGL::SharedFrameBufferObject framebuffer; ACGL::OpenGL::SharedFrameBufferObject framebuffer;
}; };