opengl es 2.0 - How can i read data by glReadPixels? -


i'm working on opengl es on android. meet problem. defined float array, used pass fragment shader.

float[] data = new float[texwidth*texheight]; // test data (int = 0; < data.length; i++) {     data[i] = 0.123f; } 

1. inittexture:

glgentextures... glbindtexture... gltexparameteri... floatbuffer fb = bufferutils.array2floatbuffer(data); glteximage2d(gl_texture_2d, 0, gl_rgba, texwidth, texheight, 0, gl_rgba, gl_unsigned_byte, fb); 

2.fbo:

glgenbuffers... glbindframebuffer... glframebuffertexture2d(gl_framebuffer, gl_color_attachment0, gl_texture_2d, texid, 0); 

3.ondrawframe:

gluseprogram(mprogram);... gldrawarrays(gl_triangle_strip, 0, 4);...  intbuffer fb = bufferutils.ibufferallocatedirect(texwidth*texheight); glreadpixels(0, 0, texwidth, texheight, gl_rgba, gl_unsigned_byte, fb); system.out.println(integer.tohexstring(fb.get(0))); system.out.println(integer.tohexstring(fb.get(1))); system.out.println(integer.tohexstring(fb.get(2))); 

fragment shader:

precision mediump float; uniform sampler2d stexture; varying vec2 vtexcoord;  void main() {     tex = texture2d(stexture, vtexcoord.st);     vec4 color = tex;     gl_fragcolor = color; } 

so, how can float data(0.123f, defined before) whith glreadpixels? ff000000(abgr), suspect shader doesn't data through way. can tell me why , how can deal it? newbie on , appreciate it.

your main problem happens before glreadpixels(). primary issue way use glteximage2d():

floatbuffer fb = bufferutils.array2floatbuffer(data); glteximage2d(gl_texture_2d, 0, gl_rgba, texwidth, texheight, 0,              gl_rgba, gl_unsigned_byte, fb); 

the gl_unsigned_byte value 8th argument specifies data passed in consists of unsigned bytes. however, values in buffer floats. float values interpreted bytes, can't possibly end because different formats, different sizes , memory layouts.

now, might tempted instead:

floatbuffer fb = bufferutils.array2floatbuffer(data); glteximage2d(gl_texture_2d, 0, gl_rgba, texwidth, texheight, 0,              gl_rgba, gl_float, fb); 

this work in desktop opengl, supports implicit format conversions part of specifying texture data. not supported in opengl es. in es 2.0, gl_float not legal value format argument. in es 3.0, legal, internal formats store floats, gl_rgba16f or gl_rgba32f. error use in combination gl_rgba internal format (3rd argument).

so unless use float textures in es 3.0 (which consume more memory), need convert original data bytes. if have float values between 0.0 , 1.0, can multiplying them 255, , rounding next integer.

then can read them bytes glreadpixels(), , should same values again.


Comments