Log in or register to post comments

Loading textures dynamically

January 9, 2012 - 6:45am #1

Hello.

I'm working on an application that displays some values in empty cases.
The empty cases are on the target and they are filled with augmented reality (the augmented objet is a plane and the texture applied on it is a generated .png with the values placed where they need to appear).

But the problem is that the values are received dynamically. So i need to reload my texture regularly but I don't know if it's possible. (I modified ImageTargets, so I think if it's possible I will need to use loadTextures, but when ?).

Thanks.

Adrien.

Re: Loading textures dynamically

March 15, 2012 - 2:03am #8

I tried bu not succeeded ...
Is it possible not to use the method with the AssetManager only for one of my three images ?
I don't find how to make my image on the SDCard a "Texture".

Thanks.

Adrien.

EDIT : i think I have just solved my problem. I didn't remember we had the possibility modify Texture.java ...

EDIT 2 : here is how I did, if it could help somebody ...
In Texture.java I copied the function loadTextureFromApk() and I renamed it loadTextureFromSDCard().

So

public static Texture loadTextureFromApk(String fileName, AssetManager assets)

becames

public static Texture loadTextureFromSDCard(String fileName)

Then I modified this line :

inputStream = assets.open(fileName, AssetManager.ACCESS_BUFFER);

in

File sdDir = Environment.getExternalStorageDirectory();
File fileToLoad = new File (sdDir + "/MyDirectory/" + fileName) ;
FileInputStream fi = new FileInputStream(fileToLoad) ;
int taille = (int)fileToLoad.length() * 8 ;
byte[] b = new byte[taille];
fi.read(b) ;
inputStream =  new ByteArrayInputStream(b) ;

And in loadTextures() in ImageTargets.java :

mTextures.add(Texture.loadTextureFromSDCard("MyImage.png"));

Re: Loading textures dynamically

March 8, 2012 - 10:48pm #7

Sure. The Texture class included in the samples uses the AssetManager to open the image file. You could replace this with a different approach that loads the file from elsewhere on the file system.

- Kim

Re: Loading textures dynamically

March 8, 2012 - 3:03am #6

Thanks for your answers.

I tried with Ksiva's method and I had no problem. I'm also trying to succeed with yours Mighter, because I think it will be usefull if there is no answer to my next question.

So, my next question is : in the function "loadTextures()", is it possible to load an image which is not in the assets file ?

Adrien.

Re: Loading textures dynamically

February 13, 2012 - 8:32am #5

Consider loading texture data in native code using libpng. I think that one of the reasons why texture data is loaded in java part in the samples, is because they were bundled with apk and prior api version 9 it was tricky to access assets data from native code.
So, look at libpng or other image library for c++ samples.
Hope this helps.

Re: Loading textures dynamically

February 9, 2012 - 7:44pm #4

You can actually call everything together from ImageTargets.java. Just call the renderFrame method on the GL thread by queuing an event with the glView object. Try this code out, it swaps the two teapot textures on tap:

Add to ImageTargets.java

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        DebugLog.LOGI("swapping textures");
        
        Texture temp = mTextures.elementAt(0);
        mTextures.set(0, mTextures.elementAt(1));
        mTextures.set(1, temp);
        
        deinitApplicationNative();
        initApplicationNative(mScreenWidth, mScreenHeight);
        
        // Run initRendering on the GL Thread
        mGlView.queueEvent(new Runnable()
        {
            public void run()
            {
                mRenderer.initRendering();
            }
        });
    }
    return true;
}

- Kim

Re: Loading textures dynamically

February 9, 2012 - 6:52am #3

I saw the methods in ImageTargets.cpp because I modified the 3D model and the targets.

I think I understant what initApplicationNative and deinitApplicationNative do, but I don't know where to call them.

I called initRendering in onDrawFrame, as you said, but i can't call initApplicationNative and deinitApplicationNative here.

Thanks for your help.

Adrien.

Re: Loading textures dynamically

January 9, 2012 - 6:06pm #2

The initApplicationNative method needs to be called first, to pull the image data from Java into native. You may want to call deinitApplicationNative first to release the previously allocated textures.

Then, call the native initRendering method from the render thread (e.g. the onDrawFrame method in ImageTargetsRenderer.java). This method will create the OpenGL textures.

The native methods are located in ImageTargets.cpp. I recommend taking a look at these methods to see what is being done in each.

- Kim

Log in or register to post comments