ellljoy / min3d

Automatically exported from code.google.com/p/min3d
0 stars 0 forks source link

Add support for Asynchronously loading Models & Textures (with FIX!) #23

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have a few very big models with textures. Loading the model in the 
initScene() method causes an ANR in Android. The solution i added was loading 
the model asyncrhonously using an AsyncTask

However the textures are not loaded in this way properly. I fixed this by 
adding an addPostponedTexture to the TextureManager.. Then once loading is 
done, the textures are uploaded to the GL thread.

I added the following code to TextureManager:

    private List<PostponedTexture> postponed = null;

    public String addTextureIdPostponed(Bitmap $b, String $id, boolean $generateMipMap)
    {
        PostponedTexture p = new PostponedTexture();
        p.b = $b;
        p.id = $id;
        p.mipmap = $generateMipMap;
        if(postponed == null) {
            postponed = new LinkedList<PostponedTexture>();
        }
        postponed.add(p);

        return $id;
    }

    public void addPostponedTextures() {
        if(postponed != null) {
            for(PostponedTexture t : postponed) {
                addTextureId(t.b, t.id, t.mipmap);
                t.b.recycle();
                t.b = null;
            }
            postponed = null;
        }
    }

    class PostponedTexture {
        Bitmap b;
        String id;
        boolean mipmap;
    }

And then in Renderer i added the following:

    public void onDrawFrame(GL10 gl)
    {
        _textureManager.addPostponedTextures();

This solves the texture not loaded when using AsyncTask to load the models.

Original issue reported on code.google.com by tje...@gmail.com on 8 Dec 2010 at 9:56

GoogleCodeExporter commented 8 years ago
I also removed the texture.recycle from the ObjParser, since this is now done 
in addPostponedTexture

Original comment by tje...@gmail.com on 8 Dec 2010 at 10:01

GoogleCodeExporter commented 8 years ago
The fix doesn't work for md2 models. Can you please point me on the right track 
for solving the md2 problem also?

I'm using an AsyncTask to do the init but the md2 animated objects have no 
texture. Also even if I do the initial init without the asynctask, if I try 
loading another md2 it loses the texture.

Thank you!

Original comment by 1.Sync....@gmail.com on 1 Jul 2011 at 11:33

GoogleCodeExporter commented 8 years ago
Can you send an example or tell us where you call your method 
"addTextureIdPostponed" because I can not implement your method

Original comment by alexis.l...@gmail.com on 22 Jul 2011 at 8:53