Add a generic function to render a box with rounded corners.

Algorithm is basically same as for shadow, perhaps even shadow will use it.

svn path=/trunk/KDE/kdebase/workspace/; revision=683514
This commit is contained in:
Rivo Laks 2007-07-04 20:33:35 +00:00
parent 69ad089958
commit 42a4d364c2
4 changed files with 78 additions and 0 deletions

View file

@ -92,6 +92,7 @@ if(OPENGL_FOUND)
data/sharpen.frag data/sharpen.frag
data/sharpen.vert data/sharpen.vert
data/shadow-texture.png data/shadow-texture.png
data/circle.png
DESTINATION ${DATA_INSTALL_DIR}/kwin ) DESTINATION ${DATA_INSTALL_DIR}/kwin )
# config modules # config modules

BIN
effects/data/circle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

@ -16,6 +16,7 @@ License. See the file "COPYING" for the exact licensing terms.
#include "kwineffects.h" #include "kwineffects.h"
#include "kdebug.h" #include "kdebug.h"
#include <kstandarddirs.h>
#include <QPixmap> #include <QPixmap>
#include <QImage> #include <QImage>
@ -250,6 +251,75 @@ void renderGLGeometryImmediate( int count, const float* vertices, const float* t
glEnd(); glEnd();
} }
void addQuadVertices(QVector<float>& verts, float x1, float y1, float x2, float y2)
{
verts << x1 << y1;
verts << x1 << y2;
verts << x2 << y2;
verts << x2 << y1;
}
void renderRoundBox( const QRect& area, float roundness, GLTexture* texture )
{
static GLTexture* circleTexture = 0;
if( !texture && !circleTexture )
{
QString texturefile = KGlobal::dirs()->findResource("data", "kwin/circle.png");
circleTexture = new GLTexture(texturefile);
}
if( !texture )
{
texture = circleTexture;
}
glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glPushMatrix();
QVector<float> verts, texcoords;
// center
addQuadVertices(verts, area.left() + roundness, area.top() + roundness, area.right() - roundness, area.bottom() - roundness);
addQuadVertices(texcoords, 0.5, 0.5, 0.5, 0.5);
// sides
// left
addQuadVertices(verts, area.left(), area.top() + roundness, area.left() + roundness, area.bottom() - roundness);
addQuadVertices(texcoords, 0.0, 0.5, 0.5, 0.5);
// top
addQuadVertices(verts, area.left() + roundness, area.top(), area.right() - roundness, area.top() + roundness);
addQuadVertices(texcoords, 0.5, 0.0, 0.5, 0.5);
// right
addQuadVertices(verts, area.right() - roundness, area.top() + roundness, area.right(), area.bottom() - roundness);
addQuadVertices(texcoords, 0.5, 0.5, 1.0, 0.5);
// bottom
addQuadVertices(verts, area.left() + roundness, area.bottom() - roundness, area.right() - roundness, area.bottom());
addQuadVertices(texcoords, 0.5, 0.5, 0.5, 1.0);
// corners
// top-left
addQuadVertices(verts, area.left(), area.top(), area.left() + roundness, area.top() + roundness);
addQuadVertices(texcoords, 0.0, 0.0, 0.5, 0.5);
// top-right
addQuadVertices(verts, area.right() - roundness, area.top(), area.right(), area.top() + roundness);
addQuadVertices(texcoords, 0.5, 0.0, 1.0, 0.5);
// bottom-left
addQuadVertices(verts, area.left(), area.bottom() - roundness, area.left() + roundness, area.bottom());
addQuadVertices(texcoords, 0.0, 0.5, 0.5, 1.0);
// bottom-right
addQuadVertices(verts, area.right() - roundness, area.bottom() - roundness, area.right(), area.bottom());
addQuadVertices(texcoords, 0.5, 0.5, 1.0, 1.0);
texture->bind();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// We have two elements per vertex in the verts array
int verticesCount = verts.count() / 2;
renderGLGeometry( verticesCount, verts.data(), texcoords.data() );
texture->unbind();
glPopMatrix();
glPopAttrib();
}
//**************************************** //****************************************
// GLTexture // GLTexture
//**************************************** //****************************************

View file

@ -30,6 +30,9 @@ namespace KWin
{ {
class GLTexture;
// Initializes GLX function pointers // Initializes GLX function pointers
void KWIN_EXPORT initGLX(); void KWIN_EXPORT initGLX();
// Initializes OpenGL stuff. This includes resolving function pointers as // Initializes OpenGL stuff. This includes resolving function pointers as
@ -87,6 +90,10 @@ KWIN_EXPORT void renderGLGeometryImmediate( int count,
const float* vertices, const float* texture = 0, const float* color = 0, const float* vertices, const float* texture = 0, const float* color = 0,
int dim = 2, int stride = 0 ); int dim = 2, int stride = 0 );
KWIN_EXPORT void renderRoundBox( const QRect& area, float roundness = 10.0f, GLTexture* texture = 0 );
class KWIN_EXPORT GLTexture class KWIN_EXPORT GLTexture
{ {
public: public: