Results 1 to 1 of 1

Thread: Dynamic QOpenGLTexture with arbitrary data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Dynamic QOpenGLTexture with arbitrary data

    I'm trying to render camera stream, the problem is camera isn't recognized and all the Qt machinery is useless.
    What i have is a pointer `void* data` that can be accessed for curret frame.
    The solution i've came up is to follow this tutorial:
    here
    only I'm using `QOpenGLTexture` filled with video data stream.

    Redering a frame looks like this:
    1. paint() is called
    2. inside of paint function updateTexture is called, followed by texture->bind()
    3. After that shader is linked and quad is painted.
    4. texture->release;


    Two important functions are below. updateTexture has also overloaded version that fills that gets the data from device and calls the "main" updateTexture.

    Qt Code:
    1. void RsFrameProvider::updateTexture(const void* data, int width, int height, rs::format format, int stride = 0) {
    2. stride = stride == 0 ? width : stride;
    3.  
    4. QOpenGLTexture::PixelType pixType;
    5. QOpenGLTexture::PixelFormat pixFormat;
    6. QOpenGLTexture::TextureFormat texFormat;
    7.  
    8. switch(format)
    9. {
    10. case rs::format::any:
    11. throw std::runtime_error("not a valid format");
    12. case rs::format::rgb8: case rs::format::bgr8: // Display both RGB and BGR by interpreting them RGB, to show the flipped byte ordering. Obviously, GL_BGR could be used on OpenGL 1.2+
    13. texFormat = QOpenGLTexture::RGBFormat;
    14. pixFormat = QOpenGLTexture::RGB;
    15. pixType = QOpenGLTexture::UInt8;
    16. std::cerr << "Texture data set.";
    17. //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    18. break;
    19.  
    20. default:
    21. {
    22. std::stringstream ss;
    23. ss << rs_format_to_string((rs_format)format) << " RS: Pixel format is not supported";
    24. throw std::runtime_error(ss.str().c_str());
    25. }
    26. }
    27.  
    28. if (!texture->isStorageAllocated()) {
    29. texture->setSize(width, height);
    30. texture->setFormat(texFormat);
    31. texture->allocateStorage(pixFormat, pixType);
    32. }
    33. texture->setData(pixFormat, pixType, data);
    34.  
    35. std::cerr << "VALUE: " << ((uchar*) data);
    36. // Texture updated ready to bind
    37. std::cerr << "Texture ready to bind, FPS:" << fps << " pixFormat" << pixFormat << std::endl;
    38. }
    To copy to clipboard, switch view to plain text mode 

    I'm sure that each frame there is non-zero texture produced since I can print the data from device on the cerr stream.

    Paint function:

    Qt Code:
    1. void Renderer::paint()
    2. {
    3. frameProvider.updateTexture();
    4. frameProvider.texture->bind(0);
    5.  
    6.  
    7. if (!m_program) {
    8. initializeOpenGLFunctions();
    9.  
    10. m_program = new QOpenGLShaderProgram();
    11. m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
    12. "attribute highp vec4 vertices;"
    13. "varying highp vec2 coords;"
    14. "attribute highp vec4 texCoord;"
    15. "varying highp vec4 texc;"
    16. "void main() {"
    17. " gl_Position = vertices;"
    18. " coords = vertices.xy;"
    19. " texc = texCoord;"
    20. "}");
    21. m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
    22. "uniform lowp float t;"
    23. "varying highp vec2 coords;"
    24.  
    25. "varying highp vec4 texc;"
    26. "uniform sampler2D tex;"
    27. "void main() {"
    28. " lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
    29. " i = smoothstep(t - 0.8, t + 0.8, i);"
    30. " i = floor(i * 20.) / 20.;"
    31. " highp vec3 color = texture2D(tex, texc.xy).rgb;"
    32. " gl_FragColor = vec4(color, 1.0);"
    33. "}");
    34.  
    35. m_program->bindAttributeLocation("vertices", 0);
    36. m_program->bindAttributeLocation("texCoord", 1);
    37.  
    38. m_program->link();
    39.  
    40. }
    41. m_program->bind();
    42.  
    43. m_program->enableAttributeArray(0);
    44. m_program->enableAttributeArray(1);
    45. float values[] = {
    46. -1, -1,
    47. 1, -1,
    48. -1, 1,
    49. 1, 1
    50. };
    51. float texCoords[] = {
    52. 0, 0,
    53. 1, 0,
    54. 0, 1,
    55. 1, 1
    56. };
    57. m_program->setAttributeArray(0, GL_FLOAT, values, 2);
    58. m_program->setAttributeArray(1, GL_FLOAT, texCoords, 2 );
    59.  
    60. m_program->setUniformValue("t", (float) m_t);
    61. m_program->setUniformValue("tex", 0);
    62.  
    63. glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
    64.  
    65. glDisable(GL_DEPTH_TEST);
    66.  
    67. glClearColor(0, 0, 0, 1);
    68. glClear(GL_COLOR_BUFFER_BIT);
    69.  
    70. glEnable(GL_BLEND);
    71. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    72.  
    73. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    74.  
    75. m_program->disableAttributeArray(0);
    76. m_program->disableAttributeArray(1);
    77.  
    78.  
    79. frameProvider.texture->release();
    80. m_program->release();
    81.  
    82. // Not strictly needed for this example, but generally useful for when
    83. // mixing with raw OpenGL.
    84. m_window->resetOpenGLState();
    85.  
    86. }
    To copy to clipboard, switch view to plain text mode 

    All I get is a black screen.
    If I use non-dynamic texture created from Image by QOpenGLTexture( url ...) binded the same way as dynamic one - I can see textured quad.

    Is it even correct way to approach video rendering?
    I want good performance and access to video frame data.
    What is wrong?
    Last edited by pietrko; 11th May 2017 at 13:12.

Similar Threads

  1. QOpenGLTexture destructor
    By kangaba in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2013, 11:41
  2. Dynamic Data Exchange (DDE)
    By mlheese in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2011, 03:57
  3. will Qt delete all dynamic data?
    By somename in forum Qt Programming
    Replies: 13
    Last Post: 1st June 2010, 00:18
  4. Replies: 4
    Last Post: 4th September 2009, 09:33
  5. Dynamic Data Display with OpenGL
    By showhand in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2006, 02:17

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.