PDA

View Full Version : OpenGL texture on QGraphicsScene background



Ovnan
10th July 2008, 12:58
Hello!

I try to draw texture on QGraphicsScene background. QGraphicsView object has QGLWidget object as the viewport.
Reimplemented QGraphicsScene::drawBackground function is:

void OWorkspaceScene::drawBackground(QPainter *painter, const QRectF &rect)
{
GLuint texture[1];
QImage t, b;

b.load("../images/image.bmp"); // image size 256x256

t = QGLWidget::convertToGLFormat(b);
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glEnable(GL_TEXTURE_2D);

glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 255);
glVertex2f(0, 255);
glTexCoord2f(255, 255);
glVertex2f(255, 255);
glTexCoord2f(255, 0);
glVertex2f(255, 0);
glEnd();

glFinish();
}

As a result i have grey square nice image instead.
When i draw color square (without texture), it works fine.
I work using QT-4.3.1 under Visual Studio 2005.

What do i do wrong?

wysota
10th July 2008, 21:34
Have you checked that the path to the image is correct and that the image gets loaded?

BTW. Using Qt methods for binding the texture might be a bit less error prone, i.e.

bindTexture(QPixmap(":/images/side.png"), GL_TEXTURE_2D);

Ovnan
11th July 2008, 08:22
Have you checked that the path to the image is correct and that the image gets loaded?

BTW. Using Qt methods for binding the texture might be a bit less error prone, i.e.

bindTexture(QPixmap(":/images/side.png"), GL_TEXTURE_2D);
Yes. The path to the image is correct and that the image gets loaded.
Same code works fine with QGLWidget in QGLWidget::paintGL().

wysota
11th July 2008, 08:30
Maybe this helps:
http://labs.trolltech.com/blogs/2008/06/27/accelerate-your-widgets-with-opengl/

Ovnan
11th July 2008, 08:55
Maybe this helps:
http://labs.trolltech.com/blogs/2008/06/27/accelerate-your-widgets-with-opengl/
Unfortunately, i use QT-4.3.1. In addition, i want solve problem with method presented above.

wysota
11th July 2008, 10:39
The version doesn't matter. I gave you the link because there is OpenGL used to implement drawBackground() for a graphics scene. Maybe you need to do something special for GL to work there and maybe you'll find it in the source code of the article.