Search code examples
c++qtopenglqt4qglwidget

QGLWidget refuses to draw anything


I've looked at a ton of articles and SO questions about OpenGL not drawing, common mistakes, etc. This one is stumping me.

I've tried several different settings for glOrtho, different vertex positions, colors, etc., all to no avail.

I can confirm the OpenGL state is valid because the clear color is purple in the code (meaning the window is purple). gDEBugger is also confirming frames are being updated (so is Fraps).

Here is the code. Lines marked as "didn't help" were not there originally, and were things that I tried and failed.

QTWindow::QTWindow( )
{
    // Enable mouse tracking
    this->setMouseTracking(true);
}

void QTWindow::initializeGL()
{
    // DEBUG
    debug("Init'ing GL");
    this->makeCurrent(); ///< Didn't help
    this->resizeGL(0, 0); ///< Didn't help
    glDisable(GL_CULL_FACE); ///< Didn't help
    glClearColor(1, 0, 1, 0);
}

void QTWindow::paintGL()
{
    // DEBUG
    debug("Painting GL");
    this->makeCurrent(); ///< Didn't help
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0,1,1);
    glBegin(GL_TRIANGLES);
    glVertex2f(500,100);
    glVertex2f(100,500);
    glVertex2f(0,0);
    glEnd();
    this->swapBuffers(); ///< Didn't help
}

void QTWindow::resizeGL(int width, int height)
{
    // DEBUG
    debug("Resizing GL");
    this->makeCurrent(); ///< Didn't help
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1000, 0, 1000, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

The triangle is not being displayed at all, even with culling turned off. However, all three debug logs are called exactly how they should be.

What am I missing?


Solution

  • The issue ended up being versions. The version string returned with glGetString(GL_VERSION) indicated 4.2 compatibility context was being used.

    Since the triangle calls in the paintGL method were removed in 3.1 (if I recall correctly), it was obvious why they weren't drawing anything. Further, no errors were being thrown because it was in compat. mode.

    Because I couldn't get the version down below 3.0 on the QGLWidget (due to the fact that QT requires 2.1, as I was informed on another message board), I set the version to 3.0 and tried using some 3.0 drawing calls and it ended up working.