簡體   English   中英

Qt OpenGL更新速度極慢

[英]Qt OpenGL Update Unusably Slow

我似乎無法讓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{}運行forever{}並從中調用update
  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 +=

我克隆了存儲庫並對代碼進行了一些更改,以幫助您調試問題。 首先,使用std :: coutpaintGL()的開頭打印一條消息:

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++;
}

這種方法清楚地表明,由於在2秒內打印到控制台的消息量, paintGL()每秒被調用近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()所有調用將生成相同的數字,這反過來將在整個第二個窗口上繪制相同的內容。 這就解釋了為什么圖紙每秒只改變一次。

為了解決這個問題,我建議看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM