1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | //
|
---|
43 | // W A R N I N G
|
---|
44 | // -------------
|
---|
45 | //
|
---|
46 | // This file is not part of the Qt API. It exists purely as an
|
---|
47 | // implementation detail. This header file may change from version to
|
---|
48 | // version without notice, or even be removed.
|
---|
49 | //
|
---|
50 | // We mean it.
|
---|
51 | //
|
---|
52 |
|
---|
53 | /*
|
---|
54 | Uniform Types in OpenGL ES 2.0
|
---|
55 | ==============================
|
---|
56 | GL_FLOAT GLfloat GLfloat
|
---|
57 | GL_FLOAT_VEC2 QGLVec2 GLfloat[2]
|
---|
58 | GL_FLOAT_VEC3 QGLVec3 GLfloat[3]
|
---|
59 | GL_FLOAT_VEC4 QGLVec4 GLfloat[4]
|
---|
60 | GL_INT GLint GLint
|
---|
61 | GL_INT_VEC2 QGLIVec2 GLint[2]
|
---|
62 | GL_INT_VEC3 QGLIVec3 GLint[3]
|
---|
63 | GL_INT_VEC4 QGLIVec4 GLint[4]
|
---|
64 | GL_BOOL GLbool GLbool
|
---|
65 | GL_BOOL_VEC2 QGLBVec2 GLbool[2]
|
---|
66 | GL_BOOL_VEC3 QGLBVec3 GLbool[3]
|
---|
67 | GL_BOOL_VEC4 QGLBVec4 GLbool[4]
|
---|
68 | GL_FLOAT_MAT2 QGLMat2 GLfloat[2][2]
|
---|
69 | GL_FLOAT_MAT3 QGLMat3 GLfloat[3][3]
|
---|
70 | GL_FLOAT_MAT4 QGLMat4 GLfloat[4][4]
|
---|
71 | GL_SAMPLER_2D QGLSampler2D GLuint
|
---|
72 | GL_SAMPLER_CUBE QGLSamplerCube GLuint
|
---|
73 |
|
---|
74 | Additional Types in Desktop OpenGL 2.0
|
---|
75 | ======================================
|
---|
76 | SAMPLER_1D,
|
---|
77 | SAMPLER_3D,
|
---|
78 | SAMPLER_1D_SHADOW,
|
---|
79 | SAMPLER_2D_SHADOW.
|
---|
80 | */
|
---|
81 |
|
---|
82 | #include <QtOpenGL>
|
---|
83 |
|
---|
84 |
|
---|
85 | typedef struct {
|
---|
86 | GLfloat a;
|
---|
87 | GLfloat b;
|
---|
88 | } QGLVec2;
|
---|
89 |
|
---|
90 | typedef struct {
|
---|
91 | GLfloat a;
|
---|
92 | GLfloat b;
|
---|
93 | GLfloat c;
|
---|
94 | } QGLVec3;
|
---|
95 |
|
---|
96 | typedef struct {
|
---|
97 | GLfloat a;
|
---|
98 | GLfloat b;
|
---|
99 | GLfloat c;
|
---|
100 | GLfloat d;
|
---|
101 | } QGLVec4;
|
---|
102 |
|
---|
103 |
|
---|
104 | class QGLShaderProgram;
|
---|
105 |
|
---|
106 | class QGLShaderPrivate;
|
---|
107 |
|
---|
108 | class QGLShader : QObject
|
---|
109 | {
|
---|
110 | Q_OBJECT
|
---|
111 | public:
|
---|
112 | enum ShaderType {VertexShader, FragmentShader};
|
---|
113 |
|
---|
114 | QGLShader(ShaderType type, const QGLContext* ctx = 0);
|
---|
115 |
|
---|
116 | GLuint id();
|
---|
117 | void clearSource();
|
---|
118 | void addSource(const QLatin1String& newSource);
|
---|
119 | bool compile();
|
---|
120 | bool isValid();
|
---|
121 | QString log();
|
---|
122 | const QGLContext* context(); //maybe make private with prog a friend?
|
---|
123 |
|
---|
124 | private:
|
---|
125 | QGLShaderPrivate* d_ptr;
|
---|
126 | Q_DECLARE_PRIVATE(QGLShader);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | public slots:
|
---|
130 | void cleanupGLContextRefs(const QGLContext *context);
|
---|
131 | */
|
---|
132 | };
|
---|
133 |
|
---|
134 |
|
---|
135 | enum QGLType {
|
---|
136 | QGLInvalidType = 0,
|
---|
137 | QGLFloatType = GL_FLOAT,
|
---|
138 | QGLVec2Type = GL_FLOAT_VEC2,
|
---|
139 | QGLVec3Type = GL_FLOAT_VEC3,
|
---|
140 | QGLVec4Type = GL_FLOAT_VEC4,
|
---|
141 | QGLIntType = GL_INT,
|
---|
142 | QGLIVec2Type = GL_INT_VEC2,
|
---|
143 | QGLIVec3Type = GL_INT_VEC3,
|
---|
144 | QGLIVec4Type = GL_INT_VEC4,
|
---|
145 | QGLBoolType = GL_BOOL,
|
---|
146 | QGLBVec2Type = GL_BOOL_VEC2,
|
---|
147 | QGLBVec3Type = GL_BOOL_VEC3,
|
---|
148 | QGLBVec4Type = GL_BOOL_VEC4,
|
---|
149 | QGLMat2Type = GL_FLOAT_MAT2,
|
---|
150 | QGLMat3Type = GL_FLOAT_MAT3,
|
---|
151 | QGLMat4Type = GL_FLOAT_MAT4,
|
---|
152 | QGLSampler2DType = GL_SAMPLER_2D,
|
---|
153 | QGLSamplerCubeType = GL_SAMPLER_CUBE
|
---|
154 | };
|
---|
155 |
|
---|
156 | class QGLUniform
|
---|
157 | {
|
---|
158 | public:
|
---|
159 |
|
---|
160 | QGLUniform(GLenum glType, GLint location, QGLContext* context)
|
---|
161 | : m_id(location), m_type(QGLType(glType)), ctx(context) {}
|
---|
162 |
|
---|
163 | QGLUniform(); // Called by QMap when there's no match on the name
|
---|
164 |
|
---|
165 | QGLType type() const {return m_type;}
|
---|
166 | GLuint id() const {return m_id;}
|
---|
167 |
|
---|
168 | // Seems odd to be const, but it doesn't actually modify any of the
|
---|
169 | // class members, only the GL state!
|
---|
170 | const QGLUniform& operator=(const GLfloat&) const;
|
---|
171 |
|
---|
172 | const QGLUniform& operator=(const QGLVec2&) const;
|
---|
173 | const QGLUniform& operator=(const QSizeF&) const;
|
---|
174 | const QGLUniform& operator=(const QPointF&) const;
|
---|
175 |
|
---|
176 | const QGLUniform& operator=(const QGLVec3&) const;
|
---|
177 |
|
---|
178 | const QGLUniform& operator=(const QGLVec4&) const;
|
---|
179 | const QGLUniform& operator=(const QColor&) const;
|
---|
180 |
|
---|
181 | const QGLUniform& operator=(const GLfloat[2][2]) const;
|
---|
182 |
|
---|
183 | const QGLUniform& operator=(const GLfloat[3][3]) const;
|
---|
184 | const QGLUniform& operator=(const QTransform&) const;
|
---|
185 |
|
---|
186 | const QGLUniform& operator=(const GLfloat[4][4]) const;
|
---|
187 |
|
---|
188 | const QGLUniform& operator=(const GLuint&) const; // sampler2d, specifying a texture unit
|
---|
189 |
|
---|
190 |
|
---|
191 | protected:
|
---|
192 | GLuint m_id;
|
---|
193 | QGLType m_type;
|
---|
194 | QGLContext* ctx;
|
---|
195 | };
|
---|
196 |
|
---|
197 | typedef QMap<QString, QGLUniform> QGLUniformList;
|
---|
198 | typedef QMapIterator<QString, QGLUniform> QGLUniformListIterator;
|
---|
199 |
|
---|
200 |
|
---|
201 | class QGLVertexAttribute
|
---|
202 | {
|
---|
203 | public:
|
---|
204 | QGLVertexAttribute(GLenum glType, GLuint location, QGLContext* context)
|
---|
205 | : m_id(location), m_type(QGLType(glType)), ctx(context) {}
|
---|
206 |
|
---|
207 | QGLVertexAttribute(); // Called by QMap when there's no match on the name
|
---|
208 |
|
---|
209 | QGLType type() const {return m_type;}
|
---|
210 | GLuint id() const {return m_id;}
|
---|
211 | void enable() const;
|
---|
212 | void disable() const;
|
---|
213 |
|
---|
214 | const QGLVertexAttribute& operator=(const GLfloat* rhs) const;
|
---|
215 | const QGLVertexAttribute& operator=(const QGLVec3* rhs) const;
|
---|
216 |
|
---|
217 | protected:
|
---|
218 | GLuint m_id;
|
---|
219 | QGLType m_type;
|
---|
220 | QGLContext* ctx;
|
---|
221 | };
|
---|
222 |
|
---|
223 | //TODO: Convert into setter overloads on QGLShaderProgram
|
---|
224 | typedef QMap<QString, QGLVertexAttribute> QGLVertexAttributeList;
|
---|
225 | typedef QMapIterator<QString, QGLVertexAttribute> QGLVertexAttributeListIterator;
|
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 | class QGLShaderProgramPrivate;
|
---|
230 |
|
---|
231 | class QGLShaderProgram : QObject
|
---|
232 | {
|
---|
233 | Q_OBJECT
|
---|
234 | public:
|
---|
235 | QGLShaderProgram(const QGLContext* ctx = 0);
|
---|
236 |
|
---|
237 | const QGLUniformList & uniforms();
|
---|
238 | const QGLVertexAttributeList& vertexAttributes();
|
---|
239 |
|
---|
240 | bool addShader(QGLShader* newShader);
|
---|
241 | bool removeShader(QGLShader* oldShader);
|
---|
242 | bool removeAllShaders();
|
---|
243 |
|
---|
244 | bool link();
|
---|
245 | QString log();
|
---|
246 | bool isValid();
|
---|
247 | void use();
|
---|
248 |
|
---|
249 | GLuint id();
|
---|
250 |
|
---|
251 | private:
|
---|
252 | QGLShaderProgramPrivate* d_ptr;
|
---|
253 | Q_DECLARE_PRIVATE(QGLShaderProgram);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | public slots:
|
---|
257 | void cleanupGLContextRefs(const QGLContext *context);
|
---|
258 | */
|
---|
259 | };
|
---|
260 |
|
---|