PDA

View Full Version : Difficult issue - depth texture and color texture in fragment shader



glTomm
03-10-2014, 09:58 AM
Hello, this is related to a thread I made in the openGL beginner forum:
www.opengl.org/discussion_boards/showthread.php/183760-Depth-texture-and-color-texture-fragment-matching
I didn't get any response so I will try to reformulate my question here. See the link for an example of how the issue appears.

I use jmonkeyengine on Windows8 and write my shader code in GLSL version110.

I write the depth texture and the color texture of a scene to a framebuffer and access these in the fragment shader. I expect that whatever coordinates I use when accessing the textures I will read values from corresponding fragments in both textures. However, based on my example in the other thread, I believe that this is not always the case.
Example:
Color texture and depth texture both have size (512, 512) and I input coordinate vec2(0.5, 0.5).
vec4 depthValue = texture2D(my_depthTexture, coordinate); // I read from fragment (256, 256)
vec4 colorValue = texture2D(my_colorTexture, coordinate); // I read from fragment (256, 256). This corresponds to the fragment in the depth texture. OK.

But it seems like that for some coordinate values I don't get matching fragments
Example:
vec2 coordinate = vec2(someValue1, someValue2);
vec4 depthValue = texture2D(my_depthTexture, coordinate); // I read from fragment (301, 300)
vec4 colorValue = texture2D(my_colorTexture, coordinate); // I read from fragment (300, 300). This does not correspond to the fragment in the depth texture. NOT OK.

I assume that this happens when the float value in the coordinate vector is in some border area between the fragments and the shader somehow ends up reading values from non-corresponding fragments in the two textures. Maybe there is a rounding bug somewhere(?) so that when reading color texture a floor function is used, and a round function when reading depth texture? I don't know where to look.

Any help is appreciated. Is it likely that the bug is in jmonkeyengine? Some windows driver? A common GLSL library?

Who/what actually compiles/reads the GLSL code? Knowing this could help me narrow down possible candidates for the bug.

glTomm
03-10-2014, 12:51 PM
I may have found the reason for my issues. The bug seems to be my understanding of textures. Apparently when reading from textures the value can be averaged between the nearest pixels if the coordinate value does not exactly hit the pixel. And this is probably the default setting in jmonkeyengine. I just need to figure out how to change this setting.

Not such a difficult issue afterall..