add more spacing
[personal-kdebase.git] / workspace / kwin / effects / test / demo_liquid.cpp
blob3a75bae75aff5211976a9a3261f3017d239b2421
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
21 #include "demo_liquid.h"
23 #include <kwinglutils.h>
27 #include <kdebug.h>
28 #include <assert.h>
31 namespace KWin
34 KWIN_EFFECT( demo_liquid, LiquidEffect )
35 KWIN_EFFECT_SUPPORTED( demo_liquid, LiquidEffect::supported() )
38 LiquidEffect::LiquidEffect() : Effect()
40 mTexture = 0;
41 mRenderTarget = 0;
42 mShader = 0;
44 mTime = 0.0f;
45 mValid = loadData();
47 LiquidEffect::~LiquidEffect()
49 delete mTexture;
50 delete mRenderTarget;
51 delete mShader;
54 bool LiquidEffect::loadData()
56 // If NPOT textures are not supported, use nearest power-of-two sized
57 // texture. It wastes memory, but it's possible to support systems without
58 // NPOT textures that way
59 int texw = displayWidth();
60 int texh = displayHeight();
61 if( !GLTexture::NPOTTextureSupported() )
63 kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ;
64 texw = nearestPowerOfTwo(texw);
65 texh = nearestPowerOfTwo(texh);
67 // Create texture and render target
68 mTexture = new GLTexture(texw, texh);
69 mTexture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
70 mTexture->setWrapMode(GL_CLAMP);
72 mRenderTarget = new GLRenderTarget(mTexture);
73 if( !mRenderTarget->valid() )
74 return false;
76 QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/liquid.frag");
77 QString vertexshader = KGlobal::dirs()->findResource("data", "kwin/liquid.vert");
78 if(fragmentshader.isEmpty() || vertexshader.isEmpty())
80 kError(1212) << "Couldn't locate shader files" << endl;
81 return false;
83 mShader = new GLShader(vertexshader, fragmentshader);
84 if(!mShader->isValid())
86 kError(1212) << "The shader failed to load!" << endl;
87 return false;
89 mShader->bind();
90 mShader->setUniform("sceneTex", 0);
91 mShader->setUniform("textureWidth", (float)texw);
92 mShader->setUniform("textureHeight", (float)texh);
93 mShader->unbind();
95 return true;
98 bool LiquidEffect::supported()
100 return GLRenderTarget::supported() &&
101 GLShader::fragmentShaderSupported() &&
102 (effects->compositingType() == OpenGLCompositing);
106 void LiquidEffect::prePaintScreen( ScreenPrePaintData& data, int time )
108 mTime += time / 1000.0f;
109 if(mValid)
111 data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
112 // Start rendering to texture
113 effects->pushRenderTarget(mRenderTarget);
116 effects->prePaintScreen(data, time);
119 void LiquidEffect::postPaintScreen()
121 // Call the next effect.
122 effects->postPaintScreen();
124 if(mValid)
126 // Disable render texture
127 assert( effects->popRenderTarget() == mRenderTarget );
128 mTexture->bind();
130 // Use the shader
131 mShader->bind();
132 mShader->setUniform("time", (float)mTime);
134 // Render fullscreen quad with screen contents
135 glBegin(GL_QUADS);
136 glVertex2f(0.0, displayHeight());
137 glVertex2f(displayWidth(), displayHeight());
138 glVertex2f(displayWidth(), 0.0);
139 glVertex2f(0.0, 0.0);
140 glEnd();
142 mShader->unbind();
143 mTexture->unbind();
145 // Make sure the animation continues
146 effects->addRepaintFull();
152 } // namespace