From 87a140cd0a4ba9a956969a035c03a70298d7ac7b Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 30 Apr 2021 21:17:16 +0300 Subject: [PATCH] broken up framebuffer processing methods fixed absent shadow bug --- light.cpp | 7 +++++-- root.cpp | 32 ++++++++++++++++++-------------- root.h | 4 +++- texture.frag | 4 +--- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/light.cpp b/light.cpp index 561aadc..7e22ea2 100755 --- a/light.cpp +++ b/light.cpp @@ -176,8 +176,11 @@ namespace vb01{ shader->setVec3(color, "light[" + to_string(thisId) + "].color"); shader->setFloat(nearPlane, "light[" + to_string(thisId) + "].near"); shader->setFloat(farPlane, "light[" + to_string(thisId) + "].far"); - shader->setInt(12, "light[" + to_string(thisId) + "].depthMap" + (type == POINT ? "Cube" : "")); - depthMap->select(12); + + int depthMapId = 10; + shader->setInt(depthMapId, "light[" + to_string(thisId) + "].depthMap"); + shader->setInt(depthMapId + 1, "light[" + to_string(thisId) + "].depthMapCube"); + depthMap->select(depthMapId + (type == POINT ? 1 : 0)); switch(type){ case POINT: diff --git a/root.cpp b/root.cpp index 2cf3f4a..5b9d8ee 100755 --- a/root.cpp +++ b/root.cpp @@ -48,7 +48,8 @@ namespace vb01{ Texture *fragTexture = new Texture(width, height, false); Texture *brightTexture = new Texture(width, height, false); - initBloomFramebuffer(fragTexture, brightTexture); + initMainFramebuffer(fragTexture, brightTexture); + initBloomFramebuffer(); initGuiPlane(fragTexture, brightTexture); } @@ -76,24 +77,26 @@ namespace vb01{ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } - void Root::initBloomFramebuffer(Texture *fragTexture, Texture *brightTexture){ + void Root::initMainFramebuffer(Texture *fragTexture, Texture *brightTexture){ glGenFramebuffers(1, &FBO); - glBindFramebuffer(GL_FRAMEBUFFER,FBO); + glBindFramebuffer(GL_FRAMEBUFFER, FBO); - for(int i = 0; i < 2; i++) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0); u32 colorAttachments[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; + for(int i = 0; i < 2; i++) + glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[i], GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0); glDrawBuffers(2, colorAttachments); glGenRenderbuffers(1, &RBO); glBindRenderbuffer(GL_RENDERBUFFER, RBO); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,RBO); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO); if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) cout << "Not complete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + void Root::initBloomFramebuffer(){ string basePath = libPath + "blur."; blurShader = new Shader(basePath + "vert", basePath + "frag"); glGenFramebuffers(2, pingpongBuffers); @@ -115,7 +118,7 @@ namespace vb01{ } void Root::update(){ - glBindFramebuffer(GL_FRAMEBUFFER,FBO); + glBindFramebuffer(GL_FRAMEBUFFER, FBO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glEnable(GL_DEPTH_TEST); @@ -138,8 +141,7 @@ namespace vb01{ glDisable(GL_DEPTH_TEST); updateBloomFramebuffer(); - - guiPlane->render(); + updateGuiPlane(); glEnable(GL_DEPTH_TEST); guiNode->update(); @@ -156,14 +158,16 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[horizontal]); blurShader->setBool(horizontal, "horizontal"); if(i == 0) - glBindTexture(GL_TEXTURE_2D, *(guiPlane->getMaterial()->getDiffuseMap(1)->getTexture())); + guiPlane->getMaterial()->getDiffuseMap(1)->select(); else pingPongTextures[!horizontal]->select(); guiPlane->render(); horizontal = !horizontal; } glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + void Root::updateGuiPlane(){ Material *material = guiPlane->getMaterial(); Shader *shader = material->getShader(); @@ -173,12 +177,12 @@ namespace vb01{ shader->setBool(hdr, "hdr"); shader->setFloat(exposure, "exposure"); shader->setFloat(gamma, "gamma"); - - material->getDiffuseMap(0)->select(); - pingPongTextures[!horizontal]->select(1); - shader->setInt(0, "frag"); shader->setInt(1, "bright"); + + material->getDiffuseMap(0)->select(0); + material->getDiffuseMap(1)->select(1); + guiPlane->render(); } void Root::createSkybox(string textures[6]){ diff --git a/root.h b/root.h index 16c78f2..cdc98be 100755 --- a/root.h +++ b/root.h @@ -61,9 +61,11 @@ namespace vb01{ void framebuffer_size_callback(GLFWwindow*, int, int); Root(); void initWindow(std::string); - void initBloomFramebuffer(Texture*, Texture*); + void initMainFramebuffer(Texture*, Texture*); + void initBloomFramebuffer(); void initGuiPlane(Texture*, Texture*); void updateBloomFramebuffer(); + void updateGuiPlane(); }; } diff --git a/texture.frag b/texture.frag index dd7a9be..57eba6d 100755 --- a/texture.frag +++ b/texture.frag @@ -46,7 +46,6 @@ float getShadow(int id){ int type = light[id].type; float shadow = 0, closestDepth = 0, currentDepth = 0, bias = .005, val = .7; -/* if(type == 0){ vec3 projDir = fragPos - light[id].pos; closestDepth = texture(light[id].depthMapCube, projDir).r * light[id].far; @@ -54,8 +53,6 @@ float getShadow(int id){ shadow = (currentDepth - bias > closestDepth) ? val : 0; } else{ - } -*/ vec4 lightSpaceFragPos = light[id].lightMat * vec4(fragPos, 1); vec3 projCoords = (lightSpaceFragPos.xyz / lightSpaceFragPos.w) * .5 + .5; closestDepth = texture(light[id].depthMap, projCoords.xy).r; @@ -67,6 +64,7 @@ float getShadow(int id){ shadow = (currentDepth - bias > closestDepth) ? val : 0; if(projCoords.z > 1) shadow = 0; + } return shadow; }