Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading textures in an Android OpenGL ES App

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?

like image 506
Alexander Trauzzi Avatar asked Mar 23 '10 15:03

Alexander Trauzzi


People also ask

How do you show textures in OpenGL?

You need to bind the texture, enable texturing (fixed function pipeline) or use a shader that does texturing (shader pipeline), and draw some shape like a quad. Don't forget the shape you draw needs texture coordinates.

Can I use OpenGL on Android?

The basics. Android supports OpenGL both through its framework API and the Native Development Kit (NDK).

What is OpenGL es texture?

The OpenGL method can be used to generate multiple handles at the same time; here we generate just one. Once we have a texture handle, we use it to load the texture.

What is the difference between OpenGL and OpenGL es?

The main difference between the two is that OpenGL ES is made for embedded systems like smartphones, while OpenGL is the one on desktops. On the coding level, OpenGL ES does not support fixed-function functions like glBegin/glEnd etc... OpenGL can support fixed-function pipeline (using a compatibility profile).


2 Answers

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.
like image 112
Lauri Avatar answered Sep 18 '22 13:09

Lauri


This should be a comment to Lauri's answer, but i can't comment with 1 rep, and there's a thing that should be pointed out:

You should re-load textures every time your EGL context is lost (i.e. when your applications is put to background and back again). So, the correct location to (re)load them is in the method

public void onSurfaceChanged(GL10 gl, int width, int height)

of the renderer. Obviously, if you have different textures sets to be loaded based (i.e.) on the game level you're playing then when you change level you should delete the textures you're not going to use and load the new textures. Also, you have to keep track of what you have to re-load when the EGL context is lost.

like image 20
zAvo Avatar answered Sep 19 '22 13:09

zAvo