cube.cpp 89.6 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
    , capListCreated( false )
92
93
    , recompileList( true )
    , glList( 0 )
94
    , m_cubeCapBuffer( NULL )
95
    , m_proxy( this )
Martin Flöser's avatar
Martin Flöser committed
96
    {
97
98
    desktopNameFont.setBold( true );
    desktopNameFont.setPointSize( 14 );
99
    desktopNameFrame->setFont( desktopNameFont );
100

101
102
    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
103
104
    const QString capshader = KGlobal::dirs()->findResource("data", "kwin/cube-cap.glsl");
    m_capShader = ShaderManager::instance()->loadFragmentShader(ShaderManager::GenericShader, capshader);
105

106
    reconfigure( ReconfigureAll );
107
108
    }

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

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

119
120
121
void CubeEffect::loadConfig( QString config )
    {
    KConfigGroup conf = effects->effectConfig( config );
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
160
    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
161
162

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

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

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

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

    // 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
247
248
249
250
    }

CubeEffect::~CubeEffect()
    {
251
252
253
254
255
256
257
258
259
260
261
262
    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
263
    delete wallpaper;
264
    delete capTexture;
265
266
    delete cylinderShader;
    delete sphereShader;
267
    delete desktopNameFrame;
268
    delete m_reflectionShader;
Martin Flöser's avatar
Martin Flöser committed
269
270
    delete m_capShader;
    delete m_cubeCapBuffer;
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
296
    }

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 );
297
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
298
299
300
301
302
303
304
305
306
307
308
309
310
        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 );
311
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
312
313
314
315
316
        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
317
318
319
320
321
322
323
324
325
326
327
    }

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 );
328
            recompileList = true;
Martin Flöser's avatar
Martin Flöser committed
329
330
331
332
            }
        if( verticalRotating )
            {
            verticalTimeLine.addTime( time );
333
            recompileList = true;
Martin Flöser's avatar
Martin Flöser committed
334
335
336
337
338
339
340
341
342
            }
        }
    effects->prePaintScreen( data, time );
    }

void CubeEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
    {
    if( activated )
        {
343
344
345
        if( recompileList )
            {
            recompileList = false;
Martin Flöser's avatar
Martin Flöser committed
346
347
348
#ifdef KWIN_HAVE_OPENGLES
            rotateCube();
#else
349
350
            glPushMatrix();
            glNewList( glList, GL_COMPILE );
351
            rotateCube();
352
353
            glEndList();
            glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
354
#endif
355
356
            }

357
358
359
        // compile List for cube
        if( useList )
            {
Martin Flöser's avatar
Martin Flöser committed
360
#ifndef KWIN_HAVE_OPENGLES
361
362
363
364
365
            glNewList( glList + 1, GL_COMPILE );
            glPushMatrix();
            paintCube( mask, region, data );
            glPopMatrix();
            glEndList();
Martin Flöser's avatar
Martin Flöser committed
366
#endif
367
368
            }

369
        QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
Martin Flöser's avatar
Martin Flöser committed
370
371
372
373
374
375
376
377
378

        // 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
379
380
        if( wallpaper )
            {
Martin Flöser's avatar
Martin Flöser committed
381
382
383
384
385
            wallpaper->bind();
            wallpaper->render( region, rect );
            wallpaper->unbind();
            }

Martin Flöser's avatar
Martin Flöser committed
386
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
387
        glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
Martin Flöser's avatar
Martin Flöser committed
388
#endif
Martin Flöser's avatar
Martin Flöser committed
389
390
391
        glEnable( GL_BLEND );
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

392
393
394
395
396
397
398
399
        // 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
400
        // reflection
401
        if( reflection && mode != Sphere )
Martin Flöser's avatar
Martin Flöser committed
402
            {
403
404
            // restrict painting the reflections to the current screen
            PaintClipper::push( QRegion( rect ));
Martin Flöser's avatar
Martin Flöser committed
405
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
406
            glPushMatrix();
Martin Flöser's avatar
Martin Flöser committed
407
#endif
408
409
410
            // 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
411
412
413
            m_reflectionMatrix.setToIdentity();
            m_reflectionMatrix.scale(1.0, -1.0, 1.0);
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
414
            glScalef( 1.0, -1.0, 1.0 );
Martin Flöser's avatar
Martin Flöser committed
415
#endif
416
417
418
419
420
421

            // 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
422
423
424
                {
                m_reflectionMatrix.translate(0.0, cos( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0);
#ifndef KWIN_HAVE_OPENGLES
425
                glTranslatef( 0.0, cos( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0 );
Martin Flöser's avatar
Martin Flöser committed
426
427
#endif
                }
428
            else
Martin Flöser's avatar
Martin Flöser committed
429
430
431
                {
                m_reflectionMatrix.translate(0.0, sin( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0);
#ifndef KWIN_HAVE_OPENGLES
432
                glTranslatef( 0.0, sin( fabs( manualAngle ) * M_PI / 360.0f * float( effects->numberOfDesktops() ) ) * addedHeight2 + addedHeight1 - float( rect.height() ), 0.0 );
Martin Flöser's avatar
Martin Flöser committed
433
434
#endif
                }
435

436

Martin Flöser's avatar
Martin Flöser committed
437
438
#ifndef KWIN_HAVE_OPENGLES
            // TODO: find a solution for GLES
439
            glEnable( GL_CLIP_PLANE0 );
Martin Flöser's avatar
Martin Flöser committed
440
#endif
Martin Flöser's avatar
Martin Flöser committed
441
            reflectionPainting = true;
442
            glEnable( GL_CULL_FACE );
Martin Flöser's avatar
Martin Flöser committed
443
            paintCap(true, -point-zTranslate);
444
445

            // cube
Martin Flöser's avatar
Martin Flöser committed
446
            glCullFace( GL_BACK );
447
448
449
450
451
452
            if( mode == Cylinder )
                {
                cylinderShader->bind();
                cylinderShader->setUniform( "front", 1.0f );
                cylinderShader->unbind();
                }
Martin Flöser's avatar
Martin Flöser committed
453
454
455
#ifdef KWIN_HAVE_OPENGLES
            paintCube( mask, region, data );
#else
456
457
            glPushMatrix();
            glCallList( glList );
458
459
460
461
462
463
464
465
            if( useList )
                glCallList( glList + 1 );
            else
                {
                glPushMatrix();
                paintCube( mask, region, data );
                glPopMatrix();
                }
466
            glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
467
#endif
468
469

            // call the inside cube effects
470
#ifndef KWIN_HAVE_OPENGLES
471
472
473
474
475
476
477
478
479
            foreach( CubeInsideEffect* inside, m_cubeInsideEffects )
                {
                glPushMatrix();
                glCallList( glList );
                glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
                glRotatef( (1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0 );
                inside->paint();
                glPopMatrix();
                }
480
#endif
481

Martin Flöser's avatar
Martin Flöser committed
482
            glCullFace( GL_FRONT );
483
484
485
486
487
488
            if( mode == Cylinder )
                {
                cylinderShader->bind();
                cylinderShader->setUniform( "front", -1.0f );
                cylinderShader->unbind();
                }
Martin Flöser's avatar
Martin Flöser committed
489
490
491
#ifdef KWIN_HAVE_OPENGLES
            paintCube( mask, region, data );
#else
492
493
            glPushMatrix();
            glCallList( glList );
494
495
496
497
498
499
500
501
            if( useList )
                glCallList( glList + 1 );
            else
                {
                glPushMatrix();
                paintCube( mask, region, data );
                glPopMatrix();
                }
502
            glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
503
#endif
504

Martin Flöser's avatar
Martin Flöser committed
505
            paintCap(false, -point-zTranslate);
506
            glDisable( GL_CULL_FACE );
Martin Flöser's avatar
Martin Flöser committed
507
            reflectionPainting = false;
Martin Flöser's avatar
Martin Flöser committed
508
509
#ifndef KWIN_HAVE_OPENGLES
            // TODO: find a solution for GLES
510
            glDisable( GL_CLIP_PLANE0 );
511
            glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
512
#endif
513

Martin Flöser's avatar
Martin Flöser committed
514
            float vertices[] = {
515
516
517
518
                -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
519
            // foreground
520
            float alpha = 0.7;
Martin Flöser's avatar
Martin Flöser committed
521
            if( start )
522
                alpha = 0.3 + 0.4 * timeLine.value();
Martin Flöser's avatar
Martin Flöser committed
523
            if( stop )
524
                alpha = 0.3 + 0.4 * ( 1.0 - timeLine.value() );
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
            ShaderManager *shaderManager = ShaderManager::instance();
            if (shaderManager->isValid() && m_reflectionShader->isValid()) {
                // ensure blending is enabled - no attribute stack
                glEnable( GL_BLEND );
                glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
                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();
                glDisable( GL_BLEND );
            } 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
573
#endif
574
            }
575
            PaintClipper::pop( QRegion( rect ));
Martin Flöser's avatar
Martin Flöser committed
576
            }
577
578
        glEnable( GL_CULL_FACE );
        // caps
Martin Flöser's avatar
Martin Flöser committed
579
        paintCap(false, -point-zTranslate);
580
581

        // cube
Martin Flöser's avatar
Martin Flöser committed
582
        glCullFace( GL_FRONT );
583
584
585
586
587
588
589
590
591
592
593
594
        if( mode == Cylinder )
            {
            cylinderShader->bind();
            cylinderShader->setUniform( "front", -1.0f );
            cylinderShader->unbind();
            }
        if( mode == Sphere )
            {
            sphereShader->bind();
            sphereShader->setUniform( "front", -1.0f );
            sphereShader->unbind();
            }
Martin Flöser's avatar
Martin Flöser committed
595
596
597
#ifdef KWIN_HAVE_OPENGLES
        paintCube( mask, region, data );
#else
598
        glPushMatrix();
599
        glCallList( glList );
600
601
602
603
604
605
606
607
        if( useList )
            glCallList( glList + 1 );
        else
            {
            glPushMatrix();
            paintCube( mask, region, data );
            glPopMatrix();
            }
608
        glPopMatrix();
Martin Flöser's avatar
Martin Flöser committed
609
#endif
610
611
612


        // call the inside cube effects
613
#ifndef KWIN_HAVE_OPENGLES
614
615
616
617
618
619
620
621
622
        foreach( CubeInsideEffect* inside, m_cubeInsideEffects )
            {
            glPushMatrix();
            glCallList( glList );
            glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
            glRotatef( (1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0 );
            inside->paint();
            glPopMatrix();
            }
623
#endif
624

Martin Flöser's avatar
Martin Flöser committed
625
        glCullFace( GL_BACK );
626
627
628
629
630
631
632
633
634
635
636
637
        if( mode == Cylinder )
            {
            cylinderShader->bind();
            cylinderShader->setUniform( "front", 1.0f );
            cylinderShader->unbind();
            }
        if( mode == Sphere )
            {
            sphereShader->bind();
            sphereShader->setUniform( "front", 1.0f );
            sphereShader->unbind();
            }
Martin Flöser's avatar
Martin Flöser committed
638
639
640
#ifdef KWIN_HAVE_OPENGLES
        paintCube( mask, region, data );
#else
641
642
        glPushMatrix();
        glCallList( glList );
643
644
645
646
647
648
649
650
        if( useList )
            glCallList( glList + 1 );
        else
            {
            glPushMatrix();
            paintCube( mask, region, data );
            glPopMatrix();
            }
Martin Flöser's avatar
Martin Flöser committed
651
        glPopMatrix();
652
653
        // we painted once without glList, now it's safe to paint using lists
        useList = true;
Martin Flöser's avatar
Martin Flöser committed
654
#endif
655

656
        // cap
Martin Flöser's avatar
Martin Flöser committed
657
        paintCap(true, -point-zTranslate);
658
659
        glDisable( GL_CULL_FACE );

Martin Flöser's avatar
Martin Flöser committed
660
        glDisable( GL_BLEND );
Martin Flöser's avatar
Martin Flöser committed
661
#ifndef KWIN_HAVE_OPENGLES
Martin Flöser's avatar
Martin Flöser committed
662
        glPopAttrib();
Martin Flöser's avatar
Martin Flöser committed
663
#endif
Martin Flöser's avatar
Martin Flöser committed
664
665

        // desktop name box - inspired from coverswitch
666
        if( displayDesktopName )
Martin Flöser's avatar
Martin Flöser committed
667
            {
668
            double opacity = 1.0;
Martin Flöser's avatar
Martin Flöser committed
669
            if( start )
670
                opacity = timeLine.value();
Martin Flöser's avatar
Martin Flöser committed
671
            if( stop )
672
                opacity = 1.0 - timeLine.value();
673
674
675
            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() );
676
677
678
            desktopNameFrame->setGeometry( frameRect );
            desktopNameFrame->setText( effects->desktopName( frontDesktop ) );
            desktopNameFrame->render( region, opacity );
Martin Flöser's avatar
Martin Flöser committed
679
680
681
682
683
684
685
686
            }
        }
    else
        {
        effects->paintScreen( mask, region, data );
        }
    }

687
void CubeEffect::rotateCube()
Martin Flöser's avatar
Martin Flöser committed
688
    {
689
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
690

Martin Flöser's avatar
Martin Flöser committed
691
    m_rotationMatrix.setToIdentity();
Martin Flöser's avatar
Martin Flöser committed
692
    float internalCubeAngle = 360.0f / effects->numberOfDesktops();
693
    float zTranslate = zPosition + zoom;
694
695
696
697
    if( start )
        zTranslate *= timeLine.value();
    if( stop )
        zTranslate *= ( 1.0 - timeLine.value() );
Martin Flöser's avatar
Martin Flöser committed
698
699
    // Rotation of the cube
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);
700
    float point = rect.width()/2*tan(cubeAngle*0.5f*M_PI/180.0f);
Martin Flöser's avatar
Martin Flöser committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
    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
756
757
        if( stop )
            angle *= (1.0 - timeLine.value());
Martin Flöser's avatar
Martin Flöser committed
758
759
760
761
        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);
#ifndef KWIN_HAVE_OPENGLES
762
        glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
Martin Flöser's avatar
Martin Flöser committed
763
        glRotatef( angle, 1.0, 0.0, 0.0 );
764
        glTranslatef( -rect.width()/2, -rect.height()/2, point+zTranslate );
Martin Flöser's avatar
Martin Flöser committed
765
#endif
Martin Flöser's avatar
Martin Flöser committed
766
        }
Martin Flöser's avatar
Martin Flöser committed
767
    if( rotating || (manualAngle != 0.0) )
Martin Flöser's avatar
Martin Flöser committed
768
        {
769
        int tempFrontDesktop = frontDesktop;
Martin Flöser's avatar
Martin Flöser committed
770
771
772
        if( manualAngle > internalCubeAngle * 0.5f )
            {
            manualAngle -= internalCubeAngle;
773
774
775
            tempFrontDesktop--;
            if( tempFrontDesktop == 0 )
                tempFrontDesktop = effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
776
777
778
779
            }
        if( manualAngle < -internalCubeAngle * 0.5f )
            {
            manualAngle += internalCubeAngle;
780
781
782
            tempFrontDesktop++;
            if( tempFrontDesktop > effects->numberOfDesktops() )
                tempFrontDesktop = 1;
Martin Flöser's avatar
Martin Flöser committed
783
784
785
786
787
788
789
790
791
792
            }
        float rotationAngle = internalCubeAngle * timeLine.value();
        if( rotationAngle > internalCubeAngle * 0.5f )
            {
            rotationAngle -= internalCubeAngle;
            if( !desktopChangedWhileRotating )
                {
                desktopChangedWhileRotating = true;
                if( rotationDirection == Left )
                    {
793
                    tempFrontDesktop++;
Martin Flöser's avatar
Martin Flöser committed
794
795
796
                    }
                else if( rotationDirection == Right )
                    {
797
                    tempFrontDesktop--;
Martin Flöser's avatar
Martin Flöser committed
798
                    }
799
800
801
802
                if( tempFrontDesktop > effects->numberOfDesktops() )
                    tempFrontDesktop = 1;
                else if( tempFrontDesktop == 0 )
                    tempFrontDesktop = effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
803
804
                }
            }
805
806
807
        // 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
808
809
810
811
        if( rotationDirection == Left )
            {
            rotationAngle *= -1;
            }
Martin Flöser's avatar
Martin Flöser committed
812
813
814
815
        if( stop )
            rotationAngle = manualAngle * (1.0 - timeLine.value());
        else
            rotationAngle += manualAngle * (1.0 - timeLine.value());
Martin Flöser's avatar
Martin Flöser committed
816
817
818
819
        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);
#ifndef KWIN_HAVE_OPENGLES
820
        glTranslatef( rect.width()/2, rect.height()/2, -point-zTranslate );
Martin Flöser's avatar
Martin Flöser committed
821
        glRotatef( rotationAngle, 0.0, 1.0, 0.0 );
822
        glTranslatef( -rect.width()/2, -rect.height()/2, point+zTranslate );
Martin Flöser's avatar
Martin Flöser committed
823
#endif
Martin Flöser's avatar
Martin Flöser committed
824
        }
825
    }
Martin Flöser's avatar
Martin Flöser committed
826

827
828
void CubeEffect::paintCube( int mask, QRegion region, ScreenPaintData& data )
    {
829
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
830
831
832
833
834
835
836
837
838
839
840
    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
841
842
843
844

    for( int i=0; i<effects->numberOfDesktops(); i++ )
        {
        // start painting the cube
845
        painting_desktop = (i + frontDesktop )%effects->numberOfDesktops();
Martin Flöser's avatar
Martin Flöser committed
846
847
848
849
        if( painting_desktop == 0 )
            {
            painting_desktop = effects->numberOfDesktops();
            }
850
851
852
        ScreenPaintData newData = data;
        RotationData rot = RotationData();
        rot.axis = RotationData::YAxis;
853
        rot.angle = internalCubeAngle * i;
854
855
856
857
858
        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
859
860
861
862
863
        }
    cube_painting = false;
    painting_desktop = effects->currentDesktop();
    }

Martin Flöser's avatar
Martin Flöser committed
864
void CubeEffect::paintCap(bool frontFirst, float zOffset)
Martin Flöser's avatar
Martin Flöser committed
865
866
867
    {
    if( ( !paintCaps ) || effects->numberOfDesktops() <= 2 )
        return;
Martin Flöser's avatar
Martin Flöser committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
    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);
902
903
904
905
906
907
908
        float opacity = cubeOpacity;
        if (start) {
            opacity *= timeLine.value();
        } else if (stop) {
            opacity *= (1.0 - timeLine.value());
        }
        m_capShader->setUniform("u_opacity", opacity);
909
        m_capShader->setUniform("u_mirror", 1);
Martin Flöser's avatar
Martin Flöser committed
910
911
912
913
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
        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 {
#ifndef KWIN_HAVE_OPENGLES
        // TODO: required transformations - should use the capMatrix and rotationMatrix
        glPushMatrix();
        glCallList( glList );
        glTranslatef( rect.width()/2, 0.0, zOffset );
        glRotatef( (1-frontDesktop)*360.0f / effects->numberOfDesktops(), 0.0, 1.0, 0.0 );
        glTranslatef( 0.0, rect.height(), 0.0 );
        if( mode == Sphere )
            {
            glPushMatrix();
            glScalef( 1.0, -1.0, 1.0 );
            }

        // TODO: mirroring should depend on the current rendering mode
        glMatrixMode( GL_TEXTURE );
        glPushMatrix();
        glLoadIdentity();
        glScalef( 1.0f, -1.0f, 1.0f );
        glTranslatef( 0.0f, -1.0f, 0.0f );
        glMatrixMode( GL_MODELVIEW );
940

941
        glColor4f( capColor.redF(), capColor.greenF(), capColor.blueF(), cubeOpacity );
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
        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
966
967
968
969
970
971
972
973
974
975
976
977
978
#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);
979
        m_capShader->setUniform("u_mirror", 0);
Martin Flöser's avatar
Martin Flöser committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
    } else {
#ifndef KWIN_HAVE_OPENGLES
        glMatrixMode( GL_TEXTURE );
        glPopMatrix();
        glMatrixMode( GL_MODELVIEW );

        if( mode == Sphere )
            glPopMatrix();
        glTranslatef( 0.0, -rect.height(), 0.0 );
#endif
    }
    glCullFace(secondCull);
    m_cubeCapBuffer->render(GL_TRIANGLES);
    glDisable(GL_BLEND);

    if (capShader) {
        ShaderManager::instance()->popShader();
        if (texturedCaps && effects->numberOfDesktops() > 3 && capTexture) {
            capTexture->unbind();
        }
    } else {
#ifndef KWIN_HAVE_OPENGLES
1002
        glPopMatrix();
1003
1004
1005
1006
1007
1008
1009
1010
1011
        if( texturedCaps && effects->numberOfDesktops() > 3 && capTexture )
            {
            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
1012
#endif
1013
    }
Martin Flöser's avatar
Martin Flöser committed
1014
    }
1015

1016
void CubeEffect::paintCubeCap()
1017
    {
1018
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
1019
1020
1021
    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);
1022
    float angle = 360.0f/effects->numberOfDesktops();
1023
    bool texture = texturedCaps && effects->numberOfDesktops() > 3 && capTexture;
Martin Flöser's avatar
Martin Flöser committed
1024
1025
    QVector<float> verts;
    QVector<float> texCoords;
Martin Flöser's avatar
Martin Flöser committed
1026
1027
    for( int i=0; i<effects->numberOfDesktops(); i++ )
        {
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
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
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
        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
1106
                    texCoords << texX1 << texY1;
1107
                    }
Martin Flöser's avatar
Martin Flöser committed
1108
                verts << x1 << 0.0 << z1;
1109
1110
                if( texture )
                    {
Martin Flöser's avatar
Martin Flöser committed
1111
                    texCoords << texX2 << texY2;
1112
                    }
Martin Flöser's avatar
Martin Flöser committed
1113
                verts << x2 << 0.0 << z2;
1114
1115
                if( texture )
                    {
Martin Flöser's avatar
Martin Flöser committed
1116
                    texCoords << texX3 << texY3;
1117
                    }
Martin Flöser's avatar
Martin Flöser committed
1118
                verts << x3 << 0.0 << z3;
1119
1120
                }
            }
Martin Flöser's avatar
Martin Flöser committed
1121
        }
Martin Flöser's avatar
Martin Flöser committed
1122
1123
1124
    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
1125
1126
    }

1127
1128
void CubeEffect::paintCylinderCap()
    {
Martin Flöser's avatar
Martin Flöser committed
1129
#ifndef KWIN_HAVE_OPENGLES
1130
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
1131
1132
1133
    float cubeAngle = (float)((float)(effects->numberOfDesktops() - 2 )/(float)effects->numberOfDesktops() * 180.0f);

    float radian = (cubeAngle*0.5)*M_PI/180;
1134
1135
    float radius = (rect.width()*0.5)*tan(radian);
    float segment = radius/30.0f;
1136

1137
1138
    bool texture = texturedCaps && effects->numberOfDesktops() > 3 && capTexture;
    for( int i=1; i<=30; i++ )
1139
1140
        {
        glBegin( GL_TRIANGLE_STRIP );
1141
1142
        int steps =  72;
        for( int j=0; j<=steps; j++ )
1143
            {
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
            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 );
1155
1156
1157
            }
        glEnd();
        }
Martin Flöser's avatar
Martin Flöser committed
1158
#endif
1159
1160
1161
1162
    }

void CubeEffect::paintSphereCap()
    {
Martin Flöser's avatar
Martin Flöser committed
1163
#ifndef KWIN_HAVE_OPENGLES
1164
    QRect rect = effects->clientArea( FullArea, activeScreen, effects->currentDesktop() );
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
    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
1208
#endif