Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use a Random texture ID?

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

I will, of course, query:

glIsTexture( m_texture )

and make sure it's false before proceeding.

Here's some background:

I am developing an image slideshow app for the mac. Previewing the slideshow is flawless. To save the slideshow, I am rendering to an FBO. I create an AGL context, instantiate a texture with glGenTextures() and render to a frame buffer. All's well except for a minor issue.

After I've saved the slideshow and return to the main window, all my image thumbnails are grey, i.e. the textures have been cleared.

I have investigated it and found out that the image thumbnails and my FBO texture somehow have the same texture ID. When I delete my FBO texture at the end of the slideshow saving operation, the thumbnail textures are also lost. This is weird because I have an AGL context during saving, and the main UI has another AGL context, presumably created in the background by Core Image and which I have no control on.

So my options, as I see it now, is to:

  1. Not delete the FBO texture.
  2. Randomly choose a high texture ID in the hopes that the main UI will not be using it.

Actually I read that you do not necessarily need to delete your texture if you are deleting the AGL opengl context. Because deleting your openGL context automatically deletes all associated textures. Is this true? If yes, option 1 makes more sense.

I do realize that there are funny things happening here that I can't really explain. For example, after I've saved my image slideshow to an .mov file, I delete the context, which was created in the same class. By right, it should not affect textures created in another context. But it does, and I seriously can't explain that.

like image 562
Chetan Avatar asked Dec 20 '11 04:12

Chetan


People also ask

What is a texture ID Roblox?

The TextureId is the content ID of the image that is to be applied to used for the meshes texture. When the TextureId property is set to an empty string, no texture will be applied to the mesh.


1 Answers

Allow me to answer your basic question first: yes, it is legal in OpenGL 2.1 to simply pick a texture name arbitrarily and use it as a texture. Note that it is not legal in 3.2 core. The fact that the OpenGL ARB removed this ability should help illustrate whether it is a good idea or not.

Now, you seem to have run into an odd bug. glGenTextures should never return a texture name that is already in use. After all that is what it is for. You appear to somehow be getting textures that are already in use. I don't know how that happens.

An OpenGL context, when destroyed, will delete all resources specific to that context. If you have created a second context that shares resources with the first, deleting the second context will not delete the shared resources (textures, buffers, etc). It will delete un-shared resources (VAOs, etc), but everything else will remain.

I would suggest not creating and deleting the FBO texture constantly. Just create it on application startup. Worst-case, you'll have to reallocate storage for the object as needed (via glTexImage2D), if you need a different size or something.

like image 52
Nicol Bolas Avatar answered Oct 01 '22 12:10

Nicol Bolas