Added simple additive/blur-with-persist glow modes, means to cycle through

modes.
This commit is contained in:
Andrzej Kapolka 2013-08-14 14:14:47 -07:00
parent d24e340c91
commit 15f129f32d
9 changed files with 185 additions and 31 deletions

View file

@ -0,0 +1,17 @@
#version 120
//
// glow_add.frag
// fragment shader
//
// Created by Andrzej Kapolka on 8/14/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
// the texture containing the original color
uniform sampler2D originalTexture;
void main(void) {
vec4 color = texture2D(originalTexture, gl_TexCoord[0].st);
gl_FragColor = color * (1.0 + color.a);
}

View file

@ -0,0 +1,20 @@
#version 120
//
// glow_add_separate.frag
// fragment shader
//
// Created by Andrzej Kapolka on 8/14/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
// the texture containing the original color
uniform sampler2D originalTexture;
// the texture containing the blurred color
uniform sampler2D blurredTexture;
void main(void) {
vec4 blurred = texture2D(blurredTexture, gl_TexCoord[0].st);
gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5);
}

View file

@ -4,25 +4,21 @@
// vertical_blur.frag // vertical_blur.frag
// fragment shader // fragment shader
// //
// Created by Andrzej Kapolka on 8/8/13. // Created by Andrzej Kapolka on 8/14/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
// //
// the texture containing the original color
uniform sampler2D originalTexture;
// the texture containing the horizontally blurred color // the texture containing the horizontally blurred color
uniform sampler2D horizontallyBlurredTexture; uniform sampler2D originalTexture;
void main(void) { void main(void) {
float dt = dFdy(gl_TexCoord[0].t); float dt = dFdy(gl_TexCoord[0].t);
vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + gl_FragColor = (texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0; texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0;
gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5);
} }

View file

@ -0,0 +1,28 @@
#version 120
//
// vertical_blur_add.frag
// fragment shader
//
// Created by Andrzej Kapolka on 8/8/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
// the texture containing the original color
uniform sampler2D originalTexture;
// the texture containing the horizontally blurred color
uniform sampler2D horizontallyBlurredTexture;
void main(void) {
float dt = dFdy(gl_TexCoord[0].t);
vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) +
texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0;
gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5);
}

View file

@ -2011,6 +2011,7 @@ void Application::initMenu() {
_renderAvatarBalls->setChecked(false); _renderAvatarBalls->setChecked(false);
renderMenu->addAction("Cycle Voxel Mode", _myAvatar.getVoxels(), SLOT(cycleMode())); renderMenu->addAction("Cycle Voxel Mode", _myAvatar.getVoxels(), SLOT(cycleMode()));
renderMenu->addAction("Cycle Face Mode", &_myAvatar.getHead().getFace(), SLOT(cycleRenderMode())); renderMenu->addAction("Cycle Face Mode", &_myAvatar.getHead().getFace(), SLOT(cycleRenderMode()));
renderMenu->addAction("Cycle Glow Mode", &_glowEffect, SLOT(cycleRenderMode()));
(_renderFrameTimerOn = renderMenu->addAction("Show Timer"))->setCheckable(true); (_renderFrameTimerOn = renderMenu->addAction("Show Timer"))->setCheckable(true);
_renderFrameTimerOn->setChecked(false); _renderFrameTimerOn->setChecked(false);
(_renderLookatOn = renderMenu->addAction("Lookat Vectors"))->setCheckable(true); (_renderLookatOn = renderMenu->addAction("Lookat Vectors"))->setCheckable(true);
@ -3162,7 +3163,9 @@ void Application::displaySide(Camera& whichCamera) {
if (isLookingAtMyAvatar(avatar)) { if (isLookingAtMyAvatar(avatar)) {
avatar->getHead().setLookAtPosition(_myCamera.getPosition()); avatar->getHead().setLookAtPosition(_myCamera.getPosition());
} }
_glowEffect.begin();
avatar->render(false, _renderAvatarBalls->isChecked()); avatar->render(false, _renderAvatarBalls->isChecked());
_glowEffect.end();
avatar->setDisplayingLookatVectors(_renderLookatOn->isChecked()); avatar->setDisplayingLookatVectors(_renderLookatOn->isChecked());
} }
@ -3173,7 +3176,9 @@ void Application::displaySide(Camera& whichCamera) {
if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { if (_myCamera.getMode() == CAMERA_MODE_MIRROR) {
_myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition());
} }
_glowEffect.begin();
_myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked());
_glowEffect.end();
_myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked());

View file

@ -14,9 +14,12 @@
#include "GlowEffect.h" #include "GlowEffect.h"
#include "ProgramObject.h" #include "ProgramObject.h"
static ProgramObject* createBlurProgram(const QString& direction) { GlowEffect::GlowEffect() : _renderMode(BLUR_ADD_MODE) {
}
static ProgramObject* createProgram(const QString& name) {
ProgramObject* program = new ProgramObject(); ProgramObject* program = new ProgramObject();
program->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/" + direction + "_blur.frag"); program->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/" + name + ".frag");
program->link(); program->link();
program->bind(); program->bind();
@ -28,12 +31,20 @@ static ProgramObject* createBlurProgram(const QString& direction) {
void GlowEffect::init() { void GlowEffect::init() {
switchToResourcesParentIfRequired(); switchToResourcesParentIfRequired();
_horizontalBlurProgram = createBlurProgram("horizontal");
_verticalBlurProgram = createBlurProgram("vertical");
_verticalBlurProgram->bind(); _addProgram = createProgram("glow_add");
_verticalBlurProgram->setUniformValue("horizontallyBlurredTexture", 1); _horizontalBlurProgram = createProgram("horizontal_blur");
_verticalBlurProgram->release(); _verticalBlurAddProgram = createProgram("vertical_blur_add");
_verticalBlurProgram = createProgram("vertical_blur");
_addSeparateProgram = createProgram("glow_add_separate");
_verticalBlurAddProgram->bind();
_verticalBlurAddProgram->setUniformValue("horizontallyBlurredTexture", 1);
_verticalBlurAddProgram->release();
_addSeparateProgram->bind();
_addSeparateProgram->setUniformValue("blurredTexture", 1);
_addSeparateProgram->release();
} }
void GlowEffect::prepare() { void GlowEffect::prepare() {
@ -93,7 +104,12 @@ void GlowEffect::render() {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
} }
} else { } else if (_renderMode == ADD_MODE) {
_addProgram->bind();
renderFullscreenQuad();
_addProgram->release();
} else { // _renderMode == BLUR_ADD_MODE || _renderMode == BLUR_PERSIST_ADD_MODE
// render the primary to the secondary with the horizontal blur // render the primary to the secondary with the horizontal blur
QOpenGLFramebufferObject* secondaryFBO = QOpenGLFramebufferObject* secondaryFBO =
Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject(); Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject();
@ -105,13 +121,48 @@ void GlowEffect::render() {
secondaryFBO->release(); secondaryFBO->release();
// render the secondary to the screen with the vertical blur if (_renderMode == BLUR_ADD_MODE) {
glActiveTexture(GL_TEXTURE1); // render the secondary to the screen with the vertical blur
glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture());
_verticalBlurAddProgram->bind();
renderFullscreenQuad();
_verticalBlurAddProgram->release();
} else { // _renderMode == BLUR_PERSIST_ADD_MODE
// render the secondary to the tertiary with horizontal blur and persistence
QOpenGLFramebufferObject* tertiaryFBO =
Application::getInstance()->getTextureCache()->getTertiaryFramebufferObject();
tertiaryFBO->bind();
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_CONSTANT_ALPHA, GL_CONSTANT_ALPHA);
const float PERSISTENCE_SMOOTHING = 0.9f;
glBlendColor(0.0f, 0.0f, 0.0f, PERSISTENCE_SMOOTHING);
glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture());
_verticalBlurProgram->bind();
renderFullscreenQuad();
_verticalBlurProgram->release();
_verticalBlurProgram->bind(); glBlendColor(0.0f, 0.0f, 0.0f, 0.0f);
renderFullscreenQuad(); glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);
_verticalBlurProgram->release(); glDisable(GL_BLEND);
// now add the tertiary to the primary buffer
tertiaryFBO->release();
glBindTexture(GL_TEXTURE_2D, primaryFBO->texture());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tertiaryFBO->texture());
_addSeparateProgram->bind();
renderFullscreenQuad();
_addSeparateProgram->release();
}
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -126,3 +177,7 @@ void GlowEffect::render() {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void GlowEffect::cycleRenderMode() {
_renderMode = (RenderMode)((_renderMode + 1) % RENDER_MODE_COUNT);
}

View file

@ -9,11 +9,17 @@
#ifndef __interface__GlowEffect__ #ifndef __interface__GlowEffect__
#define __interface__GlowEffect__ #define __interface__GlowEffect__
#include <QObject>
class ProgramObject; class ProgramObject;
class GlowEffect { class GlowEffect : public QObject {
Q_OBJECT
public: public:
GlowEffect();
void init(); void init();
void prepare(); void prepare();
@ -23,10 +29,20 @@ public:
void render(); void render();
public slots:
void cycleRenderMode();
private: private:
enum RenderMode { ADD_MODE, BLUR_ADD_MODE, BLUR_PERSIST_ADD_MODE, RENDER_MODE_COUNT };
RenderMode _renderMode;
ProgramObject* _addProgram;
ProgramObject* _horizontalBlurProgram; ProgramObject* _horizontalBlurProgram;
ProgramObject* _verticalBlurAddProgram;
ProgramObject* _verticalBlurProgram; ProgramObject* _verticalBlurProgram;
ProgramObject* _addSeparateProgram;
bool _isEmpty; bool _isEmpty;
}; };

View file

@ -14,7 +14,7 @@
#include "TextureCache.h" #include "TextureCache.h"
TextureCache::TextureCache() : _permutationNormalTextureID(0), TextureCache::TextureCache() : _permutationNormalTextureID(0),
_primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL) { _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL), _tertiaryFramebufferObject(NULL) {
} }
TextureCache::~TextureCache() { TextureCache::~TextureCache() {
@ -27,6 +27,9 @@ TextureCache::~TextureCache() {
if (_secondaryFramebufferObject != NULL) { if (_secondaryFramebufferObject != NULL) {
delete _secondaryFramebufferObject; delete _secondaryFramebufferObject;
} }
if (_tertiaryFramebufferObject != NULL) {
delete _tertiaryFramebufferObject;
}
} }
GLuint TextureCache::getPermutationNormalTextureID() { GLuint TextureCache::getPermutationNormalTextureID() {
@ -72,6 +75,14 @@ QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() {
return _secondaryFramebufferObject; return _secondaryFramebufferObject;
} }
QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() {
if (_tertiaryFramebufferObject == NULL) {
_tertiaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size());
Application::getInstance()->getGLWidget()->installEventFilter(this);
}
return _tertiaryFramebufferObject;
}
bool TextureCache::eventFilter(QObject* watched, QEvent* event) { bool TextureCache::eventFilter(QObject* watched, QEvent* event) {
if (event->type() == QEvent::Resize) { if (event->type() == QEvent::Resize) {
QSize size = static_cast<QResizeEvent*>(event)->size(); QSize size = static_cast<QResizeEvent*>(event)->size();
@ -83,6 +94,10 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) {
delete _secondaryFramebufferObject; delete _secondaryFramebufferObject;
_secondaryFramebufferObject = NULL; _secondaryFramebufferObject = NULL;
} }
if (_tertiaryFramebufferObject != NULL && _tertiaryFramebufferObject->size() != size) {
delete _tertiaryFramebufferObject;
_tertiaryFramebufferObject = NULL;
}
} }
return false; return false;
} }

View file

@ -25,7 +25,8 @@ public:
QOpenGLFramebufferObject* getPrimaryFramebufferObject(); QOpenGLFramebufferObject* getPrimaryFramebufferObject();
QOpenGLFramebufferObject* getSecondaryFramebufferObject(); QOpenGLFramebufferObject* getSecondaryFramebufferObject();
QOpenGLFramebufferObject* getTertiaryFramebufferObject();
virtual bool eventFilter(QObject* watched, QEvent* event); virtual bool eventFilter(QObject* watched, QEvent* event);
private: private:
@ -34,6 +35,7 @@ private:
QOpenGLFramebufferObject* _primaryFramebufferObject; QOpenGLFramebufferObject* _primaryFramebufferObject;
QOpenGLFramebufferObject* _secondaryFramebufferObject; QOpenGLFramebufferObject* _secondaryFramebufferObject;
QOpenGLFramebufferObject* _tertiaryFramebufferObject;
}; };
#endif /* defined(__interface__TextureCache__) */ #endif /* defined(__interface__TextureCache__) */