PDA

View Full Version : QGraphicsItem and OpenGL textures



Aldebaran
17th September 2009, 14:25
Hi everyone. I'm fairly new to QT and maybe this is a newbie question. I hope you can forgive me if that's the case.

I'm writing a application with a diagram editor and I need to display OGL textures in it. At the moment, I roughly follow this process: create a QImage from the texture data, convert this QImage to a QPixmap and use this pixmap in a QGraphicsPixmapItem. This is awfully expensive and inefficient. Any alternatives?

Should I make my custom QGraphicsItems class and write my own paint() routine using opengl calls?

Thank you.

scascio
17th September 2009, 15:10
You ca set the viewport of your QGraphicsView to use OpenGL with these instructions :

setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

The QGLWiget can also be derived and therefore respond to the GL events, initializeGL, paintGL, ... but I dont know much about it.

Then to optimize you texture drawing with a QGraphicsItem, you can derive one and overload the paint event in wich you can draw directly from QImage into QPainter.
But I don't think this will be better once the image is loaded into the QGraphicsPixmapItem.

S.Cascio

Aldebaran
18th September 2009, 11:38
I already set the viewport to use OpenGL and speed up the rendering.


Then to optimize you texture drawing with a QGraphicsItem, you can derive one and overload the paint event in wich you can draw directly from QImage into QPainter.
But I don't think this will be better once the image is loaded into the QGraphicsPixmapItem


I don't want to create a QImage or a QPixmap if I don't need to. Can I draw the loaded OGL texture overloading the paint event of a QGraphicsItem and using OGL calls? Do I have to make my custom QGLWidget and add it to the scene?

scascio
18th September 2009, 15:22
In a QLWidget you can make OGL calls. I have no experience on it but the doc may show yiou the way.

If you want to derived a QGraphicsItem that manages an OGL texture, you can use QImage as a wrapper of a buffer of char, to prevent from copying texture data. Then it is easy to draw it with in painter. QPainter::draw can draw a part of the image inside a part of your item, doing the scaling automatically, without any pre computed transformation.

S.Cascio

dpkay
22nd January 2010, 09:37
I have a related question. I would like to operate with quite a lot (between a few 100 and a few 1000) small items which I would like to draw using OpenGL. To draw them, I need to be able to do one of the following two things:
- specify own texture coordinates for the drawn item
- change the uniform shader parameters of the current shader (all of them can use the same shader)

The item geometry itself can be as simple as a rectangle. Traditionally, I would just have used a QGLWidget, but I recently read about GraphicsView and GraphicsItems. Will it be beneficial for me to use those in my case? I couldn't really find any information about how to achieve either of the above points, unless I use beginNativePainting which I believe will be slow for hundreds to thousands of items since the entire OpenGL would be copied for every drawn item (as far as I understand).

Thanks
- Dominik