views:

1139

answers:

1

I was wondering if anyone could advise on a good pattern for loading textures in an Android Java & OpenGL ES app.

My first concern is determining how many texture names to allocate and how I can efficiently go about doing this prior to rendering my vertices.

My second concern is in loading the textures, I have to infer the texture to be loaded based on my game data. This means I'll be playing around with strings, which I understand is something I really shouldn't be doing in my GL thread.

Overall I understand what's happening when loading textures, I just want to get the best lifecycle out of it. Are there any other things I should be considering?

+5  A: 

1) You should allocate as many texture names as you need. One for each texture you are using.

Loading a texture is a very heavy operation that stalls the rendering pipeline. So, you should never load textures inside your game loop. You should have a loading state before the application state in which you render the textures. The loading state is responsible for loading all the textures needed in the rendering. So when you need to render your geometry, you will have all the textures loaded and you don't have to worry about that anymore.

Note that after you don't need the textures anymore, you have to delete them using glDeleteTextures.

2) If you mean by infer that you need different textures for different levels or something similar, then you should process the level data in the loading state and decide which textures need to be loaded.

On the other hand, if you need to paint text (like current score), then things get more complicated in OpenGL. You will have the following options: prerender the needed text to textures (easy), implement your own bitmap font engine (harder) or use Bitmap and Canvas pair to generate textures on the fly (slow).

If you have limited set of messages to be shown during the game, then I would most probably prerender them to textures as the implementation is pretty trivial.

For the current score it is enough to have a texture which has a glyph for numbers from 0 to 9 and to use that to render arbitrary values. The implementation will be quite straightforward.

If you need longer localized texts then you need to start thinking about the generating textures on the fly. Basically you would create an bitmap into which you render text using a Canvas. Then you would upload it as a texture and render it as any other texture. After you don't need it any more, then you would delete it. This option is slow and should be avoided inside the application loop.

3) Concerning textures and to get the best out of the GPU you should keep at least the following things in your mind (these things will get a bit more advanced, and you should bother with them only after you get the application up and running and if you need to optimize the frame rate):

  • Minimize texture changes as it is a slow operation. Optimally you should render all the objects using the same texture in a batch. Then change the texture and render the objects needing that and so on.
  • Use texture atlases to minimize the number of textures (and texture changes)
  • If you have lots of textures, you could need to use other bit depths than 8888 to make all your textures to fit in to the memory. Using lower bit depths may also improve performance.
Lauri
When you say "loading state", is it alright for that to be at the same time as I initialize my renderer?My current thinking is to have it initialize my scene graph and query all the objects in the scene graph for the textures they'll end up requesting (by having them infer names from the game model).Each object that requested a texture would then be provided with the corresponding OpenGL integer "name" of the textures it asked for.Does this seem sound?
Omega
I like to model high level application logic using state machines. Thus the term loading state.It is completely alright to do this at the same time you do the rest of the initialization. It is just a matter of how you want to design your application logic.So it does seem sound.
Lauri