not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / lib / kwinglutils.h
blobe72f3f2817064080a34b50ceee117151acf7f2f5
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>
33 #include <QtCore/QSharedData>
35 /** @addtogroup kwineffects */
36 /** @{ */
39 template< class K, class V > class QHash;
42 namespace KWin
46 class GLTexture;
49 // Initializes GLX function pointers
50 void KWIN_EXPORT initGLX();
51 // Initializes OpenGL stuff. This includes resolving function pointers as
52 // well as checking for GL version and extensions
53 // Note that GL context has to be created by the time this function is called
54 void KWIN_EXPORT initGL();
57 // Number of supported texture units
58 extern KWIN_EXPORT int glTextureUnitsCount;
61 bool KWIN_EXPORT hasGLVersion(int major, int minor, int release = 0);
62 bool KWIN_EXPORT hasGLXVersion(int major, int minor, int release = 0);
63 // use for both OpenGL and GLX extensions
64 bool KWIN_EXPORT hasGLExtension(const QString& extension);
66 // detect OpenGL error (add to various places in code to pinpoint the place)
67 bool KWIN_EXPORT checkGLError( const char* txt );
69 inline bool KWIN_EXPORT isPowerOfTwo( int x ) { return (( x & ( x - 1 )) == 0 ); }
70 /**
71 * @return power of two integer _greater or equal to_ x.
72 * E.g. nearestPowerOfTwo(513) = nearestPowerOfTwo(800) = 1024
73 **/
74 int KWIN_EXPORT nearestPowerOfTwo( int x );
76 /**
77 * Renders quads using given vertices.
78 * If texture is not 0, each texture coordinate much have two components (st).
79 * If color is not 0, each color much have four components (rgba).
80 * Note that texture coordinates must match texture type (normalized/unnormalized
81 * for GL_TEXTURE_2D/GL_TEXTURE_ARB).
83 * @param count number of vertices to use.
84 * @param dim number of components per vertex coordinate in vertices array.
85 * @param stride byte offset of consecutive elements in arrays. If 0, then
86 * arrays must be tighly packed. Stride must be a multiple of sizeof(float)!
87 **/
88 KWIN_EXPORT void renderGLGeometry( const QRegion& region, int count,
89 const float* vertices, const float* texture = 0, const float* color = 0,
90 int dim = 2, int stride = 0 );
91 /**
92 * Same as above, renders without specified region
93 **/
94 KWIN_EXPORT void renderGLGeometry( int count,
95 const float* vertices, const float* texture = 0, const float* color = 0,
96 int dim = 2, int stride = 0 );
99 KWIN_EXPORT void renderGLGeometryImmediate( int count,
100 const float* vertices, const float* texture = 0, const float* color = 0,
101 int dim = 2, int stride = 0 );
104 KWIN_EXPORT void renderRoundBox( const QRect& area, float roundness = 10.0f, GLTexture* texture = 0 );
105 KWIN_EXPORT void renderRoundBoxWithEdge( const QRect& area, float roundness = 10.0f );
108 class KWIN_EXPORT GLTexture
109 : public QSharedData
111 public:
112 GLTexture();
113 explicit GLTexture( const QImage& image, GLenum target = GL_TEXTURE_2D );
114 explicit GLTexture( const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D );
115 GLTexture( const QString& fileName );
116 GLTexture( int width, int height );
117 virtual ~GLTexture();
119 bool isNull() const;
120 QSize size() const;
122 virtual bool load( const QImage& image, GLenum target = GL_TEXTURE_2D );
123 virtual bool load( const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D );
124 virtual bool load( const QString& fileName );
125 virtual void discard();
126 virtual void bind();
127 virtual void unbind();
128 void render( QRegion region, const QRect& rect );
130 * Set up texture transformation matrix to automatically map unnormalized
131 * texture coordinates (i.e. 0 to width, 0 to height, (0,0) is top-left)
132 * to OpenGL coordinates. Automatically adjusts for different texture
133 * types. This can be done only for one texture at a time, repeated
134 * calls for the same texture are allowed though, requiring the same
135 * amount of calls to disableUnnormalizedTexCoords().
137 void enableUnnormalizedTexCoords();
139 * Disables transformation set up using enableUnnormalizedTexCoords().
141 void disableUnnormalizedTexCoords();
143 * Set up texture transformation matrix to automatically map normalized
144 * texture coordinates (i.e. 0 to 1, 0 to 1, (0,0) is bottom-left)
145 * to OpenGL coordinates. Automatically adjusts for different texture
146 * types. This can be done only for one texture at a time, repeated
147 * calls for the same texture are allowed though, requiring the same
148 * amount of calls to disableNormalizedTexCoords().
150 void enableNormalizedTexCoords();
152 * Disables transformation set up using enableNormalizedTexCoords().
154 void disableNormalizedTexCoords();
156 GLuint texture() const;
157 GLenum target() const;
158 GLenum filter() const;
159 virtual bool isDirty() const;
160 void setTexture( GLuint texture );
161 void setTarget( GLenum target );
162 void setFilter( GLenum filter );
163 void setWrapMode( GLenum mode );
164 virtual void setDirty();
166 static void initStatic();
167 static bool NPOTTextureSupported() { return mNPOTTextureSupported; }
168 static bool framebufferObjectSupported() { return mFramebufferObjectSupported; }
169 static bool saturationSupported() { return mSaturationSupported; }
171 protected:
172 void enableFilter();
173 QImage convertToGLFormat( const QImage& img ) const;
175 GLuint mTexture;
176 GLenum mTarget;
177 GLenum mFilter;
178 QSize mSize;
179 QSizeF mScale; // to un-normalize GL_TEXTURE_2D
180 bool y_inverted; // texture has y inverted
181 bool can_use_mipmaps;
182 bool has_valid_mipmaps;
184 private:
185 void init();
186 int mUnnormalizeActive; // 0 - no, otherwise refcount
187 int mNormalizeActive; // 0 - no, otherwise refcount
189 static bool mNPOTTextureSupported;
190 static bool mFramebufferObjectSupported;
191 static bool mSaturationSupported;
192 Q_DISABLE_COPY( GLTexture )
195 class KWIN_EXPORT GLShader
197 public:
198 GLShader(const QString& vertexfile, const QString& fragmentfile);
199 ~GLShader();
201 bool isValid() const { return mValid; }
202 void bind();
203 void unbind();
205 int uniformLocation(const QString& name);
206 bool setUniform(const QString& name, float value);
207 bool setUniform(const QString& name, int value);
208 int attributeLocation(const QString& name);
209 bool setAttribute(const QString& name, float value);
212 static void initStatic();
213 static bool fragmentShaderSupported() { return mFragmentShaderSupported; }
214 static bool vertexShaderSupported() { return mVertexShaderSupported; }
217 protected:
218 bool loadFromFiles(const QString& vertexfile, const QString& fragmentfile);
219 bool load(const QString& vertexsource, const QString& fragmentsource);
222 private:
223 unsigned int mProgram;
224 bool mValid;
225 QHash< QString, int >* mVariableLocations;
226 static bool mFragmentShaderSupported;
227 static bool mVertexShaderSupported;
231 * @short Render target object
233 * Render target object enables you to render onto a texture. This texture can
234 * later be used to e.g. do post-processing of the scene.
236 * @author Rivo Laks <rivolaks@hot.ee>
238 class KWIN_EXPORT GLRenderTarget
240 public:
242 * Constructs a GLRenderTarget
243 * @param color texture where the scene will be rendered onto
245 GLRenderTarget(GLTexture* color);
246 ~GLRenderTarget();
249 * Enables this render target.
250 * All OpenGL commands from now on affect this render target until the
251 * @ref disable method is called
253 bool enable();
255 * Disables this render target, activating whichever target was active
256 * when @ref enable was called.
258 bool disable();
260 bool valid() const { return mValid; }
262 static void initStatic();
263 static bool supported() { return mSupported; }
266 protected:
267 void initFBO();
270 private:
271 static bool mSupported;
273 GLTexture* mTexture;
274 bool mValid;
276 GLuint mFramebuffer;
279 } // namespace
281 #endif
283 /** @} */
285 #endif