0

I would like to read with glReadPixels() the value from the colorbuffer, which I previously wrote in the Fragmentshader via gl_FragColor. This works on average 10 times, then an erroneous value (1 = 255) occurs.

#version 420
uniform vec2 screenXy;
uniform vec2 screenSize;
out highp vec4 fragColor;

void main(void) {

if((int(gl_FragCoord.x) == int(screenXy.x)) && ((int(screenSize.y - 1) - int(gl_FragCoord.y)) == int(screenXy.y))) {
    fragColor.r = 0.5; // any value
} else {
    fragColor = vec4(1, 1, 1, 1.0);
}

I submit the mouse xy coordinates to the fragementshader (screenXy). If the clicked pixel is in the row, I write a value (e.g 0.5) in the color buffer. Now I observe that sometimes the value is 1 (= 255) instead of 0.5 (=128).

GLfloat zc[4]; // from color buffer
m_func->glReadPixels(xy.x(), (m_pFbo->height() - 1) - xy.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, z);
qDebug() << "z0 " << z[0];

I see this behavior on win10 and android. Does anyone has an idea what i'm doing wrong?

PowerNow
  • 363
  • 1
  • 2
  • 15
  • 1
    Shouldn't it be `m_pFbo->height() - xy.y() - 1`? A texture usually has a range from [0, width-1]. If y==0, then you would be reading outside of the buffer. Also note, that `gl_FragColor` only exists in the compatibility profile and should be replaced with a custom output variable nowadays. – BDL May 07 '18 at 13:31
  • @BDL: Thxs for you hints regarding (width -1) and gl_FragColor. I changed it but unfortunaltely no change. It seems so that tendencial this erroneous values occurs at heigher value (close to 1). – PowerNow May 07 '18 at 14:15
  • Is it possible that gl_FragColor is only the color of the current pixel, but musst be not the same as in the colorbuffer at the end, like the depthbuffer? – PowerNow May 07 '18 at 14:25
  • That depends on too many variables. Blending, Gamma Correction, etc. can influence the outcome. Also the framebuffer format plays a role. – BDL May 07 '18 at 14:47
  • So is there any way to save precisly e.g. the depth value in the colorbuffer? I ask this because I would need to read out the depth buffer with glReadPixels() on opengl es 3.0. – PowerNow May 07 '18 at 15:14
  • Yes there is. But if you need the depth values why don't you read back the depth-buffer directly? The behavior that you have now is not what should happen. But it is impossible to tell where the problem lies with that little code. – BDL May 07 '18 at 15:33
  • Because unfortunately on Opengl ES 3.0 you can not read via glReadPixels(...GL_DEPTH_COMPONENT, GL_FLOAT, &value) the depth buffer. – PowerNow May 08 '18 at 06:00
  • Here to the continuitation: https://stackoverflow.com/questions/50288912/what-does-in-opengl-exactly-means-gl-fragdepth-0-0 – PowerNow May 11 '18 at 09:14
  • What are you actually trying to do here? In general inserting a call to `glReadPixels` in a game loop is a bad idea, as it flushes the rendering pipeline and generally makes a mess of performance. If you do this please make it asynchronous ... – solidpixel May 11 '18 at 23:07
  • @solidpixel: I need the depth value of a arbitrary pixel on the cpu for further calculations with open gl es. The floatToRgbaUByte works if alpha = 1.0. Thats the solution. – PowerNow May 14 '18 at 15:44

0 Answers0