简体   繁体   中英

How can I get float values using glReadPixels? [Android NDK es3.0]

c++ code:

float* data = (float*)malloc(texWidth*texHeight*sizeof(float)); // or GLfloat
float* result = (float*)malloc(texWidth*texHeight*sizeof(float)); // or GLfloat
for (int i = 0; i < texWidth * texHeight; i++) {
    data[i] = 2.22;
}
...
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
...
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
...
glReadPixels(0, 0, texWidth, texHeight, GL_RGBA, GL_UNSIGNED_BYTE, result);
for (int i = 0; i < 64; i++) {
    LOGD("%f", result[i]);
}

Now I want to read the value "2.22" back, but now what I get are some long float values. (the output is like -188856716075009999660373357998301511680.000000, not I want).

I also tried using GLubyte as result array's type, and got the color values(result[0] is R value, result[1] is G value, result[2] is B value, result[3] is A value),which is not what I want.

Questions:

  1. Since the type is GL_UNSIGNED_BYTE, should I use GLubyte as my array's type?
  2. How can I exactly get the 2.22 through glReadPixels?
  3. Can GL_FLOAT be the type of glReadPixels in GLES?
  1. Yes. Readback format has to match the texture format.
  2. You can't, as you are not reading back float data. You'll have to reconstruct it yourself from the byte values.
  3. Yes, if the texture is a float format texture (OpenGL ES 3.0 onwards)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM