2

我目前正在为我的程序使用 QtOpenGL。我的程序在 3D 空间中绘制点。这些点可以是一到几千之间的任何数字。我正在使用函数“glusphere”来绘制球体来表示每个点。有了这个,每个帧都被创建了,我需要使用 QTimer 将每个帧显示为动画。这个动画应该是fps控制的。比如fps=30,我会把t=1000/30给QTimer的setInterval(t)。不管fps=10还是30,我发现fps对我的程序没有太大的改变。我通过将 fps 设置为 0 来寻找问题,并通过使用 setInterval(0) 观察渲染时间需要多少。结果如下:

n = 球体数量

当 n=10 时:28ms => 大约 30ms

当 n=100 时:87ms => 大约 90ms

当 n=150 时:137ms => 大约 140ms

当 n=1000 时:598ms => 大约 600ms => 0.6sec

从上面的结果来看,每一帧的渲染时间随着球体的数量线性增加。以下是我的代码的一部分:

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
    setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
    scale=1.0;
    rot_x=0;
    rot_y=0;
    rot_z=0;
    trans_x=0;
    trans_y=0;
    trans_z=0;
    isloaded=false;
    isplayed=false;
    currentFrame=1;
    sphereNum=1;
    myTimer=new QTimer(this);
    //fps=60;
    myTimer->setInterval(0);

    //myTimer->setInterval();
    connect(myTimer, SIGNAL(timeout()), this, SLOT(timerEvent()));
}

void GLWidget::initializeGL()
{
    qglClearColor(QColor(Qt::black));
    glEnable(GL_DEPTH_TEST);
    srand((unsigned)time(NULL));
    //QGLFormat::setDoubleBuffer(true);
    //qDebug() << "initGL context: " << this->context();
}

void GLWidget::timerEvent()
{
    static QTime current_time, last_time;
    int elapsed_time;
    last_time=current_time;
    current_time=QTime::currentTime();
    elapsed_time=(current_time.second()*1000 + current_time.msec())-(last_time.second()*1000 + last_time.msec());
    //qDebug() << "Timer elapsed time: " << elapsed_time;
    if(isloaded && isplayed) {
        if(currentFrame<markerData.frameSize) currentFrame++;
        makeCurrent();
        updateGL();
    }
    if(currentFrame==markerData.frameSize) {
        isplayed=false;
        myTimer->stop();
        currentFrame=1;
    }
}

void GLWidget::drawSphere(GLfloat x, GLfloat y, GLfloat z, GLfloat radius)
{
    qglColor(Qt::yellow);
    glPushMatrix();
    glTranslatef(x, y, z);
    gluSphere(p, radius, 10, 10);
    //gluCylinder(p, 10, 10, 10, 4, 4);
    glPopMatrix();
}

void GLWidget::drawMarkers() {
    int markerIndex;
    glPushMatrix();
    /*for(int i=0; i<markerData.rowSize; i++) {
        markerIndex=(currentFrame-1)*markerData.rowSize+i;
        //qDebug() << markerIndex;
        drawSphere(markerData.markerSet[markerIndex].x, markerData.markerSet[markerIndex].y, markerData.markerSet[markerIndex].z, 10);
    }*/
    for(int i=0; i<sphereNum; i++) {
        markerIndex=rand()%1000;
        drawSphere(markerIndex, markerIndex, markerIndex, 10);
    }
    glPopMatrix();
}

void GLWidget::drawText(double x, double y, double z, QString txt)
{
    glDisable(GL_DEPTH_TEST);
    qglColor(Qt::white);
    renderText(x, y, z, txt, QFont("Arial", 12, QFont::Bold, false) );
    glEnable(GL_DEPTH_TEST);
}

void GLWidget::paintGL()
{
    static QTime current_time, last_time;
    int elapsed_time;
    float paint_fps;
    //if file data is loaded
    if(isloaded)
    {
        //calculate elapsed time drown 1 frame per second
        //elapsed time is millisecond
        last_time=current_time;
        current_time=QTime::currentTime();
        elapsed_time=(current_time.second()*1000 + current_time.msec())-(last_time.second()*1000 + last_time.msec());
        paint_fps=1000/elapsed_time;
        //qDebug() << "elapsed time: " << elapsed_time << "  fps: " << paint_fps;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0f,0.0f,-20.0f);
        glRotatef(30.0, 0.0, 1.0, 0.0);
        glRotatef(15.0, 1.0, 0.0, 0.0);
        glRotatef(rot_x, 1.0, 0.0, 0.0);
        glRotatef(rot_y, 0.0, 1.0, 0.0);
        glRotatef(rot_z, 0.0, 0.0, 1.0);
        glScalef(scale, scale, scale);
        glTranslatef(trans_x, trans_y, trans_z);

        //draw elapsed time(ms)
        glPushMatrix();
        drawText(100, -300, 100, QString::number(elapsed_time) + "ms"); 
        glPopMatrix();

        //draw 3-axis
        glBegin(GL_LINES);
        qglColor(Qt::red);
        glVertex3f(0, 0, 0);
        glVertex3f(3000, 0, 0);
        glEnd();
        glBegin(GL_LINES);
        qglColor(Qt::green);
        glVertex3f(0, 0, 0);
        glVertex3f(0, 3000, 0);
        glEnd();
        glBegin(GL_LINES);
        qglColor(Qt::blue);
        glVertex3f(0, 0, 0);
        glVertex3f(0, 0, 3000);
        glEnd();

        //draw spheres
        p=gluNewQuadric();
        drawMarkers();
        gluDeleteQuadric(p);

        //swapBuffers
        if(doubleBuffer()) {
            swapBuffers();
            makeCurrent();
        }
    }
}

澄清:

  • 每帧的渲染时间根据球体的数量而变化。
  • 球体的数量不应改变渲染时间
  • 即使它绘制了 1000 个球体,它也会花费半秒
  • 您认为问题在于使用胶球吗?
  • 如果有另一种方法来绘制球体,是否有可能超过 100 fps?

有人可以解释一下造成这种缓慢渲染的瓶颈在哪里吗?

4

0 回答 0