diff --git a/Shader/depth.fsh b/Shader/depth.fsh index b236c6a..ee23911 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -1,7 +1,11 @@ #version 150 +in vec4 fragPosition; + out float fragmentDepth; +uniform vec3 cameraPosition; + void main() { - fragmentDepth = gl_FragCoord.z; + fragmentDepth = length(vec3(fragPosition) - cameraPosition); } diff --git a/Shader/depth.vsh b/Shader/depth.vsh index f84c3cf..52d08f5 100644 --- a/Shader/depth.vsh +++ b/Shader/depth.vsh @@ -4,8 +4,12 @@ in vec3 aPosition; in vec3 aNormal; in vec3 aTexcoord; +out vec4 fragPosition; + +uniform mat4 modelMatrix; uniform mat4 modelViewProjectionMatrix; void main() { + fragPosition = modelMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 71f4121..d9b5dbd 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -67,7 +67,9 @@ float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float bias = 0.005; - return texture(shadowMap, vec4(lightDirection.xyz , length(lightDirection) - bias)); + float nearPlane = 0.1; + float linValue = (2.0*nearPlane) / (farPlane + nearPlane - (length(lightDirection)-nearPlane)/(farPlane-nearPlane) * (farPlane - nearPlane)); + return texture(shadowMap, vec4(lightDirection.xyz , 0.5/*length(lightDirection)-bias*/)); } float distanceToBorder(vec2 vector) { diff --git a/graphics.cc b/graphics.cc index 0f5fc6f..12aab8b 100644 --- a/graphics.cc +++ b/graphics.cc @@ -86,13 +86,14 @@ void Graphics::render() depthShader->use(); // 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); + glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.01f, farPlane); glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; framebuffer_cube->bind(); //for (unsigned int i_pointlight = 0; i_pointlightgetLights()->size(); i_pointlight++) { for (unsigned int i_pointlight = 0; i_pointlight<1 && i_pointlightgetLights()->size(); i_pointlight++) { + depthShader->setUniform("cameraPosition", level->getLights()->at(i_pointlight).getPosition()); // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { framebuffer_cube2->bind(); @@ -162,6 +163,8 @@ void Graphics::render() glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); + // Calculate light source position in camera screen space + lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4); } // set directional Light diff --git a/object.cc b/object.cc index 9d1f339..35f4194 100644 --- a/object.cc +++ b/object.cc @@ -22,8 +22,6 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, shader->setUniform("specularFactor", material.getSpecularFactor()); shader->setUniform("shininess", material.getShininess()); shader->setTexture("uTexture", material.getReference(), 0); - // set model matrix - shader->setUniform( "modelMatrix", modelMatrix); // set shadowMVPs glm::mat4 shadowMVPs[5]; for(unsigned int i = 0; (isize() && i<5); i++) { @@ -32,6 +30,8 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glUniformMatrix4fv(shader->getUniformLocation("shadowMVPs"), sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs); } + // set model matrix + shader->setUniform( "modelMatrix", modelMatrix); glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix; shader->setUniform("modelViewProjectionMatrix", mvp); // draw