2007-04-18 19:44:49 +00:00
|
|
|
/*****************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
|
|
|
|
|
|
|
|
You can Freely distribute this program under the GNU General Public
|
|
|
|
License. See the file "COPYING" for the exact licensing terms.
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "demo_liquid.h"
|
|
|
|
|
|
|
|
#include <kwinglutils.h>
|
|
|
|
|
2007-04-22 11:14:24 +00:00
|
|
|
|
2007-04-18 19:44:49 +00:00
|
|
|
#include <KStandardDirs>
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
2007-04-20 15:53:30 +00:00
|
|
|
#include <assert.h>
|
2007-04-18 19:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
KWIN_EFFECT( Demo_Liquid, LiquidEffect );
|
|
|
|
KWIN_EFFECT_SUPPORTED( Demo_Liquid, LiquidEffect::supported() );
|
|
|
|
|
|
|
|
|
|
|
|
LiquidEffect::LiquidEffect() : Effect()
|
|
|
|
{
|
2007-04-28 13:19:36 +00:00
|
|
|
mTexture = 0;
|
|
|
|
mRenderTarget = 0;
|
|
|
|
mShader = 0;
|
|
|
|
|
2007-04-18 19:44:49 +00:00
|
|
|
mTime = 0.0f;
|
|
|
|
mValid = loadData();
|
|
|
|
}
|
2007-04-28 13:19:36 +00:00
|
|
|
LiquidEffect::~LiquidEffect()
|
|
|
|
{
|
|
|
|
delete mTexture;
|
|
|
|
delete mRenderTarget;
|
|
|
|
delete mShader;
|
|
|
|
}
|
2007-04-18 19:44:49 +00:00
|
|
|
|
|
|
|
bool LiquidEffect::loadData()
|
|
|
|
{
|
|
|
|
// Create texture and render target
|
|
|
|
mTexture = new GLTexture(displayWidth(), displayHeight());
|
|
|
|
mTexture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
|
2007-04-28 12:25:54 +00:00
|
|
|
mTexture->setWrapMode(GL_CLAMP);
|
2007-04-18 19:44:49 +00:00
|
|
|
|
|
|
|
mRenderTarget = new GLRenderTarget(mTexture);
|
|
|
|
if( !mRenderTarget->valid() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/liquid.frag");
|
|
|
|
QString vertexshader = KGlobal::dirs()->findResource("data", "kwin/liquid.vert");
|
|
|
|
if(fragmentshader.isEmpty() || vertexshader.isEmpty())
|
|
|
|
{
|
|
|
|
kError() << k_funcinfo << "Couldn't locate shader files" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mShader = new GLShader(vertexshader, fragmentshader);
|
|
|
|
if(!mShader->isValid())
|
|
|
|
{
|
|
|
|
kError() << k_funcinfo << "The shader failed to load!" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mShader->bind();
|
|
|
|
mShader->setUniform("sceneTex", 0);
|
|
|
|
mShader->setUniform("displayWidth", (float)displayWidth());
|
|
|
|
mShader->setUniform("displayHeight", (float)displayHeight());
|
|
|
|
mShader->unbind();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LiquidEffect::supported()
|
|
|
|
{
|
2007-04-28 14:38:06 +00:00
|
|
|
return GLRenderTarget::supported() &&
|
2007-04-18 19:44:49 +00:00
|
|
|
GLShader::fragmentShaderSupported() &&
|
|
|
|
(effects->compositingType() == OpenGLCompositing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LiquidEffect::prePaintScreen( int* mask, QRegion* region, int time )
|
|
|
|
{
|
|
|
|
mTime += time / 1000.0f;
|
|
|
|
if(mValid)
|
|
|
|
{
|
|
|
|
*mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
|
|
|
|
// Start rendering to texture
|
2007-04-20 15:53:30 +00:00
|
|
|
effects->pushRenderTarget(mRenderTarget);
|
2007-04-18 19:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
effects->prePaintScreen(mask, region, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiquidEffect::postPaintScreen()
|
|
|
|
{
|
|
|
|
// Call the next effect.
|
|
|
|
effects->postPaintScreen();
|
|
|
|
|
|
|
|
if(mValid)
|
|
|
|
{
|
|
|
|
// Disable render texture
|
2007-04-20 15:53:30 +00:00
|
|
|
assert( effects->popRenderTarget() == mRenderTarget );
|
2007-04-18 19:44:49 +00:00
|
|
|
mTexture->bind();
|
|
|
|
|
|
|
|
// Use the shader
|
|
|
|
mShader->bind();
|
|
|
|
mShader->setUniform("time", mTime);
|
|
|
|
|
|
|
|
// Render fullscreen quad with screen contents
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex2f(0.0, displayHeight());
|
|
|
|
glVertex2f(displayWidth(), displayHeight());
|
|
|
|
glVertex2f(displayWidth(), 0.0);
|
|
|
|
glVertex2f(0.0, 0.0);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
mShader->unbind();
|
|
|
|
mTexture->unbind();
|
|
|
|
|
|
|
|
// Make sure the animation continues
|
|
|
|
effects->addRepaintFull();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|