PDA

View Full Version : Save QGraphicsScene (with OpenGL items) to JPEG



MadBear
26th September 2011, 08:27
Greetings,

I have a strange problem. I have created application where user can create several QGraphicsScenes (shown is only one in main view) and since I had to use OpenGL for some items I have also used OpenGL (FBO) to save current scene as JPEG image. Now it works great until I have several scenes in "script" and some of those scenes use QGraphicsTextItem. In that case text is not rendered correctly (it is rendered only as a filled box). That does not occur if there is only one scene. In that case text is rendered correctly. What am I doing wrong? The relevant code is:



QGLFramebufferObject *mbuff=new QGLFramebufferObject(width(),height());
QStyleOptionGraphicsItem pom;
mbuff->bind();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glViewport(0,0,mbuff->width(),mbuff->height());
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,mbuff->width(),0,mbuff->height(),-100,100);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
QPainter paint;
paint.begin(mbuff);
paint.setRenderHint(QPainter::HighQualityAntialias ing,true);
QList<QGraphicsItem*> list=items(Qt::AscendingOrder);
int i;
for(i=0;i<list.size();i++)
{
paint.save();
paint.setMatrix(list[i]->sceneMatrix(),true);
list[i]->paint(&paint,&pom,NULL);
paint.restore();
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
paint.end();
mbuff->release();
QImage pix=mbuff->toImage();
pix.save(fn);
delete mbuff;


I am realy at a loss here. I have tried software rendering but it is very slow.

Thank you in advance.

Regards,
MadBear

stampede
26th September 2011, 09:09
to save current scene as JPEG image
How about using the QGraphicsScene::render method ?


void MyGraphicsScene::renderToFile( const QString& path ){
QImage img(outputSize,QImage::Format_RGB32);
QPainter p(&img);
this->render(&p);
img.save(path);
}

It should work regardless of the scene content.

MadBear
26th September 2011, 09:24
Greetings,

Thank you for your reply. I have used QGraphicsScene::render method, but this method is very slow, because I can't use shaders for OpenGL items (I need smoothing Gaussian filter for those items).

Thank you again,
Regards,
MadBear

wysota
26th September 2011, 14:52
I think you should use QGraphicsScene::render() to render to a GL pixel (or frame) buffer and then convert that to an image. This will allow you to use GL calls for rendering.

MadBear
27th September 2011, 08:47
Greetings,

I have tried this too. It doesn't work. But isn't this the same I already did in the first place?
Thank you for your help.

Regards,
MadBear

wysota
27th September 2011, 09:25
No, it's not the same. How does "doesn't work" manifest itself?