Diffuse/add glow mode.

This commit is contained in:
Andrzej Kapolka 2013-08-14 16:13:30 -07:00
parent e2513901ef
commit a9ccca3f72
3 changed files with 67 additions and 2 deletions

View file

@ -0,0 +1,28 @@
#version 120
//
// diffuse.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 diffused color
uniform sampler2D diffusedTexture;
void main(void) {
float ds = dFdx(gl_TexCoord[0].s);
float dt = dFdy(gl_TexCoord[0].t);
gl_FragColor = (texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(-ds, -dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(0.0, -dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(ds, -dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(ds, 0.0)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(ds, dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(0.0, dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(-ds, dt)) +
texture2D(diffusedTexture, gl_TexCoord[0].st + vec2(-ds, 0.0))) / 8.5 + texture2D(originalTexture, gl_TexCoord[0].st) * 0.1;
}

View file

@ -14,7 +14,7 @@
#include "GlowEffect.h"
#include "ProgramObject.h"
GlowEffect::GlowEffect() : _renderMode(BLUR_ADD_MODE) {
GlowEffect::GlowEffect() : _renderMode(DIFFUSE_ADD_MODE) {
}
static ProgramObject* createProgram(const QString& name) {
@ -37,6 +37,7 @@ void GlowEffect::init() {
_verticalBlurAddProgram = createProgram("vertical_blur_add");
_verticalBlurProgram = createProgram("vertical_blur");
_addSeparateProgram = createProgram("glow_add_separate");
_diffuseProgram = createProgram("diffuse");
_verticalBlurAddProgram->bind();
_verticalBlurAddProgram->setUniformValue("horizontallyBlurredTexture", 1);
@ -45,6 +46,10 @@ void GlowEffect::init() {
_addSeparateProgram->bind();
_addSeparateProgram->setUniformValue("blurredTexture", 1);
_addSeparateProgram->release();
_diffuseProgram->bind();
_diffuseProgram->setUniformValue("diffusedTexture", 1);
_diffuseProgram->release();
}
void GlowEffect::prepare() {
@ -108,6 +113,36 @@ void GlowEffect::render() {
_addProgram->bind();
renderFullscreenQuad();
_addProgram->release();
} else if (_renderMode == DIFFUSE_ADD_MODE) {
// diffuse into the secondary/tertiary (alternating between frames)
QOpenGLFramebufferObject* oldDiffusedFBO =
Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject();
QOpenGLFramebufferObject* newDiffusedFBO =
Application::getInstance()->getTextureCache()->getTertiaryFramebufferObject();
if ((_isOddFrame = !_isOddFrame)) {
qSwap(oldDiffusedFBO, newDiffusedFBO);
}
newDiffusedFBO->bind();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, oldDiffusedFBO->texture());
_diffuseProgram->bind();
renderFullscreenQuad();
_diffuseProgram->release();
newDiffusedFBO->release();
// add diffused texture to the primary
glBindTexture(GL_TEXTURE_2D, newDiffusedFBO->texture());
_addSeparateProgram->bind();
renderFullscreenQuad();
_addSeparateProgram->release();
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
} else { // _renderMode == BLUR_ADD_MODE || _renderMode == BLUR_PERSIST_ADD_MODE
// render the primary to the secondary with the horizontal blur

View file

@ -35,7 +35,7 @@ public slots:
private:
enum RenderMode { ADD_MODE, BLUR_ADD_MODE, BLUR_PERSIST_ADD_MODE, RENDER_MODE_COUNT };
enum RenderMode { ADD_MODE, BLUR_ADD_MODE, BLUR_PERSIST_ADD_MODE, DIFFUSE_ADD_MODE, RENDER_MODE_COUNT };
RenderMode _renderMode;
ProgramObject* _addProgram;
@ -43,8 +43,10 @@ private:
ProgramObject* _verticalBlurAddProgram;
ProgramObject* _verticalBlurProgram;
ProgramObject* _addSeparateProgram;
ProgramObject* _diffuseProgram;
bool _isEmpty;
bool _isOddFrame;
};
#endif /* defined(__interface__GlowEffect__) */