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?
fadden
  • 51,356
  • 5
  • 116
  • 166
KiBa1215
  • 87
  • 6

1 Answers1

0
  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)
solidpixel
  • 10,688
  • 1
  • 20
  • 33