cube.cpp 86 KB
Newer Older
Martin Flöser's avatar
Martin Flöser committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

 Copyright (C) 2008 Martin Gräßlin <ubuntu@martin-graesslin.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "cube.h"
21
#include "cube_inside.h"
Martin Flöser's avatar
Martin Flöser committed
22
23
24
25
26
27
28

#include <kaction.h>
#include <kactioncollection.h>
#include <klocale.h>
#include <kwinconfig.h>
#include <kconfiggroup.h>
#include <kcolorscheme.h>
29
30
#include <kglobal.h>
#include <kstandarddirs.h>
Martin Flöser's avatar
Martin Flöser committed
31
32
33
34
35
36
37
38
39
#include <kdebug.h>

#include <QColor>
#include <QRect>
#include <QEvent>
#include <QKeyEvent>

#include <math.h>

Martin Flöser's avatar
Martin Flöser committed
40
#include <kwinglutils.h>
Martin Flöser's avatar
Martin Flöser committed
41
42
43
44
45

namespace KWin
{

KWIN_EFFECT( cube, CubeEffect )
46
KWIN_EFFECT_SUPPORTED( cube, CubeEffect::supported() )
Martin Flöser's avatar
Martin Flöser committed
47
48
49

CubeEffect::CubeEffect()
    : activated( false )
50
    , mousePolling( false )
Martin Flöser's avatar
Martin Flöser committed
51
52
53
    , cube_painting( false )
    , keyboard_grab( false )
    , schedule_close( false )
54
    , painting_desktop( 1 )
Martin Flöser's avatar
Martin Flöser committed
55
56
    , frontDesktop( 0 )
    , cubeOpacity( 1.0 )
57
58
    , opacityDesktopOnly( true )
    , displayDesktopName( false )
59
    , desktopNameFrame( effects->effectFrame( EffectFrameStyled ) )
Martin Flöser's avatar
Martin Flöser committed
60
61
62
63
64
65
66
67
    , reflection( true )
    , rotating( false )
    , desktopChangedWhileRotating( false )
    , paintCaps( true )
    , rotationDirection( Left )
    , verticalRotationDirection( Upwards )
    , verticalPosition( Normal )
    , wallpaper( NULL )
68
69
    , texturedCaps( true )
    , capTexture( NULL )
Martin Flöser's avatar
Martin Flöser committed
70
71
72
73
74
75
    , manualAngle( 0.0 )
    , manualVerticalAngle( 0.0 )
    , currentShape( TimeLine::EaseInOutCurve )
    , start( false )
    , stop( false )
    , reflectionPainting( false )
76
    , activeScreen( 0 )
Martin Flöser's avatar
Martin Flöser committed
77
    , bottomCap( false )
78
    , closeOnMouseRelease( false )
79
80
    , zoom( 0.0 )
    , zPosition( 0.0 )
81
82
    , useForTabBox( false )
    , tabBoxMode( false )
83
    , shortcutsRegistered( false )
84
85
86
87
    , mode( Cube )
    , useShaders( false )
    , cylinderShader( 0 )
    , sphereShader( 0 )
88
    , zOrderingFactor( 0.0f )
89
90
    , mAddedHeightCoeff1( 0.0f )
    , mAddedHeightCoeff2( 0.0f )
91
    , m_cubeCapBuffer( NULL )
92
    , m_proxy( this )
Martin Flöser's avatar
Martin Flöser committed
93
    {
94
95
    desktopNameFont.setBold( true );
    desktopNameFont.setPointSize( 14 );
96
    desktopNameFrame->setFont( desktopNameFont );
97

98
99
    const QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/cube-reflection.glsl");
    m_reflectionShader = ShaderManager::instance()->loadFragmentShader(ShaderManager::GenericShader, fragmentshader);
Martin Flöser's avatar
Martin Flöser committed
100
101
    const QString capshader = KGlobal::dirs()->findResource("data", "kwin/cube-cap.glsl");
    m_capShader = ShaderManager::instance()->loadFragmentShader(ShaderManager::GenericShader, capshader);
102
103
    m_textureMirrorMatrix.scale(1.0, -1.0, 1.0);
    m_textureMirrorMatrix.translate(0.0, -1.0, 0.0);
104

105
    reconfigure( ReconfigureAll );
106
107
    }

108
109
bool CubeEffect::supported()
    {
110
    return effects->compositingType() == OpenGLCompositing;
111
112
    }

113
114
115
116
117
void CubeEffect::reconfigure( ReconfigureFlags )
    {
    loadConfig( "Cube" );
    }

118
119
120
void CubeEffect::loadConfig( QString config )
    {
    KConfigGroup conf = effects->effectConfig( config );
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    foreach( ElectricBorder border, borderActivate )
        {
        effects->unreserveElectricBorder( border );
        }
    foreach( ElectricBorder border, borderActivateCylinder )
        {
        effects->unreserveElectricBorder( border );
        }
    foreach( ElectricBorder border, borderActivateSphere )
        {
        effects->unreserveElectricBorder( border );
        }
    borderActivate.clear();
    borderActivateCylinder.clear();
    borderActivateSphere.clear();
    QList<int> borderList = QList<int>();
    borderList.append( int( ElectricNone ) );
    borderList = conf.readEntry( "BorderActivate", borderList );
    foreach( int i, borderList )
        {
        borderActivate.append( ElectricBorder( i ) );
        effects->reserveElectricBorder( ElectricBorder( i ) );
        }
    borderList.clear();
    borderList.append( int( ElectricNone ) );
    borderList = conf.readEntry( "BorderActivateCylinder", borderList );
    foreach( int i, borderList )
        {
        borderActivateCylinder.append( ElectricBorder( i ) );
        effects->reserveElectricBorder( ElectricBorder( i ) );
        }
    borderList.clear();
    borderList.append( int( ElectricNone ) );
    borderList = conf.readEntry( "BorderActivateSphere", borderList );
    foreach( int i, borderList )
        {
        borderActivateSphere.append( ElectricBorder( i ) );
        effects->reserveElectricBorder( ElectricBorder( i ) );
        }
Martin Flöser's avatar
Martin Flöser committed
160
161

    cubeOpacity = (float)conf.readEntry( "Opacity", 80 )/100.0f;
162
    opacityDesktopOnly = conf.readEntry( "OpacityDesktopOnly", false );
Martin Flöser's avatar
Martin Flöser committed
163
164
    displayDesktopName = conf.readEntry( "DisplayDesktopName", true );
    reflection = conf.readEntry( "Reflection", true );
165
    rotationDuration = animationTime( conf, "RotationDuration", 500 );
Martin Flöser's avatar
Martin Flöser committed
166
167
168
    backgroundColor = conf.readEntry( "BackgroundColor", QColor( Qt::black ) );
    capColor = conf.readEntry( "CapColor", KColorScheme( QPalette::Active, KColorScheme::Window ).background().color() );
    paintCaps = conf.readEntry( "Caps", true );
169
    closeOnMouseRelease = conf.readEntry( "CloseOnMouseRelease", false );
170
171
172
173
    float defaultZPosition = 100.0f;
    if( config == "Sphere" )
        defaultZPosition = 450.0f;
    zPosition = conf.readEntry( "ZPosition", defaultZPosition );
174
    useForTabBox = conf.readEntry( "TabBox", false );
175
176
    invertKeys = conf.readEntry( "InvertKeys", false );
    invertMouse = conf.readEntry( "InvertMouse", false );
177
    capDeformationFactor = conf.readEntry( "CapDeformation", 0 )/100.0f;
178
    useZOrdering = conf.readEntry( "ZOrdering", false );
Martin Flöser's avatar
Martin Flöser committed
179
    QString file = conf.readEntry( "Wallpaper", QString("") );
180
181
    if( wallpaper )
        wallpaper->discard();
182
183
    delete wallpaper;
    wallpaper = NULL;
Martin Flöser's avatar
Martin Flöser committed
184
185
186
187
188
189
190
191
    if( !file.isEmpty() )
        {
        QImage img = QImage( file );
        if( !img.isNull() )
            {
            wallpaper = new GLTexture( img );
            }
        }
192
193
    delete capTexture;
    capTexture = NULL;
194
195
196
    texturedCaps = conf.readEntry( "TexturedCaps", true );
    if( texturedCaps )
        {
197
198
        QString capPath = conf.readEntry( "CapPath", KGlobal::dirs()->findResource( "appdata", "cubecap.png" ) );
        QImage img = QImage( capPath );
199
200
201
202
        if( !img.isNull() )
            {
            capTexture = new GLTexture( img );
            capTexture->setFilter( GL_LINEAR );
Martin Flöser's avatar
Martin Flöser committed
203
#ifndef KWIN_HAVE_OPENGLES
204
            capTexture->setWrapMode( GL_CLAMP_TO_BORDER );
Martin Flöser's avatar
Martin Flöser committed
205
#endif
206
207
            }
        }
Martin Flöser's avatar
Martin Flöser committed
208
209
210
211
212
213

    timeLine.setCurveShape( TimeLine::EaseInOutCurve );
    timeLine.setDuration( rotationDuration );

    verticalTimeLine.setCurveShape( TimeLine::EaseInOutCurve );
    verticalTimeLine.setDuration( rotationDuration );
214
215

    // do not connect the shortcut if we use cylinder or sphere
216
    if( !shortcutsRegistered )
217
        {
218
219
220
221
        KActionCollection* actionCollection = new KActionCollection( this );
        KAction* cubeAction = static_cast< KAction* >( actionCollection->addAction( "Cube" ));
        cubeAction->setText( i18n("Desktop Cube" ));
        cubeAction->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::Key_F11 ));
222
        cubeShortcut = cubeAction->globalShortcut();
223
224
        KAction* cylinderAction = static_cast< KAction* >( actionCollection->addAction( "Cylinder" ));
        cylinderAction->setText( i18n("Desktop Cylinder" ));
225
        cylinderAction->setGlobalShortcut( KShortcut(), KAction::ActiveShortcut);
226
        cylinderShortcut = cylinderAction->globalShortcut();
227
228
        KAction* sphereAction = static_cast< KAction* >( actionCollection->addAction( "Sphere" ));
        sphereAction->setText( i18n("Desktop Sphere" ));
229
        sphereAction->setGlobalShortcut( KShortcut(), KAction::ActiveShortcut);
230
        sphereShortcut = sphereAction->globalShortcut();
231
232
233
        connect( cubeAction, SIGNAL( triggered( bool )), this, SLOT( toggleCube()));
        connect( cylinderAction, SIGNAL( triggered( bool )), this, SLOT( toggleCylinder()));
        connect( sphereAction, SIGNAL( triggered( bool )), this, SLOT( toggleSphere()));
234
235
236
        connect( cubeAction, SIGNAL( globalShortcutChanged( QKeySequence )), this, SLOT( cubeShortcutChanged(QKeySequence)));
        connect( cylinderAction, SIGNAL( globalShortcutChanged( QKeySequence )), this, SLOT( cylinderShortcutChanged(QKeySequence)));
        connect( sphereAction, SIGNAL( globalShortcutChanged( QKeySequence )), this, SLOT( sphereShortcutChanged(QKeySequence)));
237
238
        shortcutsRegistered = true;
        }
Martin Flöser's avatar
Martin Flöser committed
239
240
241
242
243
244
245

    // set the cap color on the shader
    if (ShaderManager::instance()->isValid() && m_capShader->isValid()) {
        ShaderManager::instance()->pushShader(m_capShader);
        m_capShader->setUniform("u_capColor", capColor);
        ShaderManager::instance()->popShader();
    }
Martin Flöser's avatar
Martin Flöser committed
246
247
248
249
    }

CubeEffect::~CubeEffect()
    {
250
251
252
253
254
255
256
257
258
259
260
261
    foreach( ElectricBorder border, borderActivate )
        {
        effects->unreserveElectricBorder( border );
        }
    foreach( ElectricBorder border, borderActivateCylinder )
        {
        effects->unreserveElectricBorder( border );
        }
    foreach( ElectricBorder border, borderActivateSphere )
        {
        effects->unreserveElectricBorder( border );
        }
Martin Flöser's avatar
Martin Flöser committed
262
    delete wallpaper;
263
    delete capTexture;
264
265
    delete cylinderShader;
    delete sphereShader;
266
    delete desktopNameFrame;
267
    delete m_reflectionShader;
Martin Flöser's avatar
Martin Flöser committed
268
269
    delete m_capShader;
    delete m_cubeCapBuffer;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    }

bool CubeEffect::loadShader()
    {
    if( !(GLShader::fragmentShaderSupported() &&
            (effects->compositingType() == OpenGLCompositing)))
        return false;
    QString fragmentshader       =  KGlobal::dirs()->findResource( "data", "kwin/cylinder.frag" );
    QString cylinderVertexshader =  KGlobal::dirs()->findResource( "data", "kwin/cylinder.vert" );
    QString sphereVertexshader   = KGlobal::dirs()->findResource( "data", "kwin/sphere.vert" );
    if( fragmentshader.isEmpty() || cylinderVertexshader.isEmpty() || sphereVertexshader.isEmpty() )
        {
        kError(1212) << "Couldn't locate shader files" << endl;
        return false;
        }

    cylinderShader = new GLShader(cylinderVertexshader, fragmentshader);
    if( !cylinderShader->isValid() )
        {
        kError(1212) << "The cylinder shader failed to load!" << endl;
        return false;
        }
    else
        {
        cylinderShader->bind();
        cylinderShader->setUniform( "winTexture", 0 );
296
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
297
298
299
300
301
302
303
304
305
306
307
308
309
        cylinderShader->setUniform( "width", (float)rect.width() );
        cylinderShader->unbind();
        }
    sphereShader = new GLShader( sphereVertexshader, fragmentshader );
    if( !sphereShader->isValid() )
        {
        kError(1212) << "The sphere shader failed to load!" << endl;
        return false;
        }
    else
        {
        sphereShader->bind();
        sphereShader->setUniform( "winTexture", 0 );
310
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
311
312
313
314
315
        sphereShader->setUniform( "width", (float)rect.width() );
        sphereShader->setUniform( "height", (float)rect.height() );
        sphereShader->unbind();
        }
    return true;
Martin Flöser's avatar
Martin Flöser committed
316
317
318
319
320
321
322
323
324
325
326
    }

void CubeEffect::prePaintScreen( ScreenPrePaintData& data, int time )
    {
    if( activated )
        {
        data.mask |= PAINT_SCREEN_TRANSFORMED | Effect::PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS | PAINT_SCREEN_BACKGROUND_FIRST;

        if( rotating || start || stop )
            {
            timeLine.addTime( time );
327
            rotateCube();
Martin Flöser's avatar
Martin Flöser committed
328
329
330
331
            }
        if( verticalRotating )
            {
            verticalTimeLine.addTime( time );
332
            rotateCube();
Martin Flöser's avatar
Martin Flöser committed
333
334
335
336
337
338
339
340
341
            }
        }
    effects->prePaintScreen( data, time );
    }

void CubeEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
    {
    if( activated )
        {
342
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
Martin Flöser's avatar
Martin Flöser committed
343
344
345
346
347
348
349
350
351

        // background
        float clearColor[4];
        glGetFloatv( GL_COLOR_CLEAR_VALUE, clearColor );
        glClearColor( backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), 1.0 );
        glClear( GL_COLOR_BUFFER_BIT );
        glClearColor( clearColor[0], clearColor[1], clearColor[2], clearColor[3] );

        // wallpaper
352
353
        if( wallpaper )
            {
Martin Flöser's avatar
Martin Flöser committed
354
355
356
357
358
            wallpaper->bind();
            wallpaper->render( region, rect );
            wallpaper->unbind();
            }

Martin Flöser's avatar
Martin Flöser committed
359
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
360
        glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
Martin Flöser's avatar
Martin Flöser committed
361
#endif
Martin Flöser's avatar
Martin Flöser committed
362
363
364
        glEnable( GL_BLEND );
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

365
366
367
368
369
370
371
372
        // some veriables needed for painting the caps
        float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
        float point = rect.width()/2*tan(cubeAngle*0.5f*M_PI/180.0f);
        float zTranslate = zPosition + zoom;
        if( start )
            zTranslate *= timeLine.value();
        if( stop )
            zTranslate *= ( 1.0 - timeLine.value() );
Martin Flöser's avatar
Martin Flöser committed
373
        // reflection
374
        if( reflection && mode != Sphere )
Martin Flöser's avatar
Martin Flöser committed
375
            {
376
377
378
379
380
            // restrict painting the reflections to the current screen
            PaintClipper::push( QRegion( rect ));
            // we can use a huge scale factor (needed to calculate the rearground vertices)
            // as we restrict with a PaintClipper painting on the current screen
            float scaleFactor = 1000000 * tan( 60.0 * M_PI / 360.0f )/rect.height();
Martin Flöser's avatar
Martin Flöser committed
381
382
            m_reflectionMatrix.setToIdentity();
            m_reflectionMatrix.scale(1.0, -1.0, 1.0);
383
384
385
386
387
388

            // TODO reflection is not correct when mixing manual (mouse) rotating with rotation by cursor keys
            // there's also a small bug when zooming
            float addedHeight1 = -sin( asin( float( rect.height() ) / mAddedHeightCoeff1 ) + fabs( manualVerticalAngle ) * M_PI / 180.0f ) * mAddedHeightCoeff1;
            float addedHeight2 = -sin( asin( float( rect.height() ) / mAddedHeightCoeff2 ) + fabs( manualVerticalAngle ) * M_PI / 180.0f ) * mAddedHeightCoeff2 - addedHeight1;
            if( manualVerticalAngle > 0.0f && effects->numberOfDesktops() & 1 )
Martin Flöser's avatar
Martin Flöser committed
389
390
391
                {
                m_reflectionMatrix.translate(0.0, cos( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0);
                }
392
            else
Martin Flöser's avatar
Martin Flöser committed
393
394
395
                {
                m_reflectionMatrix.translate(0.0, sin( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0);
                }
396
            pushMatrix(m_reflectionMatrix);
397

Martin Flöser's avatar
Martin Flöser committed
398
399
#ifndef KWIN_HAVE_OPENGLES
            // TODO: find a solution for GLES
400
            glEnable( GL_CLIP_PLANE0 );
Martin Flöser's avatar
Martin Flöser committed
401
#endif
Martin Flöser's avatar
Martin Flöser committed
402
            reflectionPainting = true;
403
            glEnable( GL_CULL_FACE );
Martin Flöser's avatar
Martin Flöser committed
404
            paintCap(true, -point-zTranslate);
405
406

            // cube
Martin Flöser's avatar
Martin Flöser committed
407
            glCullFace( GL_BACK );
408
409
410
411
412
413
            if( mode == Cylinder )
                {
                cylinderShader->bind();
                cylinderShader->setUniform( "front", 1.0f );
                cylinderShader->unbind();
                }
414
415
416
            pushMatrix(m_rotationMatrix);
            paintCube(mask, region, data);
            popMatrix();
417
418

            // call the inside cube effects
419
#ifndef KWIN_HAVE_OPENGLES
420
421
            foreach( CubeInsideEffect* inside, m_cubeInsideEffects )
                {
422
                pushMatrix(m_rotationMatrix);
423
424
425
                glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
                glRotatef( (1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0 );
                inside->paint();
426
                popMatrix();
427
                }
428
#endif
429

Martin Flöser's avatar
Martin Flöser committed
430
            glCullFace( GL_FRONT );
431
432
433
434
435
436
            if( mode == Cylinder )
                {
                cylinderShader->bind();
                cylinderShader->setUniform( "front", -1.0f );
                cylinderShader->unbind();
                }
437
438
439
            pushMatrix(m_rotationMatrix);
            paintCube(mask, region, data);
            popMatrix();
440

Martin Flöser's avatar
Martin Flöser committed
441
            paintCap(false, -point-zTranslate);
442
            glDisable( GL_CULL_FACE );
Martin Flöser's avatar
Martin Flöser committed
443
            reflectionPainting = false;
Martin Flöser's avatar
Martin Flöser committed
444
445
#ifndef KWIN_HAVE_OPENGLES
            // TODO: find a solution for GLES
446
            glDisable( GL_CLIP_PLANE0 );
Martin Flöser's avatar
Martin Flöser committed
447
#endif
448
            popMatrix();
449

Martin Flöser's avatar
Martin Flöser committed
450
            float vertices[] = {
451
452
453
454
                -rect.width()*0.5f, rect.height(), 0.0,
                rect.width()*0.5f, rect.height(), 0.0,
                (float)rect.width()*scaleFactor, rect.height(), -5000,
                -(float)rect.width()*scaleFactor, rect.height(), -5000 };
Martin Flöser's avatar
Martin Flöser committed
455
            // foreground
456
            float alpha = 0.7;
Martin Flöser's avatar
Martin Flöser committed
457
            if( start )
458
                alpha = 0.3 + 0.4 * timeLine.value();
Martin Flöser's avatar
Martin Flöser committed
459
            if( stop )
460
                alpha = 0.3 + 0.4 * ( 1.0 - timeLine.value() );
461
462
            glEnable( GL_BLEND );
            glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
            ShaderManager *shaderManager = ShaderManager::instance();
            if (shaderManager->isValid() && m_reflectionShader->isValid()) {
                // ensure blending is enabled - no attribute stack
                shaderManager->pushShader(m_reflectionShader);
                QMatrix4x4 windowTransformation;
                windowTransformation.translate(rect.x() + rect.width()*0.5f, 0.0, 0.0);
                m_reflectionShader->setUniform("windowTransformation", windowTransformation);
                m_reflectionShader->setUniform("u_alpha", alpha);
                QVector<float> verts;
                QVector<float> texcoords;
                verts.reserve(18);
                texcoords.reserve(12);
                texcoords << 0.0 << 0.0;
                verts << vertices[6] << vertices[7] << vertices[8];
                texcoords << 0.0 << 0.0;
                verts << vertices[9] << vertices[10] << vertices[11];
                texcoords << 1.0 << 0.0;
                verts << vertices[0] << vertices[1] << vertices[2];
                texcoords << 1.0 << 0.0;
                verts << vertices[0] << vertices[1] << vertices[2];
                texcoords << 1.0 << 0.0;
                verts << vertices[3] << vertices[4] << vertices[5];
                texcoords << 0.0 << 0.0;
                verts << vertices[6] << vertices[7] << vertices[8];
                GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
                vbo->reset();
                vbo->setData(6, 3, verts.data(), texcoords.data());
                vbo->render(GL_TRIANGLES);

                shaderManager->popShader();
            } else {
#ifndef KWIN_HAVE_OPENGLES
                glColor4f( 0.0, 0.0, 0.0, alpha );
                glPushMatrix();
                glTranslatef( rect.x() + rect.width()*0.5f, 0.0, 0.0 );
                glBegin( GL_POLYGON );
                glVertex3f( vertices[0], vertices[1], vertices[2] );
                glVertex3f( vertices[3], vertices[4], vertices[5] );
                // rearground
                alpha = -1.0;
                glColor4f( 0.0, 0.0, 0.0, alpha );
                glVertex3f( vertices[6], vertices[7], vertices[8] );
                glVertex3f( vertices[9], vertices[10], vertices[11] );
                glEnd();
                glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
508
#endif
509
            }
510
            glDisable( GL_BLEND );
511
            PaintClipper::pop( QRegion( rect ));
Martin Flöser's avatar
Martin Flöser committed
512
            }
513
514
        glEnable( GL_CULL_FACE );
        // caps
Martin Flöser's avatar
Martin Flöser committed
515
        paintCap(false, -point-zTranslate);
516
517

        // cube
Martin Flöser's avatar
Martin Flöser committed
518
        glCullFace( GL_FRONT );
519
520
521
522
523
524
525
526
527
528
529
530
        if( mode == Cylinder )
            {
            cylinderShader->bind();
            cylinderShader->setUniform( "front", -1.0f );
            cylinderShader->unbind();
            }
        if( mode == Sphere )
            {
            sphereShader->bind();
            sphereShader->setUniform( "front", -1.0f );
            sphereShader->unbind();
            }
531
532
533
        pushMatrix(m_rotationMatrix);
        paintCube(mask, region, data);
        popMatrix();
534
535
536


        // call the inside cube effects
537
#ifndef KWIN_HAVE_OPENGLES
538
539
        foreach( CubeInsideEffect* inside, m_cubeInsideEffects )
            {
540
            pushMatrix(m_rotationMatrix);
541
542
543
            glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
            glRotatef( (1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0 );
            inside->paint();
544
            popMatrix();
545
            }
546
#endif
547

Martin Flöser's avatar
Martin Flöser committed
548
        glCullFace( GL_BACK );
549
550
551
552
553
554
555
556
557
558
559
560
        if( mode == Cylinder )
            {
            cylinderShader->bind();
            cylinderShader->setUniform( "front", 1.0f );
            cylinderShader->unbind();
            }
        if( mode == Sphere )
            {
            sphereShader->bind();
            sphereShader->setUniform( "front", 1.0f );
            sphereShader->unbind();
            }
561
562
563
        pushMatrix(m_rotationMatrix);
        paintCube(mask, region, data);
        popMatrix();
564

565
        // cap
Martin Flöser's avatar
Martin Flöser committed
566
        paintCap(true, -point-zTranslate);
567
568
        glDisable( GL_CULL_FACE );

Martin Flöser's avatar
Martin Flöser committed
569
        glDisable( GL_BLEND );
Martin Flöser's avatar
Martin Flöser committed
570
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
571
        glPopAttrib();
Martin Flöser's avatar
Martin Flöser committed
572
#endif
Martin Flöser's avatar
Martin Flöser committed
573
574

        // desktop name box - inspired from coverswitch
575
        if( displayDesktopName )
Martin Flöser's avatar
Martin Flöser committed
576
            {
577
            double opacity = 1.0;
Martin Flöser's avatar
Martin Flöser committed
578
            if( start )
579
                opacity = timeLine.value();
Martin Flöser's avatar
Martin Flöser committed
580
            if( stop )
581
                opacity = 1.0 - timeLine.value();
582
583
584
            QRect screenRect = effects->clientArea( ScreenArea, activeScreen, frontDesktop );
            QRect frameRect = QRect( screenRect.width() * 0.33f + screenRect.x(), screenRect.height() * 0.95f + screenRect.y(),
                screenRect.width() * 0.34f, QFontMetrics( desktopNameFont ).height() );
585
586
587
            desktopNameFrame->setGeometry( frameRect );
            desktopNameFrame->setText( effects->desktopName( frontDesktop ) );
            desktopNameFrame->render( region, opacity );
Martin Flöser's avatar
Martin Flöser committed
588
589
590
591
592
593
594
595
            }
        }
    else
        {
        effects->paintScreen( mask, region, data );
        }
    }

596
void CubeEffect::rotateCube()
Martin Flöser's avatar
Martin Flöser committed
597
    {
598
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
599

Martin Flöser's avatar
Martin Flöser committed
600
    m_rotationMatrix.setToIdentity();
Martin Flöser's avatar
Martin Flöser committed
601
    float internalCubeAngle = 360.0f / effects->numberOfDesktops();
602
    float zTranslate = zPosition + zoom;
603
604
605
606
    if( start )
        zTranslate *= timeLine.value();
    if( stop )
        zTranslate *= ( 1.0 - timeLine.value() );
Martin Flöser's avatar
Martin Flöser committed
607
608
    // Rotation of the cube
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
609
    float point = rect.width()/2*tan(cubeAngle*0.5f*M_PI/180.0f);
Martin Flöser's avatar
Martin Flöser committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    if( verticalRotating || verticalPosition != Normal || manualVerticalAngle != 0.0 )
        {
        // change the verticalPosition if manualVerticalAngle > 90 or < -90 degrees
        if( manualVerticalAngle <= -90.0 )
            {
            manualVerticalAngle += 90.0;
            if( verticalPosition == Normal )
                verticalPosition = Down;
            if( verticalPosition == Up )
                verticalPosition = Normal;
            }
        if( manualVerticalAngle >= 90.0 )
            {
            manualVerticalAngle -= 90.0;
            if( verticalPosition == Normal )
                verticalPosition = Up;
            if( verticalPosition == Down )
                verticalPosition = Normal;
            }
        float angle = 0.0;
        if( verticalPosition == Up )
            {
            angle = 90.0;
            if( !verticalRotating)
                {
                if( manualVerticalAngle < 0.0 )
                    angle += manualVerticalAngle;
                else
                    manualVerticalAngle = 0.0;
                }
            }
        else if( verticalPosition == Down )
            {
            angle = -90.0;
            if( !verticalRotating)
                {
                if( manualVerticalAngle > 0.0 )
                    angle += manualVerticalAngle;
                else
                    manualVerticalAngle = 0.0;
                }
            }
        else
            {
            angle = manualVerticalAngle;
            }
        if( verticalRotating )
            {
            angle *= verticalTimeLine.value();
            if( verticalPosition == Normal && verticalRotationDirection == Upwards )
                angle = -90.0 + 90*verticalTimeLine.value();
            if( verticalPosition == Normal && verticalRotationDirection == Downwards )
                angle = 90.0 - 90*verticalTimeLine.value();
            angle += manualVerticalAngle * (1.0-verticalTimeLine.value());
            }
Martin Flöser's avatar
Martin Flöser committed
665
666
        if( stop )
            angle *= (1.0 - timeLine.value());
Martin Flöser's avatar
Martin Flöser committed
667
668
669
        m_rotationMatrix.translate(rect.width()/2, rect.height()/2, -point-zTranslate);
        m_rotationMatrix.rotate(angle, 1.0, 0.0, 0.0);
        m_rotationMatrix.translate(-rect.width()/2, -rect.height()/2, point+zTranslate);
Martin Flöser's avatar
Martin Flöser committed
670
        }
Martin Flöser's avatar
Martin Flöser committed
671
    if( rotating || (manualAngle != 0.0) )
Martin Flöser's avatar
Martin Flöser committed
672
        {
673
        int tempFrontDesktop = frontDesktop;
Martin Flöser's avatar
Martin Flöser committed
674
675
676
        if( manualAngle > internalCubeAngle * 0.5f )
            {
            manualAngle -= internalCubeAngle;
677
678
679
            tempFrontDesktop--;
            if( tempFrontDesktop == 0 )
                tempFrontDesktop = effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
680
681
682
683
            }
        if( manualAngle < -internalCubeAngle * 0.5f )
            {
            manualAngle += internalCubeAngle;
684
685
686
            tempFrontDesktop++;
            if( tempFrontDesktop > effects->numberOfDesktops() )
                tempFrontDesktop = 1;
Martin Flöser's avatar
Martin Flöser committed
687
688
689
690
691
692
693
694
695
696
            }
        float rotationAngle = internalCubeAngle * timeLine.value();
        if( rotationAngle > internalCubeAngle * 0.5f )
            {
            rotationAngle -= internalCubeAngle;
            if( !desktopChangedWhileRotating )
                {
                desktopChangedWhileRotating = true;
                if( rotationDirection == Left )
                    {
697
                    tempFrontDesktop++;
Martin Flöser's avatar
Martin Flöser committed
698
699
700
                    }
                else if( rotationDirection == Right )
                    {
701
                    tempFrontDesktop--;
Martin Flöser's avatar
Martin Flöser committed
702
                    }
703
704
705
706
                if( tempFrontDesktop > effects->numberOfDesktops() )
                    tempFrontDesktop = 1;
                else if( tempFrontDesktop == 0 )
                    tempFrontDesktop = effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
707
708
                }
            }
709
710
711
        // don't change front desktop during stop animation as this would break some logic
        if( !stop )
            frontDesktop = tempFrontDesktop;
Martin Flöser's avatar
Martin Flöser committed
712
713
714
715
        if( rotationDirection == Left )
            {
            rotationAngle *= -1;
            }
Martin Flöser's avatar
Martin Flöser committed
716
717
718
719
        if( stop )
            rotationAngle = manualAngle * (1.0 - timeLine.value());
        else
            rotationAngle += manualAngle * (1.0 - timeLine.value());
Martin Flöser's avatar
Martin Flöser committed
720
721
722
        m_rotationMatrix.translate(rect.width()/2, rect.height()/2, -point-zTranslate);
        m_rotationMatrix.rotate(rotationAngle, 0.0, 1.0, 0.0);
        m_rotationMatrix.translate(-rect.width()/2, -rect.height()/2, point+zTranslate);
Martin Flöser's avatar
Martin Flöser committed
723
        }
724
    }
Martin Flöser's avatar
Martin Flöser committed
725

726
727
void CubeEffect::paintCube( int mask, QRegion region, ScreenPaintData& data )
    {
728
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
729
730
731
732
733
734
735
736
737
738
739
    float internalCubeAngle = 360.0f / effects->numberOfDesktops();
    cube_painting = true;
    float zTranslate = zPosition + zoom;
    if( start )
        zTranslate *= timeLine.value();
    if( stop )
        zTranslate *= ( 1.0 - timeLine.value() );

    // Rotation of the cube
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
    float point = rect.width()/2*tan(cubeAngle*0.5f*M_PI/180.0f);
Martin Flöser's avatar
Martin Flöser committed
740
741
742
743

    for( int i=0; i<effects->numberOfDesktops(); i++ )
        {
        // start painting the cube
744
        painting_desktop = (i + frontDesktop )%effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
745
746
747
748
        if( painting_desktop == 0 )
            {
            painting_desktop = effects->numberOfDesktops();
            }
749
750
751
        ScreenPaintData newData = data;
        RotationData rot = RotationData();
        rot.axis = RotationData::YAxis;
752
        rot.angle = internalCubeAngle * i;
753
754
755
756
757
        rot.xRotationPoint = rect.width()/2;
        rot.zRotationPoint = -point;
        newData.rotation = &rot;
        newData.zTranslate = -zTranslate;
        effects->paintScreen( mask, region, newData );
Martin Flöser's avatar
Martin Flöser committed
758
759
760
761
762
        }
    cube_painting = false;
    painting_desktop = effects->currentDesktop();
    }

Martin Flöser's avatar
Martin Flöser committed
763
void CubeEffect::paintCap(bool frontFirst, float zOffset)
Martin Flöser's avatar
Martin Flöser committed
764
765
766
    {
    if( ( !paintCaps ) || effects->numberOfDesktops() <= 2 )
        return;
Martin Flöser's avatar
Martin Flöser committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    GLenum firstCull = frontFirst ? GL_FRONT : GL_BACK;
    GLenum secondCull = frontFirst ? GL_BACK : GL_FRONT;
    const QRect rect = effects->clientArea(FullArea, activeScreen, effects->currentDesktop());

    // create the VBO if not yet created
    if (!m_cubeCapBuffer) {
        switch (mode) {
        case Cube:
            paintCubeCap();
            break;
        case Cylinder:
            paintCylinderCap();
            break;
        case Sphere:
            paintSphereCap();
            break;
        default:
            // impossible
            break;
        }
    }

    QMatrix4x4 capMatrix;
    capMatrix.translate(rect.width()/2, 0.0, zOffset);
    capMatrix.rotate((1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0);
    capMatrix.translate(0.0, rect.height(), 0.0);
    if (mode == Sphere) {
        capMatrix.scale(1.0, -1.0, 1.0);
    }

    bool capShader = false;
    if (ShaderManager::instance()->isValid() && m_capShader->isValid()) {
        capShader = true;
        ShaderManager::instance()->pushShader(m_capShader);
801
802
803
804
805
806
807
        float opacity = cubeOpacity;
        if (start) {
            opacity *= timeLine.value();
        } else if (stop) {
            opacity *= (1.0 - timeLine.value());
        }
        m_capShader->setUniform("u_opacity", opacity);
808
        m_capShader->setUniform("u_mirror", 1);
Martin Flöser's avatar
Martin Flöser committed
809
810
811
812
813
814
815
816
817
818
        if (reflectionPainting) {
            m_capShader->setUniform("screenTransformation", m_reflectionMatrix*m_rotationMatrix);
        } else {
            m_capShader->setUniform("screenTransformation", m_rotationMatrix);
        }
        m_capShader->setUniform("windowTransformation", capMatrix);
        if (texturedCaps && effects->numberOfDesktops() > 3 && capTexture) {
            capTexture->bind();
        }
    } else {
819
        pushMatrix(m_rotationMatrix*capMatrix);
Martin Flöser's avatar
Martin Flöser committed
820

821
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
822
        glMatrixMode( GL_TEXTURE );
823
824
#endif
        pushMatrix();
825
        loadMatrix(m_textureMirrorMatrix);
826
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
827
        glMatrixMode( GL_MODELVIEW );
828

829
        glColor4f( capColor.redF(), capColor.greenF(), capColor.blueF(), cubeOpacity );
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
        if( texturedCaps && effects->numberOfDesktops() > 3 && capTexture )
            {
            // modulate the cap texture: cap color should be background for translucent pixels
            // cube opacity should be used for all pixels
            // blend with cap color
            float color[4] = { capColor.redF(), capColor.greenF(), capColor.blueF(), cubeOpacity };
            glActiveTexture( GL_TEXTURE0 );
            capTexture->bind();
            glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
            glColor4fv( color );

            // set Opacity to cube opacity
            // TODO: change opacity during start/stop animation
            glActiveTexture( GL_TEXTURE1 );
            capTexture->bind();
            glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
            glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE );
            glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS );
            glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE );
            glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_CONSTANT );
            glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color );
            glActiveTexture( GL_TEXTURE0 );
            glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color );
            }
Martin Flöser's avatar
Martin Flöser committed
854
855
856
857
858
859
860
861
862
863
864
865
866
#endif
    }

    glEnable(GL_BLEND);
    glCullFace(firstCull);
    m_cubeCapBuffer->render(GL_TRIANGLES);

    if (mode == Sphere) {
        capMatrix.scale(1.0, -1.0, 1.0);
    }
    capMatrix.translate(0.0, -rect.height(), 0.0);
    if (capShader) {
        m_capShader->setUniform("windowTransformation", capMatrix);
867
        m_capShader->setUniform("u_mirror", 0);
Martin Flöser's avatar
Martin Flöser committed
868
869
870
    } else {
#ifndef KWIN_HAVE_OPENGLES
        glMatrixMode( GL_TEXTURE );
871
        popMatrix();
Martin Flöser's avatar
Martin Flöser committed
872
873
        glMatrixMode( GL_MODELVIEW );
#endif
874
875
        popMatrix();
        pushMatrix(m_rotationMatrix*capMatrix);
Martin Flöser's avatar
Martin Flöser committed
876
877
878
879
880
881
882
883
884
885
886
    }
    glCullFace(secondCull);
    m_cubeCapBuffer->render(GL_TRIANGLES);
    glDisable(GL_BLEND);

    if (capShader) {
        ShaderManager::instance()->popShader();
        if (texturedCaps && effects->numberOfDesktops() > 3 && capTexture) {
            capTexture->unbind();
        }
    } else {
887
        popMatrix();
888
889
        if( texturedCaps && effects->numberOfDesktops() > 3 && capTexture )
            {
890
#ifndef KWIN_HAVE_OPENGLES
891
892
893
894
895
896
            glActiveTexture( GL_TEXTURE1 );
            glDisable( capTexture->target() );
            glActiveTexture( GL_TEXTURE0 );
            glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
            glColor4f( 0.0f, 0.0f, 0.0f, 0.0f );
            capTexture->unbind();
Martin Flöser's avatar
Martin Flöser committed
897
#endif
898
            }
899
    }
Martin Flöser's avatar
Martin Flöser committed
900
    }
901

902
void CubeEffect::paintCubeCap()
903
    {
904
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
905
906
907
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
    float z = rect.width()/2*tan(cubeAngle*0.5f*M_PI/180.0f);
    float zTexture = rect.width()/2*tan(45.0f*M_PI/180.0f);
908
    float angle = 360.0f/effects->numberOfDesktops();
909
    bool texture = texturedCaps && effects->numberOfDesktops() > 3 && capTexture;
Martin Flöser's avatar
Martin Flöser committed
910
911
    QVector<float> verts;
    QVector<float> texCoords;
Martin Flöser's avatar
Martin Flöser committed
912
913
    for( int i=0; i<effects->numberOfDesktops(); i++ )
        {
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
        int triangleRows = effects->numberOfDesktops()*5;
        float zTriangleDistance = z/(float)triangleRows;
        float widthTriangle = tan( angle*0.5 * M_PI/180.0 ) * zTriangleDistance;
        float currentWidth = 0.0;
        float cosValue = cos( i*angle  * M_PI/180.0 );
        float sinValue = sin( i*angle  * M_PI/180.0 );
        for( int j=0; j<triangleRows; j++ )
            {
            float previousWidth = currentWidth;
            currentWidth = tan( angle*0.5 * M_PI/180.0 ) * zTriangleDistance * (j+1);
            int evenTriangles = 0;
            int oddTriangles = 0;
            for( int k=0; k<floor(currentWidth/widthTriangle*2-1+0.5f); k++ )
                {
                float x1 = -previousWidth;
                float x2 = -currentWidth;
                float x3 = 0.0;
                float z1 = 0.0;
                float z2 = 0.0;
                float z3 = 0.0;
                if( k%2 == 0 )
                    {
                    x1 += evenTriangles*widthTriangle*2;
                    x2 += evenTriangles*widthTriangle*2;
                    x3 = x2+widthTriangle*2;
                    z1 = j*zTriangleDistance;
                    z2 = (j+1)*zTriangleDistance;
                    z3 = (j+1)*zTriangleDistance;
                    float xRot = cosValue * x1 - sinValue * z1;
                    float zRot = sinValue * x1 + cosValue * z1;
                    x1 = xRot;
                    z1 = zRot;
                    xRot = cosValue * x2 - sinValue * z2;
                    zRot = sinValue * x2 + cosValue * z2;
                    x2 = xRot;
                    z2 = zRot;
                    xRot = cosValue * x3 - sinValue * z3;
                    zRot = sinValue * x3 + cosValue * z3;
                    x3 = xRot;
                    z3 = zRot;
                    evenTriangles++;
                    }
                else
                    {
                    x1 += oddTriangles*widthTriangle*2;
                    x2 += (oddTriangles+1)*widthTriangle*2;
                    x3 = x1+widthTriangle*2;
                    z1 = j*zTriangleDistance;
                    z2 = (j+1)*zTriangleDistance;
                    z3 = j*zTriangleDistance;
                    float xRot = cosValue * x1 - sinValue * z1;
                    float zRot = sinValue * x1 + cosValue * z1;
                    x1 = xRot;
                    z1 = zRot;
                    xRot = cosValue * x2 - sinValue * z2;
                    zRot = sinValue * x2 + cosValue * z2;
                    x2 = xRot;
                    z2 = zRot;
                    xRot = cosValue * x3 - sinValue * z3;
                    zRot = sinValue * x3 + cosValue * z3;
                    x3 = xRot;
                    z3 = zRot;
                    oddTriangles++;
                    }
                float texX1 = 0.0;
                float texX2 = 0.0;
                float texX3 = 0.0;
                float texY1 = 0.0;
                float texY2 = 0.0;
                float texY3 = 0.0;
                if( texture )
                    {
                    texX1 = x1/(rect.width())+0.5;
                    texY1 = 0.5 - z1/zTexture * 0.5;
                    texX2 = x2/(rect.width())+0.5;
                    texY2 = 0.5 - z2/zTexture * 0.5;
                    texX3 = x3/(rect.width())+0.5;
                    texY3 = 0.5 - z3/zTexture * 0.5;
Martin Flöser's avatar
Martin Flöser committed
992
                    texCoords << texX1 << texY1;
993
                    }
Martin Flöser's avatar
Martin Flöser committed
994
                verts << x1 << 0.0 << z1;
995
996
                if( texture )
                    {
Martin Flöser's avatar
Martin Flöser committed
997
                    texCoords << texX2 << texY2;
998
                    }
Martin Flöser's avatar
Martin Flöser committed
999
                verts << x2 << 0.0 << z2;
1000
1001
                if( texture )
                    {
Martin Flöser's avatar
Martin Flöser committed
1002
                    texCoords << texX3 << texY3;
1003
                    }
Martin Flöser's avatar
Martin Flöser committed
1004
                verts << x3 << 0.0 << z3;
1005
1006
                }
            }
Martin Flöser's avatar
Martin Flöser committed
1007
        }
Martin Flöser's avatar
Martin Flöser committed
1008
1009
1010
    delete m_cubeCapBuffer;
    m_cubeCapBuffer = new GLVertexBuffer(GLVertexBuffer::Static);
    m_cubeCapBuffer->setData(verts.count()/3, 3, verts.constData(), texture ? texCoords.constData() : NULL);
Martin Flöser's avatar
Martin Flöser committed
1011
1012
    }

1013
1014
void CubeEffect::paintCylinderCap()
    {
Martin Flöser's avatar
Martin Flöser committed
1015
#ifndef KWIN_HAVE_OPENGLES
1016
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
1017
1018
1019
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);

    float radian = (cubeAngle*0.5)*M_PI/180;
1020
1021
    float radius = (rect.width()*0.5)*tan(radian);
    float segment = radius/30.0f;
1022

1023
1024
    bool texture = texturedCaps && effects->numberOfDesktops() > 3 && capTexture;
    for( int i=1; i<=30; i++ )
1025
1026
        {
        glBegin( GL_TRIANGLE_STRIP );
1027
1028
        int steps =  72;
        for( int j=0; j<=steps; j++ )
1029
            {
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
            float azimuthAngle = (j*(360.0f/steps))*M_PI/180.0f;
            float x1 = segment*(i-1) * sin( azimuthAngle );
            float x2 = segment*i * sin( azimuthAngle );
            float z1 = segment*(i-1) * cos( azimuthAngle );
            float z2 = segment*i * cos( azimuthAngle );
            if( texture )
                glTexCoord2f( (radius+x1)/(radius*2.0f), 1.0f - (z1+radius)/(radius*2.0f) );
            glVertex3f( x1, 0.0, z1 );
            if( texture )
                glTexCoord2f( (radius+x2)/(radius*2.0f), 1.0f - (z2+radius)/(radius*2.0f) );
            glVertex3f( x2, 0.0, z2 );
1041
1042
1043
            }
        glEnd();
        }
Martin Flöser's avatar
Martin Flöser committed
1044
#endif
1045
1046
1047
1048
    }

void CubeEffect::paintSphereCap()
    {
Martin Flöser's avatar
Martin Flöser committed
1049
#ifndef KWIN_HAVE_OPENGLES
1050
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
    float zTexture = rect.width()/2*tan(45.0f*M_PI/180.0f);
    float radius = (rect.width()*0.5)/cos(cubeAngle*0.5*M_PI/180.0);
    float angle = acos( (rect.height()*0.5)/radius )*180.0/M_PI;
    angle /= 30;
    bool texture = texturedCaps && effects->numberOfDesktops() > 3 && capTexture;
    glPushMatrix();
    glTranslatef( 0.0, -rect.height()*0.5, 0.0 );
    glBegin( GL_QUADS );
    for( int i=0; i<30; i++ )
        {
        float topAngle = angle*i*M_PI/180.0;
        float bottomAngle = angle*(i+1)*M_PI/180.0;
        float yTop = rect.height() - radius * cos( topAngle );
        yTop -= (yTop-rect.height()*0.5)*capDeformationFactor;
        float yBottom = rect.height() -radius * cos( bottomAngle );
        yBottom -= (yBottom-rect.height()*0.5)*capDeformationFactor;
        for( int j=0; j<36; j++ )
            {
            float x = radius * sin( topAngle ) * sin( (90.0+j*10.0)*M_PI/180.0 );
            float z = radius * sin( topAngle ) * cos( (90.0+j*10.0)*M_PI/180.0 );
            if( texture )
                glTexCoord2f( x/(rect.width())+0.5, 0.5 - z/zTexture * 0.5 );
            glVertex3f( x, yTop, z );
            x = radius * sin( bottomAngle ) * sin( (90.0+j*10.0)*M_PI/180.00 );
            z = radius * sin( bottomAngle ) * cos( (90.0+j*10.0)*M_PI/180.0 );
            if( texture )
                glTexCoord2f( x/(rect.width())+0.5, 0.5 - z/zTexture * 0.5 );
            glVertex3f( x, yBottom, z );
            x = radius * sin( bottomAngle ) * sin( (90.0+(j+1)*10.0)*M_PI/180.0 );
            z = radius * sin( bottomAngle ) * cos( (90.0+(j+1)*10.0)*M_PI/180.0 );
            if( texture )
                glTexCoord2f( x/(rect.width())+0.5, 0.5 - z/zTexture * 0.5 );
            glVertex3f( x, yBottom, z );
            x = radius * sin( topAngle ) * sin( (90.0+(j+1)*10.0)*M_PI/180.0 );
            z = radius * sin( topAngle ) * cos( (90.0+(j+1)*10.0)*M_PI/180.0 );
            if( texture )
                glTexCoord2f( x/(rect.width())+0.5, 0.5 - z/zTexture * 0.5 );
            glVertex3f( x, yTop, z );
            }
        }
    glEnd();
    glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
1094
#endif
1095
1096
    }

Martin Flöser's avatar
Martin Flöser committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
void CubeEffect::postPaintScreen()
    {
    effects->postPaintScreen();
    if( activated )
        {
        if( start )
            {
            if( timeLine.value() == 1.0 )
                {
                start = false;
                timeLine.setProgress(0.0);
Martin Flöser's avatar
Martin Flöser committed
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
                // more rotations?
                if( !rotations.empty() )
                    {
                    rotationDirection = rotations.dequeue();
                    rotating = true;
                    // change the curve shape if current shape is not easeInOut
                    if( currentShape != TimeLine::EaseInOutCurve )
                        {
                        // more rotations follow -> linear curve
                        if( !rotations.empty() )
                            {
                            currentShape = TimeLine::LinearCurve;
                            }
                        // last rotation step -> easeOut curve
                        else
                            {
                            currentShape = TimeLine::EaseOutCurve;
                            }
                        timeLine.setCurveShape( currentShape );
                        }
                    else
                        {
                        // if there is at least one more rotation, we can change to easeIn
                        if( !rotations.empty() )
                            {
                            currentShape = TimeLine::EaseInCurve;
                            timeLine.setCurveShape( currentShape );
                            }
                        }
                    }
Martin Flöser's avatar
Martin Flöser committed
1138
1139
                }
            effects->addRepaintFull();
Martin Flöser's avatar
Martin Flöser committed
1140
            return; // schedule_close could have been called, start has to finish first
Martin Flöser's avatar
Martin Flöser committed
1141
1142
1143
1144
1145
            }
        if( stop )
            {
            if( timeLine.value() == 1.0 )
                {
1146
                effects->setCurrentDesktop( frontDesktop );
Martin Flöser's avatar
Martin Flöser committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
                stop = false;
                timeLine.setProgress(0.0);
                activated = false;
                // set the new desktop
                if( keyboard_grab )
                    effects->ungrabKeyboard();
                keyboard_grab = false;
                effects->destroyInputWindow( input );

                effects->setActiveFullScreenEffect( 0 );
1157

Martin Flöser's avatar
Martin Flöser committed
1158
1159
                delete m_cubeCapBuffer;
                m_cubeCapBuffer = NULL;
1160
                desktopNameFrame->free();
Martin Flöser's avatar
Martin Flöser committed
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
                }
            effects->addRepaintFull();
            }
        if( rotating || verticalRotating )
            {
            if( rotating && timeLine.value() == 1.0 )
                {
                timeLine.setProgress(0.0);
                rotating = false;
                desktopChangedWhileRotating = false;
                manualAngle = 0.0;
                // more rotations?
                if( !rotations.empty() )
                    {
                    rotationDirection = rotations.dequeue();
                    rotating = true;
                    // change the curve shape if current shape is not easeInOut
                    if( currentShape != TimeLine::EaseInOutCurve )
                        {
                        // more rotations follow -> linear curve
                        if( !rotations.empty() )
                            {
                            currentShape = TimeLine::LinearCurve;
                            }
                        // last rotation step -> easeOut curve
                        else
                            {
                            currentShape = TimeLine::EaseOutCurve;
                            }
                        timeLine.setCurveShape( currentShape );
                        }
                    else
                        {
                        // if there is at least one more rotation, we can change to easeIn
                        if( !rotations.empty() )
                            {
                            currentShape = TimeLine::EaseInCurve;
                            timeLine.setCurveShape( currentShape );
                            }
                        }
                    }
                else
                    {
                    // reset curve shape if there are no more rotations
                    if( currentShape != TimeLine::EaseInOutCurve )
                        {
                        currentShape = TimeLine::EaseInOutCurve;
                        timeLine.setCurveShape( currentShape );
                        }
                    }
                }
            if( verticalRotating && verticalTimeLine.value() == 1.0 )
                {
                verticalTimeLine.setProgress(0.0);
                verticalRotating = false;
                manualVerticalAngle = 0.0;
                // more rotations?
                if( !verticalRotations.empty() )
                    {
                    verticalRotationDirection = verticalRotations.dequeue();
                    verticalRotating = true;
                    if( verticalRotationDirection == Upwards )
                        {
                        if( verticalPosition == Normal )
                            verticalPosition = Up;
                        if( verticalPosition == Down )
                            verticalPosition = Normal;
                        }
                    if( verticalRotationDirection == Downwards )
                        {
                        if( verticalPosition == Normal )
                            verticalPosition = Down;
                        if( verticalPosition == Up )
                            verticalPosition = Normal;
                        }
                    }
                }
            effects->addRepaintFull();
            return; // rotation has to end before cube is closed
            }
        if( schedule_close )
            {
            schedule_close = false;
1244
            stop = true;
Martin Flöser's avatar
Martin Flöser committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
            effects->addRepaintFull();
            }
        }
    }

void CubeEffect::prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time )
    {
    if( activated )
        {
        if( cube_painting )
            {
1256
            if( mode == Cylinder || mode == Sphere )
1257