Can I use a random texture identifier?

Instead of using glGenTextures() to get an unused texture identifier. Can I randomly select a number, say, 99999 and use it?

I will of course ask:

 glIsTexture( m_texture ) 

and before proceeding, make sure that it is false.

Here is some background:

I am developing an image slide show application for Mac. The slide show preview is flawless. To save the slide show, I pass the FBO. I create an AGL context, create a texture using glGenTextures() and render to the frame buffer. All is well, except for a minor problem.

After I saved the slide show and return to the main window, all my image thumbnails will be gray, that is, the textures will be deleted.

I examined it and found out that the thumbnails of the images and the texture of the FBO have the same texture identifier. When I delete the FBO texture at the end of the slide show save operation, the thumbnail textures are also lost. This is strange because during the save, I have an AGL context, and the main user interface has a different AGL context, presumably created in the background using Core Image and which I do not control.

So, my options, as I see now, are:

  • Do not remove FBO texture.
  • Randomly select a high texture identifier in the hope that the main user interface will not use it.

In fact, I read that you do not have to delete the texture if you delete the AGL opengl context. Since context deletion openGL automatically deletes all related textures. It's true? If so, option 1 makes more sense.

I understand that funny things are happening here that I cannot explain. For example, after I saved a slide show of images in a .mov file, I delete the context that was created in the same class. By right, this should not affect textures created in a different context. But this is so, and I seriously cannot explain it.

+6
source share
2 answers

Let me answer your main question first: yes, in OpenGL 2.1 it is legal to simply select a texture name and use it as a texture. Please note that this is not legal in 3.2 kernel. The fact that OpenGL ARB removed this ability should help illustrate whether this is a good idea or not.

Now you seem to be facing an odd mistake. glGenTextures should never return a texture name that is already in use. In the end, what is it for. It seems you are somehow getting textures that are already in use. I do not know how this happens.

An OpenGL context upon destruction will delete all resources specific to that context. If you created a second context that shares resources with the first, deleting the second context will not delete shared resources (textures, buffers, etc.). It will remove non-shared resources (VAO, etc.), but everything else remains.

I would suggest not creating and deleting FBO textures permanently. Just create it when you start the application. In the worst case scenario, you will need to redistribute the storage for the object as needed (via glTexImage2D ) if you need a different size or something like that.

+11
source

For anyone with this problem, make sure your context is fully initialized; until it glGenTextures will always return 0. I did not understand what happens first, because it seems that 0 is still a valid texture identifier.

0
source

Source: https://habr.com/ru/post/904131/


All Articles