Java – Get GL10 instances on a separate thread on Android

Get GL10 instances on a separate thread on Android… here is a solution to the problem.

Get GL10 instances on a separate thread on Android

I’m working on a game and I’ve created a separate thread to load Assets, 3d models, etc. This way the UI thread doesn’t lock up on load. However, a loading thread requires a GL10 instance to properly load and map textures.

Here’s an overview of the issue so you can better understand my dilemma:
1. My Renderer class creates and starts a “loading” thread.
2. The loading thread loads models and textures
from assets
3. “glGenTextures” are required to load textures, but the loading thread does not have an instance of GL10

I’ve tried giving only the GL10 instance given by the loading thread Renderers on SurfaceCreated method, but it doesn’t work. (I guess it was removed, or messed up, or what happened when the function ended).

So, how can I pass a working instance of GL10 to my load thread?


The answer is you can’t.

In OpenGL Android, GL objects/contexts only exist in the render loop.
As far as I know, you can’t use the gl function outside of that thread.

The simple reason is that OpenGL is a non-thread-proof state machine
– One major reason is that if you do add tests, it slows down rendering
– As a state machine, what happens when you want to draw something, and at the same time you change a state, such as mixing

What you want to do is do all the GL-independent work in your thread (open bitmap read point files…)
In your render loop you add an if(something gl related is to be done){…}
When your thread is ready, change the flag to let the render loop know you want to load something

Related Problems and Solutions