diff --git a/Shader/phong.fsh b/Shader/phong.fsh index af8a2e8..bd723e3 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -13,6 +13,7 @@ uniform sampler2D uTexture; uniform sampler2DShadow shadowMap_near; uniform sampler2DShadow shadowMap_middle; uniform sampler2DShadow shadowMap_far; +uniform samplerCubeShadow shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -49,7 +50,7 @@ vec2 poissonDisk[16] = vec2[]( vec2( 0.14383161, -0.14100790 ) ); -float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { +float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { float visibility = 1.0; float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0))); bias = clamp(bias, 0.0, 0.01); @@ -68,6 +69,11 @@ float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { return visibility; } +float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { + float bias = 0.005; + return texture(shadowMap, vec4(lightDirection.xyz , length(lightDirection) - bias)); +} + float distanceToBorder(vec2 vector) { float xDistance = min(vector.x, 1.0-vector.x); float yDistance = min(vector.y, 1.0-vector.y); @@ -92,8 +98,10 @@ void main() } // point lights + float visibility = 1.0; for(int i = 0; i 0.001f) { vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); @@ -103,21 +111,21 @@ void main() vec3 cameraVector = normalize(camera - vec3(fragPosition)); specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; + visibility = samplePointShadow(shadowMap_cube, lightDirection); } } // shadows - float visibility = 1.0; 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 = sampleShadow(shadowMap_near, shadowCoord_near); + visibility *= sampleDirectionalShadow(shadowMap_near, shadowCoord_near); } else { - visibility = sampleShadow(shadowMap_middle, shadowCoord_middle); + visibility *= sampleDirectionalShadow(shadowMap_middle, shadowCoord_middle); } } else { - visibility = sampleShadow(shadowMap_far, shadowCoord_far); + visibility *= sampleDirectionalShadow(shadowMap_far, shadowCoord_far); } specularColor *= visibility; diff --git a/graphics.cc b/graphics.cc index 8f35b41..834f97a 100644 --- a/graphics.cc +++ b/graphics.cc @@ -70,8 +70,10 @@ void Graphics::init(Level* level) { framebuffer_far->setDepthTexture(depthTexture_far); framebuffer_far->validate(); - depth_cubeMaps = std::vector(level->getLights()->size()); - for (unsigned int i = 0; i(level->getLights()->size()); + for (unsigned int i = 0; i(1); + for (unsigned int i = 0; i<1; i++) { depth_cubeMaps.at(i) = SharedTextureCubeMap(new TextureCubeMap(glm::vec2(cube_size, cube_size), GL_DEPTH_COMPONENT16)); depth_cubeMaps.at(i)->setMinFilter(GL_NEAREST); depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); @@ -102,14 +104,14 @@ void Graphics::render() 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_pointlightgetLights()->size(); i_pointlight++) { + for (unsigned int i_pointlight = 0; i_pointlight<1; i_pointlight++) { // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0); glClear(GL_DEPTH_BUFFER_BIT); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f)); - depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_face); level->render(depthShader, false, &depthViewProjectionMatrix_face); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); @@ -182,6 +184,8 @@ void Graphics::render() } glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); + + lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4); } // set directional Light if(level->getDirectionalLight()) {