# How to obtain the currently bound texture ID in an openGL ES 2.0 project

In my game, I have:

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);


Where texID is an integer returned by my setTexture() method. Let's say, in this instance it's 1.

When I bind my textures during rendering calls, I don't want to bind this texture every single call. Because I use different atlases of various textures and it's pretty wasteful to keep re-binding a texture when it's not required.

So, I would like to do something like this (Pseudo code)

if (texID != *currentTexture*){
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);
}


Therefore, if the texture I want to use is the same as the one already bound, the call to re-bind it will be ignored.

I can't work out how to get the ID of the texture that is currently bound.

I've tried

GLES20.glActiveTexture();


But this doesn't return the textureID, it returns the texture Units

Help appreciated

Edit

I've tried the suggestion below but I'm getting the folowing errors:

When hovering over the underline, this is the error:

I had searched this site and the wider web for a couple of hours before posting here but couldn't find a proper usage example, just this out of context snippet.

• GL_INT is a symbolic constant used to identify types internally. What you are looking for is GLint, this guy is an actual type! Also, you might consider just remembering the current texture yourself, instead of querying the OpenGL state. – glampert Jun 22 '14 at 3:46

You should reconsider whether you really need to do this. Most of the time, querying the GL for such information should be avoided because it has an important performance cost.

Since you are the one doing the glBindTexture calls, you could track the resources yourself instead (this is called client state tracking). It could be as simple as having a global LastBoundTexID variable.

• Perfect. I just used a static Int to hold the last bound texture ID and checked against that - works great! :-) – BungleBonce Jun 22 '14 at 12:21

A very quick google search (2 minutes) reveals that you can get the active binding this way in opengl:

GLint whichID;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &whichID);

• Hi@ Eejin, this was one of the first things I found when I was searching for the answer. However, I've had no joy with it. I must be using it incorrectly. I'm putting it in my draw routine - just before the bind command. Would this be the correct place to put it? I'm getting an error: "GLES20.GL_INT cannot be resolved to a type" Not sure why I'm getting this, perhaps you could expand your answer to give a usage example within the context of the question? This is Android btw. Thanks :-) – BungleBonce Jun 21 '14 at 23:28
• Just use a GLint, it is an existing data type. Otherwise you could just use an integer. – Eejin Jun 22 '14 at 9:33