PDA

View Full Version : Display image with qt & opengl, timing accuracy and vsync issues, c++



jonathan.levanon
2nd February 2017, 14:19
I'm building a module that is supposed to display images at a certain rate (not pre defined, but not very high - 10Hz max for the swapping of images).

From my research I got to the conclusion that QGLWidget is the right tool for this task, after enabling vsync with openGL calls(SwapInterval family).

Yet, I am not sure how to actually implement the swapping mechanisem - should I use a timer? If I set a timer for 333.3 ms(3 Hz), when the refresh rate is 60 Hz (16.67 per cycle, thus the timer is 20 cycles), and I be sure that timing will be fine? And if the rate should be 9Hz, I need to set the timer for 100+16.67 because this is the best I can get? And if a timer is ok, should I just call paintGL() when it sends me the timeout event?
Also, do you have any reference to actually displaying an image on a qglwidget? Everything I've tried wan't successful, for example this code:

void imageDisplay::showImage()
{
glEnable(GL_TEXTURE_2D);
drawTexture(QRect(0,0,1,1),texture,GL_TEXTURE_2D);
glDisable(GL_TEXTURE_2D);

}

void imageDisplay::loadImage(QImage &img)
{
width_img=img.width();
height_img=img.height();

glEnable(GL_TEXTURE_2D); // Enable texturing
glBindTexture(GL_TEXTURE_2D, texture); // Set as the current texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glDisable(GL_TEXTURE_2D);
updateGL();
return;
}

only draws a small part of my img on the bottom of the widget

Thanks

Lesiok
2nd February 2017, 17:32
Maybe use QTimer with 100 ms interval - it is 10 Hz.