1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
21 #ifndef KWIN_GLUTILS_H
22 #define KWIN_GLUTILS_H
24 #include <kwinconfig.h> // KWIN_HAVE_OPENGL
26 #ifdef KWIN_HAVE_OPENGL
27 #include <kwinglutils_funcs.h>
29 #include <QtGui/QPixmap>
31 #include <QtGui/QImage>
32 #include <QtCore/QSize>
34 /** @addtogroup kwineffects */
38 template< class K
, class V
> class QHash
;
48 // Initializes GLX function pointers
49 void KWIN_EXPORT
initGLX();
50 // Initializes OpenGL stuff. This includes resolving function pointers as
51 // well as checking for GL version and extensions
52 // Note that GL context has to be created by the time this function is called
53 void KWIN_EXPORT
initGL();
56 // Number of supported texture units
57 extern KWIN_EXPORT
int glTextureUnitsCount
;
60 bool KWIN_EXPORT
hasGLVersion(int major
, int minor
, int release
= 0);
61 bool KWIN_EXPORT
hasGLXVersion(int major
, int minor
, int release
= 0);
62 // use for both OpenGL and GLX extensions
63 bool KWIN_EXPORT
hasGLExtension(const QString
& extension
);
65 // detect OpenGL error (add to various places in code to pinpoint the place)
66 bool KWIN_EXPORT
checkGLError( const char* txt
);
68 inline bool KWIN_EXPORT
isPowerOfTwo( int x
) { return (( x
& ( x
- 1 )) == 0 ); }
70 * @return power of two integer _greater or equal to_ x.
71 * E.g. nearestPowerOfTwo(513) = nearestPowerOfTwo(800) = 1024
73 int KWIN_EXPORT
nearestPowerOfTwo( int x
);
76 * Renders quads using given vertices.
77 * If texture is not 0, each texture coordinate much have two components (st).
78 * If color is not 0, each color much have four components (rgba).
80 * @param count number of vertices to use.
81 * @param dim number of components per vertex coordinate in vertices array.
82 * @param stride byte offset of consecutive elements in arrays. If 0, then
83 * arrays must be tighly packed. Stride must be a multiple of sizeof(float)!
85 KWIN_EXPORT
void renderGLGeometry( const QRegion
& region
, int count
,
86 const float* vertices
, const float* texture
= 0, const float* color
= 0,
87 int dim
= 2, int stride
= 0 );
89 * Same as above, renders without specified region
91 KWIN_EXPORT
void renderGLGeometry( int count
,
92 const float* vertices
, const float* texture
= 0, const float* color
= 0,
93 int dim
= 2, int stride
= 0 );
96 KWIN_EXPORT
void renderGLGeometryImmediate( int count
,
97 const float* vertices
, const float* texture
= 0, const float* color
= 0,
98 int dim
= 2, int stride
= 0 );
101 KWIN_EXPORT
void renderRoundBox( const QRect
& area
, float roundness
= 10.0f
, GLTexture
* texture
= 0 );
102 KWIN_EXPORT
void renderRoundBoxWithEdge( const QRect
& area
, float roundness
= 10.0f
);
105 class KWIN_EXPORT GLTexture
109 explicit GLTexture( const QImage
& image
, GLenum target
= GL_TEXTURE_2D
);
110 explicit GLTexture( const QPixmap
& pixmap
, GLenum target
= GL_TEXTURE_2D
);
111 GLTexture( const QString
& fileName
);
112 GLTexture( int width
, int height
);
113 virtual ~GLTexture();
117 virtual bool load( const QImage
& image
, GLenum target
= GL_TEXTURE_2D
);
118 virtual bool load( const QPixmap
& pixmap
, GLenum target
= GL_TEXTURE_2D
);
119 virtual bool load( const QString
& fileName
);
120 virtual void discard();
122 virtual void unbind();
123 void render( QRegion region
, const QRect
& rect
);
124 void enableUnnormalizedTexCoords();
125 void disableUnnormalizedTexCoords();
127 GLuint
texture() const;
128 GLenum
target() const;
129 GLenum
filter() const;
130 virtual bool isDirty() const;
131 void setTexture( GLuint texture
);
132 void setTarget( GLenum target
);
133 void setFilter( GLenum filter
);
134 void setWrapMode( GLenum mode
);
135 virtual void setDirty();
137 static void initStatic();
138 static bool NPOTTextureSupported() { return mNPOTTextureSupported
; }
139 static bool framebufferObjectSupported() { return mFramebufferObjectSupported
; }
140 static bool saturationSupported() { return mSaturationSupported
; }
144 QImage
convertToGLFormat( const QImage
& img
) const;
150 QSizeF mScale
; // to un-normalize GL_TEXTURE_2D
151 bool y_inverted
; // texture has y inverted
152 bool can_use_mipmaps
;
153 bool has_valid_mipmaps
;
158 static bool mNPOTTextureSupported
;
159 static bool mFramebufferObjectSupported
;
160 static bool mSaturationSupported
;
163 class KWIN_EXPORT GLShader
166 GLShader(const QString
& vertexfile
, const QString
& fragmentfile
);
169 bool isValid() const { return mValid
; }
173 int uniformLocation(const QString
& name
);
174 bool setUniform(const QString
& name
, float value
);
175 bool setUniform(const QString
& name
, int value
);
176 int attributeLocation(const QString
& name
);
177 bool setAttribute(const QString
& name
, float value
);
180 static void initStatic();
181 static bool fragmentShaderSupported() { return mFragmentShaderSupported
; }
182 static bool vertexShaderSupported() { return mVertexShaderSupported
; }
186 bool loadFromFiles(const QString
& vertexfile
, const QString
& fragmentfile
);
187 bool load(const QString
& vertexsource
, const QString
& fragmentsource
);
191 unsigned int mProgram
;
193 QHash
< QString
, int >* mVariableLocations
;
194 static bool mFragmentShaderSupported
;
195 static bool mVertexShaderSupported
;
199 * @short Render target object
201 * Render target object enables you to render onto a texture. This texture can
202 * later be used to e.g. do post-processing of the scene.
204 * @author Rivo Laks <rivolaks@hot.ee>
206 class KWIN_EXPORT GLRenderTarget
210 * Constructs a GLRenderTarget
211 * @param color texture where the scene will be rendered onto
213 GLRenderTarget(GLTexture
* color
);
217 * Enables this render target.
218 * All OpenGL commands from now on affect this render target until the
219 * @ref disable method is called
223 * Disables this render target, activating whichever target was active
224 * when @ref enable was called.
228 bool valid() const { return mValid
; }
230 static void initStatic();
231 static bool supported() { return mSupported
; }
239 static bool mSupported
;