I have a class called Texture, that I need to be able to initialize once on my programs startup with a texture, and then at runtime replace the texture with one of the users choosing from their HDD. Initializing the Texture works fine, and the initial texture displays as it should, being given a name by glGenTextures, and then binding the pixel data under that name. That is done in the following functions:

Upon calling my draw function this texture displays just fine. However, upon clicking "import new spritesheet", an openFileDialogue appears and the user selects a new file. The following code executes, which should (in theory) delete the texture name (stored in the GLUint 'TextureID' array), and make it available for use again, allowing us to bind new texture data under that name.

*Note the call to 'glDeleteTextures' close to the start of 'reloadTexture()'.*

What should happen at this point is that the user now sees their selected texture being displayed. But what actually happens is that the user sees the old texture (the one that loads at startup), with the new texture's (the one they selected) dimensions. Meaning that despite my call to glDeleteTextures, the deleted texture's pixel data still exists in one of openGL's buffers somewhere I can't figure out where, nor why it would still be there after i have not only deleted it, but also attempted to bind a different files data over the top of it.

Thanks for reading, and I'd appreciate any light that could be shed on why openGL would hang-on to the old data.

P.S. In the function 'texGLSecondaryInit()' , you may notice commented out calls to glBindBuffer(). This was suggested to me as a way to purge the pixel buffer on another board. It caused Access Violation Errors all over the shop. However, I've left it on display here, in case it helps you understand what I'm trying and failing to do.

The only thing that glDeleteTextures promises is that the tetxure name will be available for reuse (via a subsequent call to glGenTextures); it doesn't promise that it will delete the pixel data, and the driver is perfectly free to keep that data hanging around for subsequent reuse. This can be a good thing as the driver may be able to get away without having to allocate a new block of storage - "here's a block that's no longer being used, just hand it back".

So not deleting the pixel data is expected behaviour, but when you make the next call to glTexImage (assuming that you've bound the correct texture, of course) then the texture should be completely respecified.

The only sensible explanation here is that your glTexImage call is somehow failing. As suggested, try a few glGetError calls and double-check your parameters.

Are you use the that the pixel alignment is 1 in the texture you are trying to load. Also another thing you can do is put glGetError() between your call and see where it is failing.

Check the glPixelStore call in the OP's code...

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

I can't see why they wouldn't be. They're both .png files, loaded into openGL using the same image loader. They also both worked on a previous project using the virtually same setup. However in that old project, they were both created at the startup of my program as separate instances of my 'Texture' class. The reason I can't do that here, is because i wish for the user to be able to select a new texture at runtime from a file.

Also placing glGetError after my glDeleteTextures call, returns no errors. Like so.

The only thing that glDeleteTextures promises is that the tetxure name will be available for reuse (via a subsequent call to glGenTextures); it doesn't promise that it will delete the pixel data, and the driver is perfectly free to keep that data hanging around for subsequent reuse. This can be a good thing as the driver may be able to get away without having to allocate a new block of storage - "here's a block that's no longer being used, just hand it back".

So not deleting the pixel data is expected behaviour, but when you make the next call to glTexImage (assuming that you've bound the correct texture, of course) then the texture should be completely respecified.

The only sensible explanation here is that your glTexImage call is somehow failing. As suggested, try a few glGetError calls and double-check your parameters.

Are you use the that the pixel alignment is 1 in the texture you are trying to load. Also another thing you can do is put glGetError() between your call and see where it is failing.

Ok I've done some further testing on this matter today. glGetErrors after my glTexImage2d call returns no errors.

Even if, rather than changing the Texture via a call to:

myTex->reloadTexture("filename");

I just create a brand new one like so:

mytex = new Texture("filename");

It still simply resizes the texture.

Note: that this new method that I've tried means that glDeleteTextures would never even be called. Meaning that OGL would not free the TextureID (or Texture Name) in use for the first texture for re use, therefore openGL would give a unique name to the new Texture when the constructor was called.

Knowing this, I have observed the TextureID variable.

After the very first call to glGenTextures, textureID[0] = 1 |---->Seems normal.

At this point '1' should no longer be available as a unique texture name, as it was previously created for the first texture and glDeleteTextures was never called.

Can anybody tell me why glGenTextures would refuse to give me a unique name? Usually this problem comes because glGenTextures is called before a context is established, and glGenTextures gives you '0' as a name. But we know i have a legit context, as I draw to it!

Also, I have put glGetErrors as I have above after glGenTextures. No errors

In my program, I have TWO OpenGL views. Running on TWO OpenGL contexts. Which i must switch between using WGLMakeCurrent(), in order to make them both flush, swapbuffers individually.

The OpenGL view i refer to in this question, is the first to be made current. It has the first texture bound and then it is drawn. At this point the second OGL view is made current, has some stuff happen to it (not important), and then is drawn.

So when I attempt to load and bind a second texture to my first openGL view, I've pulled the "old switcheroo" on myself and am trying to bind it to my second view's context (because it is current at this point). Which explains why the first context still had the old pixel data in it. As I'd technically never called glDeletetextures on it

A quick call to wglMakeCurrent(handleInHere, parentwindowhere), before myTex->reloadTexture("filename") call, and my textures are loading and replacing themselves at my beckoned call.