home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qgl.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  12KB  |  511 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qgl.h   3.0.0   edited Oct 3 08:06 $
  3. **
  4. ** Definition of OpenGL classes for Qt
  5. **
  6. ** Created : 970112
  7. **
  8. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the opengl module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition licenses may use this
  22. ** file in accordance with the Qt Commercial License Agreement provided
  23. ** with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #ifndef QGL_H
  39. #define QGL_H
  40.  
  41. #ifndef QT_H
  42. #include <qwidget.h>
  43. #include "qglcolormap.h"
  44. #endif // QT_H
  45.  
  46. #if !defined( QT_MODULE_OPENGL ) || defined( QT_LICENSE_PROFESSIONAL )
  47. #define QM_EXPORT_OPENGL
  48. #else
  49. #define QM_EXPORT_OPENGL Q_EXPORT
  50. #endif
  51.  
  52. #ifndef QT_NO_COMPAT
  53. #define QGL_VERSION    450
  54. #define QGL_VERSION_STR    "4.5"
  55. QM_EXPORT_OPENGL inline const char *qGLVersion() {
  56.     qObsolete( 0, "qGLVersion", "qVersion" );
  57.     return QGL_VERSION_STR;
  58. }
  59. #endif
  60.  
  61. #if defined(Q_WS_WIN)
  62. # include <qt_windows.h>
  63. #endif
  64.  
  65. #if defined(Q_WS_MAC)
  66. #ifndef QMAC_OPENGL_DOUBLEBUFFER
  67. #define QMAC_OPENGL_DOUBLEBUFFER
  68. #endif
  69. # include <OpenGL/gl.h>
  70. # include <OpenGL/glu.h>
  71. #else
  72. # include <GL/gl.h>
  73. # include <GL/glu.h>
  74. #endif
  75.  
  76. #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
  77. class QGLCmap;
  78. #endif
  79.  
  80. class QPixmap;
  81. #if defined(Q_WS_X11)
  82. class QGLOverlayWidget;
  83. #endif
  84.  
  85. // Namespace class:
  86. class QM_EXPORT_OPENGL QGL
  87. {
  88. public:
  89.     enum FormatOption {
  90.     DoubleBuffer        = 0x0001,
  91.     DepthBuffer        = 0x0002,
  92.     Rgba            = 0x0004,
  93.     AlphaChannel        = 0x0008,
  94.     AccumBuffer        = 0x0010,
  95.     StencilBuffer        = 0x0020,
  96.     StereoBuffers        = 0x0040,
  97.     DirectRendering        = 0x0080,
  98.     HasOverlay        = 0x0100,
  99.     SingleBuffer            = DoubleBuffer  << 16,
  100.     NoDepthBuffer           = DepthBuffer   << 16,
  101.     ColorIndex              = Rgba          << 16,
  102.     NoAlphaChannel          = AlphaChannel  << 16,
  103.     NoAccumBuffer           = AccumBuffer   << 16,
  104.     NoStencilBuffer         = StencilBuffer << 16,
  105.     NoStereoBuffers         = StereoBuffers << 16,
  106.     IndirectRendering       = DirectRendering << 16,
  107.     NoOverlay           = HasOverlay << 16
  108.     };
  109. };
  110.  
  111.  
  112.  
  113. class QM_EXPORT_OPENGL QGLFormat : public QGL
  114. {
  115. public:
  116.     QGLFormat();
  117.     QGLFormat( int options, int plane = 0 );
  118.  
  119.     bool            doubleBuffer() const;
  120.     void            setDoubleBuffer( bool enable );
  121.     bool            depth() const;
  122.     void            setDepth( bool enable );
  123.     bool            rgba() const;
  124.     void            setRgba( bool enable );
  125.     bool            alpha() const;
  126.     void            setAlpha( bool enable );
  127.     bool            accum() const;
  128.     void            setAccum( bool enable );
  129.     bool            stencil() const;
  130.     void            setStencil( bool enable );
  131.     bool            stereo() const;
  132.     void            setStereo( bool enable );
  133.     bool            directRendering() const;
  134.     void            setDirectRendering( bool enable );
  135.     bool            hasOverlay() const;
  136.     void            setOverlay( bool enable );
  137.  
  138.     int            plane() const;
  139.     void        setPlane( int plane );
  140.  
  141.     void        setOption( FormatOption opt );
  142.     bool        testOption( FormatOption opt ) const;
  143.  
  144.     static QGLFormat    defaultFormat();
  145.     static void        setDefaultFormat( const QGLFormat& f );
  146.  
  147.     static QGLFormat    defaultOverlayFormat();
  148.     static void        setDefaultOverlayFormat( const QGLFormat& f );
  149.  
  150.     static bool        hasOpenGL();
  151.     static bool        hasOpenGLOverlays();
  152.  
  153.     friend QM_EXPORT_OPENGL bool operator==( const QGLFormat&, const QGLFormat& );
  154.     friend QM_EXPORT_OPENGL bool operator!=( const QGLFormat&, const QGLFormat& );
  155.  
  156. private:
  157.     uint opts;
  158.     int pln;
  159. };
  160.  
  161.  
  162. QM_EXPORT_OPENGL bool operator==( const QGLFormat&, const QGLFormat& );
  163. QM_EXPORT_OPENGL bool operator!=( const QGLFormat&, const QGLFormat& );
  164.  
  165. class QM_EXPORT_OPENGL QGLContext : public QGL
  166. {
  167. public:
  168.     QGLContext( const QGLFormat& format, QPaintDevice* device );
  169.     virtual ~QGLContext();
  170.  
  171.     virtual bool    create( const QGLContext* shareContext = 0 );
  172.     bool        isValid() const;
  173.     bool        isSharing() const;
  174.     virtual void    reset();
  175.  
  176.     QGLFormat        format() const;
  177.     QGLFormat        requestedFormat() const;
  178.     virtual void    setFormat( const QGLFormat& format );
  179.     
  180.     virtual void    makeCurrent();
  181.     virtual void    swapBuffers() const;
  182.  
  183.     QPaintDevice*    device() const;
  184.  
  185.     QColor        overlayTransparentColor() const;
  186.  
  187.     static const QGLContext*    currentContext();
  188.         
  189. protected:
  190.     virtual bool    chooseContext( const QGLContext* shareContext = 0 );
  191.     virtual void    doneCurrent();
  192.  
  193. #if defined(Q_WS_WIN)
  194.     virtual int        choosePixelFormat( void* pfd, HDC pdc );
  195. #elif defined(Q_WS_X11)
  196.     virtual void*    tryVisual( const QGLFormat& f, int bufDepth = 1 );
  197.     virtual void*    chooseVisual();
  198. #elif defined(Q_WS_MAC)
  199.     virtual void*    chooseMacVisual(GDHandle);
  200. #endif
  201.  
  202.     bool        deviceIsPixmap() const;
  203.     bool        windowCreated() const;
  204.     void        setWindowCreated( bool on );
  205.     bool        initialized() const;
  206.     void        setInitialized( bool on );
  207.  
  208.     uint        colorIndex( const QColor& c ) const;
  209.  
  210. protected:
  211. #if  defined(Q_WS_WIN)
  212.     HGLRC        rc;
  213.     HDC            dc;
  214.     WId    win;
  215.     int            pixelFormatId;
  216.     QGLCmap*        cmap;
  217. #elif defined(Q_WS_X11)
  218.     void*        vi;
  219.     void*        cx;
  220.     Q_UINT32        gpm;
  221. #elif defined(Q_WS_MAC)
  222.     void*               vi;
  223.     void*        cx;
  224. #endif
  225.  
  226.     QGLFormat        glFormat;
  227.     QGLFormat        reqFormat;
  228.  
  229. private:
  230.     class Private {
  231.     public:
  232.     bool        valid;
  233.     bool        sharing;
  234.     bool        initDone;
  235.     bool        crWin;
  236.     QPaintDevice*    paintDevice;
  237.     QColor        transpColor;
  238.     };
  239.     Private* d;
  240.     static QGLContext*    currentCtx;
  241.  
  242.     friend class QGLWidget;
  243. #ifdef Q_WS_MAC
  244.     void fixBufferRect();
  245. #endif
  246.  
  247. private:    // Disabled copy constructor and operator=
  248.     QGLContext() {}
  249.     QGLContext( const QGLContext& ) {}
  250.     QGLContext&        operator=( const QGLContext& ) { return *this; }
  251. };
  252.  
  253.  
  254.  
  255.  
  256. class QM_EXPORT_OPENGL QGLWidget : public QWidget, public QGL
  257. {
  258.     Q_OBJECT
  259. public:
  260.     QGLWidget( QWidget* parent=0, const char* name=0,
  261.            const QGLWidget* shareWidget = 0, WFlags f=0 );
  262.     QGLWidget( const QGLFormat& format, QWidget* parent=0, const char* name=0,
  263.            const QGLWidget* shareWidget = 0, WFlags f=0 );
  264.     ~QGLWidget();
  265.  
  266.     void        qglColor( const QColor& c ) const;
  267.     void        qglClearColor( const QColor& c ) const;
  268.     
  269.     bool        isValid() const;
  270.     bool        isSharing() const;
  271.     virtual void    makeCurrent();
  272.  
  273.     bool        doubleBuffer() const;
  274.     virtual void    swapBuffers();
  275.  
  276.     QGLFormat        format() const;
  277. #ifndef Q_QDOC
  278.     virtual void    setFormat( const QGLFormat& format );
  279. #endif
  280.  
  281.     const QGLContext*    context() const;
  282. #ifndef Q_QDOC
  283.     virtual void    setContext( QGLContext* context,
  284.                     const QGLContext* shareContext = 0,
  285.                     bool deleteOldContext = TRUE );
  286. #endif
  287.  
  288.     virtual QPixmap    renderPixmap( int w = 0, int h = 0,
  289.                       bool useContext = FALSE );
  290.     virtual QImage    grabFrameBuffer( bool withAlpha = FALSE );
  291.  
  292.     virtual void    makeOverlayCurrent();
  293.     const QGLContext*    overlayContext() const;
  294.  
  295.     static QImage    convertToGLFormat( const QImage& img );
  296.  
  297.     void        setMouseTracking( bool enable );
  298.     virtual void     reparent( QWidget* parent, WFlags f, const QPoint& p,
  299.                   bool showIt = FALSE );
  300.     
  301.     const QGLColormap & colormap() const;
  302.     void                setColormap( const QGLColormap & map );
  303.     
  304. public slots:
  305.     virtual void    updateGL();
  306.     virtual void    updateOverlayGL();
  307.  
  308. protected:
  309.     virtual void    initializeGL();
  310.     virtual void    resizeGL( int w, int h );
  311.     virtual void    paintGL();
  312.  
  313.     virtual void    initializeOverlayGL();
  314.     virtual void    resizeOverlayGL( int w, int h );
  315.     virtual void    paintOverlayGL();
  316.  
  317.     void        setAutoBufferSwap( bool on );
  318.     bool        autoBufferSwap() const;
  319.  
  320.     void        paintEvent( QPaintEvent* );
  321.     void        resizeEvent( QResizeEvent* );
  322.  
  323.     virtual void    glInit();
  324.     virtual void    glDraw();
  325.     
  326. private:
  327.     void                cleanupColormaps();
  328.     void        init( const QGLFormat& fmt,
  329.                   const QGLWidget* shareWidget );
  330.     bool        renderCxPm( QPixmap* pm );
  331.     QGLContext*        glcx;
  332.     bool        autoSwap;
  333.     
  334.     QGLColormap         cmap;
  335.     
  336. #if  defined(Q_WS_WIN)
  337.     QGLContext*        olcx;
  338. #elif defined(Q_WS_X11)
  339.     QGLOverlayWidget*    olw;
  340.     friend class QGLOverlayWidget;
  341. #elif defined(Q_WS_MAC)
  342.     QGLContext*        olcx;
  343. #endif
  344.  
  345. private:    // Disabled copy constructor and operator=
  346. #if defined(Q_DISABLE_COPY)
  347.     QGLWidget( const QGLWidget& );
  348.     QGLWidget&        operator=( const QGLWidget& );
  349. #endif
  350.  
  351. #ifdef Q_WS_MAC
  352. private:
  353. #ifdef QMAC_OPENGL_DOUBLEBUFFER
  354.     QPaintDevice *gl_pix;
  355.     QGLFormat req_format;
  356. #endif
  357.     friend class QWidget;
  358.     void fixReparented();
  359.     void fixBufferRect();
  360. #endif
  361. };
  362.  
  363.  
  364. //
  365. // QGLFormat inline functions
  366. //
  367.  
  368. inline bool QGLFormat::doubleBuffer() const
  369. {
  370.     return testOption( DoubleBuffer );
  371. }
  372.  
  373. inline bool QGLFormat::depth() const
  374. {
  375.     return testOption( DepthBuffer );
  376. }
  377.  
  378. inline bool QGLFormat::rgba() const
  379. {
  380.     return testOption( Rgba );
  381. }
  382.  
  383. inline bool QGLFormat::alpha() const
  384. {
  385.     return testOption( AlphaChannel );
  386. }
  387.  
  388. inline bool QGLFormat::accum() const
  389. {
  390.     return testOption( AccumBuffer );
  391. }
  392.  
  393. inline bool QGLFormat::stencil() const
  394. {
  395.     return testOption( StencilBuffer );
  396. }
  397.  
  398. inline bool QGLFormat::stereo() const
  399. {
  400.     return testOption( StereoBuffers );
  401. }
  402.  
  403. inline bool QGLFormat::directRendering() const
  404. {
  405.     return testOption( DirectRendering );
  406. }
  407.  
  408. inline bool QGLFormat::hasOverlay() const
  409. {
  410.     return testOption( HasOverlay );
  411. }
  412.  
  413. //
  414. // QGLContext inline functions
  415. //
  416.  
  417. inline bool QGLContext::isValid() const
  418. {
  419.     return d->valid;
  420. }
  421.  
  422. inline bool QGLContext::isSharing() const
  423. {
  424.     return d->sharing;
  425. }
  426.  
  427. inline QGLFormat QGLContext::format() const
  428. {
  429.     return glFormat;
  430. }
  431.  
  432. inline QGLFormat QGLContext::requestedFormat() const
  433. {
  434.     return reqFormat;
  435. }
  436.  
  437. inline QPaintDevice* QGLContext::device() const
  438. {
  439.     return d->paintDevice;
  440. }
  441.  
  442. inline bool QGLContext::deviceIsPixmap() const
  443. {
  444.     return d->paintDevice->devType() == QInternal::Pixmap;
  445. }
  446.  
  447.  
  448. inline bool QGLContext::windowCreated() const
  449. {
  450.     return d->crWin;
  451. }
  452.  
  453.  
  454. inline void QGLContext::setWindowCreated( bool on )
  455. {
  456.     d->crWin = on;
  457. }
  458.  
  459. inline bool QGLContext::initialized() const
  460. {
  461.     return d->initDone;
  462. }
  463.  
  464. inline void QGLContext::setInitialized( bool on )
  465. {
  466.     d->initDone = on;
  467. }
  468.  
  469. inline const QGLContext* QGLContext::currentContext()
  470. {
  471.     return currentCtx;
  472. }
  473.  
  474. //
  475. // QGLWidget inline functions
  476. //
  477.  
  478. inline QGLFormat QGLWidget::format() const
  479. {
  480.     return glcx->format();
  481. }
  482.  
  483. inline const QGLContext *QGLWidget::context() const
  484. {
  485.     return glcx;
  486. }
  487.  
  488. inline bool QGLWidget::doubleBuffer() const
  489. {
  490.     return glcx->format().doubleBuffer();
  491. }
  492.  
  493. inline void QGLWidget::setAutoBufferSwap( bool on )
  494. {
  495.     autoSwap = on;
  496. }
  497.  
  498. inline bool QGLWidget::autoBufferSwap() const
  499. {
  500.     return autoSwap;
  501. }
  502.  
  503. #ifdef Q_WS_MAC
  504. inline void QGLWidget::fixBufferRect()
  505. {
  506.     glcx->fixBufferRect();
  507. }
  508. #endif
  509.  
  510. #endif
  511.