Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OpenGL functions on a QT OpenGL Widget?

I'm starting off QT 4.6's example "OpenGL - 2D painting"
It uses a subclass of QGLWidget, and makes painting operations with the class QPainter.
I'd like to know how to do drawing directly with OpenGL functions on the OpenGL Widget.

like image 316
Petruza Avatar asked Jun 27 '10 17:06

Petruza


1 Answers

If you use the widget as described in its manual, you can just use the OpenGL functions as usual. For example (copied from the manual):

class MyGLDrawer : public QGLWidget
 {
     Q_OBJECT        // must include this if you use Qt signals/slots

 public:
     MyGLDrawer(QWidget *parent)
         : QGLWidget(parent) {}

 protected:

     void initializeGL()
     {
         // Set up the rendering context, define display lists etc.:
         ...
         glClearColor(0.0, 0.0, 0.0, 0.0);
         glEnable(GL_DEPTH_TEST);
         ...
     }

     void resizeGL(int w, int h)
     {
         // setup viewport, projection etc.:
         glViewport(0, 0, (GLint)w, (GLint)h);
         ...
         glFrustum(...);
         ...
     }

     void paintGL()
     {
         // draw the scene:
         ...
         glRotatef(...);
         glMaterialfv(...);
         glBegin(GL_QUADS);
         glVertex3f(...);
         glVertex3f(...);
         ...
         glEnd();
         ...
     }

 };
like image 106
Thomas Avatar answered Nov 03 '22 01:11

Thomas