1
code:
QGLFormat format;
QGLContext context(format,&pixmap);
QPainter painter;
painter.begin(context.device());
painter.setMatrix( _viewMatrix , false);
painter.drawImage(0,0,*_image,hpos,vpos);
painter.end();
cons: the context.device() returns the
pixmap so the rendering is allways done using
the software raster engine.
Qt 4.1 will most likely have some
improvements in that area.
2.
via QGLWidget::renderPixmap().
Hardware accelerate can be achieved.
cons:
Since renderPixmap() seems to create an entirely new context each time, all
texture IDs etc. are gone and have to create the textures (from image
files) over and over again.
to solve :
QGLContext::create ( const QGLContext * shareContext = 0 ) [virtual]
Create the first one. Create the others, passing in the first to
create. It will share ALL resources -- textures, display lists and arrays.
Note that this is NOT guaranteed to work, because context sharing is
an extension not a baseline feature of OpenGL.
If "isSharing()" returns false, it failed.
This method will create a pixmap and a temporary QGLContext to render
on the pixmap. It will then call initializeGL(), resizeGL(), and
paintGL() on this context. Finally, the widget's original GL context is
restored.
Usually you allocate textures and display lists from within the
initializeGL() which is normally called only once.
Now when you render offscreen with renderPixmap() then initializeGL() is
called again. So you have to make sure that you don't re-create your
textures IF 'useContext' is set to TRUE (if it is set to FALSE, then you
have to, off course).
Note also that Qt TRIES to share the context, but it's not guaranteed to
work, as someone already mentioned (since it's an extension of OpenGL,
not a requirement).
The crucial thing off course here is how to find out in initializeGL()
whether the context is shared or not (that is whether renderPixmap()'s
argument 'useContext' was set to TRUE and succeeded). You may try with
QGLWidget::isSharing() if this gives meaningful results at this point.
So you would end up with something like this:
void MyGLWidget::initializeGL()
{
...
if (this->isSharing() == false)
{
this->createMyTexturesAndDisplayLists();
}
...
}
void MyGLWidget::paintGL()
{
...
glCallList (mySharedListIDWhichIsValidForOnscreenAndOffscreen);
...
}