From 5acd5a4a8aef49866b0f4c0ad1969a73cac23935 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Fri, 24 Jun 2016 04:56:07 +0200 Subject: [PATCH] experimented with glClear renamed clearBuffer -> leaveBuffer GlitchFix BasixTexture Shader added Glow to Persistent Objects (only if Bloom Enabled) --- Weave/Graphix/Textures/BufferObject.h | 3 ++- Weave/Graphix/Textures/Texture.cpp | 20 ++++++++++++++++ Weave/Graphix/Textures/Texture.h | 2 ++ Weave/Graphix/Textures/dBufferObject.cpp | 8 +++++++ Weave/Graphix/Textures/dBufferObject.h | 1 + Weave/Graphix/Textures/fBufferObject.cpp | 30 ++++++++++++++++++++---- Weave/Graphix/Textures/fBufferObject.h | 4 +++- Weave/Graphix/Textures/rBufferObject.cpp | 10 +++++++- Weave/Graphix/Textures/rBufferObject.h | 4 ++-- Weave/Scene/Scene.cpp | 26 +++++++++++++------- Weave/Scene/SceneObject.cpp | 5 ++++ Weave/Scene/SceneObject.h | 2 +- shader/basicTexture_FS.hlsl | 1 + shader/lightingTexture_FS.hlsl | 9 +++++++ 14 files changed, 107 insertions(+), 18 deletions(-) diff --git a/Weave/Graphix/Textures/BufferObject.h b/Weave/Graphix/Textures/BufferObject.h index 1eda417..fcc089f 100644 --- a/Weave/Graphix/Textures/BufferObject.h +++ b/Weave/Graphix/Textures/BufferObject.h @@ -8,8 +8,9 @@ public: virtual void bindBuffer(unsigned int width = 0, unsigned int height = 0) = 0; virtual void unbindBuffer() = 0; virtual void useBuffer() const = 0; + virtual void clearUseBuffer(unsigned int mask) const = 0; - virtual void updateSize(unsigned int _width, unsigned int _height) = 0; + virtual void updateSize(unsigned int width, unsigned int height) = 0; virtual operator unsigned int() const { diff --git a/Weave/Graphix/Textures/Texture.cpp b/Weave/Graphix/Textures/Texture.cpp index 8db4905..3da4465 100644 --- a/Weave/Graphix/Textures/Texture.cpp +++ b/Weave/Graphix/Textures/Texture.cpp @@ -118,7 +118,17 @@ void Texture::bindTexture(unsigned int _width, unsigned int _height, bool shadow //glTexParameteri(texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); } + //if (data != nullptr) + //{ + // float* _data = new float[width*height*4](); + // std::fill_n(_data, width*height * 4, 0); + // glTexImage2D(texture_target, 0, texture_internal, width, height, 0, texture_format, texture_type, _data); + // delete _data; + //} + // + //else glTexImage2D(texture_target, 0, texture_internal, width, height, 0, texture_format, texture_type, data); + } void Texture::unbindTexture() @@ -137,6 +147,16 @@ void Texture::useTexture(bindTarget _unit) const Shader::getShader()->setUniformLocation(bindTargets[_unit], _unit); } +void Texture::leaveTexture(bindTarget _unit) +{ + + /* bind Texture*/ + glActiveTexture(GL_TEXTURE0 + _unit); + glBindTexture(GL_TEXTURE_2D, 0); //BADHACK + + Shader::getShader()->setUniformLocation(bindTargets[_unit], _unit); +} + void Texture::updateSize(unsigned int _width, unsigned int _height) { if (_width == 0 || _height == 0) diff --git a/Weave/Graphix/Textures/Texture.h b/Weave/Graphix/Textures/Texture.h index d43cab7..8f058af 100644 --- a/Weave/Graphix/Textures/Texture.h +++ b/Weave/Graphix/Textures/Texture.h @@ -39,6 +39,8 @@ public: virtual void unbindTexture(); virtual void useTexture(bindTarget = uCOLOR) const; + static void leaveTexture(bindTarget = uCOLOR); + virtual void updateSize(unsigned int width = 0, unsigned int height = 0); virtual void downscale(unsigned int lvl); diff --git a/Weave/Graphix/Textures/dBufferObject.cpp b/Weave/Graphix/Textures/dBufferObject.cpp index 0209971..52e3f59 100644 --- a/Weave/Graphix/Textures/dBufferObject.cpp +++ b/Weave/Graphix/Textures/dBufferObject.cpp @@ -57,6 +57,14 @@ void dBufferObject::bindBuffer(unsigned int _width, unsigned int _height) } +void dBufferObject::clearUseBuffer(unsigned int _mask) const +{ + glBindFramebuffer(buffer_target, handle); + + if (_mask & GL_DEPTH_BUFFER_BIT) + glClear(GL_DEPTH_BUFFER_BIT); +} + //void dBufferObject::unbindBuffer() //{ // for (unsigned int i = 0; i < dim; i++) diff --git a/Weave/Graphix/Textures/dBufferObject.h b/Weave/Graphix/Textures/dBufferObject.h index 8e764dd..86ac23d 100644 --- a/Weave/Graphix/Textures/dBufferObject.h +++ b/Weave/Graphix/Textures/dBufferObject.h @@ -16,6 +16,7 @@ public: // virtual void unbindBuffer() override; // virtual void useBuffer() const override; + virtual void clearUseBuffer(unsigned int mask = 256) const override; // virtual void clearBuffer() const override; diff --git a/Weave/Graphix/Textures/fBufferObject.cpp b/Weave/Graphix/Textures/fBufferObject.cpp index 5c45e42..704732a 100644 --- a/Weave/Graphix/Textures/fBufferObject.cpp +++ b/Weave/Graphix/Textures/fBufferObject.cpp @@ -52,7 +52,7 @@ void fBufferObject::bindBuffer(unsigned int _width, unsigned int _height) glBindFramebuffer(buffer_target, handle); /*Bind Texture Buffers (COLOR)*/ - unsigned int* attachments = new unsigned int[dim]; + attachments = new unsigned int[dim]; for (unsigned int i = 0; i < dim; i++) { textures[i].bindTexture(_width, _height); @@ -72,12 +72,12 @@ void fBufferObject::bindBuffer(unsigned int _width, unsigned int _height) glDrawBuffers(dim, attachments); if (glCheckFramebufferStatus(buffer_target) != GL_FRAMEBUFFER_COMPLETE) Message::error("fBufferObject: Framebuffer not complete!"); - - delete attachments; } void fBufferObject::unbindBuffer() { + delete attachments; + for (unsigned int i = 0; i < dim; i++) { textures[i].unbindTexture(); @@ -91,13 +91,35 @@ void fBufferObject::unbindBuffer() } +void fBufferObject::clearUseBuffer(unsigned int _mask) const +{ + glBindFramebuffer(buffer_target, handle); + + if (dim <= 1) + glClear(_mask); + else + { + if(_mask & GL_COLOR_BUFFER_BIT) + for (unsigned int i = 0; i < dim; i++) + { + glDrawBuffer(attachments[i]); + glClearColor(0.f, 0.f, 0.f, 1.f); + glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT); + } + + glDrawBuffers(dim, attachments); + glClear(_mask); + } +} + void fBufferObject::useBuffer() const { glBindFramebuffer(buffer_target, handle); } -void fBufferObject::clearBuffer() +void fBufferObject::leaveBuffer() { glBindFramebuffer(GL_FRAMEBUFFER, 0); } diff --git a/Weave/Graphix/Textures/fBufferObject.h b/Weave/Graphix/Textures/fBufferObject.h index 92654d3..d04be4f 100644 --- a/Weave/Graphix/Textures/fBufferObject.h +++ b/Weave/Graphix/Textures/fBufferObject.h @@ -14,10 +14,11 @@ public: virtual void bindBuffer(unsigned int width = 0, unsigned int height = 0) override; virtual void unbindBuffer() override; + virtual void clearUseBuffer(unsigned int mask = 16640) const override; virtual void useBuffer() const override; - static void clearBuffer(); + static void leaveBuffer(); virtual void updateSize(unsigned int _width, unsigned int _height) override; @@ -27,6 +28,7 @@ public: protected: unsigned int dim; Texture* textures = nullptr; + unsigned int* attachments; rBufferObject* rBO = nullptr; diff --git a/Weave/Graphix/Textures/rBufferObject.cpp b/Weave/Graphix/Textures/rBufferObject.cpp index 4658944..3abd1a6 100644 --- a/Weave/Graphix/Textures/rBufferObject.cpp +++ b/Weave/Graphix/Textures/rBufferObject.cpp @@ -36,13 +36,21 @@ void rBufferObject::unbindBuffer() glGenRenderbuffers(1, &handle); } +void rBufferObject::clearUseBuffer(unsigned int _mask) const +{ + glBindFramebuffer(buffer_target, handle); + + if (_mask & GL_DEPTH_BUFFER_BIT) + glClear(GL_DEPTH_BUFFER_BIT); +} + void rBufferObject::useBuffer() const { glBindRenderbuffer(buffer_target, handle); } -void rBufferObject::clearBuffer() +void rBufferObject::leaveBuffer() { glBindRenderbuffer(GL_RENDERBUFFER, 0); } diff --git a/Weave/Graphix/Textures/rBufferObject.h b/Weave/Graphix/Textures/rBufferObject.h index e00af53..48d92df 100644 --- a/Weave/Graphix/Textures/rBufferObject.h +++ b/Weave/Graphix/Textures/rBufferObject.h @@ -11,10 +11,10 @@ public: virtual void bindBuffer(unsigned int width = 0, unsigned int height = 0) override; virtual void unbindBuffer() override; + virtual void clearUseBuffer(unsigned int mask = 256) const override; virtual void useBuffer() const override; - - static void clearBuffer(); + static void leaveBuffer(); void updateSize(unsigned int _width, unsigned int _height) override; diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index 25c94e3..d486fc0 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(); + render = new fBufferObject(2); render->bindBuffer(W_WIDTH, W_HEIGHT); blurPingPong = new fBufferObject*[2]; @@ -110,7 +110,7 @@ lookat(_lookat) motionVecs = new fBufferObject(); motionVecs->bindBuffer(W_WIDTH , W_HEIGHT); - fBufferObject::clearBuffer(); + fBufferObject::leaveBuffer(); //Graphix::disableEffects(EF_BLOOM); //Graphix::disableEffects(EF_MOTION_BLUR); @@ -330,7 +330,7 @@ void Scene::draw() const } else { - fBufferObject::clearBuffer(); + fBufferObject::leaveBuffer(); } /* DRAW SCENE -> SkyBox */ @@ -347,7 +347,7 @@ void Scene::draw() const if (lastScreen != nullptr) lastScreen->useBuffer(); else - fBufferObject::clearBuffer(); + fBufferObject::leaveBuffer(); if (Events::isKToggleActive(SDLK_F6)) { @@ -374,7 +374,7 @@ void Scene::draw() const motionVecs->getTexture()->useTexture(uBLURVEC); bool firstit = true; - for (int i = 0; i < 1; i++) + for (int i = 0; i < 2; i++) { postRenderPingPong[!postRenderPP]->useBuffer(); @@ -426,7 +426,7 @@ void Scene::draw() const // } // camera.useCamera(); - // fBufferObject::clearBuffer(); + // fBufferObject::leaveBuffer(); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // drawSceneObjects(); @@ -449,6 +449,16 @@ void Scene::draw() const Shader::getShader(SH_ACTIVE)->setUniformLocation("uFilterThreshold",1.2f); lastScreen->getTexture()->useTexture(); Model::getPlaneModel()->drawModel(mat4(1.f)); + + horizontal_blurPP = !horizontal_blurPP; + + blurPingPong[!horizontal_blurPP]->useBuffer(); + glClear(GL_COLOR_BUFFER_BIT); + Shader::getShader(SH_BLEND)->useShader(); + blurPingPong[horizontal_blurPP]->getTexture(0)->useTexture(uBLEND); + render->getTexture(1)->useTexture(uCOLOR); + Model::getPlaneModel()->drawModel(mat4(1.f)); + horizontal_blurPP = !horizontal_blurPP; /* BLUR BRIGHTNESS */ @@ -472,7 +482,7 @@ void Scene::draw() const /* FINAL DRAW -> if needed*/ if (lastScreen != nullptr) { - fBufferObject::clearBuffer(); + fBufferObject::leaveBuffer(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (Graphix::testEffect(EF_BLOOM)) @@ -486,7 +496,7 @@ void Scene::draw() const Shader::getShader(SH_BASICTEXTURE)->useShader(); } - lastScreen->getTexture()->useTexture(uCOLOR); + lastScreen->getTexture(0)->useTexture(uCOLOR); Model::getPlaneModel()->drawModel(mat4(1.f)); } diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 25b243d..0e0d6cd 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -218,7 +218,12 @@ void SceneObject::draw(drawTarget _target) const case DRAW_Model: default: texture->useTexture(); + + if (timeresistant) + Shader::getShader(SH_ACTIVE)->setUniformLocation("uAddGlow", true); model->drawModel(modelMat, dr_Model, vec4(0.9f, 0.f, 0.f, 1.f), currentAnimation, AnimationTime); + if (timeresistant) + Shader::getShader(SH_ACTIVE)->setUniformLocation("uAddGlow", false); break; case DRAW_Wire: texture->useTexture(); diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 1b67688..9f81f02 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -88,7 +88,7 @@ public: virtual void startanimation(uint index, float speed); virtual void setanimationtime(float time); virtual void setAnimationLoop(bool loop); - bool timeresistant; + bool timeresistant = false; bool ignore; bool movable = true; diff --git a/shader/basicTexture_FS.hlsl b/shader/basicTexture_FS.hlsl index 6ab90c6..f22f0be 100644 --- a/shader/basicTexture_FS.hlsl +++ b/shader/basicTexture_FS.hlsl @@ -21,5 +21,6 @@ void main() else BrightColor = vec4(0.); } + BrightColor = vec4(0.f); } \ No newline at end of file diff --git a/shader/lightingTexture_FS.hlsl b/shader/lightingTexture_FS.hlsl index 4ccc436..00840d0 100644 --- a/shader/lightingTexture_FS.hlsl +++ b/shader/lightingTexture_FS.hlsl @@ -7,12 +7,14 @@ in vec3 PointLightPosition1, DirectionalLightDirection1; in vec4 DirLightSpacePos; layout(location = 0) out vec4 FragColor; +layout(location = 1) out vec4 AddGlow; uniform sampler2D uColorTexture; uniform sampler2DShadow uPointLightXP; uniform vec4 material; //vec4 in the form (ambient, point, directional, glossyness); so far it was (1, 1, 1, 3) uniform vec3 dirLightColor; uniform vec3 AmbientLightColor; +uniform bool uAddGlow = false; float DirLightCalcShadowFactor(vec4 LightSpacePos) { @@ -53,6 +55,13 @@ void main() + PointLightColor1 * material[1] * (cosThetaPoint1 + pow(SpecularCosPoint1, specularConst)) / squaredist1 + DirLightCalcShadowFactor(DirLightSpacePos) * dirLightColor * material[2] * (cosThetaDirection1 + pow(SpecularCosDirection1, specularConst)) ), uvColor.a); + + if (uAddGlow) + { + AddGlow = vec4(0.f, dot(FragColor.rgb, vec3(0.2126f, 0.7152f, 0.0722f)),0.f,.6f); + } + else + AddGlow = vec4(0.f); //FragColor = vec4((normalize(worldNormal)+1)/2, 1.0f); //FragColor = vec4(length(DirectionalLightDirection1) - 1.0f, length(DirectionalLightDirection1) - 1.0f, length(DirectionalLightDirection1) - 1.0f, 1.0f); -- 2.47.3