Texture search in rendered FBO is off half a pixel

I have a scene that is transmitted to the texture via FBO, and I take it from the fragment shader, drawing its areas with primitives, and not drawing a full-screen square: I save resources, creating only the fragments I need.

To test this, I give out the same geometry as my texture rendering, which means that the created rasterization template should be exactly the same: when my fragment shader looks through its texture with the changing coordinate that it gave, it should blend perfectly with the other meanings he gave.

This is how I pass my fragment to the coordinate shader for automatic geometry texture using a full-screen texture:

// Vertex shader
uniform mat4 proj_modelview_mat;
out vec2 f_sceneCoord;
void main(void) {
    gl_Position = proj_modelview_mat * vec4(in_pos,0.0,1.0);
    f_sceneCoord = (gl_Position.xy + vec2(1,1)) * 0.5;
}

I work in 2D, so I do not deal with the decomposition of perspective here. I simply set the value of sceneCoord using the clip space position scaled backward from [-1.1] to [0.1].

uniform sampler2D scene;
in vec2 f_sceneCoord;
//in vec4 gl_FragCoord;
in float f_alpha;   
out vec4 out_fragColor;
void main (void) {
    //vec4 color = texelFetch(scene,ivec2(gl_FragCoord.xy - vec2(0.5,0.5)),0);
    vec4 color = texture(scene,f_sceneCoord);
    if (color.a == f_alpha) {
        out_fragColor = vec4(color.rgb,1);
    } else
        out_fragColor = vec4(1,0,0,1);
}

Notice that I spat out a red fragment if my alpha does not match. The texture handler sets the alpha for each rendered object to a specific index, so I know what matches what matches.

Sorry, I don’t have an image to display, but it’s very clear that my pixels are off (0.5,0.5): I get a thin red border around my objects on their bottom and left sides, which appears on and off. This is a pretty “temporary" look. The giveaway is that it is displayed only on the bottom and left sides of the objects.

, , texelFetch: , . , texture , , . , , ? !

: , , , : , gl_Position.xy+vec2(1,1))*0.5 trick , -. ... . ? , . , -, , .

transient red fragments indicate a texture sampling mis-match

- , , .

+5
2

, , f_sceneCoord gl_FragCoord / screenSize ( , -0.5 - ), screenSize (, pre -divided). , gl_FragCoord ( i+0.5), OpenGL ((i+0.5)/textureSize).

( ) - .. , , , GL_NEAREST - . exsiting f_sceneCoord , , GL_NEAREST, . , - f_sceneCoord.

EDIT: texelFetch. , GLSL 1.30 (~ SM4/GL3/DX10-hardware, ~ GeForce 8). in/out, ( varying/attribute). , , , texelFetch, , , , texture ( GLSL 1.30, texture2D), .

+4

X, Y [0,1] ... - , ...

I use:

// align it to the nearest centered texel
curPt -= mod(curPt, (0.5 / vec2(imgW, imgH)));

works like a charm and I no longer get random rounding errors at the edges of the screen ...

+1
source

All Articles