source: trunk/src/opengl/qgl.h@ 901

Last change on this file since 901 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 20.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QGL_H
43#define QGL_H
44
45#include <QtGui/qwidget.h>
46#include <QtGui/qpaintengine.h>
47#include <QtOpenGL/qglcolormap.h>
48#include <QtCore/qmap.h>
49#include <QtCore/qscopedpointer.h>
50
51QT_BEGIN_HEADER
52
53#if defined(Q_WS_WIN)
54# include <QtCore/qt_windows.h>
55#endif
56
57#if defined(Q_WS_MAC)
58# include <OpenGL/gl.h>
59# include <OpenGL/glu.h>
60#elif defined(QT_OPENGL_ES_1)
61# include <GLES/gl.h>
62#ifndef GL_DOUBLE
63# define GL_DOUBLE GL_FLOAT
64#endif
65#ifndef GLdouble
66typedef GLfloat GLdouble;
67#endif
68#elif defined(QT_OPENGL_ES_2)
69# include <GLES2/gl2.h>
70#ifndef GL_DOUBLE
71# define GL_DOUBLE GL_FLOAT
72#endif
73#ifndef GLdouble
74typedef GLfloat GLdouble;
75#endif
76#else
77# include <GL/gl.h>
78# ifndef QT_LINUXBASE
79# include <GL/glu.h>
80# endif
81#endif
82
83QT_BEGIN_NAMESPACE
84
85QT_MODULE(OpenGL)
86
87#if defined(Q_WS_MAC) && defined (QT_BUILD_OPENGL_LIB) && !defined(QT_MAC_USE_COCOA) && !defined(QDOC)
88#define Q_MAC_COMPAT_GL_FUNCTIONS
89
90template <typename T>
91struct QMacGLCompatTypes
92{
93 typedef long CompatGLint;
94 typedef unsigned long CompatGLuint;
95 typedef unsigned long CompatGLenum;
96};
97
98template <>
99struct QMacGLCompatTypes<long>
100{
101 typedef int CompatGLint;
102 typedef unsigned int CompatGLuint;
103 typedef unsigned int CompatGLenum;
104};
105
106typedef QMacGLCompatTypes<GLint>::CompatGLint QMacCompatGLint;
107typedef QMacGLCompatTypes<GLint>::CompatGLuint QMacCompatGLuint;
108typedef QMacGLCompatTypes<GLint>::CompatGLenum QMacCompatGLenum;
109
110#endif
111
112#ifdef QT3_SUPPORT
113#define QGL_VERSION 460
114#define QGL_VERSION_STR "4.6"
115inline QT3_SUPPORT const char *qGLVersion() {
116 return QGL_VERSION_STR;
117}
118#endif
119
120#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
121class QGLCmap;
122#endif
123
124class QPixmap;
125#if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
126class QGLOverlayWidget;
127#endif
128class QGLWidgetPrivate;
129class QGLContextPrivate;
130
131// Namespace class:
132namespace QGL
133{
134 Q_OPENGL_EXPORT void setPreferredPaintEngine(QPaintEngine::Type engineType);
135
136 enum FormatOption {
137 DoubleBuffer = 0x0001,
138 DepthBuffer = 0x0002,
139 Rgba = 0x0004,
140 AlphaChannel = 0x0008,
141 AccumBuffer = 0x0010,
142 StencilBuffer = 0x0020,
143 StereoBuffers = 0x0040,
144 DirectRendering = 0x0080,
145 HasOverlay = 0x0100,
146 SampleBuffers = 0x0200,
147 DeprecatedFunctions = 0x0400,
148 SingleBuffer = DoubleBuffer << 16,
149 NoDepthBuffer = DepthBuffer << 16,
150 ColorIndex = Rgba << 16,
151 NoAlphaChannel = AlphaChannel << 16,
152 NoAccumBuffer = AccumBuffer << 16,
153 NoStencilBuffer = StencilBuffer << 16,
154 NoStereoBuffers = StereoBuffers << 16,
155 IndirectRendering = DirectRendering << 16,
156 NoOverlay = HasOverlay << 16,
157 NoSampleBuffers = SampleBuffers << 16,
158 NoDeprecatedFunctions = DeprecatedFunctions << 16
159 };
160 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
161}
162
163Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
164
165class QGLFormatPrivate;
166
167class Q_OPENGL_EXPORT QGLFormat
168{
169public:
170 QGLFormat();
171 QGLFormat(QGL::FormatOptions options, int plane = 0);
172 QGLFormat(const QGLFormat &other);
173 QGLFormat &operator=(const QGLFormat &other);
174 ~QGLFormat();
175
176 void setDepthBufferSize(int size);
177 int depthBufferSize() const;
178
179 void setAccumBufferSize(int size);
180 int accumBufferSize() const;
181
182 void setRedBufferSize(int size);
183 int redBufferSize() const;
184
185 void setGreenBufferSize(int size);
186 int greenBufferSize() const;
187
188 void setBlueBufferSize(int size);
189 int blueBufferSize() const;
190
191 void setAlphaBufferSize(int size);
192 int alphaBufferSize() const;
193
194 void setStencilBufferSize(int size);
195 int stencilBufferSize() const;
196
197 void setSampleBuffers(bool enable);
198 bool sampleBuffers() const;
199
200 void setSamples(int numSamples);
201 int samples() const;
202
203 void setSwapInterval(int interval);
204 int swapInterval() const;
205
206 bool doubleBuffer() const;
207 void setDoubleBuffer(bool enable);
208 bool depth() const;
209 void setDepth(bool enable);
210 bool rgba() const;
211 void setRgba(bool enable);
212 bool alpha() const;
213 void setAlpha(bool enable);
214 bool accum() const;
215 void setAccum(bool enable);
216 bool stencil() const;
217 void setStencil(bool enable);
218 bool stereo() const;
219 void setStereo(bool enable);
220 bool directRendering() const;
221 void setDirectRendering(bool enable);
222 bool hasOverlay() const;
223 void setOverlay(bool enable);
224
225 int plane() const;
226 void setPlane(int plane);
227
228 void setOption(QGL::FormatOptions opt);
229 bool testOption(QGL::FormatOptions opt) const;
230
231 static QGLFormat defaultFormat();
232 static void setDefaultFormat(const QGLFormat& f);
233
234 static QGLFormat defaultOverlayFormat();
235 static void setDefaultOverlayFormat(const QGLFormat& f);
236
237 static bool hasOpenGL();
238 static bool hasOpenGLOverlays();
239
240 void setVersion(int major, int minor);
241 int majorVersion() const;
242 int minorVersion() const;
243
244 enum OpenGLContextProfile {
245 NoProfile,
246 CoreProfile,
247 CompatibilityProfile
248 };
249
250 void setProfile(OpenGLContextProfile profile);
251 OpenGLContextProfile profile() const;
252
253 enum OpenGLVersionFlag {
254 OpenGL_Version_None = 0x00000000,
255 OpenGL_Version_1_1 = 0x00000001,
256 OpenGL_Version_1_2 = 0x00000002,
257 OpenGL_Version_1_3 = 0x00000004,
258 OpenGL_Version_1_4 = 0x00000008,
259 OpenGL_Version_1_5 = 0x00000010,
260 OpenGL_Version_2_0 = 0x00000020,
261 OpenGL_Version_2_1 = 0x00000040,
262 OpenGL_ES_Common_Version_1_0 = 0x00000080,
263 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
264 OpenGL_ES_Common_Version_1_1 = 0x00000200,
265 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
266 OpenGL_ES_Version_2_0 = 0x00000800,
267 OpenGL_Version_3_0 = 0x00001000,
268 OpenGL_Version_3_1 = 0x00002000,
269 OpenGL_Version_3_2 = 0x00004000,
270 OpenGL_Version_3_3 = 0x00008000,
271 OpenGL_Version_4_0 = 0x00010000
272 };
273 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
274
275 static OpenGLVersionFlags openGLVersionFlags();
276
277private:
278 QGLFormatPrivate *d;
279
280 void detach();
281
282 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
283 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
284#ifndef QT_NO_DEBUG_STREAM
285 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
286#endif
287};
288
289Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
290
291Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
292Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
293
294#ifndef QT_NO_DEBUG_STREAM
295Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
296#endif
297
298class Q_OPENGL_EXPORT QGLContext
299{
300 Q_DECLARE_PRIVATE(QGLContext)
301public:
302 QGLContext(const QGLFormat& format, QPaintDevice* device);
303 QGLContext(const QGLFormat& format);
304 virtual ~QGLContext();
305
306 virtual bool create(const QGLContext* shareContext = 0);
307 bool isValid() const;
308 bool isSharing() const;
309 void reset();
310
311 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
312
313 QGLFormat format() const;
314 QGLFormat requestedFormat() const;
315 void setFormat(const QGLFormat& format);
316
317 // ### Qt 5: return bools + maybe remove virtuals
318 virtual void makeCurrent();
319 virtual void doneCurrent();
320
321 virtual void swapBuffers() const;
322
323 enum BindOption {
324 NoBindOption = 0x0000,
325 InvertedYBindOption = 0x0001,
326 MipmapBindOption = 0x0002,
327 PremultipliedAlphaBindOption = 0x0004,
328 LinearFilteringBindOption = 0x0008,
329
330 MemoryManagedBindOption = 0x0010, // internal flag
331 CanFlipNativePixmapBindOption = 0x0020, // internal flag
332
333 DefaultBindOption = LinearFilteringBindOption
334 | InvertedYBindOption
335 | MipmapBindOption,
336 InternalBindOption = MemoryManagedBindOption
337 | PremultipliedAlphaBindOption
338 };
339 Q_DECLARE_FLAGS(BindOptions, BindOption)
340
341 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
342 BindOptions options);
343 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
344 BindOptions options);
345
346 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
347 GLint format = GL_RGBA);
348 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
349 GLint format = GL_RGBA);
350 GLuint bindTexture(const QString &fileName);
351
352 void deleteTexture(GLuint tx_id);
353
354 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
355 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
356
357#ifdef Q_MAC_COMPAT_GL_FUNCTIONS
358 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
359 QMacCompatGLint format = GL_RGBA);
360 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
361 QMacCompatGLint format = GL_RGBA);
362 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
363 BindOptions);
364 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
365 BindOptions);
366
367 void deleteTexture(QMacCompatGLuint tx_id);
368
369 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
370 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
371#endif
372
373 static void setTextureCacheLimit(int size);
374 static int textureCacheLimit();
375
376 void *getProcAddress(const QString &proc) const;
377 QPaintDevice* device() const;
378 QColor overlayTransparentColor() const;
379
380 static const QGLContext* currentContext();
381
382protected:
383 virtual bool chooseContext(const QGLContext* shareContext = 0);
384
385#if defined(Q_WS_WIN)
386 virtual int choosePixelFormat(void* pfd, HDC pdc);
387#endif
388#if defined(Q_WS_X11) && defined(QT_NO_EGL)
389 virtual void* tryVisual(const QGLFormat& f, int bufDepth = 1);
390 virtual void* chooseVisual();
391#endif
392#if defined(Q_WS_MAC)
393 virtual void* chooseMacVisual(GDHandle);
394#endif
395
396 bool deviceIsPixmap() const;
397 bool windowCreated() const;
398 void setWindowCreated(bool on);
399 bool initialized() const;
400 void setInitialized(bool on);
401 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
402
403 uint colorIndex(const QColor& c) const;
404 void setValid(bool valid);
405 void setDevice(QPaintDevice *pDev);
406
407protected:
408 static QGLContext* currentCtx;
409
410private:
411 QScopedPointer<QGLContextPrivate> d_ptr;
412
413 friend class QGLPixelBuffer;
414 friend class QGLPixelBufferPrivate;
415 friend class QGLWidget;
416 friend class QGLWidgetPrivate;
417 friend class QGLGlyphCache;
418 friend class QOpenGLPaintEngine;
419 friend class QOpenGLPaintEnginePrivate;
420 friend class QGL2PaintEngineEx;
421 friend class QGL2PaintEngineExPrivate;
422 friend class QGLEngineShaderManager;
423 friend class QGLWindowSurface;
424 friend class QGLPixmapData;
425 friend class QGLPixmapFilterBase;
426 friend class QGLTextureGlyphCache;
427 friend class QGLContextGroup;
428 friend class QGLSharedResourceGuard;
429 friend class QGLPixmapBlurFilter;
430 friend class QGLExtensions;
431 friend class QGLTexture;
432 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
433#ifdef Q_WS_MAC
434public:
435 void updatePaintDevice();
436private:
437 friend class QMacGLWindowChangeEvent;
438 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
439#endif
440 friend class QGLFramebufferObject;
441 friend class QGLFramebufferObjectPrivate;
442 friend class QGLFBOGLPaintDevice;
443 friend class QGLPaintDevice;
444 friend class QGLWidgetGLPaintDevice;
445 friend class QX11GLPixmapData;
446 friend class QX11GLSharedContexts;
447private:
448 Q_DISABLE_COPY(QGLContext)
449};
450
451Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
452
453class Q_OPENGL_EXPORT QGLWidget : public QWidget
454{
455 Q_OBJECT
456 Q_DECLARE_PRIVATE(QGLWidget)
457public:
458 explicit QGLWidget(QWidget* parent=0,
459 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
460 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
461 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
462 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
463 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
464#ifdef QT3_SUPPORT
465 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
466 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
467 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
468 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
469 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
470 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
471#endif
472 ~QGLWidget();
473
474 void qglColor(const QColor& c) const;
475 void qglClearColor(const QColor& c) const;
476
477 bool isValid() const;
478 bool isSharing() const;
479
480 // ### Qt 5: return bools
481 void makeCurrent();
482 void doneCurrent();
483
484 bool doubleBuffer() const;
485 void swapBuffers();
486
487 QGLFormat format() const;
488 void setFormat(const QGLFormat& format);
489
490 const QGLContext* context() const;
491 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
492 bool deleteOldContext = true);
493
494 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
495 QImage grabFrameBuffer(bool withAlpha = false);
496
497 void makeOverlayCurrent();
498 const QGLContext* overlayContext() const;
499
500 static QImage convertToGLFormat(const QImage& img);
501
502 void setMouseTracking(bool enable);
503
504 const QGLColormap & colormap() const;
505 void setColormap(const QGLColormap & map);
506
507 void renderText(int x, int y, const QString & str,
508 const QFont & fnt = QFont(), int listBase = 2000);
509 void renderText(double x, double y, double z, const QString & str,
510 const QFont & fnt = QFont(), int listBase = 2000);
511 QPaintEngine *paintEngine() const;
512
513 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
514 QGLContext::BindOptions options);
515 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
516 QGLContext::BindOptions options);
517
518 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
519 GLint format = GL_RGBA);
520 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
521 GLint format = GL_RGBA);
522
523 GLuint bindTexture(const QString &fileName);
524
525 void deleteTexture(GLuint tx_id);
526
527 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
528 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
529
530#ifdef Q_MAC_COMPAT_GL_FUNCTIONS
531 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
532 QMacCompatGLint format = GL_RGBA);
533 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
534 QMacCompatGLint format = GL_RGBA);
535 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
536 QGLContext::BindOptions);
537 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
538 QGLContext::BindOptions);
539
540 void deleteTexture(QMacCompatGLuint tx_id);
541
542 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
543 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
544#endif
545
546public Q_SLOTS:
547 virtual void updateGL();
548 virtual void updateOverlayGL();
549
550protected:
551 bool event(QEvent *);
552 virtual void initializeGL();
553 virtual void resizeGL(int w, int h);
554 virtual void paintGL();
555
556 virtual void initializeOverlayGL();
557 virtual void resizeOverlayGL(int w, int h);
558 virtual void paintOverlayGL();
559
560 void setAutoBufferSwap(bool on);
561 bool autoBufferSwap() const;
562
563 void paintEvent(QPaintEvent*);
564 void resizeEvent(QResizeEvent*);
565
566 virtual void glInit();
567 virtual void glDraw();
568 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
569
570private:
571 Q_DISABLE_COPY(QGLWidget)
572
573#ifdef Q_WS_MAC
574 friend class QMacGLWindowChangeEvent;
575#endif
576 friend class QGLDrawable;
577 friend class QGLPixelBuffer;
578 friend class QGLPixelBufferPrivate;
579 friend class QGLContext;
580 friend class QGLContextPrivate;
581 friend class QGLOverlayWidget;
582 friend class QOpenGLPaintEngine;
583 friend class QGLPaintDevice;
584 friend class QGLWidgetGLPaintDevice;
585};
586
587
588//
589// QGLFormat inline functions
590//
591
592inline bool QGLFormat::doubleBuffer() const
593{
594 return testOption(QGL::DoubleBuffer);
595}
596
597inline bool QGLFormat::depth() const
598{
599 return testOption(QGL::DepthBuffer);
600}
601
602inline bool QGLFormat::rgba() const
603{
604 return testOption(QGL::Rgba);
605}
606
607inline bool QGLFormat::alpha() const
608{
609 return testOption(QGL::AlphaChannel);
610}
611
612inline bool QGLFormat::accum() const
613{
614 return testOption(QGL::AccumBuffer);
615}
616
617inline bool QGLFormat::stencil() const
618{
619 return testOption(QGL::StencilBuffer);
620}
621
622inline bool QGLFormat::stereo() const
623{
624 return testOption(QGL::StereoBuffers);
625}
626
627inline bool QGLFormat::directRendering() const
628{
629 return testOption(QGL::DirectRendering);
630}
631
632inline bool QGLFormat::hasOverlay() const
633{
634 return testOption(QGL::HasOverlay);
635}
636
637inline bool QGLFormat::sampleBuffers() const
638{
639 return testOption(QGL::SampleBuffers);
640}
641
642QT_END_NAMESPACE
643
644QT_END_HEADER
645
646#endif // QGL_H
Note: See TracBrowser for help on using the repository browser.