Results 1 to 15 of 15

Thread: old problem with QGLContext and render pixmap

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default old problem with QGLContext and render pixmap

    hi, I have an old problem with render pixmap; after gen pixmap called, my textures are destroyed and I can't understand why.
    Qt Code:
    1. //inside a member of mainForm class
    2. qp = myWidget1->renderPixmap(800, 600, FALSE);
    3. qi=qp.convertToImage();
    4. qi = qi.smoothScale(500,500);
    5. qi.save(base_file+".png","PNG");
    To copy to clipboard, switch view to plain text mode 
    I know render pixmap do this: create a new contextGL call initializeGL(), resizeGL() an PaintGL(); after, it restore the previous context; but the previuos context is broken..also pixmap doesn't appear realistic: some textures are broken; but the texture broken on the final image image.jpg) and image on widgetGL aren't the same...
    I tried to do:
    Qt Code:
    1. myWidget1->initializeGL();
    2. myWidget1->resizeGL(myWidget1->width(), myWidget1->height());
    3. myWidget1->paintGL();
    To copy to clipboard, switch view to plain text mode 
    I thought this can see to me if some error in contextGL; but these 3 instrucion don't change the image in QGLWIdget....
    After this, if I put a new texture on my image (ie on one object, so that call every gl function that initialize texttures), the prevoius context is restored and every textures are in the right place)
    Help me, please; thanks
    Last edited by mickey; 14th July 2006 at 00:24.
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    If I remember well, in initializeGL() you create new textures and store their IDs in member variables. If you call initializeGL() from a different context, you will loose information about textures from previous one. You must find a way to preserve those IDs between contexts (for example copy them somewhere or keep them on a stack).

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    sorry, I don't understand good but how copy it on the stack; thanks
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    Quote Originally Posted by mickey
    I don't understand good but how copy it on the stack
    If you have something like:
    Qt Code:
    1. void GlWidget::initializeGL()
    2. {
    3. ...
    4. _textureId = generateTexture();
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    then each time you invoke initializeGL() you will loose the previous value of _textureId member variable.

    You can avoid that like this:
    Qt Code:
    1. void GlWidget::renderPixmap(...)
    2. {
    3. int oldTextureId = _textureId;
    4. QPixmap result( QGLWidget::renderPixmap( ... ) );
    5. _textureId = oldTextureId;
    6. return result;
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    sorry but I can't understand what's the textureID; but after I call genPixmap (now I put It inside myWidget class and don't change everythings), I call updateGL(),and everythings appear as before; then I think my way to put texture is ok.; i think is only a context problem....could you explain better what do you want to do with saving this texture ids?? thanks
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    Quote Originally Posted by mickey
    could you explain better what do you want to do with saving this texture ids??
    Could you post your initializeGL()? I might not remember your program well.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    take it. thanks
    Attached Files Attached Files
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    What does plain.initGL() do?

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    it initialize every textures....gen tex, bind....plain it's an object and onto this I put the texture...
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    Quote Originally Posted by mickey
    it initialize every textures....gen tex, bind....
    That's the place where the problem is. Can I see it?

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    it's very complicated. Can I mail it you? thanks
    Regards

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

    Default Re: old problem with QGLContext and render pixmap

    Quote Originally Posted by mickey
    Can I mail it you?
    Yes, you can.

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

    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.

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

    mickey (21st July 2006)

  15. #14
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    I'm thinking to your solution...and other troubles arise: is there a way to clean the contextGL? (in the same way when app started)
    then, I see that renderPixmap render my scene ok but don't render right texture multitextured; is it right? thanks
    Regards

  16. #15
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: old problem with QGLContext and render pixmap

    your solution is ok!! but when I save pixmap into a .jpg the texture multitextured appear gray oe black; if i don't use multitexture they appear ok! does depends it from qt3??Can i solve it? thanks
    Furthermore: the objcet appear with same lines that they aren't visible in glWidget; eg: in square textured is visible a diagonal line (only in .jpg not in glWidget) what's it???
    Last edited by mickey; 21st July 2006 at 01:25.
    Regards

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.