c++ - Qt OpenGL 更新非常慢

标签 c++ qt opengl animation qt5

我似乎无法让 Qt5 以有意义的速度更新我的场景。我的场景是一个 512x512 带纹理的矩形。我得到的速率大约是每秒 1 帧 (!)。

在我的构造函数中

   aTimer.setSingleShot(false);
   aTimer->setTimerType(Qt::PreciseTimer);
   connect(&aTimer,SIGNAL(timeout()),this,SLOT(animate()));
   aTimer.start(50);
   setAutoFillBackground(false);

void GLWidget::animate()
{
//Logic for every time step
updateGL();
}

有没有办法设置优先级?我做错了什么吗? Qt 中是否存在某种固有的更新限制,而且肯定不是 1 FPS 的数量级?我的理论是 Qt 忽略了我实际更新屏幕的调用。

我试过了

  1. 插入一个QCoreApplication::processEvents(); 但这没有帮助
  2. 在父部件上调用更新,并从父部件运行计时器
  3. 创建一个名为 animate() 的函数,该函数运行 forever{} 并从其中调用更新
  4. wigglywidget 示例似乎有效,这向我暗示 QT OpenGL 以某种方式折叠帧,忽略了我的更新调用。是否有启发式方法可以控制这一点?

重新创建的最少代码

(版本有点不同,仿照 wigglywidget 类,但有完全相同的问题)

git clone https://bitbucket.org/FunFarm/qtcapturesoftware.git

glwidget.h

/****************************************************************************/

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <QtOpenGL/qglshaderprogram.h>
#include <QTimer>
#include <math.h>
#include "time.h"
#include <assert.h>
#include <random>

class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();
    void addNoise();
protected:
    void initializeGL();
    void paintGL();
    void timerEvent(QTimerEvent *event);
    void resizeGL(int width, int height);
private:
    QBasicTimer timer;
    QPoint lastPos;
    GLuint textures[6];
    QVector<QVector2D> vertices;
    QVector<QVector2D> texCoords;
    QGLShaderProgram program1;
    int vertexAttr1;
    int vertexTexr1;
    //
    int heightGL;
    int widthGL;
    //
    GLubyte* noise;
    //
    QTimer* aTimer;
    //
};
#endif

glwidget.cpp

#include <QtWidgets>
#include <QtOpenGL>
#include "glwidget.h"


#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

GLWidget::GLWidget(QWidget *parent)
    : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
    setAutoFillBackground(false);
    aTimer = new QTimer();
    timer.start(30, this); // 30 fps?
}
void GLWidget::timerEvent(QTimerEvent *event)
{
    addNoise();
    update();// Doesn't matter which update function I call, this is the one from the wigglywidget example
}
 GLWidget::~GLWidget(){}
void GLWidget::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    QGLShader *vshader1 = new QGLShader(QGLShader::Vertex, this);
    const char *vsrc1 =
            "attribute vec2 coord2d;   \n"
            "attribute mediump vec4 texCoord;\n"
            "varying mediump vec4 texc;\n"
            "void main()                  \n"
            "{                            \n"
            "   gl_Position = vec4(coord2d, 0.0, 1.0); \n"
            "   texc = texCoord;\n"
            "}                          \n";
    vshader1->compileSourceCode(vsrc1);

    QGLShader *fshader1 = new QGLShader(QGLShader::Fragment, this);
    const char *fsrc1 =
            "uniform sampler2D texture;\n"
            "varying mediump vec4 texc;\n"
            "void main(void)\n"
            "{\n"
            "    gl_FragColor = texture2D(texture, texc.st);\n"
            "}\n"                                                  ;
    fshader1->compileSourceCode(fsrc1);

    program1.addShader(vshader1);
    program1.addShader(fshader1);
    program1.link();
    vertexAttr1 = program1.attributeLocation( "coord2d");
    vertexTexr1 = program1.attributeLocation( "texCoord");

    // Create the vertex buffer.
    vertices.clear();
    float u=1;
#define AVEC -u,u
#define BVEC -u,-u
#define CVEC u,u
#define DVEC u,-u
    vertices << QVector2D(AVEC);
    vertices << QVector2D(BVEC);
    vertices << QVector2D(CVEC);
    vertices << QVector2D(BVEC);
    vertices << QVector2D(DVEC);
    vertices << QVector2D(CVEC);
    // Create the texture vertex buffer
#define TAVEC 0,1
#define TBVEC 0,0
#define TCVEC 1,1
#define TDVEC 1,0
    texCoords << QVector2D(TAVEC);
    texCoords << QVector2D(TBVEC);
    texCoords << QVector2D(TCVEC);
    texCoords << QVector2D(TBVEC);
    texCoords << QVector2D(TDVEC);
    texCoords << QVector2D(TCVEC);
    QPixmap aMap(":/images/testmap.jpg");
    assert(aMap.width());
    heightGL = aMap.height();
    widthGL = aMap.width();
    textures[0] =bindTexture(aMap,GL_TEXTURE_2D,GL_LUMINANCE);
    noise = (GLubyte*) calloc(1*widthGL*heightGL,sizeof(GLubyte));//GL_RGB8
    memset(noise,100,1*widthGL*heightGL);
    //
}
void GLWidget::addNoise()
{
    std::default_random_engine e((unsigned int)(time(0)));
    GLubyte c = e()%256;
    assert(noise);
    for (int i = 0; i<heightGL;i++)
    {
        for(int j =0;j<widthGL;j++)
            noise[i*widthGL+j]= c;
    }
}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    //
    //

    program1.bind();
    program1.setUniformValue("texture", 0);
    program1.enableAttributeArray(vertexAttr1);
    program1.enableAttributeArray(vertexTexr1);
    program1.setAttributeArray(vertexAttr1, vertices.constData());
    program1.setAttributeArray(vertexTexr1, texCoords.constData());
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
                    widthGL,heightGL,GL_LUMINANCE,GL_UNSIGNED_BYTE, //lets hope these are correct
                    noise);
    glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    program1.disableAttributeArray(vertexTexr1);
    program1.disableAttributeArray(vertexAttr1);
    program1.release();
}

void GLWidget::resizeGL(int width, int height)
{
    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);

}

main.cpp

/****************************************************************************/
#include <QApplication>
#include <QDesktopWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //cameraView cV;
    //cV.show();
    GLWidget mW;
    mW.show();
    return a.exec();
}

test.pro

HEADERS       = glwidget.h \
    cameraview.h
SOURCES       = glwidget.cpp \
                main.cpp \
    cameraview.cpp
QT           += opengl widgets

CONFIG += console
CONFIG += c++11

# install
target.path = $$[QT_INSTALL_EXAMPLES]/qt_OpenGL_3x/02_First_Triangle
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS 02-first-triangle.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/qt_OpenGL_3x/02_First_Triangle
INSTALLS += target sources


simulator: warning(This example might not fully work on Simulator platform)

FORMS += \
    cameraview.ui

RESOURCES += \
    images.qrc

OTHER_FILES +=

最佳答案

我克隆了存储库并对代码进行了一些更改以帮助您调试问题。首先,在 paintGL() 的开头用 std::cout 打印一条消息:

void GLWidget::paintGL()
{
   static int xyz = 0;
   std::cout << "PaintGL begin " << xyz << std::endl;

paintGL 末尾的另一条消息。之后,增加计数器变量:

   program1.release();

   std::cout << "PaintGL end " << xyz << std::endl;
   xyz++;
}

这种方法清楚地表明 paintGL() 由于 2 秒内打印到控制台的消息量而每秒被调用近 30 次:

PaintGL begin 0
PaintGL end 0
PaintGL begin 1
PaintGL end 1
...
...
PaintGL begin 60
PaintGL end 60
PaintGL begin 61
PaintGL end 61

所以定时器有问题的理论可以抛开。 计时器没问题,您确实每秒为窗口绘制 30 个计时器左右。

问题可能出在您代码的其他地方。由于您只共享了存储库中的一部分应用程序,恐怕我无法进一步帮助您。

编辑:

我注意到在 addNoise() 中有一个随机数生成器。如果您希望根据其他方法生成的数字在每次 paintGL() 调用时更改绘图,您将会感到失望。

当前实现的数字生成机制对毫秒变化不敏感,因此同一秒内所有对 addNoise() 的调用都会生成相同的数字,进而绘制相同的数字整整一秒钟都在你的 window 上。这就解释了为什么绘图每秒仅更改一次。

要解决此问题,我建议您查看:

关于c++ - Qt OpenGL 更新非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823364/

相关文章:

c++ - OpenGL 和 Xcode 颜色问题

c++ - 确定 C++ 类是否具有私有(private)析构函数

c++ - 如何解决编译器枚举重新声明冲突

visual-studio - 如何使用 Visual Studio 2017 安装程序安装调试工具?

c++ - 仅使用 600 MB 内存时调整 QByteArray 的大小会抛出 std::bad_alloc

opengl - RenderMonkey 在 GLSL 的 TEXCOORD 流映射中是否有错误?

c++ - Stockfish 12 源代码 : Templates replacing function parameters

c++ - 为什么我不能将模板函数指针传递给可变参数函数?

c++ - 如果 arduino-ide 之前没有打开连接,则无法使用 QSerialPort 与 arduino 通信

java - LWJGL/OpenGL 滞后绘制立方体阵列