Log in or register to post comments

set mTextures after initial call

July 18, 2011 - 6:04am #1

I am trying to change mTextures from outside the loadTextures class. The
idea is to be able to change a texture after loadTextures is called at
startup. Have tried mTextures.set() and most other options from somewhere
else in the ImageTargets class. Why is the mTextures.set() in
loadTextures(), not able to change the texture and display something on
command?
Any direction would be helpful,
Thank you,

Grant

Re: set mTextures after initial call

April 11, 2012 - 9:24pm #13

Sorry, we can't help with such specific coding problems, with the snippet above you should have everything you need to get started. The rest is just application-specific logic :)

- Kim

Re: set mTextures after initial call

April 11, 2012 - 7:25am #12
ksiva wrote:

If you only have a small set of photos (e.g. less than 20) you may just want to load them all as textures and switch the texture index in the renderFrame method. If you want to load the image into texture memory on each tap you can use the code above and simply switch out the filename (e.g. cycle through a list of filenames).

- Kim

Thanks Kim..Basically the pictures (texture) are like 7/8 but when I touch the screen the picture should switch..that means every time I touch the display picture will switch. So If I want to use same coding (Above) here, how I can switch filename here

mTextures.set(0, Texture.loadTextureFromApk("TextureTeapotBlue.png", getAssets()));
mTextures.set(1, Texture.loadTextureFromApk("TextureTeapotBrass.png", getAssets()));

Can you help me with some coding here ..

Thanks

Rassall

Re: set mTextures after initial call

April 10, 2012 - 3:13pm #11

If you only have a small set of photos (e.g. less than 20) you may just want to load them all as textures and switch the texture index in the renderFrame method. If you want to load the image into texture memory on each tap you can use the code above and simply switch out the filename (e.g. cycle through a list of filenames).

- Kim

Re: set mTextures after initial call

April 9, 2012 - 8:59pm #10
ksiva wrote:

Take a look at ImageTargets.cpp. The initApplicationNative() method copies all of the texture information from Java to native. Then the initRendering() method generates OpenGL textures and loads this texture data into them. Starting with the ImageTargets sample, I was able to switch the stones target teapot texture at runtime using the following code (added to ImageTargets.java):

[CODE]
private boolean didSwitchTexture = false;

@Override
public boolean onTouchEvent(MotionEvent event)
{
if (!didSwitchTexture)
{
DebugLog.LOGI("setting texture");
mTextures.set(0, Texture.loadTextureFromApk("TextureTeapotBlue.png", getAssets()));
initApplicationNative(mScreenWidth, mScreenHeight);
mGlView.queueEvent(new Runnable()
{
.....

- Kim

Hi Kim,

I m trying to create a photo album where texture (picture) will change every time I touch the display. Now here, only 1 texture will switch. How I can add more texture (suppose 5 texture image) here to switch after every time I touch the screen.

regards

Rassall

Re: set mTextures after initial call

March 5, 2012 - 8:50am #9

The ImageTargets sample binds the texture in the ImageTargets.cpp renderFrame method with the following code:

// Choose the texture based on the target name:
int textureIndex;
if (strcmp(trackable->getName(), "chips") == 0)
{
    textureIndex = 0;
}
else if (strcmp(trackable->getName(), "stones") == 0)
{
    textureIndex = 1;
}
else
{
    textureIndex = 2;
}

const Texture* const thisTexture = textures[textureIndex];

...

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);

The application shouldn't restart when using the code in my previous post. What version of the SDK are you using? I just tried it with 1.5, ran the app, looked at the chips target, and tapped the screen to swap the texture. It seems to work fine.

Look at the log when you tap the screen to see if there are any errors. You can find the log in the DDMS perspective in Eclipse.

- Kim

Re: set mTextures after initial call

March 4, 2012 - 2:29pm #8

I put that code in my ImageTargets.java, but when I touch the screen applications restarts and still uses old texture. I think I am missing

Quote:

you have to bind these textures yourself when rendering your 3D content in the renderFrame() native method.

part. Can you make it more clear?

thank you.

ksiva wrote:

Take a look at ImageTargets.cpp. The initApplicationNative() method copies all of the texture information from Java to native. Then the initRendering() method generates OpenGL textures and loads this texture data into them. Starting with the ImageTargets sample, I was able to switch the stones target teapot texture at runtime using the following code (added to ImageTargets.java):

private boolean didSwitchTexture = false;

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (!didSwitchTexture)
    {
        DebugLog.LOGI("setting texture");
        mTextures.set(0, Texture.loadTextureFromApk("TextureTeapotBlue.png", getAssets()));
        initApplicationNative(mScreenWidth, mScreenHeight);
        mGlView.queueEvent(new Runnable()
        {
            public void run()
            {
                mRenderer.initRendering();
            }
        });
        didSwitchTexture = true;
    }
    
    return true;
}

That code will swap the texture just once on the first tap.

Where are you calling the loadArt() method from? Are there any errors in the log?

Also, just so it's clear, you have to bind these textures yourself when rendering your 3D content in the renderFrame() native method. See the sample applications.

- Kim

Re: set mTextures after initial call

July 29, 2011 - 11:28am #7

Thanks Kim all worked out well. Thank you for the help. Im going to post a new thread on a different topic so look out. Problems with gestures, such as fling, over the camera. Thats for a new thread which i'll write now in rendering open GL.

Re: set mTextures after initial call

July 25, 2011 - 2:51pm #6

Take a look at ImageTargets.cpp. The initApplicationNative() method copies all of the texture information from Java to native. Then the initRendering() method generates OpenGL textures and loads this texture data into them. Starting with the ImageTargets sample, I was able to switch the stones target teapot texture at runtime using the following code (added to ImageTargets.java):

private boolean didSwitchTexture = false;

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (!didSwitchTexture)
    {
        DebugLog.LOGI("setting texture");
        mTextures.set(0, Texture.loadTextureFromApk("TextureTeapotBlue.png", getAssets()));
        initApplicationNative(mScreenWidth, mScreenHeight);
        mGlView.queueEvent(new Runnable()
        {
            public void run()
            {
                mRenderer.initRendering();
            }
        });
        didSwitchTexture = true;
    }
    
    return true;
}

That code will swap the texture just once on the first tap.

Where are you calling the loadArt() method from? Are there any errors in the log?

Also, just so it's clear, you have to bind these textures yourself when rendering your 3D content in the renderFrame() native method. See the sample applications.

- Kim

Re: set mTextures after initial call

July 23, 2011 - 9:57pm #5

Kim I tried adding your reference to mRenderer into a separate method only called when the texture is changing. My method for this is called loadArt(). It looks like this now....

public void loadArt(){
mTextures.set(0, //file//)
initApplicationNative(mScreenWidth, mScreenHeight);
mGlView.queueEvent(new Runnable()
{
public void run()
{
mRenderer.initRendering();
}
});
Log.i("Finished:", "Run() newTexture has finished!");
}

From the log() I know that the entire method is running through when it is supposed to be called and its call is happening on cue with my program. I think i'm pretty close, but you just through something out there I don't fully get. I initialize the Rendering, but what is it adapting/adding to the native. mGLView is acting as the AR surface of interest now, but i'm still not getting an image on the new surface, No new texture.
Should I be handing the renderer the new texture items somehow?
Is there anything else that could be missing?
I have the files to be displayed as texture but they dont seem to be getting there.

Re: set mTextures after initial call

July 23, 2011 - 3:59pm #4

Ah, of course, you need to call the native initRendering() method as well to actually generate the OpenGL textures. Better yet create your own method that only updates the textures, so you don't recreate the shaders each time as well.

The trick here is that these GL textures must be generated on the GLThread, so if you are updating your textures from the main activity thread you'll need to do something like this:

mGlView.queueEvent(new Runnable()
{
    public void run()
    {
        mRenderer.initRendering();
    }
});

- Kim

Re: set mTextures after initial call

July 23, 2011 - 10:53am #3

I thank you for the start Kim, but I am still shaky on calling the Native.
I have tried something like.....

mTextures.set(0, file**I have this file only after recognition**);
initApplicationNative(mScreenWidth, mScreenHeight);

Is this what you meant I should try, because I feel I am mistaken in another way(this isnt helping create the new textures).

In onCreate() the method loadTextures() is called and at that time the only texture in the array is something statically written into that loadTextures Method. I guess my main question In addition to how/where I must call initApplicationNative() is somewhere along the lines of how can I recall the loadTextures class to identify new Textures not yet known when onCreate is run. Should I add something to onResume() or elsewhere?

Is my call to initApplicationNative sufficient?
Where can I recall if necessary the loadTextures() so that new textures in the array can be set, and shown?

Grant

Re: set mTextures after initial call

July 20, 2011 - 9:31am #2

mTextures is just an array of texture objects that the native (C++) code queries to create OpenGL textures. The samples add textures to the array with the mTextures.add(Texture.loadTextureFromApk("filename.png", getAssets())); method call. Then, initApplicationNative() is called to create the textures in native (see this method in ImageTargets.cpp).

You should be able to add textures at a later time if you call initApplicationNative() after adding them to the array. You'll probably want to release any existing textures in native first, however, to avoid leaking memory.

- Kim

Log in or register to post comments