A Question about glGenTexture and BindTexture

I’m confused about glGenTexture and BintTexture…

glGenTexture simply generates a number(s) for you.
You later use this number to bind a texture to that number.
However assuming you have many or one textures in memory,
how does OpenGL know which texture is enabled? Or are they all enabled at the same time?

How do you clear out old textures from memory? I see glDeleteTextures… but I don’t know the texture names…
(Assume a program crashes and another one comes along and wants to clean out the texture memory.)

you enable a texture by ‘binding’ it.
so, after you’ve gotten your texture name and downloaded a texture to it you would bind it.

this is all explained in the OpenGL redbook (which you should have a copy of) and in a spec from way back when on this very site.

check it out:http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node87.html

How do you clear out old textures from memory? I see glDeleteTextures… but I don’t know the texture names…

You should know the texture names…you have to hold on to these!

No, you enable a texture unit by calling glEnable for that unit with a texture_1D 2D or 3D target. In the case of a single texture you only use one texture unit. That texture unit will use the last texture bound to render with. Remember that the bind call has a target and a handle, although these targets might seem different, 1D 2D and 3D texture it’s really a bit of a redundant description. The 1D 2D and 3D tokens really only apply to their respective targets but a target is not a texture unit. It’s really an ugly and somewhat redundant semantic difference.

Let’s look at this a moment

glTexImage 1D 2D 3D() calls

glBindTexture( 1D 2D 3D ) targets

glEnable( 1D 2D 3D ) targets

None of this has anything to do with multitexture of course, this is all to support a single texture unit and clearly there’s redundancy. You could easily see that just the glTexImage 1D 2D and 3D calls would suffice from an API standpoint with generic enables and binds for the other calls.

So don’t sweat the details, if you’re using 3D texture use 3D TexImage and target tokens but don’t worry about it these are not separate units this is just an API designed 10 years before multitexture was implemented that was trying to be flexible and futureproof.

Oops, that’s right.
So, to select a texture for use, it must first be bound (glBindTexture) and then enabled (glEnable).

glGenTextures()
- Generate a name for the texture

glTexImage{1,2,3}D()
- Allocate memory and Load texture into memory

glBindTexture()
- Select which texture name to use

glEnable(GL_TEXTURE_{1,2,3}D)
- Enable textures to be drawn or not

I initialize my texture by allocating memory and loading it. Then anywhere I want to use the texture, I would use glBindTexture() to select the texture to use. If you already have enabled textures to be used, then you don’t have to do it again. Unless, of course, you have used glDisable() to turn off texturing.

NO that is wrong.

glGenTextures just reserves and delivers the texture handle(s) (a unique identifying integer). It does not allocate any texture memory, it doesn’t even know anything about the texture. Before you call glTexImage you must bind with one of the handles from glGenTextures so OpenGL knows which texture object the glTexImage applies to.

You still only have one texture unit with all these calls, the bind is just a mechanism to remember and aggregate texture object information including format filters etc. The bind also has a 1D 2D 3D etc, but as I have said already it’s redundant, just make sure the token you use is the same token (1D 2D 3D) you use in your teximage call.

I was just answering the original posters questions, trying to stay to just that information, not branching off into unrelated information, like multitexturing …

thanks. :slight_smile:

Assuming you meant glGenTextures() and not glTexGen() which is something else.

glGenTextures() generates texture names that can be safely used.

glBindTexture() specifies what texture name you currently want to use.

glTexImage*() loads image data into OpenGL and associates this image data with the currently bound name. If data is already associated with that name, it will be replaced.

glEnable(GL_TEXTURE_1D), glEnable(GL_TEXTURE_2D)etc., turns on texturing for that type of texture. When turned on, it will use whatever texture is currently bound when drawing your primitives.