Loading OpenGL textures in background

Problems with Canvas, OpenGL, etc...

Loading OpenGL textures in background

Postby compila » Thu Oct 14, 2010 11:45 am

I need to load textures from the internet in runtime. It takes quite a lot of time.

The idea is to bind static texture with "Loading" text on it instead of real textures while they are loading from the internet in background.

So the question is: what's the best way for background texture loading?

My solution is to use FutureTask and to override done() method and to call glGenTexure in it. But the problem is there is no access to GL10 gl variable to call it. :(

It is possible to call glGenTexture in the nearest onDrawFrame(GL10 gl), but it is so tricky. :?

What do you think about it?
compila
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 14, 2010 11:25 am

Top

Re: Loading OpenGL textures in background

Postby jonbonazza » Thu Oct 14, 2010 6:39 pm

Why not just handle it in a new thread?
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Loading OpenGL textures in background

Postby compila » Thu Oct 14, 2010 7:03 pm

jonbonazza, what do you mean "handle it in a new thread"?
The problem is the same - where can I get GL10 gl variable?
compila
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 14, 2010 11:25 am

Re: Loading OpenGL textures in background

Postby seed » Thu Oct 21, 2010 5:38 pm

Just store the gl10 variable as a member and use it in your draw() routine?
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Re: Loading OpenGL textures in background

Postby moblcade.com » Mon Oct 25, 2010 2:53 pm

You can only call into OpenGL from the OpenGL thread, so you'll have to finish loading your textures in the OpenGL thread. A compromise would be to load the bitmaps in a separate thread but do not send them to OpenGL yet. Once loaded into a Bitmap, add the Bitmap to a queue of Bitmaps to be processed on the OpenGL thread. At the beginning of each frame process any Bitmaps in the queue. It'll be about as responsive as completely loading the items in a separate thread. You'll have to ensure that your queue is thread-safe and also that you don't attempt to load more Bitmaps at once than you have memory for. That means make sure you call bitmap.recycle() after you've called glGenTexture.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: Loading OpenGL textures in background

Postby seed » Thu Oct 28, 2010 7:37 pm

moblcade.com wrote:You can only call into OpenGL from the OpenGL thread, so you'll have to finish loading your textures in the OpenGL thread. A compromise would be to load the bitmaps in a separate thread but do not send them to OpenGL yet. Once loaded into a Bitmap, add the Bitmap to a queue of Bitmaps to be processed on the OpenGL thread. At the beginning of each frame process any Bitmaps in the queue. It'll be about as responsive as completely loading the items in a separate thread. You'll have to ensure that your queue is thread-safe and also that you don't attempt to load more Bitmaps at once than you have memory for. That means make sure you call bitmap.recycle() after you've called glGenTexture.


This.

Sorry, didn't fully understand the question at first. Yes, download the textures from the internet in a seperate thread. When they are done downloading, queue an event to the gl thread to run the routine that will do the gl loading of the bitmap.

Code: Select all
public void done() {
   queueEvent(new Runnable() {
      @Override
      public void run() {
         glRenderer.loadBitmap(Bitmap bitmap);
      }   
   }
}
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Top

Re: Loading OpenGL textures in background

Postby moblcade.com » Fri Oct 29, 2010 11:09 pm

Yes, that's the idea. However you implement it is up to you. I'd probably just do something like this (pseudo-pseudocode):

Code: Select all
// In separate thread...
for each bitmap to load:
    bitmap = loadBitmap(...);
    synchronized (queue) {
        queue.add(bitmap);
    }
    ...

// glRenderer:
public void onDrawFrame(GL gl) {
    sychronized (queue) {
        while queue.hasRemaining():
            bmp = queue.dequeue();
            sendToOpenGl(gl, bmp);
            bmp.recycle();
    }
}

moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests