Results 1 to 4 of 4

Thread: QGLWidget + (web)camera

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default QGLWidget + (web)camera

    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

    Qt Code:
    1. ui.graphicsView_2D->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget + (web)camera

    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
    Qt Code:
    1. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0,
    2. GL_BGR_EXT, GL_UNSIGNED_BYTE, rgba_array);
    To copy to clipboard, switch view to plain text mode 

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


    http://doc.qt.digia.com/qq/qq26-openglcanvas.html
    Last edited by wagmare; 23rd September 2013 at 11:38. Reason: sorry wrong link
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default Re: QGLWidget + (web)camera

    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?

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGLWidget + (web)camera

    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:
    Qt Code:
    1. void GlWidget::pixmapCatchFromForm(QPixmap pixmap)
    2. {
    3.  
    4. deleteTexture(texture);
    5.  
    6. // image->loadFromData(bytes, "PNG");
    7.  
    8. texture = bindTexture(pixmap);
    9.  
    10. qDebug() << texture; // returns 1
    11.  
    12. updateGL();
    13. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. void GlWidget::initializeGL()
    2. {
    3. initCommon();
    4.  
    5. shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    6.  
    7. shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    8.  
    9. if(!shaderProgram.link())
    10. {
    11. exit(1);
    12. }
    13.  
    14. vertices << QVector3D(-0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) // Front
    15. << QVector3D( 0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5)
    16. << QVector3D( 0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) // Back
    17. << QVector3D(-0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5)
    18. << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) // Left
    19. << QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5)
    20. << QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) // Right
    21. << QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5)
    22. << QVector3D(-0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, -0.5) // Top
    23. << QVector3D( 0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, 0.5)
    24. << QVector3D(-0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, 0.5) // Bottom
    25. << QVector3D( 0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, -0.5);
    26. //! [3]
    27. textureCoordinates
    28. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Front
    29. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
    30. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Back
    31. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
    32. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Left
    33. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
    34. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Right
    35. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
    36. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Top
    37. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0)
    38. << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) // Bottom
    39. << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0);
    40.  
    41. updateGL();
    42. }
    To copy to clipboard, switch view to plain text mode 

    Next you're gonna modify paintGL() to render texture to the rectangle as following:
    Qt Code:
    1. shaderProgram.bind();
    2.  
    3. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    4.  
    5. shaderProgram.setUniformValue("texture", 0);
    6.  
    7. glActiveTexture(GL_TEXTURE0);
    8. glBindTexture(GL_TEXTURE_2D, texture);
    9. glActiveTexture(0);
    10.  
    11. shaderProgram.setAttributeArray("vertex", vertices.constData());
    12. shaderProgram.enableAttributeArray("vertex");
    13.  
    14. shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
    15. shaderProgram.enableAttributeArray("textureCoordinate");
    16.  
    17. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    18.  
    19. shaderProgram.disableAttributeArray("vertex");
    20.  
    21. shaderProgram.disableAttributeArray("textureCoordinate");
    22.  
    23. shaderProgram.release();
    To copy to clipboard, switch view to plain text mode 

    Cheers,

Similar Threads

  1. Replies: 0
    Last Post: 1st July 2013, 08:59
  2. One QGLWidget that manage QGLWidget childs
    By polch in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2012, 11:26
  3. Camera Frames in UI
    By Johncdy in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2011, 02:03
  4. Connect camera in Qt?
    By nthung in forum Qt Programming
    Replies: 12
    Last Post: 25th May 2010, 11:19
  5. IP camera
    By vinod sharma in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2010, 13:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.