diff --git a/shaders/programDepth.frag b/shaders/programDepth.frag new file mode 100644 index 0000000..4be7599 --- /dev/null +++ b/shaders/programDepth.frag @@ -0,0 +1,5 @@ +#version 430 core + +void main() +{ +} \ No newline at end of file diff --git a/shaders/programDepth.vert b/shaders/programDepth.vert new file mode 100644 index 0000000..e4f2307 --- /dev/null +++ b/shaders/programDepth.vert @@ -0,0 +1,13 @@ +#version 330 core + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexNormal; +layout(location = 2) in vec2 vertexTexCoord; + +uniform mat4 viewProjectionMatrix; +uniform mat4 modelMatrix; + +void main() +{ + gl_Position = viewProjectionMatrix * modelMatrix * vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/shaders/shader_9_1.frag b/shaders/shader_9_1.frag index 550f6c1..c7bc4ae 100644 --- a/shaders/shader_9_1.frag +++ b/shaders/shader_9_1.frag @@ -4,6 +4,9 @@ float AMBIENT = 0.03; float PI = 3.14; uniform sampler2D depthMap; +//uniform sampler2D depthMaplamp; +uniform sampler2D depthMapflash; + uniform vec3 cameraPos; @@ -35,9 +38,28 @@ in vec3 viewDirTS; in vec3 lightDirTS; in vec3 spotlightDirTS; in vec3 sunDirTS; +in vec4 sunSpacePos; +in vec4 lampSpacePos; +in vec4 flashSpacePos; in vec3 test; +float calculateShadow() +{ + vec4 lightSpacePosNormalized = (0.5*sunSpacePos / (sunSpacePos.w)) + 0.5; + float closestDepth = texture2D(depthMap, lightSpacePosNormalized.xy).x; + float diff = (0.001+closestDepth) - lightSpacePosNormalized.z;//lightSpacePosNormalized.z; + return (0.5*(diff)/abs(diff))+0.5; +} + +float calculateShadowOtherLigths(vec4 ligthSpacePos, sampler2D ligthDepthMap) +{ + vec4 lightSpacePosNormalized = (0.5*ligthSpacePos / (ligthSpacePos.w)) + 0.5; + float closestDepth = texture2D(ligthDepthMap, lightSpacePosNormalized.xy).x; + float diff = (0.001+closestDepth) - lightSpacePosNormalized.z;//lightSpacePosNormalized.z; + + return (0.5*(diff)/abs(diff))+0.5; +} float DistributionGGX(vec3 normal, vec3 H, float roughness){ float a = roughness*roughness; float a2 = a*a; @@ -123,13 +145,13 @@ void main() float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1); attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); - ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir); + ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor*calculateShadowOtherLigths(flashSpacePos, depthMapflash),normal,viewDir); //sun - ilumination=ilumination+PBRLight(sunDir,sunColor,normal,viewDir); + ilumination=ilumination+PBRLight(sunDir,sunColor*calculateShadow(),normal,viewDir); outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1); - //outColor = vec4(roughness,metallic,0,1); + //outColor = (0.5*sunSpacePos / (sunSpacePos.w+0.0001)) + 0.5; //outColor = vec4(test; } diff --git a/shaders/shader_9_1.vert b/shaders/shader_9_1.vert index 750196d..814168f 100644 --- a/shaders/shader_9_1.vert +++ b/shaders/shader_9_1.vert @@ -9,9 +9,6 @@ layout(location = 4) in vec3 vertexBitangent; uniform mat4 transformation; uniform mat4 modelMatrix; -out vec3 vecNormal; -out vec3 worldPos; - uniform vec3 lightPos; uniform vec3 spotlightPos; uniform vec3 cameraPos; @@ -21,6 +18,17 @@ out vec3 viewDirTS; out vec3 lightDirTS; out vec3 spotlightDirTS; out vec3 sunDirTS; +out vec3 worldPos; +out vec3 vecNormal; +out vec4 sunSpacePos; +out vec4 lampSpacePos; +out vec4 flashSpacePos; + + + +uniform mat4 LightVP; +uniform mat4 LightVPlamp; +uniform mat4 LightVPflash; void main() { @@ -40,4 +48,8 @@ void main() spotlightDirTS = TBN*SL; sunDirTS = TBN*sunDir; + sunSpacePos=LightVP*modelMatrix*vec4(vertexPosition,1); + lampSpacePos=LightVPlamp*modelMatrix*vec4(vertexPosition,1); + flashSpacePos=LightVPflash*modelMatrix*vec4(vertexPosition,1); + } diff --git a/src/ex_9_1.hpp b/src/ex_9_1.hpp index 36f1cdc..60375f5 100644 --- a/src/ex_9_1.hpp +++ b/src/ex_9_1.hpp @@ -36,13 +36,18 @@ namespace models { Core::RenderContext testContext; } -GLuint depthMapFBO; -GLuint depthMap; +GLuint depthMapFBOsun; +GLuint depthMapFBOlamp; +GLuint depthMapFBOflash; +GLuint depthMapsun; +GLuint depthMaplamp; +GLuint depthMapflash; GLuint program; GLuint programSun; GLuint programTest; GLuint programTex; +GLuint programDepth; Core::Shader_Loader shaderLoader; @@ -74,20 +79,11 @@ glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3; float spotlightPhi = 3.14 / 4; +glm::mat4 lightVPsun; +glm::mat4 lightVPlamp; +glm::mat4 lightVPflash; -float lastTime = -1.f; -float deltaTime = 0.f; -void updateDeltaTime(float time) { - if (lastTime < 0) { - lastTime = time; - return; - } - - deltaTime = time - lastTime; - if (deltaTime > 0.1) deltaTime = 0.1; - lastTime = time; -} glm::mat4 createCameraMatrix() { glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); @@ -124,6 +120,24 @@ glm::mat4 createPerspectiveMatrix() return perspectiveMatrix; } +glm::mat4 createPerspectiveMatrixC(float n, float f) +{ + + glm::mat4 perspectiveMatrix; + float a1 = glm::min(aspectRatio, 1.f); + float a2 = glm::min(1 / aspectRatio, 1.f); + perspectiveMatrix = glm::mat4({ + 1,0.,0.,0., + 0.,aspectRatio,0.,0., + 0.,0.,(f + n) / (n - f),2 * f * n / (n - f), + 0.,0.,-1.,0., + }); + + + perspectiveMatrix = glm::transpose(perspectiveMatrix); + + return perspectiveMatrix; +} void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) { @@ -151,15 +165,85 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec glUniform3f(glGetUniformLocation(program, "spotlightPos"), spotlightPos.x, spotlightPos.y, spotlightPos.z); glUniform3f(glGetUniformLocation(program, "spotlightColor"), spotlightColor.x, spotlightColor.y, spotlightColor.z); glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spotlightPhi); + + + glUniformMatrix4fv(glGetUniformLocation(program, "LightVP"), 1, GL_FALSE, (float*)&lightVPsun); + //glUniformMatrix4fv(glGetUniformLocation(program, "LightVPlamp"), 1, GL_FALSE, (float*)&lightVPlamp); + glUniformMatrix4fv(glGetUniformLocation(program, "LightVPflash"), 1, GL_FALSE, (float*)&lightVPflash); + + + //glUniform1i(glGetUniformLocation(program, "depthMap"), depthMap); + //glActiveTexture(GL_TEXTURE0); + //glBindTexture(GL_TEXTURE_2D, depthMap); + //void Core::SetActiveTexture(GLuint textureID, const char* shaderVariableName, GLuint programID, int textureUnit) + //{ + //glUniform1i(glGetUniformLocation(programID, shaderVariableName), textureUnit); + //glActiveTexture(GL_TEXTURE0 + textureUnit); + //glBindTexture(GL_TEXTURE_2D, textureID); + //} + glUniform1i(glGetUniformLocation(program, "depthMapflash"), 1); + glActiveTexture(GL_TEXTURE0+1); + glBindTexture(GL_TEXTURE_2D, depthMapflash); + //glActiveTexture(GL_TEXTURE0 + 1); + //glBindTexture(GL_TEXTURE_2D, depthMaplamp); + glUniform1i(glGetUniformLocation(program, "depthMap"), 2); + glActiveTexture(GL_TEXTURE0 + 2); + glBindTexture(GL_TEXTURE_2D, depthMapsun); + Core::DrawContext(context); } -void renderShadowapSun() { +void drawObjectDepth(Core::RenderContext& RenderContext, glm::mat4 viewProjection, glm::mat4 modelMatrix) +{ + + glUniformMatrix4fv(glGetUniformLocation(programDepth, "viewProjectionMatrix"), 1, GL_FALSE, (float*)&viewProjection); + glUniformMatrix4fv(glGetUniformLocation(programDepth, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); + Core::DrawContext(RenderContext); +} +void renderShadowapSun(GLuint depthMapFBO, glm::mat4 lightVP) { float time = glfwGetTime(); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); //uzupelnij o renderowanie glebokosci do tekstury + glUseProgram(programDepth); + //ustawianie przestrzeni rysowania + glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); + //bindowanie FBO + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); + //czyszczenie mapy głębokości + glClear(GL_DEPTH_BUFFER_BIT); + //ustawianie programu + glUseProgram(programDepth); + + + drawObjectDepth(sphereContext, lightVP, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::scale(glm::vec3(0.3f))); + + drawObjectDepth(sphereContext, + lightVP, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f))); + + drawObjectDepth(models::bedContext, lightVP, glm::mat4()); + drawObjectDepth(models::chairContext, lightVP, glm::mat4()); + drawObjectDepth(models::deskContext, lightVP, glm::mat4()); + drawObjectDepth(models::doorContext, lightVP, glm::mat4()); + drawObjectDepth(models::drawerContext, lightVP, glm::mat4()); + drawObjectDepth(models::marbleBustContext, lightVP, glm::mat4()); + drawObjectDepth(models::materaceContext, lightVP, glm::mat4()); + drawObjectDepth(models::pencilsContext, lightVP, glm::mat4()); + drawObjectDepth(models::planeContext, lightVP, glm::mat4()); + drawObjectDepth(models::roomContext, lightVP, glm::mat4()); + drawObjectDepth(models::windowContext, lightVP, glm::mat4()); + + glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); + glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir)); + glm::mat4 specshipCameraRotrationMatrix = glm::mat4({ + spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0, + spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0, + -spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0, + 0.,0.,0.,1., + }); + drawObjectDepth(shipContext, lightVP, + glm::translate(spaceshipPos)* specshipCameraRotrationMatrix* glm::eulerAngleY(glm::pi())* glm::scale(glm::vec3(0.03f))); @@ -174,8 +258,14 @@ void renderScene(GLFWwindow* window) glClearColor(0.4f, 0.4f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float time = glfwGetTime(); - updateDeltaTime(time); - renderShadowapSun(); + + lightVPsun = glm::ortho(-3.f, 2.3f, -1.3f, 3.f, -1.0f, 40.0f) * glm::lookAt(sunPos, sunPos - sunDir, glm::vec3(0, 1, 0)); + lightVPlamp = createPerspectiveMatrixC(0.5, 30) * glm::lookAt(pointlightPos, glm::vec3(0, -1, 0), glm::vec3(0, 1, 0)); + lightVPflash = createPerspectiveMatrixC(0.05, 20) * glm::lookAt(spotlightPos, spotlightConeDir, glm::vec3(0, 1, 0)); + + renderShadowapSun(depthMapFBOsun, lightVPsun); + renderShadowapSun(depthMapFBOlamp, lightVPlamp); + renderShadowapSun(depthMapFBOflash, lightVPflash); //space lamp glUseProgram(programSun); @@ -231,11 +321,11 @@ void renderScene(GLFWwindow* window) - //test depth buffer + ////test depth buffer //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glUseProgram(programTest); //glActiveTexture(GL_TEXTURE0); - //glBindTexture(GL_TEXTURE_2D, depthMap); + //glBindTexture(GL_TEXTURE_2D, depthMaplamp); //Core::DrawContext(models::testContext); glUseProgram(0); @@ -261,14 +351,74 @@ void loadModelToContext(std::string path, Core::RenderContext& context) context.initFromAssimpMesh(scene->mMeshes[0]); } +void initDepthMap() +{ + glGenFramebuffers(1, &depthMapFBOsun); + + glGenTextures(1, &depthMapsun); + glBindTexture(GL_TEXTURE_2D, depthMapsun); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, + SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBOsun); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMapsun, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + + + glGenFramebuffers(1, &depthMapFBOlamp); + + glGenTextures(1, &depthMaplamp); + glBindTexture(GL_TEXTURE_2D, depthMaplamp); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, + SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBOlamp); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMaplamp, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + + + glGenFramebuffers(1, &depthMapFBOflash); + + glGenTextures(1, &depthMapflash); + glBindTexture(GL_TEXTURE_2D, depthMapflash); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, + SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBOflash); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMapflash, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + void init(GLFWwindow* window) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + initDepthMap(); glEnable(GL_DEPTH_TEST); program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); + programDepth = shaderLoader.CreateProgram("shaders/programDepth.vert", "shaders/programDepth.frag"); loadModelToContext("./models/sphere.obj", sphereContext); loadModelToContext("./models/spaceship.obj", shipContext); @@ -301,8 +451,8 @@ void processInput(GLFWwindow* window) { glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f,1.f,0.f))); glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f); - float angleSpeed = 0.05f * deltaTime * 60; - float moveSpeed = 0.05f * deltaTime * 60; + float angleSpeed = 0.005f; + float moveSpeed = 0.005f; if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } @@ -327,9 +477,9 @@ void processInput(GLFWwindow* window) cameraDir = spaceshipDir; if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) - exposition -= 0.05; + exposition -= 0.005; if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) - exposition += 0.05; + exposition += 0.005; if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) { printf("spaceshipPos = glm::vec3(%ff, %ff, %ff);\n", spaceshipPos.x, spaceshipPos.y, spaceshipPos.z);