From 0d6b01421f90a63440cb2943b3b384fbf218d50b Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 1 Mar 2020 19:10:27 +0200 Subject: [PATCH] Gaussian blur bug fixes --- blur.frag | 2 +- light.cpp | 22 +++++++------- material.cpp | 16 +++++++---- mesh.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++- mesh.h | 10 ++++++- model.cpp | 9 ++++++ model.h | 3 +- particle.frag | 8 ++++-- postProcess.frag | 36 ++++++++--------------- root.cpp | 29 +++++++++---------- root.h | 10 +++++-- skybox.frag | 4 ++- texture.cpp | 10 +++---- texture.frag | 23 ++++++++++----- texture.geo | 41 +++++++++++++++++++++++++++ texture.h | 2 +- texture.vert | 4 --- 17 files changed, 220 insertions(+), 83 deletions(-) create mode 100644 texture.geo diff --git a/blur.frag b/blur.frag index ba4f58d..e7de584 100644 --- a/blur.frag +++ b/blur.frag @@ -10,7 +10,7 @@ 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); + vec2 texel=1.0/textureSize(tex,0); vec3 c=texture(tex,texCoords).rgb*weight[0]; for(int i=1;isetVec3(color,"light["+to_string(thisId)+"].color"); shader->setFloat(nearPlane,"light["+to_string(thisId)+"].near"); shader->setFloat(farPlane,"light["+to_string(thisId)+"].far"); - //shader->setInt(0,"tex"); - if(type==POINT) - shader->setInt(1,"light["+to_string(thisId)+"].depthMapCube"); - else - shader->setInt(1,"light["+to_string(thisId)+"].depthMap"); - depthMap->select(1); - /* - */ + shader->setInt(0,"diffuseMap"); + //shader->setInt(1,"normalMap"); + shader->setInt(2,"specularMap"); + shader->setInt(3,"parallaxMap"); + shader->setInt(4,"environmentMap"); + shader->setInt(5,"light["+to_string(thisId)+"].depthMapCube"); + shader->setInt(6,"light["+to_string(thisId)+"].depthMap"); + depthMap->select(type==POINT?5:6); switch(type){ case POINT: diff --git a/material.cpp b/material.cpp index ba6fc98..8d3c4b8 100755 --- a/material.cpp +++ b/material.cpp @@ -28,7 +28,13 @@ namespace vb01{ shaderName="text."; break; } - shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag"); + /* + if(type==MATERIAL_2D) + //shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag"); + shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag",basePath+shaderName+"geo"); + else + */ + shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag"); } Material::~Material(){ @@ -56,13 +62,13 @@ namespace vb01{ } if(texturingEnabled){ for(Texture *t : diffuseMapTextures) - t->update(); + t->update(0); for(Texture *t : normalMapTextures) - t->update(2); + t->update(1); for(Texture *t : specularMapTextures) - t->update(3); + t->update(2); for(Texture *t : parallaxMapTextures) - t->update(4); + t->update(3); } else { shader->setVec4(diffuseColor,"diffuseColor"); diff --git a/mesh.cpp b/mesh.cpp index d5e3601..fed5f37 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -1,6 +1,7 @@ #include"root.h" #include"node.h" -#include"mesh.h" +#include"box.h" +#include"texture.h" #include"skeleton.h" #include #include @@ -21,6 +22,7 @@ namespace vb01{ this->vertices=vertices; this->indices=indices; this->numTris=numTris; + construct(); } @@ -39,6 +41,27 @@ namespace vb01{ } void Mesh::construct(){ + u32 RBO; + string basePath="../../vb01/depthMap."; + environmentShader=new Shader(basePath+"vert",basePath+"frag",basePath+"geo"); + environmentMap=new Texture(width,false); + + glGenFramebuffers(1,&environmentBuffer); + glBindFramebuffer(GL_FRAMEBUFFER,environmentBuffer); + glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,*(environmentMap->getTexture()),0); + + /* + glGenRenderbuffers(1,&RBO); + glBindRenderbuffer(GL_RENDERBUFFER,RBO); + glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH24_STENCIL8,width,width); + 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); + glGenVertexArrays(1,&VAO); glGenBuffers(1,&VBO); glGenBuffers(1,&EBO); @@ -77,6 +100,49 @@ namespace vb01{ camPos=cam->getPosition(); } + if(reflect){ + glViewport(0,0,width,width); + glBindFramebuffer(GL_FRAMEBUFFER,environmentBuffer); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + vec3 dirs[]{ + vec3(1,0,0), + vec3(-1,0,0), + vec3(0,1,0), + vec3(0,-1,0), + vec3(0,0,1), + vec3(0,0,-1) + }; + environmentShader->use(); + Node *rootNode=root->getRootNode(); + vector descendants; + rootNode->getDescendants(rootNode,descendants); + for(Node *n : descendants){ + for(Mesh *m : n->getMeshes()) + if(m!=this){ + Vector3 position=n->getWorldPosition(); + Quaternion rotation=n->getWorldOrientation(); + Vector3 rotAxis=rotation.norm().getAxis(); + + if(rotAxis==Vector3::VEC_ZERO) + rotAxis=Vector3::VEC_I; + float far=100; + vec3 p=vec3(position.x,position.y,position.z); + mat4 proj=perspective(radians(90.f),1.f,.1f,far); + mat4 model=translate(mat4(1.),p); + model=rotate(model,rotation.norm().getAngle(),vec3(rotAxis.x,rotAxis.y,rotAxis.z)); + environmentShader->setMat4(model,"model"); + environmentShader->setBool(true,"point"); + environmentShader->setVec3(pos,"lightPos"); + environmentShader->setFloat(far,"farPlane"); + for(int i=0;i<6;i++) + environmentShader->setMat4(proj*lookAt(p,p+dirs[i],1render(); + } + } + glBindFramebuffer(GL_FRAMEBUFFER,*(root->getFBO())); + glViewport(0,0,root->getWidth(),root->getHeight()); + } + Vector3 rotAxis=orient.norm().getAxis(); if(rotAxis==Vector3::VEC_ZERO) rotAxis=Vector3::VEC_I; @@ -89,10 +155,16 @@ namespace vb01{ material->update(); Shader *shader=material->getShader(); shader->setBool(castShadow,"castShadow"); + shader->setBool(false,"environment"); shader->setMat4(view,"view"); shader->setMat4(proj,"proj"); shader->setVec3(camPos,"camPos"); shader->setMat4(model,"model"); + if(reflect){ + shader->setBool(reflect,"environmentMapEnabled"); + root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4); + //environmentMap->select(4); + } if(skeleton){ } if(material->getType()==Material::MATERIAL_GUI){ diff --git a/mesh.h b/mesh.h index 80a52a9..968c44d 100755 --- a/mesh.h +++ b/mesh.h @@ -2,6 +2,7 @@ #define MESH_H #include"vector.h" +#include"util.h" #include #include #include"material.h" @@ -9,6 +10,8 @@ namespace vb01{ class Node; + class Shader; + class Texture; class Skeleton; class Mesh{ @@ -31,6 +34,7 @@ namespace vb01{ void setMaterial(Material *mat){this->material=mat;} inline void setNode(Node *node){this->node=node;} inline void setCastShadow(bool castShadow){this->castShadow=castShadow;} + inline void setReflect(bool r){this->reflect=r;} inline void setWireframe(bool w){this->wireframe=w;} inline Node* getNode(){return node;} inline Material* getMaterial(){return material;} @@ -40,16 +44,20 @@ namespace vb01{ inline Vertex* getVerts(){return vertices;} inline unsigned int* getIndices(){return indices;} private: + Shader *environmentShader=nullptr; + u32 environmentBuffer; + Texture *environmentMap; Material *material=nullptr; Node *node=nullptr; std::vector meshes; std::string name=""; + int width=800; Skeleton *skeleton=nullptr; protected: Mesh(); void construct(); - bool staticVerts=true,castShadow=false,wireframe=false; + bool staticVerts=true,castShadow=false,reflect=false,wireframe=false; Vertex *vertices; VertexGroup groups; unsigned int *indices,VAO,VBO,EBO; diff --git a/model.cpp b/model.cpp index 6583dde..2d5af5f 100755 --- a/model.cpp +++ b/model.cpp @@ -229,6 +229,15 @@ namespace vb01{ this->castShadow=castShadow; } + void Model::setReflect(bool reflect){ + vector descendants; + getDescendants(this,descendants); + for(Node *node : descendants) + for(Mesh *mesh : node->getMeshes()) + mesh->setReflect(reflect); + this->reflect=reflect; + } + void Model::setWireframe(bool wirefame){ vector descendants; getDescendants(this,descendants); diff --git a/model.h b/model.h index 471c431..6be023f 100755 --- a/model.h +++ b/model.h @@ -22,6 +22,7 @@ namespace vb01{ void update(); void setMaterial(Material*); void setCastShadow(bool); + void setReflect(bool); void setWireframe(bool); private: void processNode(aiNode*, const aiScene*, Node*); @@ -30,7 +31,7 @@ namespace vb01{ void processMaterial(); Material* material; - bool castShadow=false,wireframe=false; + bool castShadow=false,reflect=false,wireframe=false; }; } diff --git a/particle.frag b/particle.frag index 716b66e..1f9610f 100755 --- a/particle.frag +++ b/particle.frag @@ -4,7 +4,8 @@ in vec2 texCoords; flat in int id; -out vec4 FragColor; +layout (location=0) out vec4 FragColor; +layout (location=1) out vec4 BrightColor; uniform sampler2D tex; uniform vec4 color[200]; @@ -12,6 +13,9 @@ uniform vec4 color[200]; void main(){ float alpha=texture(tex,texCoords).r; vec4 c=color[id]; - //FragColor=vec4(1,0,0,1); + c=vec4(c.x,c.y,c.z,c.w*alpha); + float brightness = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722)); + if(brightness>1) + BrightColor=vec4(c.rgb,1); FragColor=vec4(c.x,c.y,c.z,c.w*alpha); } diff --git a/postProcess.frag b/postProcess.frag index af37bb0..41f0a8f 100644 --- a/postProcess.frag +++ b/postProcess.frag @@ -6,38 +6,26 @@ out vec4 FragColor; uniform sampler2D frag; uniform sampler2D bright; +uniform bool hdr; uniform bool bloom; +uniform float exposure; +uniform float gamma; 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; - } + if(bloom) + hdrColor+=brightColor; + + if(hdr){ + //Reinhardt + //c.rgb=c.rgb/(c.rgb+vec3(1.)); + + //Exp + hdrColor.rgb=vec3(1.)-exp(-hdrColor.rgb*exposure); } - 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; diff --git a/root.cpp b/root.cpp index 4ca124e..9aee6aa 100755 --- a/root.cpp +++ b/root.cpp @@ -76,8 +76,6 @@ namespace vb01{ */ if(glCheckFramebufferStatus(GL_FRAMEBUFFER)!=GL_FRAMEBUFFER_COMPLETE) cout<<"Not complete\n"; - else - cout<<"Complete\n"; glBindFramebuffer(GL_FRAMEBUFFER,0); guiPlane=new Quad(Vector3(width,height,-1),false); @@ -86,16 +84,15 @@ namespace vb01{ mat->addDiffuseMap(brightTexture); guiPlane->setMaterial(mat); - string basePath="../../vb01/"; - blurShader=new Shader(basePath+"blur.vert",basePath+"blur.frag"); + string basePath="../../vb01/blur."; + blurShader=new Shader(basePath+"vert",basePath+"frag"); glGenFramebuffers(2,pingpongBuffers); for(int i=0;i<2;i++){ + glBindFramebuffer(GL_FRAMEBUFFER,pingpongBuffers[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); } } @@ -125,29 +122,29 @@ namespace vb01{ Material *material=guiPlane->getMaterial(); Shader *shader=material->getShader(); - /* - int ammount=10; bool horizontal=false; blurShader->use(); - for(int i=0;isetBool(horizontal,"horizontal"); + blurShader->setVec2(Vector2(width,height),"screen"); + for(int i=0;isetBool(horizontal,"horizontal"); if(i==0) - material->getDiffuseMap(1)->update(); + glBindTexture(GL_TEXTURE_2D,*(material->getDiffuseMap(1)->getTexture())); else - pingPongTextures[!horizontal]->update(); + pingPongTextures[!horizontal]->select(); 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->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"); guiPlane->render(); diff --git a/root.h b/root.h index 2434a04..6fc1b42 100755 --- a/root.h +++ b/root.h @@ -28,15 +28,21 @@ namespace vb01{ inline int getHeight(){return height;} inline unsigned int* getFBO(){return &FBO;} inline GLFWwindow* getWindow(){return window;} + inline void setHDREnabled(bool hdr){this->hdr=hdr;} + inline void setExposure(float exposure){this->exposure=exposure;} + inline void setGamma(float gamme){this->gamma=gamma;} inline void setBloom(bool bloom){this->bloom=bloom;} + inline void setBlurLevel(bool level){this->blurLevel=level;} + inline Box* getSkybox(){return skybox;} void createSkybox(std::string[6]); static Root* getSingleton(); int numLights=0; private: - bool bloom=false; + bool bloom=false,hdr=false; + float exposure=1,gamma=1; Box *skybox=nullptr; Quad *guiPlane=nullptr; - int width,height; + int width,height,blurLevel=10; unsigned int FBO,RBO,pingpongBuffers[2]; GLFWwindow *window; Node *rootNode,*guiNode; diff --git a/skybox.frag b/skybox.frag index 522c2ff..d4b8242 100755 --- a/skybox.frag +++ b/skybox.frag @@ -2,10 +2,12 @@ in vec3 texCoords; -out vec4 FragColor; +layout (location=0) out vec4 FragColor; +layout (location=1) out vec4 BrightColor; uniform samplerCube skybox; void main(){ + BrightColor=vec4(0); FragColor=texture(skybox,texCoords); } diff --git a/texture.cpp b/texture.cpp index cc2830e..15685bd 100755 --- a/texture.cpp +++ b/texture.cpp @@ -25,10 +25,10 @@ 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_FLOAT,NULL); + glTexImage2D(GL_TEXTURE_2D,0,GL_RGB16F,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_WRAP_S,GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } @@ -94,7 +94,7 @@ namespace vb01{ } - Texture::Texture(int width){ + Texture::Texture(int width,bool depth){ this->width=width; this->type=TextureType::TEXTURE_CUBEMAP; texture=new u32; @@ -103,7 +103,7 @@ namespace vb01{ glBindTexture(GL_TEXTURE_CUBE_MAP,texture[0]); for(int i=0;i<6;i++) - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i,0,GL_DEPTH_COMPONENT,width,width,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i,0,depth?GL_DEPTH_COMPONENT:GL_RGB,width,width,0,depth?GL_DEPTH_COMPONENT:GL_RGB,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MAG_FILTER,GL_NEAREST); diff --git a/texture.frag b/texture.frag index 5430d26..8ab2fa4 100755 --- a/texture.frag +++ b/texture.frag @@ -27,12 +27,15 @@ uniform sampler2D diffuseMap; uniform sampler2D normalMap; uniform sampler2D specularMap; uniform sampler2D parallaxMap; +uniform samplerCube environmentMap; uniform Light light[numLights]; uniform bool lightingEnabled; uniform bool texturingEnabled; uniform bool normalMapEnabled; uniform bool castShadow; +uniform bool environmentMapEnabled; uniform vec4 diffuseColor; +uniform float specularStrength; uniform vec3 camPos; float linDepth(float depth,int i){ @@ -44,15 +47,15 @@ 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; currentDepth=length(projDir); shadow=(currentDepth-bias>closestDepth)?val:0; } +/* */ - if(type!=0){ + else{ vec3 projCoords=(light[id].lightMat*vec4(fragPos,1)).xyz/(light[id].lightMat*vec4(fragPos,1)).w; projCoords=projCoords*.5+.5; closestDepth=texture(light[id].depthMap,projCoords.xy).r; @@ -79,6 +82,8 @@ void main(){ vec3 diffuseColor=vec3(0),specularColor=vec3(0); float coef=1; + + for(int i=0;i1) BrightColor=vec4(finalColor.rgb,1); diff --git a/texture.geo b/texture.geo new file mode 100644 index 0000000..203457c --- /dev/null +++ b/texture.geo @@ -0,0 +1,41 @@ +#version 330 core + +/* +*/ +layout (triangles) in; +layout (triangle_strip, max_vertices=3) out; + +in VS_OUT{ + vec2 tCoords; +}gs_in[]; + +out vec4 FragPos; +out vec2 texCoords; + +uniform mat4 shadowMat[6]; +uniform bool environment; + +void main(){ +/* + if(environment) + for(int i=0;i<6;i++){ + gl_Layer=i; + for(int j=0;j<3;j++){ + FragPos=gl_in[j].gl_Position; + gl_Position=shadowMat[i]*FragPos; + EmitVertex(); + } + EndPrimitive(); + } +*/ +gl_Position=gl_in[0].gl_Position; +texCoords=gs_in[0].tCoords; +EmitVertex(); +gl_Position=gl_in[1].gl_Position; +texCoords=gs_in[1].tCoords; +EmitVertex(); +gl_Position=gl_in[2].gl_Position; +texCoords=gs_in[2].tCoords; +EmitVertex(); +EndPrimitive(); +} diff --git a/texture.h b/texture.h index 2f74f23..78022f9 100755 --- a/texture.h +++ b/texture.h @@ -15,7 +15,7 @@ namespace vb01{ Texture(int,int,bool=true); Texture(std::string[],int,int=0,bool=false); Texture(std::string[6],bool=false); - Texture(int); + Texture(int,bool=true); Texture(FT_Face&,char); void select(int=0); void update(int=0); diff --git a/texture.vert b/texture.vert index 072ff64..5529b18 100755 --- a/texture.vert +++ b/texture.vert @@ -15,10 +15,6 @@ out vec2 texCoords; uniform mat4 model; uniform mat4 view; uniform mat4 proj; -/* -uniform vec3 aTan; -uniform vec3 aBiTan; -*/ void main(){ gl_Position=proj*view*model*vec4(aPos,1);