Results 1 to 4 of 4

Thread: Convert a GL texture (GLuint) to QImage or QOpenGLTexture

  1. #1
    Join Date
    May 2017
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Convert a GL texture (GLuint) to QImage or QOpenGLTexture

    Hi,

    I find a lot of way to pass a QImage to GL but in fact i retrieve a gl texture from an API. So i'm trying to convert it either in a QImage or in a QOpenGLTexture to display it. I didn't see any function that could do it. I'm still trying by retrieving the data with glteximage and trying to fill a QImage but i don't know if it will work.

    This is my code to display :
    Qt Code:
    1. void GLWidget::paintGL()
    2. {
    3. glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
    4. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5.  
    6. QMatrix4x4 m;
    7. m.ortho(-1, 1, +1.f, -01.f, 1.0f, 20.0f);
    8. m.translate(0.0f, 0.0f, -10.0f);
    9. nv.StartCapture();
    10. program->setUniformValue("matrix", m);
    11. program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
    12. program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
    13. program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
    14. program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
    15. textures[0]->bind();
    16. glDrawArrays(GL_TRIANGLE_FAN, 0 * 4, 4);
    17. }
    To copy to clipboard, switch view to plain text mode 

    If you have any idea.
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert a GL texture (GLuint) to QImage or QOpenGLTexture

    There is no direct way to do it since the texture resides in your graphics card's memory. The most direct way I can think of is to render a rectangle with the texture to a frame buffer object and that can easily be converted to a QImage. As for QOpenGLTexture I see nothing in its API that could be used to bind it to an externally defined texture.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2017
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert a GL texture (GLuint) to QImage or QOpenGLTexture

    Hi,
    Thanks for your answer
    Currently, i retrieve the Gltexture from an opengl extension and i was thinking that the best way to draw it was to convert it in QImage. but if i can directly draw it in my glwidget, that's better. I've tried to set the GLTexture in uniform param but i have a black screen for the moment. I'm going to look at the framebuffer.

    Qt Code:
    1. void GLWidget::initializeGL()
    2. {
    3. initializeOpenGLFunctions();
    4. GLenum te=glewInit();
    5. glGenFramebuffers(1, &frameBuffer);
    6. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
    7.  
    8.  
    9. nvc = NVConfig();
    10. nvc.GlobalInit();
    11. nv= NVGL(nvc.l_vioConfig,GetDC((HWND)this->winId()));
    12. makeObject();
    13.  
    14. glEnable(GL_DEPTH_TEST);
    15. glEnable(GL_CULL_FACE);
    16.  
    17. #define PROGRAM_VERTEX_ATTRIBUTE 0
    18. #define PROGRAM_TEXCOORD_ATTRIBUTE 1
    19.  
    20. QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
    21. const char *vsrc =
    22. "attribute highp vec4 vertex;\n"
    23. "attribute mediump vec4 texCoord;\n"
    24. "varying mediump vec4 texc;\n"
    25. "uniform mediump mat4 matrix;\n"
    26. "void main(void)\n"
    27. "{\n"
    28. " gl_Position = matrix * vertex;\n"
    29. " texc = texCoord;\n"
    30. "}\n";
    31. vshader->compileSourceCode(vsrc);
    32.  
    33. QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
    34. const char *fsrc =
    35. "uniform sampler2D texture;\n"
    36. "varying mediump vec4 texc;\n"
    37. "void main(void)\n"
    38. "{\n"
    39. " gl_FragColor = texture2D(texture, texc.st);\n"
    40. "}\n";
    41. fshader->compileSourceCode(fsrc);
    42.  
    43. program = new QOpenGLShaderProgram;
    44. program->addShader(vshader);
    45. program->addShader(fshader);
    46. program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
    47. program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
    48. program->link();
    49. program->bind();
    50. program->setUniformValue("texture", nv.m_videoTexture);
    51. }
    52.  
    53. void GLWidget::paintGL()
    54. {
    55. glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
    56. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    57.  
    58. QMatrix4x4 m;
    59. m.ortho(-1, 1, +1.f, -01.f, 1.0f, 20.0f);
    60. m.translate(0.0f, 0.0f, -10.0f);
    61. nv.StartCapture(); //This function retrieve the GLTexture
    62. program->setUniformValue("matrix", m);
    63. program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
    64. program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
    65. program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
    66. program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
    67. glDrawArrays(GL_TRIANGLE_FAN, 0 * 4, 4);
    68. }
    To copy to clipboard, switch view to plain text mode 

    EDIT***
    I still have a black screen, so to be sure i have a correct texture, i saved my texture in .tiff file. And i got a correct result. So my problem is really to draw my texture on the widget
    Last edited by miyoku157; 12th June 2017 at 12:41.

  4. #4
    Join Date
    May 2017
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert a GL texture (GLuint) to QImage or QOpenGLTexture

    I finally found my mistake, i was just misbinding my texture. Work great now.

Similar Threads

  1. How to convert QString to QImage
    By gunturrohith in forum Qt Programming
    Replies: 12
    Last Post: 28th July 2015, 08:34
  2. QOpenGLTexture error: "Texture has not been destroyed"
    By giordi in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2015, 13:06
  3. QImage bind texture fails
    By saman_artorious in forum Qt Programming
    Replies: 7
    Last Post: 22nd September 2013, 17:44
  4. How To Convert CGImageRef to QImage
    By nareshqt in forum Qt Programming
    Replies: 0
    Last Post: 23rd June 2008, 09:21
  5. Convert RAW 8 bit pixels into a QImage
    By danielperaza in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2008, 15:53

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.