android - Opengl - Convert Texture to Array -


in android app, have frame buffer object takes me rendered scene texture. app origami game , user can fold paper freely:

enter image description here

enter image description here

in every fold, current rendered scene saves texture using fbo , redraw paper new coordinates new texture attached it, seem folded paper. , way user can fold paper many time wants.

i want in every frame check rendered scene, determinate user riches final shape (assume have final shape in 2d-array 0 , 1 filled, 0 transparency , 1 colored pixels)

what want, how, convert texture 2d-array filled 0 , 1, 0 transparency pixel, , 1 colored pixel of texture. need compare result known 2d-array determinate if texture shape want or not.

is possible save texture data array?

i cant use glreadpixels because heavy , not possible call every frame.

here fbo class (i want have rendertex[0] array):

public class fbo { int [] fb, rendertex; int texw;  int texh;     public fbo(int width,int height){     texw = width;      texh = height;     fb = new int[1];     rendertex= new int[1]; } public void setup(gl10 gl){     // generate     ((gl11extensionpack)gl).glgenframebuffersoes(1, fb, 0);      gl.glenable(gl10.gl_texture_2d);     gl.glgentextures(1, rendertex, 0);// generate texture     gl.glbindtexture(gl10.gl_texture_2d, rendertex[0]);     gl.gltexparameterf(gl10.gl_texture_2d,             gl10.gl_texture_min_filter, gl10.gl_nearest);     gl.gltexparameterf(gl10.gl_texture_2d,             gl10.gl_texture_mag_filter, gl10.gl_nearest);     gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_s,             gl10.gl_clamp_to_edge);      gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_t,             gl10.gl_clamp_to_edge);     //texbuffer = bytebuffer.allocatedirect(buf.length*4).order(byteorder.nativeorder()).asintbuffer();     //gl.gltexenvf(gl10.gl_texture_env, gl10.gl_texture_env_mode,gl10.gl_modulate);     gl.glteximage2d(gl10.gl_texture_2d, 0, gl10.gl_rgba, texw, texh, 0, gl10.gl_rgba, gl10.gl_unsigned_byte, null);     gl.gldisable(gl10.gl_texture_2d);  }     public boolean renderstart(gl10 gl){     log.d("textureandfbo", ""+rendertex[0] + " , " +fb[0]);     // bind framebuffer     ((gl11extensionpack)gl).glbindframebufferoes(gl11extensionpack.gl_framebuffer_oes, fb[0]);      // specify texture color attachment     ((gl11extensionpack)gl).glframebuffertexture2does(gl11extensionpack.gl_framebuffer_oes, gl11extensionpack.gl_color_attachment0_oes, gl10.gl_texture_2d, rendertex[0], 0);      int error = gl.glgeterror();     if (error != gl10.gl_no_error) {         log.d("err", "first background load glerror: " + error+"      ");     }     int status = ((gl11extensionpack)gl).glcheckframebufferstatusoes(gl11extensionpack.gl_framebuffer_oes);     if (status != gl11extensionpack.gl_framebuffer_complete_oes)     {         log.d("err", "second background load glerror: " + status+"      ");;         return true;     }     gl.glclear(gl10.gl_color_buffer_bit);     return true; }   public void renderend(gl10 gl){     ((gl11extensionpack)gl).glbindframebufferoes(gl11extensionpack.gl_framebuffer_oes, 0);      gl.glclear(gl10.gl_color_buffer_bit | gl10.gl_depth_buffer_bit);      gl.glenable(gl10.gl_texture_2d);     gl.glbindtexture(gl10.gl_texture_2d, 0);     gl.glcolor4f(1.0f,1.0f,1.0f,1.0f);     gl.gldisable(gl10.gl_texture_2d); }  public int gettexture(){     return rendertex[0]; } public int getfbo(){     return fb[0]; }   } 

if using opengl es 3.0 , later pbo solution. think can use eglimage. because needs opengl es 1.1 or 2.0.

the function create eglimagekhr is:

eglimagekhr eglcreateimagekhr(egldisplay dpy,                           eglcontext ctx,                           eglenum target,                           eglclientbuffer buffer,                           const eglint *attrib_list) 

to allocate anativewindowbuffer, android has simple wrapper called graphicbuffer:

    graphicbuffer *window = new graphicbuffer(width, height, pixel_format_rgba_8888, graphicbuffer::usage_sw_read_often | graphicbuffer::usage_hw_texture); struct anativewindowbuffer *buffer = window->getnativebuffer(); eglimagekhr *image = eglcreateimagekhr(eglgetcurrentdisplay(), egl_no_context, egl_native_buffer_android, *attribs); 

to read pixels fbo use 1 of these 2 methods below:

void eglimagetargettexture2does(enum target, eglimageoes image)  void eglimagetargetrenderbufferstorageoes(enum target, eglimageoes image) 

these 2 methods esablishes properties of target gl_texture_2d or gl_renderbuffer

uint8_t *ptr; glbindtexture(gl_texture_2d, texture_id); gleglimagetargettexture2does(gl_texture_2d, image);  window->lock(graphicbuffer::usage_sw_read_often, &ptr); memcpy(pixels, ptr, width * height * 4); window->unlock(); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -