From: Peter Schaefer Date: Thu, 23 Jun 2016 22:40:48 +0000 (+0200) Subject: new blur Shader X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=d8c76c7a3699d5b77a82991637b2a1a5798ca9cd;p=cgue_weave.git new blur Shader added MotionBlur (only for TimeRewind) --- diff --git a/CGUE2015_Weave.sln b/CGUE2015_Weave.sln index 3654fbe..f0c6277 100644 --- a/CGUE2015_Weave.sln +++ b/CGUE2015_Weave.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shader", "shader", "{75179E shader\basicTexture_VS.hlsl = shader\basicTexture_VS.hlsl shader\blend_FS.hlsl = shader\blend_FS.hlsl shader\blur_FS.hlsl = shader\blur_FS.hlsl + shader\blurDirectional2_FS.hlsl = shader\blurDirectional2_FS.hlsl + shader\blurDirectional_FS.hlsl = shader\blurDirectional_FS.hlsl shader\filterBright_FS.hlsl = shader\filterBright_FS.hlsl shader\filterMotion_FS.hlsl = shader\filterMotion_FS.hlsl shader\lightingTexture_FS.hlsl = shader\lightingTexture_FS.hlsl diff --git a/Weave/Events.cpp b/Weave/Events.cpp index 121c2c4..949544f 100644 --- a/Weave/Events.cpp +++ b/Weave/Events.cpp @@ -165,7 +165,7 @@ void Events::KeyDown(int _key) break; case SDLK_LSHIFT: timeshift = true; - Graphix::enableEffects(EF_MOTION_BLUR_ACC); + Graphix::enableEffects(EF_MOTION_BLUR); break; default: break; @@ -178,7 +178,7 @@ void Events::KeyUp(int _key) { case SDLK_LSHIFT: timeshift = false; - Graphix::disableEffects(EF_MOTION_BLUR_ACC); + Graphix::disableEffects(EF_MOTION_BLUR); break; default: break; diff --git a/Weave/Graphix/Graphix.cpp b/Weave/Graphix/Graphix.cpp index daf2ec4..9faa62b 100644 --- a/Weave/Graphix/Graphix.cpp +++ b/Weave/Graphix/Graphix.cpp @@ -312,7 +312,7 @@ SDL_Window* Graphix::sdl_window; SDL_GLContext Graphix::sdl_glcontext; -short Graphix::effects = EF_TRANSPARENCY | EF_BLOOM | EF_FRUSTUM_CULLING | EF_MOTION_BLUR; +short Graphix::effects = EF_TRANSPARENCY | EF_BLOOM | EF_FRUSTUM_CULLING; int Graphix::obj = -1; Fps Graphix::fps(0.3f); diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index a8e604b..3c7ddc3 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -293,6 +293,8 @@ Model* Model::getPlaneModel() PlaneModel->bindShader(Shader::getShader(SH_BLUR)); PlaneModel->bindShader(Shader::getShader(SH_BLEND)); PlaneModel->bindShader(Shader::getShader(SH_FILTER_BRIGHT)); + PlaneModel->bindShader(Shader::getShader(SH_BLUR_DIRECTIONAL)); + } return PlaneModel; } diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index a0bfe07..bffc7f5 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -6,7 +6,7 @@ using namespace std; -#define SHADER_NUM 10 +#define SHADER_NUM 11 Shader::Shader(string _shader1, string _shader2) { @@ -250,6 +250,7 @@ void Shader::init() shList[SH_POINTLIGHT] = new Shader("pointlight_VS.hlsl", "pointlight_FS.hlsl"); shList[SH_FILTER_BRIGHT] = new Shader("basic_VS.hlsl", "filterBright_FS.hlsl"); shList[SH_FILTER_MOTION] = new Shader("basic_VS.hlsl", "filterMotion_FS.hlsl"); + shList[SH_BLUR_DIRECTIONAL] = new Shader("basic_VS.hlsl", "blurDirectional2_FS.hlsl"); } initShader = true; diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h index e127c15..93df822 100644 --- a/Weave/Graphix/Shader.h +++ b/Weave/Graphix/Shader.h @@ -19,7 +19,8 @@ enum ShaderTarget { SH_SHADOWDIR = 6, SH_POINTLIGHT = 7, SH_FILTER_BRIGHT = 8, - SH_FILTER_MOTION = 9 + SH_FILTER_MOTION = 9, + SH_BLUR_DIRECTIONAL = 10 }; //Don't forget to resize the shList Array at the begining of shader.cpp! class Shader diff --git a/Weave/Graphix/Textures/Texture.cpp b/Weave/Graphix/Textures/Texture.cpp index a5eae56..8db4905 100644 --- a/Weave/Graphix/Textures/Texture.cpp +++ b/Weave/Graphix/Textures/Texture.cpp @@ -9,7 +9,7 @@ using std::string; using std::unordered_map; -string bindTargets[] = {"uColorTexture", "uBlendTexture", "uPointLightXP", "uPointLightXM", "uPointLightYP", "uPointLightYM", "uPointLightZP", "uPointLightZM", "uBlurVec2" }; +string bindTargets[] = {"uColorTexture", "uBlendTexture", "uPointLightXP", "uPointLightXM", "uPointLightYP", "uPointLightYM", "uPointLightZP", "uPointLightZM", "uBlurVec" }; Texture::Texture(texTarget _target, unsigned int _type) diff --git a/Weave/Graphix/Textures/Texture.h b/Weave/Graphix/Textures/Texture.h index 5de1672..d43cab7 100644 --- a/Weave/Graphix/Textures/Texture.h +++ b/Weave/Graphix/Textures/Texture.h @@ -23,7 +23,7 @@ enum bindTarget { uPLightYM = 5, uPLightZP = 6, uPLightZM = 7, - uBLURVEC2 = 8 + uBLURVEC = 8 }; //Don't forget to assign the correct name in bindTargets at the beginning of Texture.cpp! class Texture diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index 109b635..25c94e3 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -87,7 +87,7 @@ lookat(_lookat) bt_dynamics_world->setGravity(btVector3(0, YFALL_SPEED, 0)); /*BUFFERS*/ - render = new fBufferObject(2); + render = new fBufferObject(); render->bindBuffer(W_WIDTH, W_HEIGHT); blurPingPong = new fBufferObject*[2]; @@ -100,15 +100,20 @@ lookat(_lookat) shadowdir = new dBufferObject(); shadowdir->bindBuffer(1024, 1024); - postRender = new fBufferObject(1, false); - postRender->bindBuffer(W_WIDTH, W_HEIGHT); - - postRenderSmall = new fBufferObject(1, false); - postRenderSmall->bindBuffer(W_WIDTH / SMALL_F, W_HEIGHT / SMALL_F); + postRenderPingPong = new fBufferObject*[2]; + for (int i = 0; i < 2; i++) + { + postRenderPingPong[i] = new fBufferObject(1, false); + postRenderPingPong[i]->bindBuffer(W_WIDTH, W_HEIGHT); + } + + motionVecs = new fBufferObject(); + motionVecs->bindBuffer(W_WIDTH , W_HEIGHT); fBufferObject::clearBuffer(); //Graphix::disableEffects(EF_BLOOM); + //Graphix::disableEffects(EF_MOTION_BLUR); for (int s = 0; s < NELEMS(shader); s++) { @@ -146,8 +151,10 @@ Scene::~Scene() delete blurPingPong; delete shadowdir; - delete postRender; - delete postRenderSmall; + for (int i = 0; i < 2; i++) + delete postRenderPingPong[i]; + delete postRenderPingPong; + delete motionVecs; //if (viewPort != nullptr) @@ -270,14 +277,19 @@ void Scene::update(float deltaT) void Scene::draw() const { + GLboolean horizontal_blurPP = true, postRenderPP = true; + + fBufferObject* lastScreen = nullptr; + + /* Update Camera */ for (int s = 0; s < NELEMS(shader); s++) { Shader::getShader(shader[s])->useShader(); camera.useView(); } - /*Draw WireFrame ONLY*/ + /* Draw WireFrame ONLY */ if (Events::isKToggleActive(SDLK_F3)) { Shader::getShader(SH_BASICTEXTURE)->useShader(); @@ -288,8 +300,7 @@ void Scene::draw() const return; } - - //Directional Light Shadow + /* Prepare Shadows */ glCullFace(GL_FRONT); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(4.f, 0.f); @@ -307,59 +318,87 @@ void Scene::draw() const glDisable(GL_POLYGON_OFFSET_FILL); glViewport(0, 0, W_WIDTH, W_HEIGHT); + /* Prepare DRAW SCENE */ camera.useCamera(); - render->useBuffer(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - dirLight->updateDirLightView(); - Shader::getShader(SH_LIGHTING)->useShader(); - fBufferObject::clearBuffer(); - shadowdir->getTexture()->useTexture(uPLightXP); - - - - /* DRAW SCENE */ if (Graphix::testEffect(EF_BLOOM) || Graphix::testEffect(EF_MOTION_BLUR)) { render->useBuffer(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + lastScreen = render; } else { fBufferObject::clearBuffer(); } - /*DRAW SCENE -> SkyBox*/ + /* DRAW SCENE -> SkyBox */ Shader::getShader(SH_BASICTEXTURE)->useShader(); SkyBox.draw(); - //Shader::getShader(SH_BASICTEXTURE)->useShader(); - //Shader::getShader(SH_BASICTEXTURE)->setUniformLocation("uFar", false); - + /* DRAW SCENE -> Rest */ Shader::getShader(SH_LIGHTING)->useShader(); + dirLight->updateDirLightView(); + shadowdir->getTexture()->useTexture(uPLightXP); drawSceneObjects(); + /*DRAW BBOX WIRES*/ + if (lastScreen != nullptr) + lastScreen->useBuffer(); + else + fBufferObject::clearBuffer(); + + if (Events::isKToggleActive(SDLK_F6)) + { + Shader::getShader(SH_BASIC)->useShader(); + drawSceneObjects(DRAW_Coll); + } /* MOTION BLUR */ if (Graphix::testEffect(EF_MOTION_BLUR)) { - postRender->useBuffer(); + + /* calculate Motion */ + motionVecs->useBuffer(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Shader::getShader(SH_FILTER_MOTION)->useShader(); camera.useOldProjView(); - Shader::getShader(SH_ACTIVE)->setUniformLocation("uDelta", Graphix::fps.getCappedDelta()); - drawSceneObjects(DRAW_ModelMotionBlur); - } - else - { + drawSceneObjects(DRAW_ModelMotionBlur); + + /* Blur Scene */ + Shader::getShader(SH_BLUR_DIRECTIONAL)->useShader(); + Shader::getShader(SH_ACTIVE)->setUniformLocation("uDelta", Graphix::fps.getCappedDelta()); + motionVecs->getTexture()->useTexture(uBLURVEC); + + bool firstit = true; + for (int i = 0; i < 1; i++) + { + + postRenderPingPong[!postRenderPP]->useBuffer(); + glClear(GL_COLOR_BUFFER_BIT); + if (firstit) + { + lastScreen->getTexture()->useTexture(uCOLOR); + firstit = false; + } + else + postRenderPingPong[postRenderPP]->getTexture()->useTexture(uCOLOR); + Model::getPlaneModel()->drawModel(mat4(1.f)); + postRenderPP = !postRenderPP; + } + + + lastScreen = postRenderPingPong[postRenderPP]; } + //Pointlights & their shadows //for (auto i = pointlights.begin(); i != pointlights.end(); ++i) //{ - // postRender->useBuffer(); + // postRenderPingPong->useBuffer(); // glClear(GL_COLOR_BUFFER_BIT); // Shader::getShader(SH_POINTLIGHT)->useShader(); // render->getTexture()->useTexture(); @@ -372,7 +411,7 @@ void Scene::draw() const // drawSceneObjects(DRAW_Model, false); - // postRender->useBuffer(); + // postRenderPingPong->useBuffer(); // Shader::getShader(SH_POINTLIGHT)->useShader(); // //shadowdir->getTexture()->useTexture(uPLightXP + j); // switch (j) @@ -392,84 +431,72 @@ void Scene::draw() const // drawSceneObjects(); // render->useBuffer(); - // //TODO copy the texture from postrender to render + // //TODO copy the texture from postRenderPingPong to render // //render->getTexture()-> //} - - GLboolean horizontal = true, firstit = true; if (Graphix::testEffect(EF_BLOOM)) { /* DRAW BRIGHTNESS */ glViewport(0, 0, W_WIDTH / SMALL_F, W_HEIGHT / SMALL_F); - postRenderSmall->useBuffer(); + blurPingPong[!horizontal_blurPP]->useBuffer(); glClear(GL_COLOR_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - render->getTexture()->downscale(1); + lastScreen->getTexture()->downscale(1); Shader::getShader(SH_FILTER_BRIGHT)->useShader(); Shader::getShader(SH_ACTIVE)->setUniformLocation("uFilterThreshold",1.2f); - render->getTexture()->useTexture(); + lastScreen->getTexture()->useTexture(); Model::getPlaneModel()->drawModel(mat4(1.f)); + horizontal_blurPP = !horizontal_blurPP; /* BLUR BRIGHTNESS */ Shader::getShader(SH_BLUR)->useShader(); for (int i = 0; i < 4; i++) { - blurPingPong[horizontal]->useBuffer(); + blurPingPong[!horizontal_blurPP]->useBuffer(); glClear(GL_COLOR_BUFFER_BIT); - Shader::getShader(SH_ACTIVE)->setUniformLocation("uHorizontal", horizontal); - if (firstit) - { - postRenderSmall->getTexture()->useTexture(); - firstit = false; - } - else - blurPingPong[!horizontal]->getTexture()->useTexture(); + + Shader::getShader(SH_ACTIVE)->setUniformLocation("uHorizontal", horizontal_blurPP); + blurPingPong[horizontal_blurPP]->getTexture()->useTexture(); Model::getPlaneModel()->drawModel(mat4(1.f)); - horizontal = !horizontal; + horizontal_blurPP = !horizontal_blurPP; } glViewport(0, 0, W_WIDTH, W_HEIGHT); } - - /*DRAW BBOX WIRES*/ - if (Graphix::testEffect(EF_BLOOM)) - render->useBuffer(); - if (Events::isKToggleActive(SDLK_F6)) - { - Shader::getShader(SH_BASIC)->useShader(); - drawSceneObjects(DRAW_Coll); - } - - - if (Graphix::testEffect(EF_BLOOM)) + /* FINAL DRAW -> if needed*/ + if (lastScreen != nullptr) { fBufferObject::clearBuffer(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - /* BLEND BRIGHTNESS TO ORIGINAL*/ - Shader::getShader(SH_BLEND)->useShader(); - render->getTexture(0)->useTexture(uCOLOR); - blurPingPong[!horizontal]->getTexture()->useTexture(uBLEND); - - - - //Shader::getShader(SH_BASICTEXTURE)->useShader(); - //postRender->getTexture(1)->useTexture(uCOLOR); - //SceneObjects.front()->gTexture()->useTexture(); - //blurPingPong[!horizontal]->getTexture()->useTexture(); - //postRenderSmall->getTexture()->useTexture(); - + if (Graphix::testEffect(EF_BLOOM)) + { + /* BLEND BRIGHTNESS TO ORIGINAL*/ + Shader::getShader(SH_BLEND)->useShader(); + blurPingPong[horizontal_blurPP]->getTexture()->useTexture(uBLEND); + } + else + { + Shader::getShader(SH_BASICTEXTURE)->useShader(); + } + lastScreen->getTexture()->useTexture(uCOLOR); Model::getPlaneModel()->drawModel(mat4(1.f)); } + //Shader::getShader(SH_BASICTEXTURE)->useShader(); + //SceneObjects.front()->gTexture()->useTexture(); + //blurPingPong[horizontal_blurPP]->getTexture()->useTexture(); + //postRenderPingPong[postRenderPP]->getTexture()->useTexture(); + //shadowdir->getTexture()->useTexture(); + } void Scene::drawSceneObjects(drawTarget _target, bool _culling) const @@ -541,10 +568,13 @@ void Scene::setView(unsigned int width, unsigned int height, bool updateProjecti } render->updateSize(width, height); - postRender->updateSize(width, height); - postRenderSmall->updateSize(width / SMALL_F, height / SMALL_F); + motionVecs->updateSize(width, height); for (int i = 0; i < 2; i++) + { blurPingPong[i]->updateSize(width / SMALL_F, height / SMALL_F); + postRenderPingPong[i]->updateSize(width, height); + } + //shadowdir->updateSize(width, height); } diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index 0f3cc0a..4f56207 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -88,8 +88,8 @@ protected: btDiscreteDynamicsWorld* bt_dynamics_world; fBufferObject* render; - fBufferObject* postRender; - fBufferObject* postRenderSmall; + fBufferObject** postRenderPingPong; + fBufferObject* motionVecs; fBufferObject** blurPingPong; fBufferObject* shadowdir; diff --git a/shader/blurDirectional2_FS.hlsl b/shader/blurDirectional2_FS.hlsl new file mode 100644 index 0000000..1fdb8d0 --- /dev/null +++ b/shader/blurDirectional2_FS.hlsl @@ -0,0 +1,47 @@ +//Fragment Shader +#version 330 core +in vec2 fUVs; + +layout(location = 0) out vec4 FragColor; + +uniform sampler2D uColorTexture; +uniform sampler2D uBlurVec; + +uniform float uDeltaT = 1.f; +uniform int uSamples = 5; +uniform bool uSymetric = true; + +void main() +{ + + vec4 preBlurVec = texture(uBlurVec, fUVs); + + if (preBlurVec.z <= 0.1f) + { + FragColor = texture(uColorTexture, fUVs); + return; + } + + vec3 result = texture(uColorTexture, fUVs).rgb; + vec2 blurVec = preBlurVec.xy * uDeltaT * preBlurVec.z * preBlurVec.z / 10; + + + if (uSymetric) + { + for (int i = 1; i <= uSamples; ++i) + { + result += texture(uColorTexture, fUVs + blurVec * (float(i) / uSamples)).rgb + texture(uColorTexture, fUVs - blurVec * (float(i) / uSamples)).rgb; + } + FragColor = vec4(result / (2 * uSamples + 1), 1.0); + } + else + { + for (int i = 1; i <= uSamples; ++i) + { + result += texture(uColorTexture, fUVs + blurVec * (float(i) / uSamples)).rgb; + } + FragColor = vec4(result / (uSamples + 1), 1.0); + } + + //FragColor = vec4(vec3(preBlurVec.z), 1.f); +} \ No newline at end of file diff --git a/shader/blurDirectional_FS.hlsl b/shader/blurDirectional_FS.hlsl new file mode 100644 index 0000000..ddc9454 --- /dev/null +++ b/shader/blurDirectional_FS.hlsl @@ -0,0 +1,40 @@ +//Fragment Shader +#version 330 core +in vec2 fUVs; + +layout(location = 0) out vec4 FragColor; + +uniform sampler2D uColorTexture; +uniform sampler2D uBlurVec; + +uniform float uDeltaT = 1.f; +uniform bool uSymetric = true; + +uniform float weight[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162); +//uniform float weight[5] = float[](0.2222222222, 0.1111111111, 0.1111111111, 0.1111111111, 0.1111111111); + + +void main() +{ + vec4 preBlurVec = texture(uBlurVec, fUVs); + + if (preBlurVec.z == 0) + { + FragColor = texture(uColorTexture, fUVs); + return; + } + + vec3 result = texture(uColorTexture, fUVs).rgb * weight[0]; + vec2 blurVec = preBlurVec.xy * uDeltaT * preBlurVec.z * preBlurVec.z / 10; + + for (int i = 1; i <= 4; ++i) + { + result += texture(uColorTexture, fUVs + blurVec * (i / 4)).rgb *weight[i]; + if (uSymetric) + result += texture(uColorTexture, fUVs - blurVec * (i / 4)).rgb *weight[i]; + } + + FragColor = vec4(result, 1.0); + + //FragColor = vec4(vec3(preBlurVec.z), 1.f); +} \ No newline at end of file diff --git a/shader/blur_FS.hlsl b/shader/blur_FS.hlsl index df8ce2a..6103fe0 100644 --- a/shader/blur_FS.hlsl +++ b/shader/blur_FS.hlsl @@ -6,7 +6,6 @@ layout(location = 0) out vec4 FragColor; uniform sampler2D uColorTexture; uniform bool uHorizontal = true; -//uniform bool uSymetric = true; uniform float weight[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162); @@ -20,8 +19,7 @@ void main() for (int i = 1; i < 5; ++i) { result += texture(uColorTexture, fUVs + vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; - //if(uSymetric) - result += texture(uColorTexture, fUVs - vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; + result += texture(uColorTexture, fUVs - vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; } } else @@ -29,8 +27,7 @@ void main() for (int i = 1; i < 5; ++i) { result += texture(uColorTexture, fUVs + vec2(0.0, tex_offset.y * i)).rgb * weight[i]; - //if(uSymetric) - result += texture(uColorTexture, fUVs - vec2(0.0, tex_offset.y * i)).rgb * weight[i]; + result += texture(uColorTexture, fUVs - vec2(0.0, tex_offset.y * i)).rgb * weight[i]; } } FragColor = vec4(result, 1.0); diff --git a/shader/filterMotion_FS.hlsl b/shader/filterMotion_FS.hlsl index ad7712f..babcde0 100644 --- a/shader/filterMotion_FS.hlsl +++ b/shader/filterMotion_FS.hlsl @@ -4,12 +4,14 @@ in vec4 pixPosition; in mat4 new2old; -layout(location = 0) out vec3 MotionVec; +layout(location = 0) out vec4 MotionVec; void main() { vec3 currentPos = pixPosition.xyz / pixPosition.w; vec4 oldPos = new2old * vec4(currentPos, 1); - MotionVec = oldPos.xyz / oldPos.w - currentPos.xyz; + vec3 lastPos = oldPos.xyz / oldPos.w; + + MotionVec = vec4(lastPos.xy - currentPos.xy, (lastPos.z+currentPos.z)/2,1.f); }