From 7f13e899351c024b4798d1084df80c68a6a8d0ed Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 26 Jan 2015 21:48:44 +0100 Subject: [PATCH 01/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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/29] 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 75792a57237ab6de652a96279f78c32ed4321616 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 13 Feb 2015 15:46:00 +0100 Subject: [PATCH 21/29] added debug Camear (keys L and K to switch) --- camera.cc | 10 ++++++++++ camera.hh | 4 +++- graphics.cc | 3 ++- level.cc | 7 ++++++- level.hh | 2 +- main.cc | 7 +++++-- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/camera.cc b/camera.cc index c9430e5..fc2e1a6 100644 --- a/camera.cc +++ b/camera.cc @@ -3,6 +3,7 @@ Camera::Camera(glm::vec2 rotation, float distance) { this->rotation = rotation; this->distance = distance; + this->setIsPhysicsCamera(true); } Camera::Camera() { @@ -31,6 +32,15 @@ void Camera::setRotation(glm::vec2 rotation) { updatePosition(); } + +bool Camera::getIsPhysicsCamera() +{ + return usePhysicsCamera; +} +void Camera::setIsPhysicsCamera(bool val) +{ + usePhysicsCamera = val; +} void Camera::updateRotation(glm::vec2 rotation) { this->rotation += rotation; if((this->rotation.x + rotation.x) >= 1.57f) { diff --git a/camera.hh b/camera.hh index 71b6b86..e0bea51 100644 --- a/camera.hh +++ b/camera.hh @@ -19,7 +19,8 @@ class Camera { glm::vec3 getPosition(); void setDirection(glm::vec3 dir); glm::vec3 getDirection(); - + bool getIsPhysicsCamera(); + void setIsPhysicsCamera(bool val); private: void updatePosition(); float distance; @@ -27,6 +28,7 @@ class Camera { glm::vec3 vector; glm::vec3 position; glm::vec3 direction; + bool usePhysicsCamera; }; #endif diff --git a/graphics.cc b/graphics.cc index d51ecb6..29f7434 100644 --- a/graphics.cc +++ b/graphics.cc @@ -213,7 +213,8 @@ void Graphics::resize(glm::uvec2 windowSize) { 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)); + if(level->getCamera()->getIsPhysicsCamera()) + 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()), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); diff --git a/level.cc b/level.cc index bafb957..79148da 100644 --- a/level.cc +++ b/level.cc @@ -54,7 +54,7 @@ void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, } } -void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed) { +void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed,bool kPressed, bool lPressed) { // Ignore first two mouse updates, because they are incorrect // DON'T try to move this functionallity elsewhere static int i = 0; @@ -82,6 +82,11 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre physics.rollRight(camera.getVector(),strength); } + if(kPressed) + camera.setIsPhysicsCamera(true); + if(lPressed) + camera.setIsPhysicsCamera(false); + physics.takeUpdateStep(runTime); cameraCenter->setPosition(physics.getPos(0)); diff --git a/level.hh b/level.hh index 69b3412..5b43467 100644 --- a/level.hh +++ b/level.hh @@ -24,7 +24,7 @@ class Level { Level(); ~Level(); void load(); - void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed); + void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed, bool kPressed, bool lPressed); void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs=0); glm::vec3 getAmbientLight(); diff --git a/main.cc b/main.cc index 2cfb0a5..08ddf72 100644 --- a/main.cc +++ b/main.cc @@ -161,16 +161,19 @@ int main( int argc, char *argv[] ) int stateA = glfwGetKey(window, GLFW_KEY_A); int stateS = glfwGetKey(window, GLFW_KEY_S); int stateD = glfwGetKey(window, GLFW_KEY_D); + int stateK = glfwGetKey(window, GLFW_KEY_K); + int stateL = glfwGetKey(window, GLFW_KEY_L); + double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); glfwSetCursorPos(window, app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); app.getLevel()->update(now - lastUpdate, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, (float)xpos-app.getGraphics()->getWindowSize().x/2), - stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS); + stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS,stateK == GLFW_PRESS,stateL == GLFW_PRESS); } else { - app.getLevel()->update(now - lastUpdate, glm::vec2(0.0f, 0.0f), false, false, false, false); + app.getLevel()->update(now - lastUpdate, glm::vec2(0.0f, 0.0f), false, false, false, false,false,false); if (app.isLocked()) { app.ignoredOneMouseUpdate(); } From 55434daa249211ff10d42bc3f5d63f98d222eae0 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 15:51:49 +0100 Subject: [PATCH 22/29] 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); From 98718b517edb37ff1b86665615bdd23179a389fd Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 13 Feb 2015 16:09:18 +0100 Subject: [PATCH 23/29] Fixed non rotating objects --- physics.cc | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/physics.cc b/physics.cc index 0f5cd42..7050a08 100644 --- a/physics.cc +++ b/physics.cc @@ -204,7 +204,7 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(glmQuat.x,glmQuat.y,glmQuat.z,glmQuat.w),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); btVector3 inertia(0,0,0); - if(mass != 0.0 && rotate) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable) + if(mass != 0.0) { shape->calculateLocalInertia((btScalar)mass,inertia); } @@ -219,6 +219,10 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision); + if(!rotate)//rotate lets certain objects get inertia (0,0,0) (not rotateable) + { + body->setAngularFactor(btVector3(0,0,0)); + } if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); @@ -237,7 +241,7 @@ void Physics::addButton(float width, float height, float length, Entity entity, btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(glmQuat.x,glmQuat.y,glmQuat.z,glmQuat.w),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); btVector3 inertia(0,0,0); - if(mass != 0.0 && rotate) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable) + if(mass != 0.0) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable) { box->calculateLocalInertia((btScalar)mass,inertia); } @@ -251,6 +255,11 @@ void Physics::addButton(float width, float height, float length, Entity entity, bodies.push_back(body); + if(!rotate) + { + body->setAngularFactor(btVector3(0,0,0)); + } + if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); } @@ -266,11 +275,12 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(glmQuat.x,glmQuat.y,glmQuat.z,glmQuat.w),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); btVector3 inertia(0,0,0); - if(mass != 0.0 && rotate) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable) + if(mass != 0.0) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable) { box->calculateLocalInertia((btScalar)mass,inertia); } + btRigidBody::btRigidBodyConstructionInfo info(mass,motion,box,inertia); btRigidBody* body = new btRigidBody(info); @@ -281,6 +291,11 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo bodies.push_back(body); + if(!rotate) + { + body->setAngularFactor(btVector3(0,0,0)); + } + if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); } @@ -311,6 +326,11 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f bodies.push_back(body); + if(!rotate)//rotate lets certain objects get inertia (0,0,0) (not rotateable) + { + body->setAngularFactor(btVector3(0,0,0)); + } + body->setSleepingThresholds(0,0); if(bodies.size() != indice) From 281466ebd581552c4961df299363e507f002f00d Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 16:20:22 +0100 Subject: [PATCH 24/29] Added maxShadowRenderCount. Controls how many shadows get rendered. --- application.cc | 6 +++++- application.hh | 2 ++ data/config.xml | 2 ++ graphics.cc | 7 +++++-- graphics.hh | 3 ++- loader.cc | 4 +++- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/application.cc b/application.cc index b16e9c0..a3290f5 100644 --- a/application.cc +++ b/application.cc @@ -5,7 +5,7 @@ Application::Application() { Loader loader = Loader(); //load the config.xml loader.loadConfig(this); - graphics = Graphics(glm::uvec2(windowWidth, windowHeight), 0.1f, farPlane, shadowCubeSize); + graphics = Graphics(glm::uvec2(windowWidth, windowHeight), 0.1f, farPlane, shadowCubeSize, maxShadowRenderCount); } void Application::init() @@ -122,3 +122,7 @@ void Application::setHeightmapPath(std::string heightmapPath) { void Application::setLevelXmlPath(std::string levelXmlPath) { this->levelXmlPath = levelXmlPath; } + +void Application::setMaxShadowRenderCount(int count) { + this->maxShadowRenderCount = count; +} diff --git a/application.hh b/application.hh index 0a58d84..169a664 100644 --- a/application.hh +++ b/application.hh @@ -22,6 +22,7 @@ class Application { void setWindowHeight(int windowHeight); void setShadowCubeSize(int shadowCubeSize); void setFarPlane(float farPlane); + void setMaxShadowRenderCount(int count); void setCompositionsPath(std::string compositionsPath); void setShaderPath(std::string shaderPath); void setGeometryPath(std::string geometryPath); @@ -38,6 +39,7 @@ class Application { int windowWidth; int windowHeight; int shadowCubeSize; + int maxShadowRenderCount; float farPlane; std::string compositionsPath; std::string shaderPath; diff --git a/data/config.xml b/data/config.xml index fd1c30f..fc95c9d 100644 --- a/data/config.xml +++ b/data/config.xml @@ -7,6 +7,8 @@ 150.0 +10 + ../Levels/ObjectSetups/Compositions.xml Shader/ diff --git a/graphics.cc b/graphics.cc index 958e292..3362e17 100644 --- a/graphics.cc +++ b/graphics.cc @@ -11,11 +11,14 @@ using namespace ACGL::OpenGL; const double lightUpdateDelay = 0.5f; -Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size) { +Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, + float farPlane, int cube_size, + unsigned int maxShadowRenderCount) { this->windowSize = windowSize; this->nearPlane = nearPlane; this->farPlane = farPlane; this->cube_size = cube_size; + this->maxShadowRenderCount = maxShadowRenderCount; } Graphics::Graphics() { @@ -109,7 +112,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_pointlightsize(); i_pointlight++) { + for (unsigned int i_pointlight = 0; i_pointlightsize() && i_pointlight < maxShadowRenderCount; 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); diff --git a/graphics.hh b/graphics.hh index 67a3beb..1876899 100644 --- a/graphics.hh +++ b/graphics.hh @@ -10,7 +10,7 @@ class Graphics { public: - Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size); + Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size, unsigned int maxShadowRenderCount); Graphics(); void init(Level* level); void render(double time); @@ -38,6 +38,7 @@ class Graphics { std::vector depth_cubeMaps; ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube; int cube_size; + unsigned int maxShadowRenderCount; Level* level; int number_of_texture_units = 0; }; diff --git a/loader.cc b/loader.cc index 2c49802..a65dbc8 100644 --- a/loader.cc +++ b/loader.cc @@ -5,7 +5,7 @@ Loader::Loader() { } void Loader::loadConfig(Application* application) { - int windowWidth, windowHeight, shadowCubeSize; + int windowWidth, windowHeight, shadowCubeSize, maxShadowRenderCount; float farPlane; std::string compositionsPath, shaderPath, geometryPath, texturePath, scriptPath, heightmapPath, levelXmlPath; XMLDocument* config = new XMLDocument(); @@ -20,6 +20,7 @@ void Loader::loadConfig(Application* application) { errorCheck(resolution->FirstChildElement("height")->QueryIntText(&windowHeight)); errorCheck(config->FirstChildElement("shadowCubeSize")->QueryIntText(&shadowCubeSize)); errorCheck(config->FirstChildElement("farPlane")->QueryFloatText(&farPlane)); + errorCheck(config->FirstChildElement("maxShadowRenderCount")->QueryIntText(&maxShadowRenderCount)); const char* charCompositionsPath = config->FirstChildElement("compositionsPath")->GetText(); if(charCompositionsPath == NULL){ @@ -74,6 +75,7 @@ void Loader::loadConfig(Application* application) { application->setWindowHeight(windowHeight); application->setShadowCubeSize(shadowCubeSize); application->setFarPlane(farPlane); + application->setMaxShadowRenderCount(maxShadowRenderCount); application->setCompositionsPath(compositionsPath); application->setShaderPath(shaderPath); application->setGeometryPath(geometryPath); From 818178e665683fcfbd1d89bdb39b783f285e8e02 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 13 Feb 2015 16:34:39 +0100 Subject: [PATCH 25/29] Camera now with less jitter! --- physics.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/physics.cc b/physics.cc index 7050a08..60a28b6 100644 --- a/physics.cc +++ b/physics.cc @@ -354,6 +354,9 @@ void Physics::addCamera() //Camera Creator automatically called when player is c cameraBody->setDamping(0.9,0.5); //this damping factor leaves a relativly smoothe system + info.m_friction = 0; + info.m_restitution = 0; + world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision); cameraBody->setGravity(btVector3(0,0,0)); From 3f1616adf1f339593589ad6cdc83611d6bb5f054 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 13 Feb 2015 16:35:35 +0100 Subject: [PATCH 26/29] corrected camera position for specular lighting --- graphics.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphics.cc b/graphics.cc index 809f67f..38702f1 100644 --- a/graphics.cc +++ b/graphics.cc @@ -177,7 +177,7 @@ void Graphics::render(double time) // set Material Parameters lightingShader->setUniform("ambientColor", level->getAmbientLight()); - lightingShader->setUniform("camera", level->getCameraPosition()); + lightingShader->setUniform("camera", level->getPhysics()->getCameraPosition()); //set view and projection matrix glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level); From 377902a2558d492f3ed56c762e709f40a1bcf040 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 17:02:36 +0100 Subject: [PATCH 27/29] Now also ignoring *.swp files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b3bbf0c..e30bed8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ CMakeLists.txt.user *.zip *.db *.blend1 +*.swp From fd1f32be6ae164fe06575bf623ada3311d2d2476 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 17:09:25 +0100 Subject: [PATCH 28/29] maxShadowRenderCount now works correctly. --- Shader/phong.fsh | 31 ++++++++++++------------------- graphics.cc | 47 +++++++++++++++++++++-------------------------- graphics.hh | 3 +-- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Shader/phong.fsh b/Shader/phong.fsh index e9a7f13..08a6bf0 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -26,6 +26,7 @@ uniform float specularFactor; uniform vec3 camera; uniform float shininess; uniform int lightCount; +uniform int maxShadowRenderCount; uniform vec3 directionalLightVector; uniform vec3 directionalColor; uniform float directionalIntensity; @@ -115,45 +116,37 @@ void main() for(int i = 0; ibind(); - for (unsigned int i_pointlight = 0; i_pointlightsize() && i_pointlight < maxShadowRenderCount; i_pointlight++) { + for (unsigned int i_pointlight = 0; i_pointlightgetObjectName(), 0); glClear(GL_DEPTH_BUFFER_BIT); - glm::mat4 viewMatrix = glm::lookAt(closestLights->at(i_pointlight).getPosition(), - closestLights->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); @@ -200,43 +200,38 @@ bool Graphics::compareLightDistances(Light a, Light b) { } 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; - } + closestLights = std::vector(*level->getLights()); + std::sort(closestLights.begin(), + closestLights.end(), + [this](Light a, Light b) {return compareLightDistances(a, b); }); + closestLights = std::vector(&closestLights[0], + &closestLights[31]); } void Graphics::updateShaderLights() { updateClosestLights(); - if (closestLights->size() > 0) { - lightingShader->setUniform("lightCount", (int) closestLights->size()); + if (closestLights.size() > 0) { + lightingShader->setUniform("lightCount", (int) closestLights.size()); + lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights.size(), (int)maxShadowRenderCount)); // Build light position array - glm::vec3 lightSources[closestLights->size()]; - for(unsigned int i = 0; isize(); i++) { - lightSources[i] = closestLights->at(i).getPosition(); + glm::vec3 lightSources[closestLights.size()]; + for(unsigned int i = 0; igetUniformLocation("lightSources"), sizeof(lightSources), (GLfloat*) lightSources); // Build light colour array - glm::vec3 lightColours[closestLights->size()]; - for(unsigned int i = 0; isize(); i++) { - lightColours[i] = closestLights->at(i).getColour(); + glm::vec3 lightColours[closestLights.size()]; + for(unsigned int i = 0; igetUniformLocation("lightColors"), sizeof(lightColours), (GLfloat*) lightColours); // Build light attenuation array - float lightIntensities[closestLights->size()]; - for(unsigned int i = 0; isize(); i++) { - lightIntensities[i] = closestLights->at(i).getIntensity(); + float lightIntensities[closestLights.size()]; + for(unsigned int i = 0; igetUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); diff --git a/graphics.hh b/graphics.hh index 1876899..ec348f2 100644 --- a/graphics.hh +++ b/graphics.hh @@ -28,8 +28,7 @@ class Graphics { 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 + std::vector closestLights; ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram depthCubeShader; ACGL::OpenGL::SharedShaderProgram depthShader; From 5e07aec885adb7ec3323c9b0ffcc27fd5f57b96a Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Fri, 13 Feb 2015 17:11:47 +0100 Subject: [PATCH 29/29] Updated Readme.md. --- Readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Readme.md b/Readme.md index 78890de..8da28f9 100644 --- a/Readme.md +++ b/Readme.md @@ -5,3 +5,8 @@ This will be a Marble Race Game developed for the Softwarepraktikum at the RWTH ##Building Currently only tested on Linux. To build execute build.sh. Resulting binary will be in binaries. You can also use the run.sh script to directly start the binary after building it. + +##Control + +You can control the camera by moving the mouse. You can zoom with the mouse wheel. +The marble is controlled via the W,A,S and D keys.