3

I'm making a game and I've created a separate thread for loading assets, 3d models, etc. So that the UI thread won't lock up while its loading. However, the thread for loading requires an instance of GL10 in order to load and map textures correctly.

Heres an overview of the problem so you can better understand my predicament:
1. my Renderer class creates and starts the "loading" thread.
2. The loading thread loads the models and textures from assets
3. 'glGenTextures' is required to load the textures, but the loading thread doesn't have an instance of GL10

I tried just giving the loading thread the GL10 instance given by the Renderers onSurfaceCreated method, but it doesn't work.(I guess it gets deleted, or messed up, or something when the function is over)

So, how would I be able to pass a working instance of GL10 to my loading thread?

2 Answers 2

5

The answer is your can't.

In opengl android, the gl object/context only exist in the rendering loop. To my knowledge you can't use the gl functions outside that thread.

the reason is simple opengl is a state machine that isnot thread proof -one main reason is that it would slow down the rendering if you did add the test -being a state machine what happens when you want to draw something and you change a state like blend at the same time -...

What you want to do is do all the work that is non gl related in your thread (open bitmaps read point files ...) in your rendering loop you add a if(something gl related is to be done){...} and when your thread is ready change the flag to let the rendering loop know you want to load something

1
  • 2
    I'm curious, did you manage to make it work in a thread or did you make it work like i explained ? Commented Jan 18, 2011 at 1:14
0

This is quite do-able on iOS and OSX, and as long as you synchronize your access to the GL state machine, there are no theoretical problems unless Android has created limitations in it's implementation. I've just tried uploading a texture while I refrain from imaging and things don't work. Like I said, this technique works fine every else ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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