开发者

Getting GL10 instance on separate thread on Android

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开发者_JS百科 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?


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


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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜