how to make updateGL() realtime in QT

前端 未结 1 671
梦毁少年i
梦毁少年i 2021-02-08 11:58

I wonder if calling updateGL() in fixed timer intervals may slow down the rendering process. So, I want to try making the render real time. I do not the function to make it exec

1条回答
  •  星月不相逢
    2021-02-08 12:12

    Yes, you can make it realtime, with no tearing (e.g., redrawing at exactly 60fps, the refresh rate of your monitor).

    For this, you have to enable V_sync, and using a QTimer timer; of interval 0. Enabling V_sync will make the automatically called swapBuffers() command to CPU-wait for the vertical refresh signal from your monitor. Then, the timer will actually be synchronized with the monitor refresh rate.

    A related info can be found here: http://blog.qt.digia.com/blog/2010/12/02/velvet-and-the-qml-scene-graph/ . Note the QGLFormat::setSwapInterval(1) to enable V_sync, if not done automatically by your driver settings:

    class MyGLWidget: public QGLWidget
    {
        // ...
    private:
        QTimer timer;
    }
    
    QGLFormat desiredFormat()
    {
        QGLFormat fmt;
        fmt.setSwapInterval(1);
        return fmt;
    }
    
    MyGLWidget::MyGLWidget() :
        QGLWidget(desiredFormat())
    {
        // Configure the timer
        connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));
        if(format().swapInterval() == -1)
        {
            // V_blank synchronization not available (tearing likely to happen)
            qDebug("Swap Buffers at v_blank not available: refresh at approx 60fps.");
            timer.setInterval(17);
        }
        else
        {
            // V_blank synchronization available
            timer.setInterval(0);
        }
        timer.start();
    }
    

    In parallel, you can run a QElapsedTimer to measure how much time has passed between two drawing (normally approx 16.6ms), and use this information to update your scene, for instance.

    0 讨论(0)
提交回复
热议问题