Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Originally, I was using individual textures (ie, individual .png files) for my sprite's animation and recently altered my code so that I could put all my animation frames into 1 file (so it basically is like a mini sprite sheet - I say 'mini' because I'm using 1 file per sprite, I'm not using 1 file for all my sprites).

Anyway my question relates to glBindTexture. Originally, I was switching textures when I wanted to update the animation frame so was calling this in my sprite's draw method:

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);  //texID being the texture ID

However, now what happens is that I work out the texture offset and just point to part of the texture that I require. The texture itself does not change anymore, so do I still require this line every time I draw?

I tried removing the line from my draw() method and just calling it once when I loaded the texture initially, but the sprite doesn't draw with the correct texture, it uses a completely different, un-related texture. So what is the correct course of action in cases like this where a sprite-sheet is being used?

At the moment, I'm not sure why this would be any faster than using individual files and switching the texture every frame. In fact as I'm now having to work out the texture offset (whereas previously it was fixed), there is more overhead. So I'm guessing the advantage comes from not having to call this glBindTexture line every time I draw (as it's expensive). So how do I correctly choose the texture to assign to this sprite other than within the draw method?

Any help would be appreciate, if you need code please ask I didn't post my whole draw method as I didn't think it was necessary to explain the issue.

This is what I was doing previously

Bind texture 1 (Animation frame 1, textures 1-8 belong to this sprite)
Draw Sprite 1

Bind texture 9
Draw Sprite 2

Repeat binding texture 2, then 3 etc for sprite 1 to animate it

I am now using a single texture for sprite1 which contains all 8 animation frames

Bind texture 1 (all frames)
Draw Sprite 1 (pointing to frames 1, 2, 3 etc)

Bind texture 5
draw sprite 2
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You need to bind the texture you are using before any draw calls you want to use that texture with. I assume you have something like this:

  • Sprite 1 using texture 1
  • Sprite 2 using texture 2
  • Sprite 3 using texture 3

Then you must draw them like this:

  1. Bind texture 1
  2. Draw sprite 1
  3. Bind texture 2
  4. Draw sprite 2
  5. Bind texture 3
  6. Draw sprite 3

Now you can improve the speed of rendering the sprites quite a bit. If you do not want to include ALL sprites in one texture, then you might do it like this:

  • Sprite 1 using texture 1
  • Sprite 2 using texture 2
  • Sprite 3 using texture 1

And drawing:

  1. Bind texture 1
  2. Draw sprite 1
  3. Draw sprite 3
  4. Bind texture 2
  5. Draw sprite 2

Now you see, there is reduced amount of the expensive texture binding calls. But you can still improve this by putting all sprites in one texture:

  1. Bind large texture with all sprites
  2. Draw sprite 1
  3. Draw sprite 2
  4. Draw sprite 3

This way you minimize the texture binding calls, which speeds up the rendering process a bit.

share|improve this answer
    
Hey @Lasse. many thanks for the comment. Basically, I'm only using 1 texture per sprite. So let's say I have 20 sprites each with their own texture, each one an individual object of my sprite class (which contains my render method) - do I need to put glBindTexture in my draw() method (bearing in mind that it's in the class of which my sprites are an instance, so therefore will only be using one texture)? Hope I'm making sense. Basically, can I not just bind the texture to the object once at object creation? If not, what are the advantages of using a sprite-sheet? why is is deemed 'faster'? –  user22241 Aug 19 '13 at 15:45
    
Yes, if you have unique textures for each sprite you draw, then you need to call the texture binding function before every drawing call. –  Lasse Aug 19 '13 at 15:50
    
Let me edit my answer a bit. –  Lasse Aug 19 '13 at 15:50
    
When you had all the animation frames in separate textures, it just took more time to load and more memory was used. Your current implementation helps on those issues, but if you want more speed in rendering, you need to combine your sprites in to less textures. –  Lasse Aug 19 '13 at 16:02
    
Thanks @Lasse, I didn't realise this, I've edited my question to show how I am now doing things, so as far as rendering goes there is no advantage doing what I'm doing as I still need to keep re-binding the texture? If I have an instance variable attached to one of my sprite objects it is specific to that object, is this not the case with GL textures? ie, the binding and rendering code are specific to an instance of my sprite class, but the actual texture ID doesn't tie itself to that instance for some reason and needs to keep being re-bound? Just so I'm clear on this - thanks! –  user22241 Aug 19 '13 at 16:13
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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