PDA

View Full Version : OpenGL and QPainter



wererabit
13th August 2010, 04:11
Hi,

I am trying to add openGL into my program. What I want is to have a gradient background on the back (using QPainter) and say a triangle (openGL) in the front. My problem is that the 2D painting always lies on top of the OpenGL painting. Could anyone show me how I can fix this?

I included the code below in case anyone want to have a look. So what I currently have is only the gradient background and no triangles since they are hidden behind the background.

Thanks a tons



void GLWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(gradient);
painter.drawRect(rect()); //paint the background with linear gradient
painter.end();

makeCurrent();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_MULTISAMPLE);
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

setupViewport(width(), height());

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);

glPushAttrib(GL_ALL_ATTRIB_BITS);
qglColor(QColor(255, 239, 191));
glBegin(GL_TRIANGLES); //draw 2 random triangles to test
glVertex3f(+0.3, +0.3, -0.3);
glVertex3f(-0.3, +0.3, -0.3);
glVertex3f(+0.3, -0.3, -0.3);
glVertex3f(-0.3, -0.3, -0.3);
glVertex3f(+0.3, -0.3, +0.3);
glVertex3f(-0.3, -0.3, +0.3);
glEnd();
glPopAttrib();

glShadeModel(GL_FLAT);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

franz
13th August 2010, 07:25
Maybe the wolfenqt (http://labs.trolltech.com/blogs/2008/12/02/widgets-enter-the-third-dimension-wolfenqt/) example can help you there.