Qt OpenGL绘图圈太慢了

时间:2015-12-19 23:27:56

标签: c++ performance qt opengl draw

我有一个带有OpenGl小部件的QT应用程序,我根据鼠标位置绘制一个红色圆圈。每次有鼠标移动事件时,都会调用paintGL。问题是它太慢,圆圈不像鼠标移动那么快,我怎样才能加快速度或者调用paintGl少得多?是否可以将 mouseMoveEvent 限制为例如每秒一次?

void GLWidget::paintGL()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawPixels(data.width(), data.height(), GL_RGBA, GL_UNSIGNED_BYTE, gldata.bits());

    if( drawCircle ) {
        int mousePosYInverse = 0;
        glColor3f(1, 0, 0);
        glLineWidth(2.0f);
        glBegin(GL_LINE_LOOP);
        for(int i=0; i < 360; i += 20) {
            mousePosYInverse = (-1)*(mousePosY-395); //otherwise circle goes up when mouse goes down
            float alpha = i*M_PI/180.0;
            float xPos = mousePosX + mouseRadius*sin(alpha);
            float yPos = mousePosYInverse + mouseRadius*cos(alpha);
            glVertex2f(xPos, yPos);
        }
        glEnd();
        drawCircle = false;
    }
    //qDebug("paintGL");
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    //Move circle if circle button was pressed until right mouse click
    drawCircle = true;
    this->mousePosX = event->pos().x();
    this->mousePosY = event->pos().y();
    updateGL();
}

0 个答案:

没有答案