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


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

Help appreciated


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

enter image description here

When hovering over the underline, this is the error:

enter image description here

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – 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.

  • \$\begingroup\$ Perfect. I just used a static Int to hold the last bound texture ID and checked against that - works great! :-) \$\endgroup\$ – 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);
  • \$\begingroup\$ 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 :-) \$\endgroup\$ – BungleBonce Jun 21 '14 at 23:28
  • \$\begingroup\$ Just use a GLint, it is an existing data type. Otherwise you could just use an integer. \$\endgroup\$ – Eejin Jun 22 '14 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.