Textures disappear after full screen toggle

I’ve recoded some OpenGL functions so I can put them in a DLL and use them in several programs. The problem is that now the textures disappear, but only after I toggle fullscreen.

The texture maps fine and the program does what it is supposed to do until I change screen modes and then the texture disapears. I’ve tried hard coding values for texture, but that doesn’t work. So I can only assume that the texture is being deleted somehow.

The only thing I can think off is that the textures were not initialised in the main routine and are not global.

Any help would be much appreciated.

Here is some of the code:
//---------------------------
// Texture Initialisation
//---------------------------
CAirspeed::init()
{
GLuint tex;
// generate and bind the OpenGL texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

// copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, sizeX, sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, dial_texture);

// set up texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// free the texture variable
free(dial_texture);
CAirspeed::texture = tex;
initFlag = true;

}

//---------------------------
// CAirspeed Draw Function
//---------------------------
DLL_API CAirspeed::draw()
{
if (!initFlag)
CAirspeed::init();

//Draw some stuff
<snip>

}

I’ve been looking more into this problem. When I toggle fullscreen, the window is killed then a new one is created. Because of this, a new rendering context is created.

So my thinking is that the textures are bound to the original RC and are deleted when full screen is toggled. Is this what actually happens? If so, is there a way to transfer textures between RC’s?

Also, if this is the case, why does this not happen in other programs? ie. when the textures are created in the main app the textures are still there when full screen is toggled.

Hi Colin,

You need to recreate your OpenGL texture objects after creating a new context (there’s no way around this).

You can share lists between existing contexts with wglShareLists, but that’s a different story.

If you’re just changing the full-screen resolution and not the pixel format, you can probably get away with not recreating the window, but I’m not 100% sure that’s totally reliable 100% of the time. You can only set the pixel format of a window once. Recreating the window should always work.

So basically if you need to change full-screen modes, flush all the OpenGL (texture) objects and reload everything upon context creation.

Cheers,

Bill

Thanks for that Bill.

I added some code to the top of the draw function and it now works when toggleing to/from full screen:
// Check the current Rendering Context
HGLRC hNewRC = wglGetCurrentContext();
if(hNewRC!=hOldRC)
initFlag = false;

This way if the RC is changed it will re initialse.

This works but I feel like it’s a bit of a hack. So if anyone else has a better idea, I’m open to suggestions.

Thanks again.

It is possible to create the new fullscreen context while current windowed context is still active, use wglShareLists to share all textures, then destroy windowed context ?

Hi Zbuffer,

That sounds totally doable, actually.

Bill

Colin,

It’s pretty common practice to just recreate the context after a mode switch. If you change your pixel format you will need to reload your extension procs, so it’s perhaps just as well to just go the whole nine yards. If you set up your context init function correctly, one call will have have you back in action.

I don’t know the details of your application, but perhaps there’s a another way or place to test for a mode switch?

Bill