PDA

View Full Version : Use OpenGL in QGraphicsItem paint method



subsonic
27th November 2011, 15:54
Hi,

Is it possible to use opengl to draw e.g. an OpenGL texture or a simple triangle inside an QGraphicsItem bounding rect?

I tried following, but the triangle is drawn outside of the bounding rect of the QGraphicsItem.


class MyItem : public QGraphicsItem
{
public:
MyItem( QGraphicsItem * parent = 0 ) : QGraphicsItem(parent),
w(200), h(200)
{}

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(boundingRect());
painter->beginNativePainting();

glViewport(20, 20, w, h); /* Reset The Current Viewport And Perspective Transformation */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
glTranslatef(0.0f , 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

painter->endNativePainting();
}

QRectF boundingRect () const{
return QRectF(20,20,w,h);
}

private:

int w, h;
};