PDA

View Full Version : QGLWidget + (web)camera



samvdj
23rd September 2013, 12:04
Hi all,

Been searching the forums and online tutorials to find any info on this, but no luck yet.

Can anyone of you guide me in the right direction as to how I should organize my program if I want
to combine live images coming from a high-speed camera with a QGLWidget in my Qt GUI? I can succesfully
extract the images coming from the camera (using Pleora GEV drivers) and I can display the images using a
regular


ui.graphicsView_2D->setScene(scene);

routine, but I am clueless as to how I should build the program when I want to use a QGLWidget... how do I
let the main GL (paint, initialize,...) loop run...

Many thanks in advance

wagmare
23rd September 2013, 12:38
u need to use textures in opengl to render the video .
consider u have a rgba_array(1024*1024*4) of raw video data .
just map to a opengl texture

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0,
GL_BGR_EXT, GL_UNSIGNED_BYTE, rgba_array);

for qt display use this project
http://doc.qt.digia.com/qq/qq26-openglcanvas.html


http://doc.qt.digia.com/qq/qq26-openglcanvas.html

samvdj
23rd September 2013, 13:32
Thanks.

Yes, and where do you extract an image from the camera and then map to texture?

In the initializeGL() function or in the paintGL() one? Or in yet another one?

saman_artorious
23rd September 2013, 14:38
Yes, and where do you extract an image from the camera and then map to texture?
Extract it in the class dedicated to do this job. Then you can signal the image contents to glwidget.cpp to set your texture like this:


void GlWidget::pixmapCatchFromForm(QPixmap pixmap)
{

deleteTexture(texture);

// image->loadFromData(bytes, "PNG");

texture = bindTexture(pixmap);

qDebug() << texture; // returns 1

updateGL();
}


In the initializeGL() function or in the paintGL() one? Or in yet another one?

do this is a slot of glwidget source. let's make this more interesting. let's say I want to show the image in each side of a cube. if any case first you need to define a rectangle or a cube to render the texture into it. so, build it like this:


void GlWidget::initializeGL()
{
initCommon();

shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, ":/vertexShader.vsh");

shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, ":/fragmentShader.fsh");

if(!shaderProgram.link())
{
exit(1);
}

vertices << QVector3D(-0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) // Front
<< QVector3D( 0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5)
<< QVector3D( 0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) // Back
<< QVector3D(-0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5)
<< QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) // Left
<< QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5)
<< QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) // Right
<< QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5)
<< QVector3D(-0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, -0.5) // Top
<< QVector3D( 0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, 0.5)
<< QVector3D(-0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, 0.5) // Bottom
<< QVector3D( 0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, -0.5);
//! [3]
textureCoordinates
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Front
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Back
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Left
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Right
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Top
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
<< QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Bottom
<< QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0);

updateGL();
}


Next you're gonna modify paintGL() to render texture to the rectangle as following:


shaderProgram.bind();

shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

shaderProgram.setUniformValue("texture", 0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(0);

shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");

shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
shaderProgram.enableAttributeArray("textureCoordinate");

glDrawArrays(GL_TRIANGLES, 0, vertices.size());

shaderProgram.disableAttributeArray("vertex");

shaderProgram.disableAttributeArray("textureCoordinate");

shaderProgram.release();


Cheers,