Here's the problematic part:
void Plain::initGL() {
...
glGenTextures(dimX*dimZ, tex);
glGenTextures(1, &lastTex);
...
glBindTexture(GL_TEXTURE_2D, lastTex );
...
glBindTexture(GL_TEXTURE_2D, tex[contite]);
...
}
void Plain::initGL() {
...
glGenTextures(dimX*dimZ, tex);
glGenTextures(1, &lastTex);
...
glBindTexture(GL_TEXTURE_2D, lastTex );
...
glBindTexture(GL_TEXTURE_2D, tex[contite]);
...
}
To copy to clipboard, switch view to plain text mode
Every time you invoke Plain::initGL() new textures are generated. The images are stored inside OpenGL context (represented by QGLContex) and your program receives only ID numbers of those textures (that's what you store in tex and lastTex).
Now if you invoke initGL() for the second time (within the same context), tex and lastTex will be filled with new values --- the old textures will be still in memory, but you won't be able to access them, since those variables will hold IDs of the new textures. It's not a big problem (except for the resource leak).
But what happens when you invoke initGL() within a different context (just like it happens during renderPixmap())? initGL() will generate new textures, that will be stored in a new context, and it will store their IDs in tex and lastTex (the same ones, since you have only one Plain object). Now when renderPixmap() ends, it destroys that temporary context and the new textures are destroyed too, but tex and lastTex still hold their IDs. The problem is that when you will try to draw something later, you will use invalid texture IDs.
You can solve this problem like this:
void GlWidget::renderPixmap(...)
{
plain.store();
plain.restore();
return result;
}
...
void Plain::store()
{
// copy tex and lastTex values somewhere
// for example onto QValueStack
}
void Plain::restore()
{
// restore tex and lastTex values
}
void GlWidget::renderPixmap(...)
{
plain.store();
QPixmap result( QGLWidget::renderPixmap( ... ) );
plain.restore();
return result;
}
...
void Plain::store()
{
// copy tex and lastTex values somewhere
// for example onto QValueStack
}
void Plain::restore()
{
// restore tex and lastTex values
}
To copy to clipboard, switch view to plain text mode
Bookmarks