From 7f13e899351c024b4798d1084df80c68a6a8d0ed Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 26 Jan 2015 21:48:44 +0100 Subject: [PATCH 01/21] Implemented saving depthbuffer face to png. Doesn't work right now. --- graphics.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- graphics.hh | 3 +++ main.cc | 5 +++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/graphics.cc b/graphics.cc index 5384fc4..68a1b20 100644 --- a/graphics.cc +++ b/graphics.cc @@ -1,4 +1,5 @@ #include "graphics.hh" +#include "lodepng.h" #include #include @@ -72,12 +73,18 @@ void Graphics::init(Level* level) { depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE); depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + + saveDepthBufferBool = false; } glm::uvec2 Graphics::getWindowSize() { return windowSize; } +void Graphics::saveDepthBuffer() { + saveDepthBufferBool = true; +} + void Graphics::render(double time) { // At first render shadows @@ -85,10 +92,11 @@ void Graphics::render(double time) // 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::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)}; + glm::vec3 looking_directions[6] = {glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0001f, 1.0f, 0.0f), + glm::vec3(0.0001f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; framebuffer_cube->bind(); + static bool printed = false; //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++) { // render each side of the cube @@ -97,10 +105,26 @@ void Graphics::render(double time) 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)); + if (!printed) { + printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", + depthViewProjectionMatrix_face[0][0], depthViewProjectionMatrix_face[0][1], depthViewProjectionMatrix_face[0][2], depthViewProjectionMatrix_face[0][3], + depthViewProjectionMatrix_face[1][0], depthViewProjectionMatrix_face[1][1], depthViewProjectionMatrix_face[1][2], depthViewProjectionMatrix_face[1][3], + depthViewProjectionMatrix_face[2][0], depthViewProjectionMatrix_face[2][1], depthViewProjectionMatrix_face[2][2], depthViewProjectionMatrix_face[2][3], + depthViewProjectionMatrix_face[3][0], depthViewProjectionMatrix_face[3][1], depthViewProjectionMatrix_face[3][2], depthViewProjectionMatrix_face[3][3] + ); + } level->render(depthShader, false, &depthViewProjectionMatrix_face); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } + if (saveDepthBufferBool && i_face == 2) { + printf("Doing stuff...\n"); + saveDepthBufferToDisk(framebuffer_cube, "face2.png"); + saveDepthBufferBool = false; + } + } + if (!printed) { + printed = true; } } // render depth texture for sun @@ -223,3 +247,25 @@ glm::mat4 Graphics::buildViewMatrix(Level* level) { float Graphics::getFarPlane() { return farPlane; } + +void Graphics::saveDepthBufferToDisk(SharedFrameBufferObject fbo, std::string filename) { + printf("Starting saving of depth buffer...\n"); + float *depthbuffer = new float[windowSize.x * windowSize.y]; + std::vector image (windowSize.x * windowSize.y * 4); + + glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); + for (unsigned int i = 0; isaveDepthBuffer(); + } + app.getGraphics()->render(now); lastUpdate = now; From 377372d1c65ccaa4199a8e2cd7b485f06e8ff3ac Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 26 Jan 2015 23:11:04 +0100 Subject: [PATCH 02/21] Fixed saving of depthbuffer. --- Levels/ObjectSetups/Compositions.xml | 2 +- .../BroadphaseCollision/btQuantizedBvh.h | 2 +- graphics.cc | 20 +++++++++---------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Levels/ObjectSetups/Compositions.xml b/Levels/ObjectSetups/Compositions.xml index 015900a..8df8ad9 100644 --- a/Levels/ObjectSetups/Compositions.xml +++ b/Levels/ObjectSetups/Compositions.xml @@ -66,7 +66,7 @@ 0.0 - 3 + 2 0.0 1.0 1.0 diff --git a/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h b/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h index 78382da..5f8007d 100644 --- a/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h +++ b/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h @@ -354,7 +354,7 @@ public: btAssert(m_useQuantization); - btAssert(point.getX() <= m_bvhAabbMax.getX()); + //btAssert(point.getX() <= m_bvhAabbMax.getX()); btAssert(point.getY() <= m_bvhAabbMax.getY()); btAssert(point.getZ() <= m_bvhAabbMax.getZ()); diff --git a/graphics.cc b/graphics.cc index 68a1b20..c4fc5a2 100644 --- a/graphics.cc +++ b/graphics.cc @@ -117,7 +117,7 @@ void Graphics::render(double time) if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } - if (saveDepthBufferBool && i_face == 2) { + if (saveDepthBufferBool && i_face == 3) { printf("Doing stuff...\n"); saveDepthBufferToDisk(framebuffer_cube, "face2.png"); saveDepthBufferBool = false; @@ -250,17 +250,17 @@ float Graphics::getFarPlane() { void Graphics::saveDepthBufferToDisk(SharedFrameBufferObject fbo, std::string filename) { printf("Starting saving of depth buffer...\n"); - float *depthbuffer = new float[windowSize.x * windowSize.y]; - std::vector image (windowSize.x * windowSize.y * 4); + float *depthbuffer = new float[1024*1024]; + std::vector image (1024 * 1024 * 4); - glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); - for (unsigned int i = 0; i Date: Tue, 27 Jan 2015 11:14:00 +0100 Subject: [PATCH 03/21] Small parameter changes. --- graphics.cc | 7 +++---- graphics.hh | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/graphics.cc b/graphics.cc index c4fc5a2..d90c834 100644 --- a/graphics.cc +++ b/graphics.cc @@ -118,8 +118,7 @@ void Graphics::render(double time) printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } if (saveDepthBufferBool && i_face == 3) { - printf("Doing stuff...\n"); - saveDepthBufferToDisk(framebuffer_cube, "face2.png"); + saveDepthBufferToDisk(3, "face.png"); saveDepthBufferBool = false; } } @@ -248,12 +247,12 @@ float Graphics::getFarPlane() { return farPlane; } -void Graphics::saveDepthBufferToDisk(SharedFrameBufferObject fbo, std::string filename) { +void Graphics::saveDepthBufferToDisk(int face, std::string filename) { printf("Starting saving of depth buffer...\n"); float *depthbuffer = new float[1024*1024]; std::vector image (1024 * 1024 * 4); - glGetTexImage(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); + glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); for (unsigned int i = 0; i<1024*1024; i++) { image[i * 4 + 0] = depthbuffer[i] * 255; image[i * 4 + 1] = depthbuffer[i] * 255; diff --git a/graphics.hh b/graphics.hh index c0ceabb..270ecbd 100644 --- a/graphics.hh +++ b/graphics.hh @@ -22,7 +22,7 @@ class Graphics { private: bool saveDepthBufferBool; void updateLights(); - void saveDepthBufferToDisk(ACGL::OpenGL::SharedFrameBufferObject fbo, std::string); + void saveDepthBufferToDisk(int face, std::string); double lastUpdate; glm::uvec2 windowSize; float nearPlane; From a3e6da2adaad3ec3587ce176db2da4c07952234f Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 28 Jan 2015 17:26:02 +0100 Subject: [PATCH 04/21] Now mirroring the cube face on their own again. --- Shader/phong.fsh | 5 +++-- graphics.cc | 38 +++++++++++++++++++++++++++++++------- graphics.hh | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 447b1fe..f2756d4 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -71,8 +71,7 @@ float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; float bias = 0.005; - vec3 vector = vec3(-lightDirection.x, -lightDirection.y, lightDirection.z); - return texture(shadowMap, vec4(vector , compValue - bias)); + return texture(shadowMap, vec4(lightDirection , compValue - bias)); } float distanceToBorder(vec2 vector) { @@ -114,6 +113,8 @@ void main() *specularFactor*intensity*lightColors[i]; visibility = samplePointShadow(shadowMap_cube, lightDirection); } + /*float value = texture(shadowMap_cube, lightDirection); + oColor = vec4(value, value, value, 255);*/ } // shadows diff --git a/graphics.cc b/graphics.cc index d90c834..8422f49 100644 --- a/graphics.cc +++ b/graphics.cc @@ -74,6 +74,9 @@ void Graphics::init(Level* level) { depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + framebuffer_cube_mirror = SharedFrameBufferObject(new FrameBufferObject()); + framebuffer_cube_mirror->setDepthTexture(depthTexture_cube); + saveDepthBufferBool = false; } @@ -92,7 +95,7 @@ void Graphics::render(double time) // 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::vec3 looking_directions[6] = {glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0001f, 1.0f, 0.0f), + glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0001f, 1.0f, 0.0f), glm::vec3(0.0001f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; framebuffer_cube->bind(); @@ -101,19 +104,29 @@ void Graphics::render(double time) for (unsigned int i_pointlight = 0; i_pointlight<1 && i_pointlightgetLights()->size(); i_pointlight++) { // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { + framebuffer_cube_mirror->bind(); + glClear(GL_DEPTH_BUFFER_BIT); + framebuffer_cube->bind(); 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(), + glm::mat4 viewMatrix = 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)); + glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; if (!printed) { printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", - depthViewProjectionMatrix_face[0][0], depthViewProjectionMatrix_face[0][1], depthViewProjectionMatrix_face[0][2], depthViewProjectionMatrix_face[0][3], - depthViewProjectionMatrix_face[1][0], depthViewProjectionMatrix_face[1][1], depthViewProjectionMatrix_face[1][2], depthViewProjectionMatrix_face[1][3], - depthViewProjectionMatrix_face[2][0], depthViewProjectionMatrix_face[2][1], depthViewProjectionMatrix_face[2][2], depthViewProjectionMatrix_face[2][3], - depthViewProjectionMatrix_face[3][0], depthViewProjectionMatrix_face[3][1], depthViewProjectionMatrix_face[3][2], depthViewProjectionMatrix_face[3][3] + viewMatrix[0][0], viewMatrix[0][1], viewMatrix[0][2], viewMatrix[0][3], + viewMatrix[1][0], viewMatrix[1][1], viewMatrix[1][2], viewMatrix[1][3], + viewMatrix[2][0], viewMatrix[2][1], viewMatrix[2][2], viewMatrix[2][3], + viewMatrix[3][0], viewMatrix[3][1], viewMatrix[3][2], viewMatrix[3][3] ); } level->render(depthShader, false, &depthViewProjectionMatrix_face); + glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube->getObjectName()); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); + glBlitFramebuffer(0, 0, cube_size, cube_size, cube_size, cube_size, 0, 0, GL_DEPTH_BUFFER_BIT, GL_NEAREST); + glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube->getObjectName()); + glBlitFramebuffer(0, 0, cube_size, cube_size, 0, 0, cube_size, cube_size, GL_DEPTH_BUFFER_BIT, GL_NEAREST); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -238,8 +251,19 @@ glm::mat4 Graphics::buildViewMatrix(Level* level) { //construct lookAt (cameraPosition = cameraCenter + cameraVector) //return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); - return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), + glm::mat4 depthViewProjectionMatrix_face = glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); + static int i = 5; + if (i == 0) { + printf("\n\nView matrix Camera:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", + depthViewProjectionMatrix_face[0][0], depthViewProjectionMatrix_face[0][1], depthViewProjectionMatrix_face[0][2], depthViewProjectionMatrix_face[0][3], + depthViewProjectionMatrix_face[1][0], depthViewProjectionMatrix_face[1][1], depthViewProjectionMatrix_face[1][2], depthViewProjectionMatrix_face[1][3], + depthViewProjectionMatrix_face[2][0], depthViewProjectionMatrix_face[2][1], depthViewProjectionMatrix_face[2][2], depthViewProjectionMatrix_face[2][3], + depthViewProjectionMatrix_face[3][0], depthViewProjectionMatrix_face[3][1], depthViewProjectionMatrix_face[3][2], depthViewProjectionMatrix_face[3][3] + ); + } + i--; + return depthViewProjectionMatrix_face; } diff --git a/graphics.hh b/graphics.hh index 270ecbd..9a471d4 100644 --- a/graphics.hh +++ b/graphics.hh @@ -33,6 +33,7 @@ class Graphics { ACGL::OpenGL::SharedFrameBufferObject framebuffer; std::vector depth_cubeMaps; ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; + ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube_mirror; ACGL::OpenGL::SharedTexture2D depthTexture_cube; static const int cube_size; Level* level; From 60372cf1a17a1b7346af1f1bbe4f3036e6e0bce6 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 30 Jan 2015 15:11:00 +0100 Subject: [PATCH 05/21] Only sample shadows from the first light source. --- Shader/phong.fsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index f2756d4..337fc63 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -111,7 +111,9 @@ 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); + if (i == 0) { + visibility = samplePointShadow(shadowMap_cube, lightDirection); + } } /*float value = texture(shadowMap_cube, lightDirection); oColor = vec4(value, value, value, 255);*/ From 2ddd7535752603145826a01cc9479059a453f10c Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 30 Jan 2015 15:47:07 +0100 Subject: [PATCH 06/21] Additional debug stuff. --- Shader/phong.fsh | 21 +++++++++++++++------ application.cc | 2 +- graphics.cc | 13 +++++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 337fc63..978983b 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -9,7 +9,7 @@ out vec4 oColor; uniform sampler2D uTexture; uniform sampler2DShadow shadowMap; -uniform samplerCubeShadow shadowMap_cube; +uniform samplerCube shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -65,13 +65,15 @@ float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { return visibility; } -float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { +float samplePointShadow(samplerCube shadowMap, vec3 lightDirection) { float nearPlane = 0.1; float A = -(farPlane+nearPlane)/(farPlane-nearPlane); float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; float bias = 0.005; - return texture(shadowMap, vec4(lightDirection , compValue - bias)); + //return texture(shadowMap, vec4(lightDirection , compValue - bias)); + float value1 = texture(shadowMap, vec3(lightDirection)); + return value1 - compValue; } float distanceToBorder(vec2 vector) { @@ -112,7 +114,14 @@ void main() specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; if (i == 0) { - visibility = samplePointShadow(shadowMap_cube, lightDirection); + //visibility = samplePointShadow(shadowMap_cube, lightDirection); + float value = samplePointShadow(shadowMap_cube, lightDirection); + if (abs(value) < 0.0001f) { + oColor = vec4(0, 255, 0, 255); + } + else { + oColor = vec4(255, 0, 0, 255); + } } } /*float value = texture(shadowMap_cube, lightDirection); @@ -131,6 +140,6 @@ void main() fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0); vec4 texture = texture(uTexture, vTexCoord).rgba; - oColor = vec4(finalColor, 1.0f)*texture; - oColor = mix(oColor, fogColor, fogFactor); + //oColor = vec4(finalColor, 1.0f)*texture; + //oColor = mix(oColor, fogColor, fogFactor); } diff --git a/application.cc b/application.cc index ff80cac..b2b00f9 100644 --- a/application.cc +++ b/application.cc @@ -2,7 +2,7 @@ Application::Application() { - graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f); + graphics = Graphics(glm::uvec2(1024, 1024), 0.1f, 150.0f); } void Application::init() diff --git a/graphics.cc b/graphics.cc index 8422f49..81fbb71 100644 --- a/graphics.cc +++ b/graphics.cc @@ -62,7 +62,7 @@ void Graphics::init(Level* level) { depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); depth_cubeMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE); depth_cubeMaps.at(i)->setWrapT(GL_CLAMP_TO_EDGE); - depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + //depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); } framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); @@ -72,7 +72,7 @@ void Graphics::init(Level* level) { depthTexture_cube->setMagFilter(GL_NEAREST); depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE); depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + //depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); framebuffer_cube_mirror = SharedFrameBufferObject(new FrameBufferObject()); framebuffer_cube_mirror->setDepthTexture(depthTexture_cube); @@ -100,6 +100,7 @@ void Graphics::render(double time) framebuffer_cube->bind(); static bool printed = false; + glm::mat4 reproduceMatrix; //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++) { // render each side of the cube @@ -112,6 +113,9 @@ void Graphics::render(double time) glm::mat4 viewMatrix = 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)); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; + if (i_face == 0) { + reproduceMatrix = depthViewProjectionMatrix_face; + } if (!printed) { printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", viewMatrix[0][0], viewMatrix[0][1], viewMatrix[0][2], viewMatrix[0][3], @@ -140,7 +144,7 @@ void Graphics::render(double time) } } // render depth texture for sun - glViewport(0, 0, windowSize.x, windowSize.y); + //glViewport(0, 0, windowSize.x, windowSize.y); // far pass framebuffer->bind(); @@ -202,7 +206,8 @@ void Graphics::render(double time) shadowVPs.push_back(depthBiasVP); // render the level - level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); + //level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); + level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); } void Graphics::updateLights() { From b8dc153f10a38b25145f757dc794da8946794647 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 31 Jan 2015 20:02:23 +0100 Subject: [PATCH 07/21] Tried correcting an error. Unsure if it will work. --- Shader/depth.fsh | 4 +++- Shader/depth.vsh | 4 ++++ graphics.cc | 4 +++- object.cc | 5 ++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index b236c6a..a2eab59 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -1,7 +1,9 @@ #version 150 +in vec4 fragPosition; + out float fragmentDepth; void main() { - fragmentDepth = gl_FragCoord.z; + fragmentDepth = fragPosition.z; } diff --git a/Shader/depth.vsh b/Shader/depth.vsh index f84c3cf..2618bfa 100644 --- a/Shader/depth.vsh +++ b/Shader/depth.vsh @@ -5,7 +5,11 @@ in vec3 aNormal; in vec3 aTexcoord; uniform mat4 modelViewProjectionMatrix; +uniform mat4 modelViewMatrix; + +out vec4 fragPosition; void main() { + fragPosition = modelViewMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/graphics.cc b/graphics.cc index 8422f49..f21457e 100644 --- a/graphics.cc +++ b/graphics.cc @@ -120,7 +120,9 @@ void Graphics::render(double time) viewMatrix[3][0], viewMatrix[3][1], viewMatrix[3][2], viewMatrix[3][3] ); } - level->render(depthShader, false, &depthViewProjectionMatrix_face); + std::vector vector = std::vector(); + vector.push_back(viewMatrix); + level->render(depthShader, false, &depthViewProjectionMatrix_face, &vector); glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube->getObjectName()); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); glBlitFramebuffer(0, 0, cube_size, cube_size, cube_size, cube_size, 0, 0, GL_DEPTH_BUFFER_BIT, GL_NEAREST); diff --git a/object.cc b/object.cc index 9d1f339..e6f7825 100644 --- a/object.cc +++ b/object.cc @@ -23,7 +23,7 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, shader->setUniform("shininess", material.getShininess()); shader->setTexture("uTexture", material.getReference(), 0); // set model matrix - shader->setUniform( "modelMatrix", modelMatrix); + shader->setUniform("modelMatrix", modelMatrix); // set shadowMVPs glm::mat4 shadowMVPs[5]; for(unsigned int i = 0; (isize() && i<5); i++) { @@ -32,6 +32,9 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glUniformMatrix4fv(shader->getUniformLocation("shadowMVPs"), sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs); } + else { + //shader->setUniform("modelViewMatrix", shadowVPs->at(0) * modelMatrix); + } glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix; shader->setUniform("modelViewProjectionMatrix", mvp); // draw From b3e0d5ca47467a83eea51e731b01bc6eb9587bb5 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 2 Feb 2015 19:58:16 +0100 Subject: [PATCH 08/21] Changed how depth values are written. Works even less than before. --- Shader/depth.fsh | 5 +++-- Shader/phong.fsh | 4 ++-- graphics.cc | 6 +++--- object.cc | 4 +++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index a2eab59..5c868ce 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -2,8 +2,9 @@ in vec4 fragPosition; -out float fragmentDepth; +out float gl_FragDepth; void main() { - fragmentDepth = fragPosition.z; + gl_FragDepth = fragPosition.z; + //gl_FragDepth = 0.0f; } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 978983b..93f00cb 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -73,7 +73,7 @@ float samplePointShadow(samplerCube shadowMap, vec3 lightDirection) { float bias = 0.005; //return texture(shadowMap, vec4(lightDirection , compValue - bias)); float value1 = texture(shadowMap, vec3(lightDirection)); - return value1 - compValue; + return value1 - length(lightDirection); } float distanceToBorder(vec2 vector) { @@ -116,7 +116,7 @@ void main() if (i == 0) { //visibility = samplePointShadow(shadowMap_cube, lightDirection); float value = samplePointShadow(shadowMap_cube, lightDirection); - if (abs(value) < 0.0001f) { + if (abs(value) < 0.1f) { oColor = vec4(0, 255, 0, 255); } else { diff --git a/graphics.cc b/graphics.cc index eb26598..2c36483 100644 --- a/graphics.cc +++ b/graphics.cc @@ -124,9 +124,9 @@ void Graphics::render(double time) viewMatrix[3][0], viewMatrix[3][1], viewMatrix[3][2], viewMatrix[3][3] ); } - std::vector vector = std::vector(); - vector.push_back(viewMatrix); - level->render(depthShader, false, &depthViewProjectionMatrix_face, &vector); + std::vector foovector = std::vector(); + foovector.push_back(viewMatrix); + level->render(depthShader, false, &depthViewProjectionMatrix_face, &foovector); glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube->getObjectName()); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); glBlitFramebuffer(0, 0, cube_size, cube_size, cube_size, cube_size, 0, 0, GL_DEPTH_BUFFER_BIT, GL_NEAREST); diff --git a/object.cc b/object.cc index e6f7825..d3e156c 100644 --- a/object.cc +++ b/object.cc @@ -33,7 +33,9 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs); } else { - //shader->setUniform("modelViewMatrix", shadowVPs->at(0) * modelMatrix); + if (shadowVPs) { + shader->setUniform("modelViewMatrix", shadowVPs->at(0) * modelMatrix); + } } glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix; shader->setUniform("modelViewProjectionMatrix", mvp); From 2e90ac57d83b3324aaf6e8bb1e41c8fb01b608ec Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 14:27:41 +0100 Subject: [PATCH 09/21] Got Point Light Shadows working. YEEHAW! --- Shader/depth.fsh | 6 ++++-- Shader/phong.fsh | 21 ++++++--------------- application.cc | 2 +- graphics.cc | 37 ++++++++++++++++++------------------- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index 5c868ce..0fcb690 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -2,9 +2,11 @@ in vec4 fragPosition; +uniform float farPlane; + out float gl_FragDepth; void main() { - gl_FragDepth = fragPosition.z; - //gl_FragDepth = 0.0f; + gl_FragDepth = length(fragPosition)/farPlane; + //gl_FragDepth = 1.5f; } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 93f00cb..09a8bb8 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -9,7 +9,7 @@ out vec4 oColor; uniform sampler2D uTexture; uniform sampler2DShadow shadowMap; -uniform samplerCube shadowMap_cube; +uniform samplerCubeShadow shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -65,15 +65,13 @@ float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { return visibility; } -float samplePointShadow(samplerCube shadowMap, vec3 lightDirection) { +float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float nearPlane = 0.1; float A = -(farPlane+nearPlane)/(farPlane-nearPlane); float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; float bias = 0.005; - //return texture(shadowMap, vec4(lightDirection , compValue - bias)); - float value1 = texture(shadowMap, vec3(lightDirection)); - return value1 - length(lightDirection); + return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); } float distanceToBorder(vec2 vector) { @@ -114,14 +112,7 @@ void main() specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; if (i == 0) { - //visibility = samplePointShadow(shadowMap_cube, lightDirection); - float value = samplePointShadow(shadowMap_cube, lightDirection); - if (abs(value) < 0.1f) { - oColor = vec4(0, 255, 0, 255); - } - else { - oColor = vec4(255, 0, 0, 255); - } + visibility = samplePointShadow(shadowMap_cube, lightDirection); } } /*float value = texture(shadowMap_cube, lightDirection); @@ -140,6 +131,6 @@ void main() fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0); vec4 texture = texture(uTexture, vTexCoord).rgba; - //oColor = vec4(finalColor, 1.0f)*texture; - //oColor = mix(oColor, fogColor, fogFactor); + oColor = vec4(finalColor, 1.0f)*texture; + oColor = mix(oColor, fogColor, fogFactor); } diff --git a/application.cc b/application.cc index b2b00f9..ff80cac 100644 --- a/application.cc +++ b/application.cc @@ -2,7 +2,7 @@ Application::Application() { - graphics = Graphics(glm::uvec2(1024, 1024), 0.1f, 150.0f); + graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f); } void Application::init() diff --git a/graphics.cc b/graphics.cc index 2c36483..941b27c 100644 --- a/graphics.cc +++ b/graphics.cc @@ -62,7 +62,7 @@ void Graphics::init(Level* level) { depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); depth_cubeMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE); depth_cubeMaps.at(i)->setWrapT(GL_CLAMP_TO_EDGE); - //depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + depth_cubeMaps.at(i)->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); } framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); @@ -72,7 +72,7 @@ void Graphics::init(Level* level) { depthTexture_cube->setMagFilter(GL_NEAREST); depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE); depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); - //depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); + depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); framebuffer_cube_mirror = SharedFrameBufferObject(new FrameBufferObject()); framebuffer_cube_mirror->setDepthTexture(depthTexture_cube); @@ -92,11 +92,14 @@ void Graphics::render(double time) { // At first render shadows depthShader->use(); + depthShader->setUniform("farPlane", farPlane); // 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::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0001f, 1.0f, 0.0f), - glm::vec3(0.0001f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; + glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)), + glm::normalize(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 upvectors[6] = {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),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)}; framebuffer_cube->bind(); static bool printed = false; @@ -111,28 +114,24 @@ void Graphics::render(double time) 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 viewMatrix = 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], upvectors[i_face]); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; if (i_face == 0) { reproduceMatrix = depthViewProjectionMatrix_face; } if (!printed) { - printf("\n\nView matrix:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", - viewMatrix[0][0], viewMatrix[0][1], viewMatrix[0][2], viewMatrix[0][3], - viewMatrix[1][0], viewMatrix[1][1], viewMatrix[1][2], viewMatrix[1][3], - viewMatrix[2][0], viewMatrix[2][1], viewMatrix[2][2], viewMatrix[2][3], - viewMatrix[3][0], viewMatrix[3][1], viewMatrix[3][2], viewMatrix[3][3] + printf("\n\nView matrix:\n %2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n\n\n", + viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0], viewMatrix[3][0], + viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1], viewMatrix[3][1], + viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2], viewMatrix[3][2], + viewMatrix[0][3], viewMatrix[1][3], viewMatrix[2][3], viewMatrix[3][3] ); + + //std::cout<<"View matrix:" < foovector = std::vector(); foovector.push_back(viewMatrix); level->render(depthShader, false, &depthViewProjectionMatrix_face, &foovector); - glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube->getObjectName()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); - glBlitFramebuffer(0, 0, cube_size, cube_size, cube_size, cube_size, 0, 0, GL_DEPTH_BUFFER_BIT, GL_NEAREST); - glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_cube_mirror->getObjectName()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_cube->getObjectName()); - glBlitFramebuffer(0, 0, cube_size, cube_size, 0, 0, cube_size, cube_size, GL_DEPTH_BUFFER_BIT, GL_NEAREST); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -146,7 +145,7 @@ void Graphics::render(double time) } } // render depth texture for sun - //glViewport(0, 0, windowSize.x, windowSize.y); + glViewport(0, 0, windowSize.x, windowSize.y); // far pass framebuffer->bind(); @@ -208,8 +207,8 @@ void Graphics::render(double time) shadowVPs.push_back(depthBiasVP); // render the level - //level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); - level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); + level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); + //level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); } void Graphics::updateLights() { From 10082466cfa7c4a0633466e6378f079580a6522c Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 22:54:40 +0100 Subject: [PATCH 10/21] Did a little bit of cleanup. --- Shader/depth.fsh | 1 - graphics.cc | 48 +++++++----------------------------------------- object.cc | 10 +++++----- object.hh | 2 +- 4 files changed, 13 insertions(+), 48 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index 0fcb690..79e5487 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -8,5 +8,4 @@ out float gl_FragDepth; void main() { gl_FragDepth = length(fragPosition)/farPlane; - //gl_FragDepth = 1.5f; } diff --git a/graphics.cc b/graphics.cc index 941b27c..635fe1c 100644 --- a/graphics.cc +++ b/graphics.cc @@ -96,14 +96,12 @@ void Graphics::render(double time) // 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::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)), - glm::normalize(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 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)}; glm::vec3 upvectors[6] = {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),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)}; framebuffer_cube->bind(); - static bool printed = false; - glm::mat4 reproduceMatrix; //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++) { // render each side of the cube @@ -116,32 +114,13 @@ void Graphics::render(double time) glm::mat4 viewMatrix = glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; - if (i_face == 0) { - reproduceMatrix = depthViewProjectionMatrix_face; - } - if (!printed) { - printf("\n\nView matrix:\n %2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n%2.10f, %2.10f, %2.10f, %2.10f\n\n\n", - viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0], viewMatrix[3][0], - viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1], viewMatrix[3][1], - viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2], viewMatrix[3][2], - viewMatrix[0][3], viewMatrix[1][3], viewMatrix[2][3], viewMatrix[3][3] - ); - - //std::cout<<"View matrix:" < foovector = std::vector(); - foovector.push_back(viewMatrix); - level->render(depthShader, false, &depthViewProjectionMatrix_face, &foovector); + std::vector viewMatrixVector = std::vector(); + viewMatrixVector.push_back(viewMatrix); + level->render(depthShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); + level->render(depthShader, false, &viewMatrix); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } - if (saveDepthBufferBool && i_face == 3) { - saveDepthBufferToDisk(3, "face.png"); - saveDepthBufferBool = false; - } - } - if (!printed) { - printed = true; } } // render depth texture for sun @@ -208,7 +187,6 @@ void Graphics::render(double time) // render the level level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); - //level->render(lightingShader, true, &reproduceMatrix, &shadowVPs); } void Graphics::updateLights() { @@ -257,20 +235,8 @@ glm::mat4 Graphics::buildViewMatrix(Level* level) { //construct lookAt (cameraPosition = cameraCenter + cameraVector) //return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); - glm::mat4 depthViewProjectionMatrix_face = glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), + return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); - static int i = 5; - if (i == 0) { - printf("\n\nView matrix Camera:\n %2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n%2.2f, %2.2f, %2.2f, %2.2f\n\n\n", - depthViewProjectionMatrix_face[0][0], depthViewProjectionMatrix_face[0][1], depthViewProjectionMatrix_face[0][2], depthViewProjectionMatrix_face[0][3], - depthViewProjectionMatrix_face[1][0], depthViewProjectionMatrix_face[1][1], depthViewProjectionMatrix_face[1][2], depthViewProjectionMatrix_face[1][3], - depthViewProjectionMatrix_face[2][0], depthViewProjectionMatrix_face[2][1], depthViewProjectionMatrix_face[2][2], depthViewProjectionMatrix_face[2][3], - depthViewProjectionMatrix_face[3][0], depthViewProjectionMatrix_face[3][1], depthViewProjectionMatrix_face[3][2], depthViewProjectionMatrix_face[3][3] - ); - } - i--; - return depthViewProjectionMatrix_face; - } float Graphics::getFarPlane() { diff --git a/object.cc b/object.cc index d3e156c..94ecf97 100644 --- a/object.cc +++ b/object.cc @@ -13,7 +13,7 @@ Object::~Object() { } void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { + glm::mat4* viewProjectionMatrix, std::vector* additionalMatrices) { glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale(glm::vec3(model.getScale())); if (lightingPass) { // set lightning parameters for this object @@ -26,15 +26,15 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, shader->setUniform("modelMatrix", modelMatrix); // set shadowMVPs glm::mat4 shadowMVPs[5]; - for(unsigned int i = 0; (isize() && i<5); i++) { - shadowMVPs[i] = shadowVPs->at(i) * modelMatrix; + for(unsigned int i = 0; (isize() && i<5); i++) { + shadowMVPs[i] = additionalMatrices->at(i) * modelMatrix; } glUniformMatrix4fv(shader->getUniformLocation("shadowMVPs"), sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs); } else { - if (shadowVPs) { - shader->setUniform("modelViewMatrix", shadowVPs->at(0) * modelMatrix); + if (additionalMatrices) { + shader->setUniform("modelViewMatrix", additionalMatrices->at(0) * modelMatrix); } } glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix; diff --git a/object.hh b/object.hh index 321e9a0..4713202 100644 --- a/object.hh +++ b/object.hh @@ -16,7 +16,7 @@ class Object : public Entity { Object(); ~Object(); void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjcetionMatrix, std::vector* shadowVPs); + glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices); private: Model model; Material material; From 8babafadfd094446f8dbd106fe26d0de1a04f31a Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 22:55:34 +0100 Subject: [PATCH 11/21] Reintroducing broken assert, because it's important. --- .../src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h b/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h index 5f8007d..78382da 100644 --- a/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h +++ b/extern/bullet/src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h @@ -354,7 +354,7 @@ public: btAssert(m_useQuantization); - //btAssert(point.getX() <= m_bvhAabbMax.getX()); + btAssert(point.getX() <= m_bvhAabbMax.getX()); btAssert(point.getY() <= m_bvhAabbMax.getY()); btAssert(point.getZ() <= m_bvhAabbMax.getZ()); From ac19eac67110abdb288d093652a654e0e07e55e7 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 23:08:23 +0100 Subject: [PATCH 12/21] Additional cleanup. --- graphics.cc | 19 ------------------- graphics.hh | 4 ---- main.cc | 5 ----- 3 files changed, 28 deletions(-) diff --git a/graphics.cc b/graphics.cc index 635fe1c..6678bca 100644 --- a/graphics.cc +++ b/graphics.cc @@ -66,28 +66,12 @@ void Graphics::init(Level* level) { } framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); - - depthTexture_cube = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); - depthTexture_cube->setMinFilter(GL_NEAREST); - depthTexture_cube->setMagFilter(GL_NEAREST); - depthTexture_cube->setWrapS(GL_CLAMP_TO_EDGE); - depthTexture_cube->setWrapT(GL_CLAMP_TO_EDGE); - depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); - - framebuffer_cube_mirror = SharedFrameBufferObject(new FrameBufferObject()); - framebuffer_cube_mirror->setDepthTexture(depthTexture_cube); - - saveDepthBufferBool = false; } glm::uvec2 Graphics::getWindowSize() { return windowSize; } -void Graphics::saveDepthBuffer() { - saveDepthBufferBool = true; -} - void Graphics::render(double time) { // At first render shadows @@ -106,9 +90,6 @@ void Graphics::render(double time) for (unsigned int i_pointlight = 0; i_pointlight<1 && i_pointlightgetLights()->size(); i_pointlight++) { // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { - framebuffer_cube_mirror->bind(); - glClear(GL_DEPTH_BUFFER_BIT); - framebuffer_cube->bind(); 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 viewMatrix = glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), diff --git a/graphics.hh b/graphics.hh index 9a471d4..0d11513 100644 --- a/graphics.hh +++ b/graphics.hh @@ -18,9 +18,7 @@ class Graphics { glm::uvec2 getWindowSize(); void resize(glm::uvec2 windowSize); float getFarPlane(); - void saveDepthBuffer(); private: - bool saveDepthBufferBool; void updateLights(); void saveDepthBufferToDisk(int face, std::string); double lastUpdate; @@ -33,8 +31,6 @@ class Graphics { ACGL::OpenGL::SharedFrameBufferObject framebuffer; std::vector depth_cubeMaps; ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; - ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube_mirror; - ACGL::OpenGL::SharedTexture2D depthTexture_cube; static const int cube_size; Level* level; }; diff --git a/main.cc b/main.cc index c516936..c639348 100644 --- a/main.cc +++ b/main.cc @@ -177,11 +177,6 @@ int main( int argc, char *argv[] ) } } - int stateP = glfwGetKey(window, GLFW_KEY_P); - if (stateP == GLFW_PRESS) { - app.getGraphics()->saveDepthBuffer(); - } - app.getGraphics()->render(now); lastUpdate = now; From 7c8d149e3afd6a12e59d7ede2795e8a6cdb33683 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 4 Feb 2015 23:10:51 +0100 Subject: [PATCH 13/21] Increased precision of depth textures. --- graphics.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphics.cc b/graphics.cc index 6678bca..bebcc31 100644 --- a/graphics.cc +++ b/graphics.cc @@ -42,7 +42,7 @@ void Graphics::init(Level* level) { depthShader = ShaderProgramCreator("depth") .attributeLocations(vao->getAttributeLocations()).create(); - depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT16)); + depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); depthTexture->setMinFilter(GL_NEAREST); depthTexture->setMagFilter(GL_NEAREST); depthTexture->setWrapS(GL_CLAMP_TO_EDGE); @@ -57,7 +57,7 @@ void Graphics::init(Level* level) { for (unsigned int i = 0; i(std::min(int(level->getLights()->size()), 1)); for (unsigned int i = 0; i<1 && isetMinFilter(GL_NEAREST); depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); depth_cubeMaps.at(i)->setWrapS(GL_CLAMP_TO_EDGE); From ed10994ebc737c54eced70fce3e227ed9d7e43ff Mon Sep 17 00:00:00 2001 From: Faerbit Date: Thu, 5 Feb 2015 13:19:51 +0100 Subject: [PATCH 14/21] Added non-linearization to get rid of fragments. --- Shader/depth.fsh | 7 ++++++- Shader/phong.fsh | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Shader/depth.fsh b/Shader/depth.fsh index 79e5487..d7beeb7 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -7,5 +7,10 @@ uniform float farPlane; out float gl_FragDepth; void main() { - gl_FragDepth = length(fragPosition)/farPlane; + float nearPlane = 0.1; + float A = -(farPlane+nearPlane)/(farPlane-nearPlane); + float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); + float value = 0.5*(-A*length(fragPosition) + B)/length(fragPosition) + 0.5; + gl_FragDepth = value; + //gl_FragDepth = length(fragPosition)/farPlane; } diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 09a8bb8..984b17d 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -71,7 +71,8 @@ float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; float bias = 0.005; - return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); + //return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); + return texture(shadowMap, vec4(lightDirection , compValue - bias)); } float distanceToBorder(vec2 vector) { From c28b35667f4af4466592a94666b3a85e0a4ebac7 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 9 Feb 2015 14:26:04 +0100 Subject: [PATCH 15/21] Changed cube map shadows sampler to a sampler array. --- CMakeLists.txt | 4 ++-- Shader/phong.fsh | 4 ++-- graphics.cc | 9 +++++++-- graphics.hh | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index caf8bbb..b4e6f5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ add_subdirectory(converter) PROJECT(MarbleRaceGroupC) # ACGL setup -SET(ACGL_OPENGL_SUPPORT CORE_32) -ADD_DEFINITIONS(-DACGL_OPENGL_VERSION_32) +SET(ACGL_OPENGL_SUPPORT CORE_40) +ADD_DEFINITIONS(-DACGL_OPENGL_VERSION_40) ADD_DEFINITIONS(-DACGL_OPENGL_PROFILE_CORE) # create the newest availabe OpenGL context, independent of the ACGL version: #ADD_DEFINITIONS(-DGLFW_OPENGL_LATEST_VERSION) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 984b17d..95afb30 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -9,7 +9,7 @@ out vec4 oColor; uniform sampler2D uTexture; uniform sampler2DShadow shadowMap; -uniform samplerCubeShadow shadowMap_cube; +uniform samplerCubeShadow shadowMap_cube[1]; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -113,7 +113,7 @@ void main() specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; if (i == 0) { - visibility = samplePointShadow(shadowMap_cube, lightDirection); + visibility = samplePointShadow(shadowMap_cube[0], lightDirection); } } /*float value = texture(shadowMap_cube, lightDirection); diff --git a/graphics.cc b/graphics.cc index bebcc31..94e27b1 100644 --- a/graphics.cc +++ b/graphics.cc @@ -66,6 +66,9 @@ void Graphics::init(Level* level) { } framebuffer_cube = SharedFrameBufferObject(new FrameBufferObject()); + + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); + printf("Your graphics card supports %d texture units.\n", number_of_texture_units); } glm::uvec2 Graphics::getWindowSize() { @@ -98,7 +101,6 @@ void Graphics::render(double time) std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); level->render(depthShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); - level->render(depthShader, false, &viewMatrix); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -125,7 +127,10 @@ void Graphics::render(double time) lightingShader->use(); if (level->getLights()->size() > 0) { - lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4); + glActiveTexture(GL_TEXTURE0+2); + glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubeMaps.at(0)->getObjectName()); + GLint textureUnits[1] = {2}; + glUniform1iv(lightingShader->getUniformLocation("shadowMap_cube"), 1, textureUnits); } //set lighting parameters diff --git a/graphics.hh b/graphics.hh index 0d11513..6c3d7e2 100644 --- a/graphics.hh +++ b/graphics.hh @@ -33,6 +33,7 @@ class Graphics { ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; static const int cube_size; Level* level; + int number_of_texture_units = 0; }; #endif From 282eb32267fa05ac5d4bdd450d1c1e2fcfcb487f Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 9 Feb 2015 21:29:44 +0100 Subject: [PATCH 16/21] Got multiple shadows working. The really ugly way. --- CMakeLists.txt | 4 ++-- Shader/phong.fsh | 43 +++++++++++++++++++++++++++++++++++++++---- graphics.cc | 30 +++++++++++++++--------------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4e6f5e..caf8bbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ add_subdirectory(converter) PROJECT(MarbleRaceGroupC) # ACGL setup -SET(ACGL_OPENGL_SUPPORT CORE_40) -ADD_DEFINITIONS(-DACGL_OPENGL_VERSION_40) +SET(ACGL_OPENGL_SUPPORT CORE_32) +ADD_DEFINITIONS(-DACGL_OPENGL_VERSION_32) ADD_DEFINITIONS(-DACGL_OPENGL_PROFILE_CORE) # create the newest availabe OpenGL context, independent of the ACGL version: #ADD_DEFINITIONS(-DGLFW_OPENGL_LATEST_VERSION) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 95afb30..d43e311 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -9,7 +9,38 @@ out vec4 oColor; uniform sampler2D uTexture; uniform sampler2DShadow shadowMap; -uniform samplerCubeShadow shadowMap_cube[1]; +uniform samplerCubeShadow shadowMap_cube0; +uniform samplerCubeShadow shadowMap_cube1; +uniform samplerCubeShadow shadowMap_cube2; +uniform samplerCubeShadow shadowMap_cube3; +uniform samplerCubeShadow shadowMap_cube4; +uniform samplerCubeShadow shadowMap_cube5; +uniform samplerCubeShadow shadowMap_cube6; +uniform samplerCubeShadow shadowMap_cube7; +uniform samplerCubeShadow shadowMap_cube8; +uniform samplerCubeShadow shadowMap_cube9; +uniform samplerCubeShadow shadowMap_cube10; +uniform samplerCubeShadow shadowMap_cube11; +uniform samplerCubeShadow shadowMap_cube12; +uniform samplerCubeShadow shadowMap_cube13; +uniform samplerCubeShadow shadowMap_cube14; +uniform samplerCubeShadow shadowMap_cube15; +uniform samplerCubeShadow shadowMap_cube16; +uniform samplerCubeShadow shadowMap_cube17; +uniform samplerCubeShadow shadowMap_cube18; +uniform samplerCubeShadow shadowMap_cube19; +uniform samplerCubeShadow shadowMap_cube20; +uniform samplerCubeShadow shadowMap_cube21; +uniform samplerCubeShadow shadowMap_cube22; +uniform samplerCubeShadow shadowMap_cube23; +uniform samplerCubeShadow shadowMap_cube24; +uniform samplerCubeShadow shadowMap_cube25; +uniform samplerCubeShadow shadowMap_cube26; +uniform samplerCubeShadow shadowMap_cube27; +uniform samplerCubeShadow shadowMap_cube28; +uniform samplerCubeShadow shadowMap_cube29; +uniform samplerCubeShadow shadowMap_cube30; +uniform samplerCubeShadow shadowMap_cube31; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -113,11 +144,15 @@ void main() specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; if (i == 0) { - visibility = samplePointShadow(shadowMap_cube[0], lightDirection); + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 1) { + visibility *= samplePointShadow(shadowMap_cube1, lightDirection); + } + if (i == 2) { + visibility *= samplePointShadow(shadowMap_cube2, lightDirection); } } - /*float value = texture(shadowMap_cube, lightDirection); - oColor = vec4(value, value, value, 255);*/ } // shadows diff --git a/graphics.cc b/graphics.cc index 94e27b1..a2be832 100644 --- a/graphics.cc +++ b/graphics.cc @@ -53,10 +53,9 @@ void Graphics::init(Level* level) { framebuffer->setDepthTexture(depthTexture); framebuffer->validate(); - /*depth_cubeMaps = std::vector(level->getLights()->size()); - for (unsigned int i = 0; i(std::min(int(level->getLights()->size()), 1)); - for (unsigned int i = 0; i<1 && i(32); + for (unsigned int i = 0; isetMinFilter(GL_NEAREST); depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); @@ -69,6 +68,17 @@ void Graphics::init(Level* level) { glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); printf("Your graphics card supports %d texture units.\n", number_of_texture_units); + + lightingShader->use(); + + lightingShader->setTexture("shadowMap", depthTexture, 1); + + if (level->getLights()->size() > 0) { + for(unsigned int i = 0; i<32; i++){ + // start with texture unit 2 because the first two are used by the texture and the directional shadow map + lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), i+2); + } + } } glm::uvec2 Graphics::getWindowSize() { @@ -89,8 +99,7 @@ void Graphics::render(double time) glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.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++) { + for (unsigned int i_pointlight = 0; i_pointlightgetLights()->size(); 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); @@ -126,13 +135,6 @@ void Graphics::render(double time) lightingShader->use(); - if (level->getLights()->size() > 0) { - glActiveTexture(GL_TEXTURE0+2); - glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubeMaps.at(0)->getObjectName()); - GLint textureUnits[1] = {2}; - glUniform1iv(lightingShader->getUniformLocation("shadowMap_cube"), 1, textureUnits); - } - //set lighting parameters // TODO look into doing this less often, offload to another thread? @@ -153,8 +155,6 @@ void Graphics::render(double time) ); glm::mat4 depthBiasVP = biasMatrix*depthViewProjectionMatrix; - lightingShader->setTexture("shadowMap", depthTexture, 1); - lightingShader->setUniform("farPlane", farPlane); // set fog Parameters From 9afb3595d0842916d0030ad2ea748920a52c8c13 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Tue, 10 Feb 2015 00:27:13 +0100 Subject: [PATCH 17/21] Fixed directional shadows. --- Shader/depth.fsh | 11 +---------- Shader/depth.vsh | 4 ---- Shader/depth_cube.fsh | 16 ++++++++++++++++ Shader/depth_cube.vsh | 15 +++++++++++++++ Shader/phong.fsh | 3 ++- graphics.cc | 10 +++++++--- graphics.hh | 1 + 7 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 Shader/depth_cube.fsh create mode 100644 Shader/depth_cube.vsh diff --git a/Shader/depth.fsh b/Shader/depth.fsh index d7beeb7..4e98194 100644 --- a/Shader/depth.fsh +++ b/Shader/depth.fsh @@ -1,16 +1,7 @@ #version 150 -in vec4 fragPosition; - -uniform float farPlane; - out float gl_FragDepth; void main() { - float nearPlane = 0.1; - float A = -(farPlane+nearPlane)/(farPlane-nearPlane); - float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); - float value = 0.5*(-A*length(fragPosition) + B)/length(fragPosition) + 0.5; - gl_FragDepth = value; - //gl_FragDepth = length(fragPosition)/farPlane; + gl_FragDepth = gl_FragCoord.z; } diff --git a/Shader/depth.vsh b/Shader/depth.vsh index 2618bfa..f84c3cf 100644 --- a/Shader/depth.vsh +++ b/Shader/depth.vsh @@ -5,11 +5,7 @@ in vec3 aNormal; in vec3 aTexcoord; uniform mat4 modelViewProjectionMatrix; -uniform mat4 modelViewMatrix; - -out vec4 fragPosition; void main() { - fragPosition = modelViewMatrix * vec4(aPosition, 1.0); gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/Shader/depth_cube.fsh b/Shader/depth_cube.fsh new file mode 100644 index 0000000..d7beeb7 --- /dev/null +++ b/Shader/depth_cube.fsh @@ -0,0 +1,16 @@ +#version 150 + +in vec4 fragPosition; + +uniform float farPlane; + +out float gl_FragDepth; + +void main() { + float nearPlane = 0.1; + float A = -(farPlane+nearPlane)/(farPlane-nearPlane); + float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); + float value = 0.5*(-A*length(fragPosition) + B)/length(fragPosition) + 0.5; + gl_FragDepth = value; + //gl_FragDepth = length(fragPosition)/farPlane; +} diff --git a/Shader/depth_cube.vsh b/Shader/depth_cube.vsh new file mode 100644 index 0000000..2618bfa --- /dev/null +++ b/Shader/depth_cube.vsh @@ -0,0 +1,15 @@ +#version 150 + +in vec3 aPosition; +in vec3 aNormal; +in vec3 aTexcoord; + +uniform mat4 modelViewProjectionMatrix; +uniform mat4 modelViewMatrix; + +out vec4 fragPosition; + +void main() { + fragPosition = modelViewMatrix * vec4(aPosition, 1.0); + gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); +} diff --git a/Shader/phong.fsh b/Shader/phong.fsh index d43e311..5c29256 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -101,7 +101,8 @@ float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { float A = -(farPlane+nearPlane)/(farPlane-nearPlane); float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane); float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5; - float bias = 0.005; + float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0))); + bias = clamp(bias, 0.0, 0.01); //return texture(shadowMap, vec4(lightDirection , length(lightDirection)/farPlane - bias)); return texture(shadowMap, vec4(lightDirection , compValue - bias)); } diff --git a/graphics.cc b/graphics.cc index a2be832..e4a0b72 100644 --- a/graphics.cc +++ b/graphics.cc @@ -42,6 +42,9 @@ void Graphics::init(Level* level) { depthShader = ShaderProgramCreator("depth") .attributeLocations(vao->getAttributeLocations()).create(); + depthCubeShader = ShaderProgramCreator("depth_cube") + .attributeLocations(vao->getAttributeLocations()).create(); + depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24)); depthTexture->setMinFilter(GL_NEAREST); depthTexture->setMagFilter(GL_NEAREST); @@ -88,8 +91,8 @@ glm::uvec2 Graphics::getWindowSize() { void Graphics::render(double time) { // At first render shadows - depthShader->use(); - depthShader->setUniform("farPlane", farPlane); + depthCubeShader->use(); + depthCubeShader->setUniform("farPlane", farPlane); // 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); @@ -109,13 +112,14 @@ void Graphics::render(double time) glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); - level->render(depthShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); + level->render(depthCubeShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } } } // render depth texture for sun + depthShader->use(); glViewport(0, 0, windowSize.x, windowSize.y); // far pass diff --git a/graphics.hh b/graphics.hh index 6c3d7e2..e512636 100644 --- a/graphics.hh +++ b/graphics.hh @@ -26,6 +26,7 @@ class Graphics { float nearPlane; float farPlane; ACGL::OpenGL::SharedShaderProgram lightingShader; + ACGL::OpenGL::SharedShaderProgram depthCubeShader; ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedTexture2D depthTexture; ACGL::OpenGL::SharedFrameBufferObject framebuffer; From ba2cada6dca0638ab41c555fa7c8e1b06dd8c60a Mon Sep 17 00:00:00 2001 From: Faerbit Date: Tue, 10 Feb 2015 18:19:26 +0100 Subject: [PATCH 18/21] Completed shadows for now. Lighting model is still a little bit off. --- Shader/phong.fsh | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 5c29256..7fb384d 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -153,6 +153,93 @@ void main() if (i == 2) { visibility *= samplePointShadow(shadowMap_cube2, lightDirection); } + if (i == 3) { + visibility *= samplePointShadow(shadowMap_cube3, lightDirection); + } + if (i == 4) { + visibility *= samplePointShadow(shadowMap_cube4, lightDirection); + } + if (i == 5) { + visibility *= samplePointShadow(shadowMap_cube5, lightDirection); + } + if (i == 6) { + visibility *= samplePointShadow(shadowMap_cube6, lightDirection); + } + if (i == 7) { + visibility *= samplePointShadow(shadowMap_cube7, lightDirection); + } + if (i == 8) { + visibility *= samplePointShadow(shadowMap_cube8, lightDirection); + } + if (i == 9) { + visibility *= samplePointShadow(shadowMap_cube9, lightDirection); + } + if (i == 10) { + visibility *= samplePointShadow(shadowMap_cube10, lightDirection); + } + if (i == 11) { + visibility *= samplePointShadow(shadowMap_cube11, lightDirection); + } + if (i == 12) { + visibility *= samplePointShadow(shadowMap_cube12, lightDirection); + } + if (i == 13) { + visibility *= samplePointShadow(shadowMap_cube13, lightDirection); + } + if (i == 14) { + visibility *= samplePointShadow(shadowMap_cube14, lightDirection); + } + if (i == 15) { + visibility *= samplePointShadow(shadowMap_cube15, lightDirection); + } + if (i == 16) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 17) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 18) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 19) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 20) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 21) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 22) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 23) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 24) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 25) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 26) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 27) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 28) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 29) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 30) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 31) { + visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + } } } From 1d8bbba1caf3a833a33f7d65b66bdb86a61c035e Mon Sep 17 00:00:00 2001 From: Faerbit Date: Thu, 12 Feb 2015 01:13:26 +0100 Subject: [PATCH 19/21] Now only using the 32 closest light sources. --- graphics.cc | 73 +++++++++++++++++++++++++++++++++++++++-------------- graphics.hh | 7 ++++- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/graphics.cc b/graphics.cc index e4a0b72..adb3053 100644 --- a/graphics.cc +++ b/graphics.cc @@ -3,6 +3,7 @@ #include #include +#include #include @@ -56,6 +57,14 @@ void Graphics::init(Level* level) { framebuffer->setDepthTexture(depthTexture); framebuffer->validate(); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); + printf("Your graphics card supports %d texture units.\n", number_of_texture_units); + // Exit if we need more texture units + if (number_of_texture_units < 34) { + printf("You need at least 34 texture units to run this application. Exiting\n"); + exit(-1); + } + // always generate and bind 32 cube maps, because otherwise the shader won't work depth_cubeMaps = std::vector(32); for (unsigned int i = 0; iuse(); lightingShader->setTexture("shadowMap", depthTexture, 1); if (level->getLights()->size() > 0) { - for(unsigned int i = 0; i<32; i++){ + for(unsigned int i = 0; isetTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), i+2); } } + updateClosestLights(); } glm::uvec2 Graphics::getWindowSize() { @@ -102,13 +110,13 @@ void Graphics::render(double time) glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)}; framebuffer_cube->bind(); - for (unsigned int i_pointlight = 0; i_pointlightgetLights()->size(); i_pointlight++) { + for (unsigned int i_pointlight = 0; i_pointlightsize(); 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 viewMatrix = glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), - level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); + glm::mat4 viewMatrix = glm::lookAt(closestLights->at(i_pointlight).getPosition(), + closestLights->at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); @@ -143,10 +151,11 @@ void Graphics::render(double time) // TODO look into doing this less often, offload to another thread? // TODO figure out how to deal with bigger numbers of lights. load the nearest on demand? + double nextUpdate = lastUpdate + lightUpdateDelay; if (time >= nextUpdate) { - updateLights(); + updateShaderLights(); lastUpdate = time; } @@ -179,28 +188,54 @@ void Graphics::render(double time) level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); } -void Graphics::updateLights() { - if (level->getLights()->size() > 0) { - lightingShader->setUniform("lightCount", (int) level->getLights()->size()); +bool Graphics::compareLightDistances(Light a, Light b) { + if (glm::distance(this->level->getCameraCenter()->getPosition(), a.getPosition()) < + glm::distance(this->level->getCameraCenter()->getPosition(), b.getPosition())) { + return true; + } + else { + return false; + } +} + +void Graphics::updateClosestLights() { + if (level->getLights()->size() <= 32) { + this->closestLights = level->getLights(); + } + else { + closestLightsVector = std::vector(*level->getLights()); + std::sort(closestLightsVector.begin(), + closestLightsVector.end(), + [this](Light a, Light b) {return compareLightDistances(a, b); }); + closestLightsVector = std::vector(&closestLightsVector[0], + &closestLightsVector[31]); + closestLights = &closestLightsVector; + } +} + +void Graphics::updateShaderLights() { + updateClosestLights(); + if (closestLights->size() > 0) { + lightingShader->setUniform("lightCount", (int) closestLights->size()); // Build light position array - glm::vec3 lightSources[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightSources[i] = level->getLights()->at(i).getPosition(); + glm::vec3 lightSources[closestLights->size()]; + for(unsigned int i = 0; isize(); i++) { + lightSources[i] = closestLights->at(i).getPosition(); } glUniform3fv(lightingShader->getUniformLocation("lightSources"), sizeof(lightSources), (GLfloat*) lightSources); // Build light colour array - glm::vec3 lightColours[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightColours[i] = level->getLights()->at(i).getColour(); + glm::vec3 lightColours[closestLights->size()]; + for(unsigned int i = 0; isize(); i++) { + lightColours[i] = closestLights->at(i).getColour(); } glUniform3fv(lightingShader->getUniformLocation("lightColors"), sizeof(lightColours), (GLfloat*) lightColours); // Build light attenuation array - float lightIntensities[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightIntensities[i] = level->getLights()->at(i).getIntensity(); + float lightIntensities[closestLights->size()]; + for(unsigned int i = 0; isize(); i++) { + lightIntensities[i] = closestLights->at(i).getIntensity(); } glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); diff --git a/graphics.hh b/graphics.hh index e512636..b05b92b 100644 --- a/graphics.hh +++ b/graphics.hh @@ -19,12 +19,17 @@ class Graphics { void resize(glm::uvec2 windowSize); float getFarPlane(); private: - void updateLights(); + void updateShaderLights(); + void updateClosestLights(); + bool compareLightDistances(Light a, Light b); void saveDepthBufferToDisk(int face, std::string); double lastUpdate; glm::uvec2 windowSize; float nearPlane; float farPlane; + // pointer to either use the vector from the level or from here + std::vector* closestLights; + std::vector closestLightsVector; // contains the 32 closest lights ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram depthCubeShader; ACGL::OpenGL::SharedShaderProgram depthShader; From 1ac9e4e270435781fe13cc705dcfbad85d81c90a Mon Sep 17 00:00:00 2001 From: Faerbit Date: Thu, 12 Feb 2015 16:28:24 +0100 Subject: [PATCH 20/21] Fixing parameters. --- Shader/phong.fsh | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 7fb384d..93a71df 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -193,52 +193,67 @@ void main() visibility *= samplePointShadow(shadowMap_cube15, lightDirection); } if (i == 16) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube16, lightDirection); } + if (i == 17) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube17, lightDirection); } + if (i == 18) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube18, lightDirection); } + if (i == 19) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube19, lightDirection); } + if (i == 20) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube20, lightDirection); } + if (i == 21) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube21, lightDirection); } + if (i == 22) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube22, lightDirection); } + if (i == 23) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube23, lightDirection); } + if (i == 24) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube24, lightDirection); } + if (i == 25) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube25, lightDirection); } + if (i == 26) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube26, lightDirection); } + if (i == 27) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube27, lightDirection); } + if (i == 28) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube28, lightDirection); } + if (i == 29) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube29, lightDirection); } + if (i == 30) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube30, lightDirection); } + if (i == 31) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); + visibility *= samplePointShadow(shadowMap_cube31, lightDirection); } } } From 55434daa249211ff10d42bc3f5d63f98d222eae0 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 15:51:49 +0100 Subject: [PATCH 21/21] Updated how shadows are applied. Looks nice now. --- Shader/phong.fsh | 194 ++++++++++++----------------------------------- graphics.cc | 6 +- 2 files changed, 52 insertions(+), 148 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index 93a71df..e9a7f13 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -19,28 +19,6 @@ uniform samplerCubeShadow shadowMap_cube6; uniform samplerCubeShadow shadowMap_cube7; uniform samplerCubeShadow shadowMap_cube8; uniform samplerCubeShadow shadowMap_cube9; -uniform samplerCubeShadow shadowMap_cube10; -uniform samplerCubeShadow shadowMap_cube11; -uniform samplerCubeShadow shadowMap_cube12; -uniform samplerCubeShadow shadowMap_cube13; -uniform samplerCubeShadow shadowMap_cube14; -uniform samplerCubeShadow shadowMap_cube15; -uniform samplerCubeShadow shadowMap_cube16; -uniform samplerCubeShadow shadowMap_cube17; -uniform samplerCubeShadow shadowMap_cube18; -uniform samplerCubeShadow shadowMap_cube19; -uniform samplerCubeShadow shadowMap_cube20; -uniform samplerCubeShadow shadowMap_cube21; -uniform samplerCubeShadow shadowMap_cube22; -uniform samplerCubeShadow shadowMap_cube23; -uniform samplerCubeShadow shadowMap_cube24; -uniform samplerCubeShadow shadowMap_cube25; -uniform samplerCubeShadow shadowMap_cube26; -uniform samplerCubeShadow shadowMap_cube27; -uniform samplerCubeShadow shadowMap_cube28; -uniform samplerCubeShadow shadowMap_cube29; -uniform samplerCubeShadow shadowMap_cube30; -uniform samplerCubeShadow shadowMap_cube31; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -123,11 +101,13 @@ void main() // direction lighting if(length(directionalLightVector)>0.0f) { vec3 directionalVector = normalize(directionalLightVector); + float directionalVisibility = sampleDirectionalShadow(shadowMap, shadowCoord); diffuseColor += clamp(dot(normalize(vNormal), directionalVector) - *diffuseFactor*directionalIntensity*directionalColor, 0.0, 1.0); + *diffuseFactor*directionalIntensity*directionalColor, 0.0, 1.0)*directionalVisibility; vec3 cameraVector = normalize(camera - vec3(fragPosition)); - specularColor += clamp(pow((dot((cameraVector+directionalVector),normalize(vNormal))/(length(cameraVector+directionalVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) - *specularFactor*directionalIntensity*directionalColor; + specularColor += clamp(pow((dot((cameraVector+directionalVector),normalize(vNormal))/ + (length(cameraVector+directionalVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) + *specularFactor*directionalIntensity*directionalColor*directionalVisibility; } // point lights @@ -135,134 +115,58 @@ void main() for(int i = 0; i 0.001f) { + if (distance < farPlane) { + if (i == 0) { + pointVisibility = samplePointShadow(shadowMap_cube0, lightDirection); + } + if (i == 1) { + pointVisibility = samplePointShadow(shadowMap_cube1, lightDirection); + } + + if (i == 2) { + pointVisibility = samplePointShadow(shadowMap_cube2, lightDirection); + } + + if (i == 3) { + pointVisibility = samplePointShadow(shadowMap_cube3, lightDirection); + } + + if (i == 4) { + pointVisibility = samplePointShadow(shadowMap_cube4, lightDirection); + } + + if (i == 5) { + pointVisibility = samplePointShadow(shadowMap_cube5, lightDirection); + } + + if (i == 6) { + pointVisibility = samplePointShadow(shadowMap_cube6, lightDirection); + } + + if (i == 7) { + pointVisibility = samplePointShadow(shadowMap_cube7, lightDirection); + } + + if (i == 8) { + pointVisibility = samplePointShadow(shadowMap_cube8, lightDirection); + } + + if (i == 9) { + pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection); + } vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); float intensity = clamp(exp(-(1/lightIntensities[i])*distance), 0.0, 1.0); diffuseColor += clamp(dot(normalize(vNormal), lightVector) - *diffuseFactor*intensity*lightColors[i], 0.0, 1.0); + *diffuseFactor*intensity*lightColors[i], 0.0, 1.0)*pointVisibility; 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]; - if (i == 0) { - visibility *= samplePointShadow(shadowMap_cube0, lightDirection); - } - if (i == 1) { - visibility *= samplePointShadow(shadowMap_cube1, lightDirection); - } - if (i == 2) { - visibility *= samplePointShadow(shadowMap_cube2, lightDirection); - } - if (i == 3) { - visibility *= samplePointShadow(shadowMap_cube3, lightDirection); - } - if (i == 4) { - visibility *= samplePointShadow(shadowMap_cube4, lightDirection); - } - if (i == 5) { - visibility *= samplePointShadow(shadowMap_cube5, lightDirection); - } - if (i == 6) { - visibility *= samplePointShadow(shadowMap_cube6, lightDirection); - } - if (i == 7) { - visibility *= samplePointShadow(shadowMap_cube7, lightDirection); - } - if (i == 8) { - visibility *= samplePointShadow(shadowMap_cube8, lightDirection); - } - if (i == 9) { - visibility *= samplePointShadow(shadowMap_cube9, lightDirection); - } - if (i == 10) { - visibility *= samplePointShadow(shadowMap_cube10, lightDirection); - } - if (i == 11) { - visibility *= samplePointShadow(shadowMap_cube11, lightDirection); - } - if (i == 12) { - visibility *= samplePointShadow(shadowMap_cube12, lightDirection); - } - if (i == 13) { - visibility *= samplePointShadow(shadowMap_cube13, lightDirection); - } - if (i == 14) { - visibility *= samplePointShadow(shadowMap_cube14, lightDirection); - } - if (i == 15) { - visibility *= samplePointShadow(shadowMap_cube15, lightDirection); - } - if (i == 16) { - visibility *= samplePointShadow(shadowMap_cube16, lightDirection); - } - - if (i == 17) { - visibility *= samplePointShadow(shadowMap_cube17, lightDirection); - } - - if (i == 18) { - visibility *= samplePointShadow(shadowMap_cube18, lightDirection); - } - - if (i == 19) { - visibility *= samplePointShadow(shadowMap_cube19, lightDirection); - } - - if (i == 20) { - visibility *= samplePointShadow(shadowMap_cube20, lightDirection); - } - - if (i == 21) { - visibility *= samplePointShadow(shadowMap_cube21, lightDirection); - } - - if (i == 22) { - visibility *= samplePointShadow(shadowMap_cube22, lightDirection); - } - - if (i == 23) { - visibility *= samplePointShadow(shadowMap_cube23, lightDirection); - } - - if (i == 24) { - visibility *= samplePointShadow(shadowMap_cube24, lightDirection); - } - - if (i == 25) { - visibility *= samplePointShadow(shadowMap_cube25, lightDirection); - } - - if (i == 26) { - visibility *= samplePointShadow(shadowMap_cube26, lightDirection); - } - - if (i == 27) { - visibility *= samplePointShadow(shadowMap_cube27, lightDirection); - } - - if (i == 28) { - visibility *= samplePointShadow(shadowMap_cube28, lightDirection); - } - - if (i == 29) { - visibility *= samplePointShadow(shadowMap_cube29, lightDirection); - } - - if (i == 30) { - visibility *= samplePointShadow(shadowMap_cube30, lightDirection); - } - - if (i == 31) { - visibility *= samplePointShadow(shadowMap_cube31, lightDirection); - } + specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/ + (length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) + *specularFactor*intensity*lightColors[i]*pointVisibility; } } - // shadows - visibility *= sampleDirectionalShadow(shadowMap, shadowCoord); - - specularColor *= visibility; - diffuseColor *= visibility; vec3 finalColor = specularColor + diffuseColor + ambientColor; float distanceCameraCenter = distance(cameraCenter, vec3(fragPosition)); diff --git a/graphics.cc b/graphics.cc index adb3053..128961b 100644 --- a/graphics.cc +++ b/graphics.cc @@ -60,13 +60,13 @@ void Graphics::init(Level* level) { glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); printf("Your graphics card supports %d texture units.\n", number_of_texture_units); // Exit if we need more texture units - if (number_of_texture_units < 34) { - printf("You need at least 34 texture units to run this application. Exiting\n"); + if (number_of_texture_units < 12) { + printf("You need at least 12 texture units to run this application. Exiting\n"); exit(-1); } // always generate and bind 32 cube maps, because otherwise the shader won't work - depth_cubeMaps = std::vector(32); + depth_cubeMaps = std::vector(10); for (unsigned int i = 0; isetMinFilter(GL_NEAREST);