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>
34 KWIN_EFFECT( demo_liquid
, LiquidEffect
)
35 KWIN_EFFECT_SUPPORTED( demo_liquid
, LiquidEffect::supported() )
38 LiquidEffect::LiquidEffect() : Effect()
47 LiquidEffect::~LiquidEffect()
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() )
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
;
83 mShader
= new GLShader(vertexshader
, fragmentshader
);
84 if(!mShader
->isValid())
86 kError(1212) << "The shader failed to load!" << endl
;
90 mShader
->setUniform("sceneTex", 0);
91 mShader
->setUniform("textureWidth", (float)texw
);
92 mShader
->setUniform("textureHeight", (float)texh
);
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
;
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();
126 // Disable render texture
127 assert( effects
->popRenderTarget() == mRenderTarget
);
132 mShader
->setUniform("time", (float)mTime
);
134 // Render fullscreen quad with screen contents
136 glVertex2f(0.0, displayHeight());
137 glVertex2f(displayWidth(), displayHeight());
138 glVertex2f(displayWidth(), 0.0);
139 glVertex2f(0.0, 0.0);
145 // Make sure the animation continues
146 effects
->addRepaintFull();