Log in or register to post comments

Load textures from apk at runtime

October 20, 2012 - 7:11am #1

Hey Guys,

Need help on a small thing. What i want to do is that i want to load textures from apk or from sdcard at runtime and be able to use them. I do not want to initialise them at the startup of the app e.g. if a user presses a button these textures should load from the sdcard or apk and be ready for use. Also i have got a basic idea that i can do this using the async task in background but not able to implement it i.e.what function calls to do in order to go complete the total loading process and update mtextures. Could somebody please give me some hints on how to move forward on this?

Thanks in advance,

Tarun

Load textures from apk at runtime

August 13, 2016 - 3:28am #11

Hi! I just solved this problem and I'd like to share my methods with you. The key problem is not about the texture size( it doesn't need to be size of power of 2).

I worked on the ImageTargets sample, and I needed to get dynamic textures from a server through TCP link. After I dynamically load the texture into mTexture in ImageTarget.java like this( I referred to the loadTextureFromApk() function and wrote the loadTextureFromByteArray() function ):

mTextures.set(0,Texture.loadTextureFromByteArray(buffer,offset,texLen));

I added the following codes into the begining of renderFrame() function in ImageTargetRenderer.java, to generate a new texture and bind it to GLES20.GL_TEXTURE_2D, and it worked! 

            GLES20.glGenTextures(1, mTextures.get(0).mTextureID, 0);

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures.get(0).mTextureID[0]);

            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,

                    GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,

                    GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,

                    mTextures.get(0).mWidth, mTextures.get(0).mHeight, 0, GLES20.GL_RGBA,

                    GLES20.GL_UNSIGNED_BYTE, mTextures.get(0).mData);

Before that, I tried to call initRendering() after the texture is loaded, but it didn't work. I also tried to change my texture into 512*512 size, which didn't work either.

I've been searching for the solution for almost two days and finally I fixed it with myself! Hope this can help you. 

This solution may cost lots of CPU time and I'm looking for a faster solution.

Glad to hear it. You're

January 10, 2013 - 2:45pm #10

Glad to hear it. You're welcome.

Hi AllessandroB.   thanks for

January 10, 2013 - 1:39pm #9

Hi AllessandroB.

 

thanks for your fast replay, i now managed to solve the issue and load textures at run-time

 

 

thanks alot

ozi

Hi ozieg, if I understand

January 10, 2013 - 12:52pm #8

Hi ozieg,

if I understand correctly, you call glTexImage at every rendering frame, are you?

Note that this should not be necessary, i.e. you should call glTexImage only if you detect that a new image has been loaded and a new texture must be created;

then, once the new texture has been created (and you have called glTexImage just once to update its content), in general you would simply need to call glBindTexture( ) which is an extremely cheap operation (i.e. it should not slow you down), while the glTexImage() is very expensive.

I hope what I'm saying is clear to you. Let me know if you need more details. I'm happy to help.

 

Hi.   i have the same

January 10, 2013 - 11:01am #7

Hi.

 

i have the same problem, trying to load textures at run time does not work for me,

i found a work-around for the time beeing: i followed everything allesandroB suggested , as well as calling

"glTexImage2D" after each call to "glBindTexture" , this does work , but slows the system a lot , is there any other solution?

 

 

thanks

ozi

 

Load textures from apk at runtime

December 3, 2012 - 3:27am #6

Hi, can you paste here the relevant code snippets where you do:

1- create your textures in OpenGL (code with glGenTextures(...) and surrounding code)

2- bind your texture to render them (code with glBindTexture() and its surrounding code)

?

Also, have you checked your Logs to see if there is any I/O error/exception thrown by Java when loading the images ?

(so to be sure that the App can really find the images and correctly load them)

Load textures from apk at runtime

December 2, 2012 - 11:01am #5

Hey,
Tried everything as you said and the textures are being loaded into the system. But when it comes to render the textures it turns out black(the image is 256*256). Not getting what the problem is? Any help or pointers please?

 

Thanks 

Load textures from apk at runtime

October 22, 2012 - 7:59am #4

Yes, let me know how it goes, and if I can help more.

Load textures from apk at runtime

October 22, 2012 - 7:09am #3

Thanks a lot for the detailed reply!!Smile Will try out what you have suggested and get back!

 

 

Load textures from apk at runtime

October 22, 2012 - 2:18am #2

It is certainly possible to load textures at runtime; to do that you will need to pay attention to the following (I’m referring to the ImageTargets sample to have a common point of reference):

1.       When you create a new texture in Java, you will need to create a corresponding new texture on the native side (see the code “Texture::create( env, textureObject )” in the native code); this is currently done all "in one go" in the initApplicationNative in current sample, so in your case you’ll need to define some native “addTextureNative” function (or call it how you prefer) and call it from Java whenever you load a new texture.

2.       For each new texture added in native, you will need to create a corresponding OpenGL texture; now this is all done in the initRendering function in ImageTargets.cpp, but you will need to actually do this for each new texture, therefore put the code in the renderFrame() and call it when you detect that there is a newly created texture that was not there before.

 

For what concerns How to load a bitmap without blocking the UI and the rendering (in case the loading process is slow), you can have a look at this nice article which explains how to load bitmaps using an AsyncTask: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

Note that if the images you want to load are rather small, you might be able to load them fast enough without problems (i.e. without having to use an AsyncTask);

Also, this article explains you some tips about loading large bitmaps (very useful reading):

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

I hope this helps.

Log in or register to post comments