1

I have a QT application with an OpenGl widget where I am drawing a red circle based on the mouse position. Every time there is a mouse move event the paintGL is called. The problem is that it is too slow, the circle is not a quick as the mouse movement, how can I speed this up or call paintGl much less? Is it possible to limit the mouseMoveEvent to e.g. just once per second?

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();
}
Greeneco
  • 691
  • 2
  • 8
  • 23
  • While the code for drawing the circle looks inefficient, I would be much more worried about the `glDrawPixels()` call. You can easily confirm or refute this theory by removing the `glDrawPixels()` call, and seeing if it gets more responsive. – Reto Koradi Dec 20 '15 at 00:42
  • 1
    If you want to update less often, declare a boolean member variable (initialized to false) in your class, then instead of always calling updateGL() inside mouseMoveEvent(), do this: if (the_boolean == false) {the_boolean = true; QTimer::singleShot(100, this, SLOT(myTimeoutSlot());} ... then in your myTimeoutSlot() slot-function: {the_boolean = false; updateGL();} – Jeremy Friesner Dec 20 '15 at 04:46
  • @RetoKoradi as far as I know I have to redraw the image (my background) with glDrawPixels every time the circle moves, as long as I am not using a Frame Buffer Object (FBO) but I have commented it out once and it is not really faster, I have to make my circle draw much more efficient I guess but I still have not found anything that really improves the speed – Greeneco Dec 20 '15 at 17:06
  • 1
    I really doubt that drawing a polyline with 18 points would be a performance bottleneck, no matter how inefficient the code is. If you want an example of how to draw a circle with more current versions of OpenGL, I posted some code in an answer here: http://stackoverflow.com/questions/25279009/how-to-draw-a-circle-using-vbo-in-es2-0. – Reto Koradi Dec 20 '15 at 17:19
  • I have now used the suggestion from @JeremyFriesner , not it looks better, I may find the reason why it was so slow later on – Greeneco Dec 20 '15 at 18:37

0 Answers0