Results 1 to 8 of 8

Thread: QImage bind texture fails

  1. #1
    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 QImage bind texture fails

    I want to bind a QImage/QPixmap to texture and render it on screen. The step Ive been after is to
    1. Grab a screen shot
    2. Send pixmap bytes to another code segment
    3. convert bytes to QImage
    4. bind QImage to texture

    I am sure that the rendering is correct. However, the program terminates when it reaches the bindTexture() function which takes QImage as input. This QImage as I noted is created from the pixmap byte array generated by screen shot.

    Qt Code:
    1. /* save png directly to qbytearray */
    2. QPixmap pixmap;
    3. QByteArray bytes;
    4. QBuffer buffer(&bytes);
    5. pixmap = QPixmap::grabWidget(this);
    6. buffer.open(QIODevice::WriteOnly);
    7. pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
    8.  
    9. emit sendPixmapToMainWin(bytes);
    To copy to clipboard, switch view to plain text mode 

    Where program terminates:
    Qt Code:
    1. void GlWidget::pixmapCatchFromForm(QByteArray bytes)
    2. {
    3. QImage image((const uchar* )bytes.constData(), glWidth, glHeight, QImage::Format_ARGB32);
    4. texture = bindTexture(image); // something's wrong with image apparently
    5. qDebug() << texture; // returns 1
    6. }
    To copy to clipboard, switch view to plain text mode 


    Rendering is correct, I set the texture here to be rendered:
    Qt Code:
    1. void GlWidget::paintGL()
    2. {
    3. //! [5]
    4. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5.  
    6. QMatrix4x4 mMatrix;
    7. QMatrix4x4 vMatrix;
    8.  
    9. QMatrix4x4 cameraTransformation;
    10. cameraTransformation.rotate(alpha, 0, 1, 0);
    11. cameraTransformation.rotate(beta, 1, 0, 0);
    12.  
    13. QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    14. QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
    15.  
    16. vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
    17.  
    18. //! [6]
    19. shaderProgram.bind();
    20.  
    21. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    22.  
    23. shaderProgram.setUniformValue("texture", 0);
    24.  
    25. glActiveTexture(GL_TEXTURE0);
    26. glBindTexture(GL_TEXTURE_2D, texture);
    27. glActiveTexture(0);
    28.  
    29. shaderProgram.setAttributeArray("vertex", vertices.constData());
    30. shaderProgram.enableAttributeArray("vertex");
    31.  
    32. shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
    33. shaderProgram.enableAttributeArray("textureCoordinate");
    34.  
    35. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    36.  
    37. shaderProgram.disableAttributeArray("vertex");
    38.  
    39. shaderProgram.disableAttributeArray("textureCoordinate");
    40.  
    41. shaderProgram.release();
    42. }
    To copy to clipboard, switch view to plain text mode 

  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: QImage bind texture fails

    I would start with:

    Quote Originally Posted by QImage docs
    The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer.
    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
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QImage bind texture fails

    Quote Originally Posted by wysota View Post
    I would start with:
    Thank you. This solved the problem:
    Qt Code:
    1. void GlWidget::pixmapCatchFromForm(QByteArray bytes)
    2. {
    3. image->loadFromData(bytes, "PNG");
    4. texture = bindTexture(*image);
    5. qDebug() << texture; // returns 1
    6. updateGL();
    7. }
    To copy to clipboard, switch view to plain text mode 

    One thing I am very curious about is what keeps the CPU so busy. In my program, I have a timer which triggers every 40 ms, shoots screen and signals it to glWidget which is responsible for creating a texture from that screen shot. The program uses up to 90% of CPU. even without texturing! a simple timer and a screen shot grabWidget function takes up to 90%, I do not understand why!
    Last edited by saman_artorious; 19th September 2013 at 09:41.

  4. #4
    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: QImage bind texture fails

    grabWidget is a costly method.
    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.


  5. #5
    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: QImage bind texture fails

    Quote Originally Posted by wysota View Post
    grabWidget is a costly method.
    is there any alternative?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QImage bind texture fails

    If I read your code's intent correctly you are:
    1. Creating a QPixmap using grabWidget()
    2. Converting the QPixmap to a PNG encoded data stream in a QByteArray
    3. Passing the QByteArray to another routine.
    4. Creating a QImage from the PNG encoded data stream
    5. Binding the QImage texture into a QGLWidget.

    Perhaps I have missed something but steps 2 and 4 seem pointless. Why not just pass the QPixmap and call QGLWidget::bindTexture() on that? You avoid two costly conversions through PNG and the construction of a QImage.

  7. #7
    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: QImage bind texture fails

    Thanks for your idea. I tried it, but it had no effect in term of CPU reduction.

  8. #8
    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: QImage bind texture fails

    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.


Similar Threads

  1. [QGLWidget] Cannot bind texture
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2009, 19:53
  2. [mac] QImage.load fails
    By snoopy67 in forum Qt Programming
    Replies: 4
    Last Post: 7th November 2007, 14:06
  3. Texture in QGLWidget
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2006, 08:47
  4. renderPixmap and texture
    By techno in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2006, 12:13

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.