Results 1 to 3 of 3

Thread: OpenGL render to texture

  1. #1
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default OpenGL render to texture

    Hi all! I have a quick question.

    I'm trying to render a bunch of stuff (mainly QPainterPaths) to an OpenGL texture in order to cache it for future usage. My naïve approach was to paint in a QImage then use QGLWidget::bindTexture to send it to OpenGL.

    However, this approach has a double drawback: rendering in a QImage isn't done with OpenGL (which is way slower) and then data has to copied from the RAM to the VRAM in order to create that texture. I'm no expert, but it's my understanding that render to texture is merely a copy inside the VRAM.

    So, if I could render those painter paths with OpenGL directly in a texture, I'd gain time on both actions (if I'm not mistaken).

    The big question is of course how does one do that? I have an OpenGL context in a QGraphicsView, but render to texture involves some off-screen rendering that I'm not sure how to do. I've seen there is a QGLFrameBufferObject class, but I'm not sure about the way I'm supposed to use it...

    Any pointers on that topic? Thanks in advance!

  2. #2
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL render to texture

    The documentation for QGLFrameBufferObject includes an example: http://doc.trolltech.com/latest/open...idget-cpp.html
    It doesn't seem too difficult:
    - First Create a QGLFrameBufferObject specifying the size of the texture.
    - Then when you are ready to draw/paint create a QPainter(QGLFrameBufferObject*)
    - Now QGLFrameBufferObject->texture() returns the texture-id of the texture containing whatever you painted to the framebufferobject

  3. #3
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: OpenGL render to texture

    Oh, thanks, I hadn't noticed! ^^"
    Well, it seems to work, except for one thing: I can't find a way to erase the buffer before drawing on it. Therefore when I update a texture everything is drawn on the previous one. I'd like to fill it with a transparent background before drawing; is there a way do to that with QPainter or QGLFrameBufferObject or do I have to make a native glClear call? Thanks in advance!

    EDIT: a native OpenGL call to glClear works:
    Qt Code:
    1. buffer.bind();
    2. glClear(GL_COLOR_BUFFER_BIT);
    3. buffer.release();
    To copy to clipboard, switch view to plain text mode 
    However that might be a bit heavy. Is this the "right way" of cleaning the buffer?
    Last edited by Nicuvëo; 26th March 2011 at 12:18.

Similar Threads

  1. Replies: 1
    Last Post: 7th May 2010, 17:20
  2. OpenGL texture on QGraphicsScene background
    By Ovnan in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2008, 10:39
  3. QBrush texture pattern
    By edb in forum Qt Programming
    Replies: 4
    Last Post: 17th April 2007, 13:18
  4. Texture in QGLWidget
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2006, 08:47
  5. renderPixmap and texture
    By techno in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2006, 12:13

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.