PDA

View Full Version : 2D texture problem



hbadino
04-11-2012, 08:52 AM
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.



// 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.


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


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);


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?

Rosario Leonardi
04-11-2012, 12:28 PM
-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.

hbadino
04-11-2012, 05:12 PM
Rosario, thanks very much for your reply.


-You are working with multiple thread?
My OpenGL code doesn't run on multiple threads.


-Contexts share something?
No, they don't share anything.


-The context used to load the texture and the context used to display the texture are the same?
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.


-Are they active at the right time?
What does it mean "to be active at the same time". I'm not sure about this.


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

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

Ilian Dinev
04-12-2012, 12:51 AM
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()

V-man
04-12-2012, 05:37 AM
For future reference
(Windows)
http://www.opengl.org/wiki/Platform_specifics:_Windows#Multiple_Windows

hbadino
04-12-2012, 02:16 PM
Geniuses!

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