java - Android: How to change OpenGLES texture data / bitmap from gallery / camera -


i try reuse android media effects samples , want able change texture data new bitmap camera or gallery pick. no solution far

this initial load texture sample : https://github.com/googlesamples/android-mediaeffects/blob/master/application/src/main/java/com/example/android/mediaeffects/mediaeffectsfragment.java

    private void loadtextures() {         // generate textures         gles20.glgentextures(2, mtextures, 0);          // load input bitmap         bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.puppy);         mimagewidth = bitmap.getwidth();         mimageheight = bitmap.getheight();         mtexrenderer.updatetexturesize(mimagewidth, mimageheight);          // upload texture         gles20.glbindtexture(gles20.gl_texture_2d, mtextures[0]);         glutils.teximage2d(gles20.gl_texture_2d, 0, bitmap, 0);          // set texture parameters         gltoolbox.inittexparams();     } 

and trouble function :

public void changebackground(uri bitmapuri) {    log.d(tag, "changebackground : " + bitmapuri.getpath());    bitmap bitmap = bitmapfactory.decodefile(bitmapuri.getpath());   mimagewidth = bitmap.getwidth();   mimageheight = bitmap.getheight();   mtexrenderer.updatetexturesize(mimagewidth, mimageheight);    glutils.texsubimage2d(gles20.gl_texture_2d, 0, 0, 0, bitmap);    thebackground.requestrender(); //my glsurfaceview   bitmap.recycle(); } 

i tried several ways :

  • using texsubimage2d -> texture not updated

  • delete , recreate textures new bitmap -> black texture

note: bitmap dimension fixed, @ 1080 x 1080

how change/modify/replace texture data new bitmap ?

update:

i think problem more relate question : setting texture on 3d object using image photo gallery. call changebackground() after grabbing image gallery intent , crop (another intent).

now, how change texture data bitmap gallery intent? have clue?

solved

i solved problem defering texture load process onsurfacechange() function. use flag indicate file load , onsurfacechange check flag again.

private uri mfiletoload = null;    public void changebackground(uri bitmapuri) {     log.d(tag, "changebackground : " + bitmapuri.getpath());     mfiletoload = bitmapuri;     thebackground.requestrender();  }

and onsurfacechange()

    public void onsurfacechanged(gl10 gl, int width, int height) {          log.d(tag, "onsurfacechanged w:" + width + " h:" + height);          if (mfiletoload!=null) {              gles20.gldeletetextures(mtextures.length, mtextures, 0);              gles20.glgentextures(mtextures.length, mtextures, 0);                bitmap bitmap = bitmapfactory.decodefile(mfiletoload.getpath());              mimagewidth = bitmap.getwidth();              mimageheight = bitmap.getheight();              mtexrenderer.updatetexturesize(mimagewidth, mimageheight);                // upload texture              gles20.glbindtexture(gles20.gl_texture_2d, mtextures[0]);              glutils.teximage2d(gles20.gl_texture_2d, 0, bitmap, 0);                // set texture parameters              gltoolbox.inittexparams();                mfiletoload = null;              bitmap.recycle();          }            if (mtexrenderer != null) {              mtexrenderer.updateviewsize(width, height);          }      }

so far works.

it looks changebackground() method called ui thread, or @ least thread other rendering thread.

glsurfaceview creates separate rendering thread, has current opengl context when rendering methods called. therefore ready opengl calls. not case other threads. current opengl context per thread. unless (which possible, adds significant complexity), can't make opengl calls other threads.

there few options resolve this. easiest 1 using queueevent() method on glsurfaceview, allows pass in runnable executed in rendering thread.

i'm not sure mtexrenderer.updatetexturesize() method in code does. generally, if texture size changes, you'll have use glutils.teximage2d() instead of glutils.texsubimage2d() update texture.

the code (untested, might not work typed):

final bitmap bitmap = bitmapfactory.decodefile(bitmapuri.getpath());  thebackground.queueevent(new runnable() {     @override     void run() {         mimagewidth = bitmap.getwidth();         mimageheight = bitmap.getheight();         mtexrenderer.updatetexturesize(mimagewidth, mimageheight);          gles20.glbindtexture(gles20.gl_texture_2d, mtextures[0]);         glutils.teximage2d(gles20.gl_texture_2d, 0, bitmap, 0);          bitmap.recycle();     } });  thebackground.requestrender(); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -