2D texture problem

Hi!

I’ve been struggling with a texture problem for the last three and half years and no one I ever asked about this knew how to solve the problem.

I’ve written a simple application that shows images and some overlay drawings on a screen. The images are shown using 2D textures. The application works fine, as long as I don’t open a second OpenGL window on a different context (which is based on an external library). When I do this, the textures stop working and are not displayed any more. What happens is that the last displayed textures before opening the second GL context remain unchanged as long as I keep that second GL context open. The weird thing is that the drawings (GL_LINES, GL_LINE_LOOP, etc.) keep on working fine. I’ve observe this behavior on multiple machines (with different library version) running my code, so I guess I must be doing something wrong with the textures.

My code is pretty standard and not much different from that found in the examples everywhere. I generate first the texture for each image that will be displayed.

[b]
// newImage_p is the input image to show
m_image_v.push_back(newImage_p);

// Generate texture.
glGenTextures(1, &newImage_p->textureId_ui);

//first call transfers the texture to hw
glBindTexture( GL_TEXTURE_RECTANGLE_NV, newImage_p->textureId_ui );

glPixelStorei( GL_UNPACK_ALIGNMENT, 1);

glTexParameteri( GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri( GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);

// Interpolation settings.
glTexParameteri( GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glPixelTransferf ( GL_RED_SCALE, newImage_p->scale_f );
glPixelTransferf ( GL_RED_BIAS, newImage_p->bias_f );
glPixelTransferf ( GL_GREEN_SCALE, newImage_p->scale_f );
glPixelTransferf ( GL_GREEN_BIAS, newImage_p->bias_f );
glPixelTransferf ( GL_BLUE_SCALE, newImage_p->scale_f );
glPixelTransferf ( GL_BLUE_BIAS, newImage_p->bias_f );

glTexImage2D( GL_TEXTURE_RECTANGLE_NV,
0, // base level (only 0 allowed for GL_TEXTURE_RECTANGLE_NV).
newImage_p->image.getImageFormat(), // internal format.
newImage_p->image.getWidth(),
newImage_p->image.getHeight(),
0, // border.
newImage_p->image.getImageFormat(), // format of pixel data array.
newImage_p->image.getDataType(), // data type of pixel data array.
newImage_p->image.getDataPointer()); // data.
[/b]

And then, when displaying (on paintGL), I have:

[b]
glEnable( GL_TEXTURE_RECTANGLE_NV);

for (DisplayImageList_t::const_iterator i = m_image_v.begin(); i != last; ++i )
{
glBindTexture( GL_TEXTURE_RECTANGLE_NV, (*i)->textureId_ui );

float endX_f = (*i)->u_f + (*i)->width_f;
float endY_f = (*i)->v_f + (*i)->height_f;

glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex2f((*i)->u_f, (*i)->v_f);

glTexCoord2f((*i)->image.getWidth(), 0);
glVertex2f(endX_f, (*i)->v_f);

glTexCoord2f((*i)->image.getWidth(), (*i)->image.getHeight());
glVertex2f(endX_f, endY_f);

glTexCoord2f(0, (*i)->image.getHeight());
glVertex2f((*i)->u_f, endY_f);

glEnd();        

}

glDisable(GL_TEXTURE_RECTANGLE_NV);
[/b]

I have the feeling that the problem is some kind of resource conflict between the two contexts, although only the one showing the images gets affected.

Can anyone please give me at least a hint where the problem might be?

-You are working with multiple thread?
-Contexts share something?
-The context used to load the texture and the context used to display the texture are the same?
-Are they active at the right time?

For me looks like you are creating the texture in one context and then try to show them with another context active.

Rosario, thanks very much for your reply.

My OpenGL code doesn’t run on multiple threads.

No, they don’t share anything.

That’s a good question. As far as I know, yes. My code is based on Qt, and Qt generates the context for me. I don’t generate any additional context in my code.

What does it mean “to be active at the same time”. I’m not sure about this.

Is there some way to check the context to see if this is occurring?

wglMakeCurrent() and such select the active context. You can have only one active context per thread. Rosario guessed right that when e.g. you try to use textures from context 1, you’re in context 2.
So, before starting to draw everything for a window, use makeCurrent()

For future reference
(Windows)
http://www.opengl.org/wiki/Platform_specifics:_Windows#Multiple_Windows

Geniuses!

I’m using Linux so I had to change the function call, but that was the problem! Thank you very much!