From f6a88683b73de8ac96ed832a39068e8d29b710cf Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 19 Jan 2015 00:52:57 +0100 Subject: [PATCH] Removed Cascaded Shadow Mapping because it is not realistic to get it working in the remaining time. --- Shader/phong.fsh | 20 ++---------- Shader/phong.vsh | 8 ++--- graphics.cc | 85 ++++++++++-------------------------------------- graphics.hh | 8 ++--- 4 files changed, 25 insertions(+), 96 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 77cb8d6..7c5ead4 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -3,16 +3,12 @@ in vec3 vNormal; in vec2 vTexCoord; in vec4 fragPosition; -in vec4 shadowCoord_near; -in vec4 shadowCoord_middle; -in vec4 shadowCoord_far; +in vec4 shadowCoord; out vec4 oColor; uniform sampler2D uTexture; -uniform sampler2DShadow shadowMap_near; -uniform sampler2DShadow shadowMap_middle; -uniform sampler2DShadow shadowMap_far; +uniform sampler2DShadow shadowMap; uniform samplerCubeShadow shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; @@ -116,17 +112,7 @@ void main() } // shadows - if (distanceToBorder(shadowCoord_middle.xy) <= 0.5 && distanceToBorder(shadowCoord_middle.xy) > 0.0) { - if (distanceToBorder(shadowCoord_near.xy) <= 0.5 && distanceToBorder(shadowCoord_near.xy) > 0.0) { - visibility *= sampleDirectionalShadow(shadowMap_near, shadowCoord_near); - } - else { - visibility *= sampleDirectionalShadow(shadowMap_middle, shadowCoord_middle); - } - } - else { - visibility *= sampleDirectionalShadow(shadowMap_far, shadowCoord_far); - } + visibility *= sampleDirectionalShadow(shadowMap, shadowCoord); specularColor *= visibility; diffuseColor *= visibility; diff --git a/Shader/phong.vsh b/Shader/phong.vsh index 11d9863..f0361a8 100644 --- a/Shader/phong.vsh +++ b/Shader/phong.vsh @@ -11,17 +11,13 @@ in vec2 aTexCoord; out vec3 vNormal; out vec2 vTexCoord; out vec4 fragPosition; -out vec4 shadowCoord_near; -out vec4 shadowCoord_middle; -out vec4 shadowCoord_far; +out vec4 shadowCoord; void main() { fragPosition = modelMatrix * vec4(aPosition, 1.0); vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal; vTexCoord = aTexCoord; - shadowCoord_near = shadowMVPs[0] * vec4(aPosition, 1.0); - shadowCoord_middle = shadowMVPs[1] * vec4(aPosition, 1.0); - shadowCoord_far = shadowMVPs[2] * vec4(aPosition, 1.0); + shadowCoord = shadowMVPs[0] * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/graphics.cc b/graphics.cc index f373ae1..f3be9d8 100644 --- a/graphics.cc +++ b/graphics.cc @@ -37,38 +37,16 @@ void Graphics::init(Level* level) { depthShader = ShaderProgramCreator("depth") .attributeLocations(vao->getAttributeLocations()).create(); - depthTexture_near = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); - depthTexture_near->setMinFilter(GL_NEAREST); - depthTexture_near->setMagFilter(GL_NEAREST); - depthTexture_near->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_near->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_near->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); + depthTexture->setMinFilter(GL_NEAREST); + depthTexture->setMagFilter(GL_NEAREST); + depthTexture->setWrapS(GL_CLAMP_TO_EDGE); + depthTexture->setWrapT(GL_CLAMP_TO_EDGE); + depthTexture->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); - framebuffer_near = SharedFrameBufferObject(new FrameBufferObject()); - framebuffer_near->setDepthTexture(depthTexture_near); - framebuffer_near->validate(); - - depthTexture_middle = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); - depthTexture_middle->setMinFilter(GL_NEAREST); - depthTexture_middle->setMagFilter(GL_NEAREST); - depthTexture_middle->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_middle->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_middle->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); - - framebuffer_middle = SharedFrameBufferObject(new FrameBufferObject()); - framebuffer_middle->setDepthTexture(depthTexture_middle); - framebuffer_middle->validate(); - - depthTexture_far = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); - depthTexture_far->setMinFilter(GL_NEAREST); - depthTexture_far->setMagFilter(GL_NEAREST); - depthTexture_far->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_far->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_far->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); - - framebuffer_far = SharedFrameBufferObject(new FrameBufferObject()); - framebuffer_far->setDepthTexture(depthTexture_far); - framebuffer_far->validate(); + framebuffer = SharedFrameBufferObject(new FrameBufferObject()); + framebuffer->setDepthTexture(depthTexture); + framebuffer->validate(); /*depth_cubeMaps = std::vector(level->getLights()->size()); for (unsigned int i = 0; ibind(); - glClear(GL_DEPTH_BUFFER_BIT); - glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition()); - glm::mat4 depthViewProjectionMatrix_near = glm::ortho(-5, 5, -5, 5, -5, 5) * - glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - level->render(depthShader, false, &depthViewProjectionMatrix_near); - if (!framebuffer_near->isFrameBufferObjectComplete()) { - printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); - } - - // middle pass - framebuffer_middle->bind(); - glClear(GL_DEPTH_BUFFER_BIT); - glm::mat4 depthViewProjectionMatrix_middle = glm::ortho(-20, 20, -20, 20, -20, 20) * - glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - level->render(depthShader, false, &depthViewProjectionMatrix_middle); - if (!framebuffer_middle->isFrameBufferObjectComplete()) { - printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); - } // far pass - framebuffer_far->bind(); + framebuffer->bind(); glClear(GL_DEPTH_BUFFER_BIT); - glm::mat4 depthViewProjectionMatrix_far = glm::ortho(-farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f) * + glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition()); + glm::mat4 depthViewProjectionMatrix = glm::ortho(-farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f) * glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - level->render(depthShader, false, &depthViewProjectionMatrix_far); - if (!framebuffer_far->isFrameBufferObjectComplete()) { + level->render(depthShader, false, &depthViewProjectionMatrix); + if (!framebuffer->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -204,13 +163,9 @@ void Graphics::render() 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); - glm::mat4 depthBiasVP_near = biasMatrix*depthViewProjectionMatrix_near; - glm::mat4 depthBiasVP_middle = biasMatrix*depthViewProjectionMatrix_middle; - glm::mat4 depthBiasVP_far = biasMatrix*depthViewProjectionMatrix_far; + glm::mat4 depthBiasVP = biasMatrix*depthViewProjectionMatrix; - lightingShader->setTexture("shadowMap_near", depthTexture_near, 1); - lightingShader->setTexture("shadowMap_middle", depthTexture_middle, 2); - lightingShader->setTexture("shadowMap_far", depthTexture_far, 3); + lightingShader->setTexture("shadowMap", depthTexture, 1); lightingShader->setUniform("farPlane", farPlane); @@ -226,9 +181,7 @@ void Graphics::render() glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level); std::vector shadowVPs = std::vector(); - shadowVPs.push_back(depthBiasVP_near); - shadowVPs.push_back(depthBiasVP_middle); - shadowVPs.push_back(depthBiasVP_far); + shadowVPs.push_back(depthBiasVP); // render the level level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); @@ -236,9 +189,7 @@ void Graphics::render() void Graphics::resize(glm::uvec2 windowSize) { this->windowSize = windowSize; - depthTexture_near->resize(glm::vec2(windowSize.x, windowSize.y)); - depthTexture_middle->resize(glm::vec2(windowSize.x, windowSize.y)); - depthTexture_far->resize(glm::vec2(windowSize.x, windowSize.y)); + depthTexture->resize(glm::vec2(windowSize.x, windowSize.y)); } glm::mat4 Graphics::buildViewMatrix(Level* level) { diff --git a/graphics.hh b/graphics.hh index 416f5ce..bb37a19 100644 --- a/graphics.hh +++ b/graphics.hh @@ -28,12 +28,8 @@ class Graphics { GLFWwindow* window; ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram depthShader; - ACGL::OpenGL::SharedTexture2D depthTexture_near; - ACGL::OpenGL::SharedFrameBufferObject framebuffer_near; - ACGL::OpenGL::SharedTexture2D depthTexture_middle; - ACGL::OpenGL::SharedFrameBufferObject framebuffer_middle; - ACGL::OpenGL::SharedTexture2D depthTexture_far; - ACGL::OpenGL::SharedFrameBufferObject framebuffer_far; + ACGL::OpenGL::SharedTexture2D depthTexture; + ACGL::OpenGL::SharedFrameBufferObject framebuffer; std::vector depth_cubeMaps; ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; static const int cube_size;