diff --git a/blur.frag b/blur.frag new file mode 100644 index 0000000..ba4f58d --- /dev/null +++ b/blur.frag @@ -0,0 +1,22 @@ +#version 330 core + +in vec2 texCoords; + +out vec4 FragColor; + +uniform bool horizontal; +uniform sampler2D tex; + +void main(){ + int amount=5; + float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054,0.016216); + vec2 texel=1/textureSize(tex,0); + vec3 c=texture(tex,texCoords).rgb*weight[0]; + + for(int i=1;igetTexture()),0); + Texture *fragTexture=new Texture(width,height,false); + Texture *brightTexture=new Texture(width,height,false); + 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}; + glDrawBuffers(2,colorAttachments); glGenRenderbuffers(1,&RBO); glBindRenderbuffer(GL_RENDERBUFFER,RBO); @@ -76,11 +81,22 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER,0); guiPlane=new Quad(Vector3(width,height,-1),false); - Material *mat=new Material(Material::MATERIAL_GUI); - mat->addDiffuseMap(texture); + Material *mat=new Material(Material::MATERIAL_POST); + mat->addDiffuseMap(fragTexture); + mat->addDiffuseMap(brightTexture); guiPlane->setMaterial(mat); - mat->getShader()->use(); - mat->getShader()->setBool(true,"guiPlane"); + + string basePath="../../vb01/"; + blurShader=new Shader(basePath+"blur.vert",basePath+"blur.frag"); + glGenFramebuffers(2,pingpongBuffers); + for(int i=0;i<2;i++){ + pingPongTextures[i]=new Texture(width,height,false); + glBindFramebuffer(GL_RENDERBUFFER,pingpongBuffers[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,*(pingPongTextures[i]->getTexture()),0); + if(glCheckFramebufferStatus(GL_FRAMEBUFFER)!=GL_FRAMEBUFFER_COMPLETE) + cout<<"Not complete\n"; + glBindFramebuffer(GL_RENDERBUFFER,0); + } } void Root::update(){ @@ -106,7 +122,35 @@ namespace vb01{ glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); - guiPlane->update(); + Material *material=guiPlane->getMaterial(); + Shader *shader=material->getShader(); + + /* + int ammount=10; + bool horizontal=false; + blurShader->use(); + for(int i=0;isetBool(horizontal,"horizontal"); + glBindFramebuffer(GL_FRAMEBUFFER,pingpongBuffers[horizontal]); + if(i==0) + material->getDiffuseMap(1)->update(); + else + pingPongTextures[!horizontal]->update(); + guiPlane->render(); + horizontal=!horizontal; + } + glBindFramebuffer(GL_FRAMEBUFFER,0); + */ + + shader->use(); + shader->setVec2(Vector2(width,height),"screen"); + shader->setBool(bloom,"bloom"); + material->getDiffuseMap(0)->update(); + material->getDiffuseMap(1)->update(1); + //pingPongTextures[1]->update(1); + shader->setInt(0,"frag"); + shader->setInt(1,"bright"); + guiPlane->render(); glEnable(GL_DEPTH_TEST); guiNode->update(); diff --git a/root.h b/root.h index 844a0cf..2434a04 100755 --- a/root.h +++ b/root.h @@ -13,8 +13,9 @@ namespace vb01{ class Mesh; class Box; class Quad; - class Shader; class Node; + class Texture; + class Shader; class Root{ public: @@ -27,17 +28,21 @@ namespace vb01{ inline int getHeight(){return height;} inline unsigned int* getFBO(){return &FBO;} inline GLFWwindow* getWindow(){return window;} + inline void setBloom(bool bloom){this->bloom=bloom;} void createSkybox(std::string[6]); static Root* getSingleton(); int numLights=0; private: + bool bloom=false; Box *skybox=nullptr; Quad *guiPlane=nullptr; int width,height; - unsigned int FBO,RBO; + unsigned int FBO,RBO,pingpongBuffers[2]; GLFWwindow *window; Node *rootNode,*guiNode; Camera *camera; + Shader *blurShader; + Texture *pingPongTextures[2]; std::vector meshes; void framebuffer_size_callback(GLFWwindow*,int,int); diff --git a/texture.cpp b/texture.cpp index 54748f0..cc2830e 100755 --- a/texture.cpp +++ b/texture.cpp @@ -25,7 +25,8 @@ namespace vb01{ glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,borderColor); } else{ - glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA16F,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); + glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA16F,width,height,0,GL_RGB,GL_FLOAT,NULL); + //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA16F,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); diff --git a/texture.frag b/texture.frag index f83f41e..5430d26 100755 --- a/texture.frag +++ b/texture.frag @@ -8,7 +8,8 @@ in vec3 norm; in vec2 texCoords; in vec4 lightSpaceFragPos; -out vec4 FragColor; +layout (location=0) out vec4 FragColor; +layout (location=1) out vec4 BrightColor; //0-POINT,1-DIRECTIONAL,2-SPOT @@ -121,5 +122,8 @@ void main(){ finalColor*=vec4(diffuseColor+specularColor,1); } + float brightness = dot(finalColor.rgb, vec3(0.2126, 0.7152, 0.0722)); + if(brightness>1) + BrightColor=vec4(finalColor.rgb,1); FragColor=finalColor; }