Results 1 to 15 of 15

Thread: old problem with QGLContext and render pixmap

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: old problem with QGLContext and render pixmap

    Here's the problematic part:
    Qt Code:
    1. void Plain::initGL() {
    2. ...
    3. glGenTextures(dimX*dimZ, tex);
    4. glGenTextures(1, &lastTex);
    5. ...
    6. glBindTexture(GL_TEXTURE_2D, lastTex );
    7. ...
    8. glBindTexture(GL_TEXTURE_2D, tex[contite]);
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 
    Every time you invoke Plain::initGL() new textures are generated. The images are stored inside OpenGL context (represented by QGLContex) and your program receives only ID numbers of those textures (that's what you store in tex and lastTex).

    Now if you invoke initGL() for the second time (within the same context), tex and lastTex will be filled with new values --- the old textures will be still in memory, but you won't be able to access them, since those variables will hold IDs of the new textures. It's not a big problem (except for the resource leak).

    But what happens when you invoke initGL() within a different context (just like it happens during renderPixmap())? initGL() will generate new textures, that will be stored in a new context, and it will store their IDs in tex and lastTex (the same ones, since you have only one Plain object). Now when renderPixmap() ends, it destroys that temporary context and the new textures are destroyed too, but tex and lastTex still hold their IDs. The problem is that when you will try to draw something later, you will use invalid texture IDs.

    You can solve this problem like this:
    Qt Code:
    1. void GlWidget::renderPixmap(...)
    2. {
    3. plain.store();
    4. QPixmap result( QGLWidget::renderPixmap( ... ) );
    5. plain.restore();
    6. return result;
    7. }
    8. ...
    9. void Plain::store()
    10. {
    11. // copy tex and lastTex values somewhere
    12. // for example onto QValueStack
    13. }
    14.  
    15. void Plain::restore()
    16. {
    17. // restore tex and lastTex values
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 14th July 2006 at 15:31.

  2. The following user says thank you to jacek for this useful post:

    mickey (21st July 2006)

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.