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();
}