qglshaderprogram.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtOpenGL module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QGLSHADERPROGRAM_H
00043 #define QGLSHADERPROGRAM_H
00044 
00045 #include <QtOpenGL/qgl.h>
00046 #include <QtGui/qvector2d.h>
00047 #include <QtGui/qvector3d.h>
00048 #include <QtGui/qvector4d.h>
00049 #include <QtGui/qmatrix4x4.h>
00050 
00051 QT_BEGIN_HEADER
00052 
00053 QT_BEGIN_NAMESPACE
00054 
00055 QT_MODULE(OpenGL)
00056 
00057 #if !defined(QT_OPENGL_ES_1)
00058 
00059 class QGLShaderProgram;
00060 class QGLShaderPrivate;
00061 
00062 class Q_OPENGL_EXPORT QGLShader : public QObject
00063 {
00064     Q_OBJECT
00065 public:
00066     enum ShaderTypeBit
00067     {
00068         Vertex          = 0x0001,
00069         Fragment        = 0x0002,
00070         Geometry        = 0x0004
00071     };
00072     Q_DECLARE_FLAGS(ShaderType, ShaderTypeBit)
00073 
00074     explicit QGLShader(QGLShader::ShaderType type, QObject *parent = 0);
00075     QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObject *parent = 0);
00076     virtual ~QGLShader();
00077 
00078     QGLShader::ShaderType shaderType() const;
00079 
00080     bool compileSourceCode(const char *source);
00081     bool compileSourceCode(const QByteArray& source);
00082     bool compileSourceCode(const QString& source);
00083     bool compileSourceFile(const QString& fileName);
00084 
00085     QByteArray sourceCode() const;
00086 
00087     bool isCompiled() const;
00088     QString log() const;
00089 
00090     GLuint shaderId() const;
00091 
00092     static bool hasOpenGLShaders(ShaderType type, const QGLContext *context = 0);
00093 
00094 private:
00095     friend class QGLShaderProgram;
00096 
00097     Q_DISABLE_COPY(QGLShader)
00098     Q_DECLARE_PRIVATE(QGLShader)
00099 };
00100 
00101 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLShader::ShaderType)
00102 
00103 
00104 class QGLShaderProgramPrivate;
00105 
00106 #ifndef GL_EXT_geometry_shader4
00107 #  define GL_LINES_ADJACENCY_EXT 0xA
00108 #  define GL_LINE_STRIP_ADJACENCY_EXT 0xB
00109 #  define GL_TRIANGLES_ADJACENCY_EXT 0xC
00110 #  define GL_TRIANGLE_STRIP_ADJACENCY_EXT 0xD
00111 #endif
00112 
00113 
00114 class Q_OPENGL_EXPORT QGLShaderProgram : public QObject
00115 {
00116     Q_OBJECT
00117 public:
00118     explicit QGLShaderProgram(QObject *parent = 0);
00119     explicit QGLShaderProgram(const QGLContext *context, QObject *parent = 0);
00120     virtual ~QGLShaderProgram();
00121 
00122     bool addShader(QGLShader *shader);
00123     void removeShader(QGLShader *shader);
00124     QList<QGLShader *> shaders() const;
00125 
00126     bool addShaderFromSourceCode(QGLShader::ShaderType type, const char *source);
00127     bool addShaderFromSourceCode(QGLShader::ShaderType type, const QByteArray& source);
00128     bool addShaderFromSourceCode(QGLShader::ShaderType type, const QString& source);
00129     bool addShaderFromSourceFile(QGLShader::ShaderType type, const QString& fileName);
00130 
00131     void removeAllShaders();
00132 
00133     virtual bool link();
00134     bool isLinked() const;
00135     QString log() const;
00136 
00137     bool bind();
00138     void release();
00139 
00140     GLuint programId() const;
00141 
00142     int maxGeometryOutputVertices() const;
00143 
00144     void setGeometryOutputVertexCount(int count);
00145     int geometryOutputVertexCount() const;
00146 
00147     void setGeometryInputType(GLenum inputType);
00148     GLenum geometryInputType() const;
00149 
00150     void setGeometryOutputType(GLenum outputType);
00151     GLenum geometryOutputType() const;
00152 
00153     void bindAttributeLocation(const char *name, int location);
00154     void bindAttributeLocation(const QByteArray& name, int location);
00155     void bindAttributeLocation(const QString& name, int location);
00156 
00157     int attributeLocation(const char *name) const;
00158     int attributeLocation(const QByteArray& name) const;
00159     int attributeLocation(const QString& name) const;
00160 
00161     void setAttributeValue(int location, GLfloat value);
00162     void setAttributeValue(int location, GLfloat x, GLfloat y);
00163     void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z);
00164     void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00165     void setAttributeValue(int location, const QVector2D& value);
00166     void setAttributeValue(int location, const QVector3D& value);
00167     void setAttributeValue(int location, const QVector4D& value);
00168     void setAttributeValue(int location, const QColor& value);
00169     void setAttributeValue(int location, const GLfloat *values, int columns, int rows);
00170 
00171     void setAttributeValue(const char *name, GLfloat value);
00172     void setAttributeValue(const char *name, GLfloat x, GLfloat y);
00173     void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
00174     void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00175     void setAttributeValue(const char *name, const QVector2D& value);
00176     void setAttributeValue(const char *name, const QVector3D& value);
00177     void setAttributeValue(const char *name, const QVector4D& value);
00178     void setAttributeValue(const char *name, const QColor& value);
00179     void setAttributeValue(const char *name, const GLfloat *values, int columns, int rows);
00180 
00181     void setAttributeArray
00182         (int location, const GLfloat *values, int tupleSize, int stride = 0);
00183     void setAttributeArray
00184         (int location, const QVector2D *values, int stride = 0);
00185     void setAttributeArray
00186         (int location, const QVector3D *values, int stride = 0);
00187     void setAttributeArray
00188         (int location, const QVector4D *values, int stride = 0);
00189     void setAttributeArray
00190         (int location, GLenum type, const void *values, int tupleSize, int stride = 0);
00191     void setAttributeArray
00192         (const char *name, const GLfloat *values, int tupleSize, int stride = 0);
00193     void setAttributeArray
00194         (const char *name, const QVector2D *values, int stride = 0);
00195     void setAttributeArray
00196         (const char *name, const QVector3D *values, int stride = 0);
00197     void setAttributeArray
00198         (const char *name, const QVector4D *values, int stride = 0);
00199     void setAttributeArray
00200         (const char *name, GLenum type, const void *values, int tupleSize, int stride = 0);
00201 
00202     void setAttributeBuffer
00203         (int location, GLenum type, int offset, int tupleSize, int stride = 0);
00204     void setAttributeBuffer
00205         (const char *name, GLenum type, int offset, int tupleSize, int stride = 0);
00206 
00207 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
00208     void setAttributeArray
00209         (int location, QMacCompatGLenum type, const void *values, int tupleSize, int stride = 0);
00210     void setAttributeArray
00211         (const char *name, QMacCompatGLenum type, const void *values, int tupleSize, int stride = 0);
00212     void setAttributeBuffer
00213         (int location, QMacCompatGLenum type, int offset, int tupleSize, int stride = 0);
00214     void setAttributeBuffer
00215         (const char *name, QMacCompatGLenum type, int offset, int tupleSize, int stride = 0);
00216 #endif
00217 
00218     void enableAttributeArray(int location);
00219     void enableAttributeArray(const char *name);
00220     void disableAttributeArray(int location);
00221     void disableAttributeArray(const char *name);
00222 
00223     int uniformLocation(const char *name) const;
00224     int uniformLocation(const QByteArray& name) const;
00225     int uniformLocation(const QString& name) const;
00226 
00227 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
00228     void setUniformValue(int location, QMacCompatGLint value);
00229     void setUniformValue(int location, QMacCompatGLuint value);
00230     void setUniformValue(const char *name, QMacCompatGLint value);
00231     void setUniformValue(const char *name, QMacCompatGLuint value);
00232     void setUniformValueArray(int location, const QMacCompatGLint *values, int count);
00233     void setUniformValueArray(int location, const QMacCompatGLuint *values, int count);
00234     void setUniformValueArray(const char *name, const QMacCompatGLint *values, int count);
00235     void setUniformValueArray(const char *name, const QMacCompatGLuint *values, int count);
00236 #endif
00237 
00238     void setUniformValue(int location, GLfloat value);
00239     void setUniformValue(int location, GLint value);
00240     void setUniformValue(int location, GLuint value);
00241     void setUniformValue(int location, GLfloat x, GLfloat y);
00242     void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z);
00243     void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00244     void setUniformValue(int location, const QVector2D& value);
00245     void setUniformValue(int location, const QVector3D& value);
00246     void setUniformValue(int location, const QVector4D& value);
00247     void setUniformValue(int location, const QColor& color);
00248     void setUniformValue(int location, const QPoint& point);
00249     void setUniformValue(int location, const QPointF& point);
00250     void setUniformValue(int location, const QSize& size);
00251     void setUniformValue(int location, const QSizeF& size);
00252     void setUniformValue(int location, const QMatrix2x2& value);
00253     void setUniformValue(int location, const QMatrix2x3& value);
00254     void setUniformValue(int location, const QMatrix2x4& value);
00255     void setUniformValue(int location, const QMatrix3x2& value);
00256     void setUniformValue(int location, const QMatrix3x3& value);
00257     void setUniformValue(int location, const QMatrix3x4& value);
00258     void setUniformValue(int location, const QMatrix4x2& value);
00259     void setUniformValue(int location, const QMatrix4x3& value);
00260     void setUniformValue(int location, const QMatrix4x4& value);
00261     void setUniformValue(int location, const GLfloat value[2][2]);
00262     void setUniformValue(int location, const GLfloat value[3][3]);
00263     void setUniformValue(int location, const GLfloat value[4][4]);
00264     void setUniformValue(int location, const QTransform& value);
00265 
00266     void setUniformValue(const char *name, GLfloat value);
00267     void setUniformValue(const char *name, GLint value);
00268     void setUniformValue(const char *name, GLuint value);
00269     void setUniformValue(const char *name, GLfloat x, GLfloat y);
00270     void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
00271     void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00272     void setUniformValue(const char *name, const QVector2D& value);
00273     void setUniformValue(const char *name, const QVector3D& value);
00274     void setUniformValue(const char *name, const QVector4D& value);
00275     void setUniformValue(const char *name, const QColor& color);
00276     void setUniformValue(const char *name, const QPoint& point);
00277     void setUniformValue(const char *name, const QPointF& point);
00278     void setUniformValue(const char *name, const QSize& size);
00279     void setUniformValue(const char *name, const QSizeF& size);
00280     void setUniformValue(const char *name, const QMatrix2x2& value);
00281     void setUniformValue(const char *name, const QMatrix2x3& value);
00282     void setUniformValue(const char *name, const QMatrix2x4& value);
00283     void setUniformValue(const char *name, const QMatrix3x2& value);
00284     void setUniformValue(const char *name, const QMatrix3x3& value);
00285     void setUniformValue(const char *name, const QMatrix3x4& value);
00286     void setUniformValue(const char *name, const QMatrix4x2& value);
00287     void setUniformValue(const char *name, const QMatrix4x3& value);
00288     void setUniformValue(const char *name, const QMatrix4x4& value);
00289     void setUniformValue(const char *name, const GLfloat value[2][2]);
00290     void setUniformValue(const char *name, const GLfloat value[3][3]);
00291     void setUniformValue(const char *name, const GLfloat value[4][4]);
00292     void setUniformValue(const char *name, const QTransform& value);
00293 
00294     void setUniformValueArray(int location, const GLfloat *values, int count, int tupleSize);
00295     void setUniformValueArray(int location, const GLint *values, int count);
00296     void setUniformValueArray(int location, const GLuint *values, int count);
00297     void setUniformValueArray(int location, const QVector2D *values, int count);
00298     void setUniformValueArray(int location, const QVector3D *values, int count);
00299     void setUniformValueArray(int location, const QVector4D *values, int count);
00300     void setUniformValueArray(int location, const QMatrix2x2 *values, int count);
00301     void setUniformValueArray(int location, const QMatrix2x3 *values, int count);
00302     void setUniformValueArray(int location, const QMatrix2x4 *values, int count);
00303     void setUniformValueArray(int location, const QMatrix3x2 *values, int count);
00304     void setUniformValueArray(int location, const QMatrix3x3 *values, int count);
00305     void setUniformValueArray(int location, const QMatrix3x4 *values, int count);
00306     void setUniformValueArray(int location, const QMatrix4x2 *values, int count);
00307     void setUniformValueArray(int location, const QMatrix4x3 *values, int count);
00308     void setUniformValueArray(int location, const QMatrix4x4 *values, int count);
00309 
00310     void setUniformValueArray(const char *name, const GLfloat *values, int count, int tupleSize);
00311     void setUniformValueArray(const char *name, const GLint *values, int count);
00312     void setUniformValueArray(const char *name, const GLuint *values, int count);
00313     void setUniformValueArray(const char *name, const QVector2D *values, int count);
00314     void setUniformValueArray(const char *name, const QVector3D *values, int count);
00315     void setUniformValueArray(const char *name, const QVector4D *values, int count);
00316     void setUniformValueArray(const char *name, const QMatrix2x2 *values, int count);
00317     void setUniformValueArray(const char *name, const QMatrix2x3 *values, int count);
00318     void setUniformValueArray(const char *name, const QMatrix2x4 *values, int count);
00319     void setUniformValueArray(const char *name, const QMatrix3x2 *values, int count);
00320     void setUniformValueArray(const char *name, const QMatrix3x3 *values, int count);
00321     void setUniformValueArray(const char *name, const QMatrix3x4 *values, int count);
00322     void setUniformValueArray(const char *name, const QMatrix4x2 *values, int count);
00323     void setUniformValueArray(const char *name, const QMatrix4x3 *values, int count);
00324     void setUniformValueArray(const char *name, const QMatrix4x4 *values, int count);
00325 
00326     static bool hasOpenGLShaderPrograms(const QGLContext *context = 0);
00327 
00328 private Q_SLOTS:
00329     void shaderDestroyed();
00330 
00331 private:
00332     Q_DISABLE_COPY(QGLShaderProgram)
00333     Q_DECLARE_PRIVATE(QGLShaderProgram)
00334 
00335     bool init();
00336 };
00337 
00338 #endif
00339 
00340 QT_END_NAMESPACE
00341 
00342 QT_END_HEADER
00343 
00344 #endif