Important: Please read the Qt Code of Conduct -

OpenGL and Threads

  • Hi

    I'm developing a software that displays a complex object made by faces (12). Each face is calculated using some CPU intensive calculations, and the face with higher resolution has 16793604 vertexs.

    For the viewer I'm using libQGLViewer that enables zoom and rotation, and I'm offloading the CPU intensive calculations for a working thread. The problem is that I can't create a OpenGL display list inside that working thread, and if I construct it in the main thread, it takes around 2 seconds for create a display list for each face, and it freezes the user interaction (as obvious).

    There is any way of create that display lists in the working thread ? I have read some documentation, and some forum posts, and I think it can solved by creating a new context for each working thread, and then share that context with the main thread, but until now I couldn't make it working.

    QGLContext* newContext = new QGLContext(QGLFormat::defaultFormat());

    I tried this code, but I get seg. fault when creating a display list.

    The class that use this code needs to inherit from some OpenGL widget ?
    There are some code missing ?

    Thanks in advice

  • Did you call
    ? You always have to do this before using gl*() commands...

    Apart from that, maybe these links will help you:

  • I tried the makeCurrent but I got error.

    As suggested by pcpower in irc (thanks), I have changed my code, and now instead of display lists I'm using Virtual Buffer Objects, with QGLBuffer. If my code is not threaded everything is working fine, but when I run threads, the buffers are created by threads (without error), but nothing is displayed - I think its because they have different contexts. Here's a sample of my code:

    Viewer::Viewer(QWidget *parent) : QGLViewer(parent) {}

    void Viewer::draw()
    for(int i=0; i<facesToDisplay; i++)

    void Viewer::loadNeededFaces()
    for(int i=0; i<totalFaces; i++)
    QtConcurrent::run(this, &Viewer;::loadFace, i);

    void Viewer::loadFace(int faceNumber)
    Face* face = new Face(faceNumber);

    void Face::createBuffer()
    vertexBuffer = new QGLBuffer(QGLBuffer::VertexBuffer);
    vertexBuffer->allocate(vertexs, 3totalVerticessizeof(GLfloat));

    void Face::draw()
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
    glDrawElements(GL_LINE_STRIP, 5, GL_UNSIGNED_INT, 0);
    glDrawArrays(GL_QUAD_STRIP, 0, totalVertices);

    The idea is to have a main GUI thread (instance of Viewer) to display the visible faces, and forecast the faces that will needed soon, to preload them using working threads, so when the user rotates the object, the faces that will appear are already precalculated (and possible in the GPU), so the loading will be smooth.

    The code isn't 100% correct, its just a sample of my code. Anyone can give me a hint on how to put that code working ? I think I need to get some context and then use makeCurrent. I have already tried to pass to the Face instance, a pointer to the Viewer, and then call the makeCurrent on that pointer, but with no success - "QGLContext::makeCurrent(): Failed."

Log in to reply