~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/effects/_test/demo_liquid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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
*********************************************************************/
 
20
 
 
21
#include "demo_liquid.h"
 
22
 
 
23
#include <kwinglutils.h>
 
24
 
 
25
 
 
26
 
 
27
#include <kdebug.h>
 
28
#include <assert.h>
 
29
 
 
30
 
 
31
namespace KWin
 
32
{
 
33
 
 
34
KWIN_EFFECT(demo_liquid, LiquidEffect)
 
35
KWIN_EFFECT_SUPPORTED(demo_liquid, LiquidEffect::supported())
 
36
 
 
37
 
 
38
LiquidEffect::LiquidEffect() : Effect()
 
39
{
 
40
    mTexture = 0;
 
41
    mRenderTarget = 0;
 
42
    mShader = 0;
 
43
 
 
44
    mTime = 0.0f;
 
45
    mValid = loadData();
 
46
}
 
47
LiquidEffect::~LiquidEffect()
 
48
{
 
49
    delete mTexture;
 
50
    delete mRenderTarget;
 
51
    delete mShader;
 
52
}
 
53
 
 
54
bool LiquidEffect::loadData()
 
55
{
 
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()) {
 
62
        kWarning(1212) << "NPOT textures not supported, wasting some memory" ;
 
63
        texw = nearestPowerOfTwo(texw);
 
64
        texh = nearestPowerOfTwo(texh);
 
65
    }
 
66
    // Create texture and render target
 
67
    mTexture = new GLTexture(texw, texh);
 
68
    mTexture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
 
69
    mTexture->setWrapMode(GL_CLAMP);
 
70
 
 
71
    mRenderTarget = new GLRenderTarget(mTexture);
 
72
    if (!mRenderTarget->valid())
 
73
        return false;
 
74
 
 
75
    QString fragmentshader =  KGlobal::dirs()->findResource("data", "kwin/liquid.frag");
 
76
    QString vertexshader =  KGlobal::dirs()->findResource("data", "kwin/liquid.vert");
 
77
    if (fragmentshader.isEmpty() || vertexshader.isEmpty()) {
 
78
        kError(1212) << "Couldn't locate shader files" << endl;
 
79
        return false;
 
80
    }
 
81
    mShader = new GLShader(vertexshader, fragmentshader);
 
82
    if (!mShader->isValid()) {
 
83
        kError(1212) << "The shader failed to load!" << endl;
 
84
        return false;
 
85
    }
 
86
    mShader->bind();
 
87
    mShader->setUniform("sceneTex", 0);
 
88
    mShader->setUniform("textureWidth", (float)texw);
 
89
    mShader->setUniform("textureHeight", (float)texh);
 
90
    mShader->unbind();
 
91
 
 
92
    return true;
 
93
}
 
94
 
 
95
bool LiquidEffect::supported()
 
96
{
 
97
    return GLRenderTarget::supported() &&
 
98
           GLShader::fragmentShaderSupported() &&
 
99
           (effects->compositingType() == OpenGLCompositing);
 
100
}
 
101
 
 
102
 
 
103
void LiquidEffect::prePaintScreen(ScreenPrePaintData& data, int time)
 
104
{
 
105
    mTime += time / 1000.0f;
 
106
    if (mValid) {
 
107
        data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
 
108
        // Start rendering to texture
 
109
        effects->pushRenderTarget(mRenderTarget);
 
110
    }
 
111
 
 
112
    effects->prePaintScreen(data, time);
 
113
}
 
114
 
 
115
void LiquidEffect::postPaintScreen()
 
116
{
 
117
    // Call the next effect.
 
118
    effects->postPaintScreen();
 
119
 
 
120
    if (mValid) {
 
121
        // Disable render texture
 
122
        assert(effects->popRenderTarget() == mRenderTarget);
 
123
        mTexture->bind();
 
124
 
 
125
        // Use the shader
 
126
        mShader->bind();
 
127
        mShader->setUniform("time", (float)mTime);
 
128
 
 
129
        // Render fullscreen quad with screen contents
 
130
        glBegin(GL_QUADS);
 
131
        glVertex2f(0.0, displayHeight());
 
132
        glVertex2f(displayWidth(), displayHeight());
 
133
        glVertex2f(displayWidth(), 0.0);
 
134
        glVertex2f(0.0, 0.0);
 
135
        glEnd();
 
136
 
 
137
        mShader->unbind();
 
138
        mTexture->unbind();
 
139
 
 
140
        // Make sure the animation continues
 
141
        effects->addRepaintFull();
 
142
    }
 
143
 
 
144
}
 
145
 
 
146
 
 
147
} // namespace
 
148