mirror of
https://github.com/devZoGok/vb01.git
synced 2026-08-26 19:43:30 +00:00
blurring (not Gaussian blur)
This commit is contained in:
@@ -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;i<amount;i++){
|
||||
vec2 offset=vec2(horizontal?texel.x:0,horizontal?0:texel.y);
|
||||
c+=texture(tex,texCoords+offset*i).rgb*weight[i];
|
||||
c+=texture(tex,texCoords-offset*i).rgb*weight[i];
|
||||
}
|
||||
FragColor=vec4(c,1);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location=0) in vec3 aVert;
|
||||
layout (location=2) in vec2 aTexCoords;
|
||||
|
||||
out vec2 texCoords;
|
||||
|
||||
uniform vec2 screen;
|
||||
|
||||
void main(){
|
||||
float x,y;
|
||||
x=(aVert.x-screen.x*.5)/(screen.x*.5);
|
||||
y=(screen.y*.5-aVert.y)/(screen.y*.5);
|
||||
gl_Position=vec4(x,y,aVert.z,1);
|
||||
texCoords=aTexCoords;
|
||||
}
|
||||
@@ -21,6 +21,9 @@ namespace vb01{
|
||||
case MATERIAL_GUI:
|
||||
shaderName="gui.";
|
||||
break;
|
||||
case MATERIAL_POST:
|
||||
shaderName="postProcess.";
|
||||
break;
|
||||
case MATERIAL_TEXT:
|
||||
shaderName="text.";
|
||||
break;
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
namespace vb01{
|
||||
class Material{
|
||||
public:
|
||||
enum Type{MATERIAL_2D,MATERIAL_PARTICLE,MATERIAL_SKYBOX,MATERIAL_GUI,MATERIAL_TEXT};
|
||||
enum Type{MATERIAL_2D,MATERIAL_PARTICLE,MATERIAL_SKYBOX,MATERIAL_GUI,MATERIAL_POST,MATERIAL_TEXT};
|
||||
|
||||
Material(Type=MATERIAL_2D);
|
||||
~Material();
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#version 330 core
|
||||
|
||||
in vec2 texCoords;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform sampler2D frag;
|
||||
uniform sampler2D bright;
|
||||
uniform bool bloom;
|
||||
|
||||
void main(){
|
||||
vec4 hdrColor=texture(frag,texCoords);
|
||||
vec4 brightColor=texture(bright,texCoords);
|
||||
|
||||
if(bloom){
|
||||
float blurKernel[9]=float[](
|
||||
1.0/16,2.0/16,1.0/16,
|
||||
2.0/16,4.0/16,1.0/16,
|
||||
1.0/16,2.0/16,1.0/16);
|
||||
vec2 texel=1.0/textureSize(bright,0);
|
||||
|
||||
vec3 sum=vec3(0);
|
||||
for(int i=0;i<3;i++){
|
||||
for(int j=0;j<3;j++){
|
||||
vec3 adjSample=texture(frag,texCoords+vec2(texel.x*(j-1),texel.y*(i-1))).rgb;
|
||||
adjSample*=blurKernel[i*3+j];
|
||||
sum+=adjSample;
|
||||
}
|
||||
}
|
||||
hdrColor.rgb=sum;
|
||||
}
|
||||
|
||||
//hdrColor=brightColor;
|
||||
float gamma=1,exposure=1;
|
||||
|
||||
//Reinhardt
|
||||
//c.rgb=c.rgb/(c.rgb+vec3(1.));
|
||||
|
||||
//Exp
|
||||
hdrColor.rgb=vec3(1.)-exp(-hdrColor.rgb*exposure);
|
||||
|
||||
hdrColor.rgb=pow(hdrColor.rgb,vec3(1/gamma));
|
||||
FragColor=hdrColor;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location=0) in vec3 aVert;
|
||||
layout (location=2) in vec2 aTexCoords;
|
||||
|
||||
out vec2 texCoords;
|
||||
|
||||
uniform vec2 screen;
|
||||
|
||||
void main(){
|
||||
float x,y;
|
||||
x=(aVert.x-screen.x*.5)/(screen.x*.5);
|
||||
y=(screen.y*.5-aVert.y)/(screen.y*.5);
|
||||
gl_Position=vec4(x,y,aVert.z,1);
|
||||
texCoords=aTexCoords;
|
||||
}
|
||||
@@ -60,8 +60,13 @@ namespace vb01{
|
||||
|
||||
glGenFramebuffers(1,&FBO);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER,FBO);
|
||||
Texture *texture=new Texture(width,height,false);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,*(texture->getTexture()),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;i<ammount;i++){
|
||||
blurShader->setBool(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();
|
||||
|
||||
@@ -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<Mesh*> meshes;
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow*,int,int);
|
||||
|
||||
+2
-1
@@ -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);
|
||||
|
||||
+5
-1
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user