Point light shadows for the first point light does something. Seems pretty broken.

This commit is contained in:
Faerbit 2014-12-15 15:58:55 +01:00
parent df182abd06
commit 05cac9c520
2 changed files with 22 additions and 10 deletions

View File

@ -13,6 +13,7 @@ uniform sampler2D uTexture;
uniform sampler2DShadow shadowMap_near; uniform sampler2DShadow shadowMap_near;
uniform sampler2DShadow shadowMap_middle; uniform sampler2DShadow shadowMap_middle;
uniform sampler2DShadow shadowMap_far; uniform sampler2DShadow shadowMap_far;
uniform samplerCubeShadow shadowMap_cube;
uniform vec3 ambientColor; uniform vec3 ambientColor;
uniform float ambientFactor; uniform float ambientFactor;
uniform float diffuseFactor; uniform float diffuseFactor;
@ -49,7 +50,7 @@ vec2 poissonDisk[16] = vec2[](
vec2( 0.14383161, -0.14100790 ) vec2( 0.14383161, -0.14100790 )
); );
float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) {
float visibility = 1.0; float visibility = 1.0;
float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0))); float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0)));
bias = clamp(bias, 0.0, 0.01); bias = clamp(bias, 0.0, 0.01);
@ -68,6 +69,11 @@ float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) {
return visibility; 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 distanceToBorder(vec2 vector) {
float xDistance = min(vector.x, 1.0-vector.x); float xDistance = min(vector.x, 1.0-vector.x);
float yDistance = min(vector.y, 1.0-vector.y); float yDistance = min(vector.y, 1.0-vector.y);
@ -92,8 +98,10 @@ void main()
} }
// point lights // point lights
float visibility = 1.0;
for(int i = 0; i<lightCount; i++) { for(int i = 0; i<lightCount; i++) {
float distance = distance(lightSources[i], vec3(fragPosition)); vec3 lightDirection = vec3(fragPosition) - lightSources[i];
float distance = length(lightDirection);
// only take lights into account with meaningful contribution // only take lights into account with meaningful contribution
if (distance > 0.001f) { if (distance > 0.001f) {
vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition));
@ -103,21 +111,21 @@ void main()
vec3 cameraVector = normalize(camera - vec3(fragPosition)); 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) specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0)
*specularFactor*intensity*lightColors[i]; *specularFactor*intensity*lightColors[i];
visibility = samplePointShadow(shadowMap_cube, lightDirection);
} }
} }
// shadows // shadows
float visibility = 1.0;
if (distanceToBorder(shadowCoord_middle.xy) <= 0.5 && distanceToBorder(shadowCoord_middle.xy) > 0.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) { 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 { else {
visibility = sampleShadow(shadowMap_middle, shadowCoord_middle); visibility *= sampleDirectionalShadow(shadowMap_middle, shadowCoord_middle);
} }
} }
else { else {
visibility = sampleShadow(shadowMap_far, shadowCoord_far); visibility *= sampleDirectionalShadow(shadowMap_far, shadowCoord_far);
} }
specularColor *= visibility; specularColor *= visibility;

View File

@ -70,8 +70,10 @@ void Graphics::init(Level* level) {
framebuffer_far->setDepthTexture(depthTexture_far); framebuffer_far->setDepthTexture(depthTexture_far);
framebuffer_far->validate(); framebuffer_far->validate();
depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(level->getLights()->size()); /*depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(level->getLights()->size());
for (unsigned int i = 0; i<depth_cubeMaps.size(); i++) { for (unsigned int i = 0; i<depth_cubeMaps.size(); i++) {*/
depth_cubeMaps = std::vector<ACGL::OpenGL::SharedTextureCubeMap>(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) = SharedTextureCubeMap(new TextureCubeMap(glm::vec2(cube_size, cube_size), GL_DEPTH_COMPONENT16));
depth_cubeMaps.at(i)->setMinFilter(GL_NEAREST); depth_cubeMaps.at(i)->setMinFilter(GL_NEAREST);
depth_cubeMaps.at(i)->setMagFilter(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)}; 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(); framebuffer_cube->bind();
for (unsigned int i_pointlight = 0; i_pointlight<level->getLights()->size(); i_pointlight++) { //for (unsigned int i_pointlight = 0; i_pointlight<level->getLights()->size(); i_pointlight++) {
for (unsigned int i_pointlight = 0; i_pointlight<1; i_pointlight++) {
// render each side of the cube // render each side of the cube
for (int i_face = 0; i_face<6; i_face++) { 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); 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); glClear(GL_DEPTH_BUFFER_BIT);
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), 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)); 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); level->render(depthShader, false, &depthViewProjectionMatrix_face);
if (!framebuffer_cube->isFrameBufferObjectComplete()) { if (!framebuffer_cube->isFrameBufferObjectComplete()) {
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
@ -182,6 +184,8 @@ void Graphics::render()
} }
glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), glUniform1fv(lightingShader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities); sizeof(lightIntensities), (GLfloat*) lightIntensities);
lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4);
} }
// set directional Light // set directional Light
if(level->getDirectionalLight()) { if(level->getDirectionalLight()) {