KWinLibraries
kwinglutils.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef KWIN_GLUTILS_H
00022 #define KWIN_GLUTILS_H
00023
00024 #include <kwinconfig.h>
00025
00026 #ifdef KWIN_HAVE_OPENGL
00027 #include <kwinglutils_funcs.h>
00028
00029 #include <QtGui/QPixmap>
00030
00031 #include <QtGui/QImage>
00032 #include <QtCore/QSize>
00033 #include <QtCore/QSharedData>
00034
00039 template< class K, class V > class QHash;
00040
00041
00042 namespace KWin
00043 {
00044
00045
00046 class GLTexture;
00047
00048
00049
00050 void KWIN_EXPORT initGLX();
00051
00052
00053
00054 void KWIN_EXPORT initGL();
00055
00056
00057
00058 extern KWIN_EXPORT int glTextureUnitsCount;
00059
00060
00061 bool KWIN_EXPORT hasGLVersion(int major, int minor, int release = 0);
00062 bool KWIN_EXPORT hasGLXVersion(int major, int minor, int release = 0);
00063
00064 bool KWIN_EXPORT hasGLExtension(const QString& extension);
00065
00066
00067 bool KWIN_EXPORT checkGLError( const char* txt );
00068
00069 inline bool KWIN_EXPORT isPowerOfTwo( int x ) { return (( x & ( x - 1 )) == 0 ); }
00074 int KWIN_EXPORT nearestPowerOfTwo( int x );
00075
00086 KWIN_EXPORT void renderGLGeometry( const QRegion& region, int count,
00087 const float* vertices, const float* texture = 0, const float* color = 0,
00088 int dim = 2, int stride = 0 );
00092 KWIN_EXPORT void renderGLGeometry( int count,
00093 const float* vertices, const float* texture = 0, const float* color = 0,
00094 int dim = 2, int stride = 0 );
00095
00096
00097 KWIN_EXPORT void renderGLGeometryImmediate( int count,
00098 const float* vertices, const float* texture = 0, const float* color = 0,
00099 int dim = 2, int stride = 0 );
00100
00101
00102 KWIN_EXPORT void renderRoundBox( const QRect& area, float roundness = 10.0f, GLTexture* texture = 0 );
00103 KWIN_EXPORT void renderRoundBoxWithEdge( const QRect& area, float roundness = 10.0f );
00104
00105
00106 class KWIN_EXPORT GLTexture
00107 : public QSharedData
00108 {
00109 public:
00110 GLTexture();
00111 explicit GLTexture( const QImage& image, GLenum target = GL_TEXTURE_2D );
00112 explicit GLTexture( const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D );
00113 GLTexture( const QString& fileName );
00114 GLTexture( int width, int height );
00115 virtual ~GLTexture();
00116
00117 bool isNull() const;
00118 QSize size() const;
00119
00120 virtual bool load( const QImage& image, GLenum target = GL_TEXTURE_2D );
00121 virtual bool load( const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D );
00122 virtual bool load( const QString& fileName );
00123 virtual void discard();
00124 virtual void bind();
00125 virtual void unbind();
00126 void render( QRegion region, const QRect& rect );
00127 void enableUnnormalizedTexCoords();
00128 void disableUnnormalizedTexCoords();
00129
00130 GLuint texture() const;
00131 GLenum target() const;
00132 GLenum filter() const;
00133 virtual bool isDirty() const;
00134 void setTexture( GLuint texture );
00135 void setTarget( GLenum target );
00136 void setFilter( GLenum filter );
00137 void setWrapMode( GLenum mode );
00138 virtual void setDirty();
00139
00140 static void initStatic();
00141 static bool NPOTTextureSupported() { return mNPOTTextureSupported; }
00142 static bool framebufferObjectSupported() { return mFramebufferObjectSupported; }
00143 static bool saturationSupported() { return mSaturationSupported; }
00144
00145 protected:
00146 void enableFilter();
00147 QImage convertToGLFormat( const QImage& img ) const;
00148
00149 GLuint mTexture;
00150 GLenum mTarget;
00151 GLenum mFilter;
00152 QSize mSize;
00153 QSizeF mScale;
00154 bool y_inverted;
00155 bool can_use_mipmaps;
00156 bool has_valid_mipmaps;
00157
00158 private:
00159 void init();
00160
00161 static bool mNPOTTextureSupported;
00162 static bool mFramebufferObjectSupported;
00163 static bool mSaturationSupported;
00164 Q_DISABLE_COPY( GLTexture )
00165 };
00166
00167 class KWIN_EXPORT GLShader
00168 {
00169 public:
00170 GLShader(const QString& vertexfile, const QString& fragmentfile);
00171 ~GLShader();
00172
00173 bool isValid() const { return mValid; }
00174 void bind();
00175 void unbind();
00176
00177 int uniformLocation(const QString& name);
00178 bool setUniform(const QString& name, float value);
00179 bool setUniform(const QString& name, int value);
00180 int attributeLocation(const QString& name);
00181 bool setAttribute(const QString& name, float value);
00182
00183
00184 static void initStatic();
00185 static bool fragmentShaderSupported() { return mFragmentShaderSupported; }
00186 static bool vertexShaderSupported() { return mVertexShaderSupported; }
00187
00188
00189 protected:
00190 bool loadFromFiles(const QString& vertexfile, const QString& fragmentfile);
00191 bool load(const QString& vertexsource, const QString& fragmentsource);
00192
00193
00194 private:
00195 unsigned int mProgram;
00196 bool mValid;
00197 QHash< QString, int >* mVariableLocations;
00198 static bool mFragmentShaderSupported;
00199 static bool mVertexShaderSupported;
00200 };
00201
00210 class KWIN_EXPORT GLRenderTarget
00211 {
00212 public:
00217 GLRenderTarget(GLTexture* color);
00218 ~GLRenderTarget();
00219
00225 bool enable();
00230 bool disable();
00231
00232 bool valid() const { return mValid; }
00233
00234 static void initStatic();
00235 static bool supported() { return mSupported; }
00236
00237
00238 protected:
00239 void initFBO();
00240
00241
00242 private:
00243 static bool mSupported;
00244
00245 GLTexture* mTexture;
00246 bool mValid;
00247
00248 GLuint mFramebuffer;
00249 };
00250
00251 }
00252
00253 #endif
00254
00257 #endif