One on one mapping between texels and screen pixels in OpenGL

Viewed 981

I am doing the following with OpenGL. My screen is of size 512*512. I have a texture of the same size as the screen. I want to draw a quad that covers the whole screen and establishes a one on one mapping between pixels and texels, so that I can do texelFetch with screen coordinates in shaders.

First, if I setup the corners of the quad like the following:

(-1, 1)+-----------+(1,1)
       |           |
       |           |
       |           |
       |           |
       |           |
(-1,-1)+-----------+(1,-1)

and set their corresponding texture coordinates as:

(0,511)+-----------+(511,511)
       |           |
       |           |
       |           |
       |           |
       |           |
(0,  0)+-----------+(511,0)

I just got a white screen using the following fragment shader which tries to assign different colors to its four boundaries:

#version 330

in vec2 fUV;                              // sent from vertex shader

out vec4 color;

void main() {
    if (fUV.x == 0)                       // leftmost
        color = vec4(1.0, 0.0, 0.0, 1.0);
    else if (fUV.y == 0)                  // bottom
        color = vec4(0.0, 1.0, 0.0, 1.0);
    else if (fUV.x == 511)                // rightmost
        color = vec4(0.0, 0.0, 1.0, 1.0);
    else if (fUV.y == 511)                // top
        color = vec4(1.0, 0.0, 1.0, 1.0);
    else                                  // otherwise
        color = vec4(1.0, 1.0, 1.0, 1.0);

    // check if uv equals an integer
    if (fUV.y == 100)
        color = vec4(0.0, 0.0, 0.0, 1.0);
}

which means all equality test have failed.

Secondly, If I add a half pixel width offset d= 1.0 / 512 to all -1 coordinates:

(-1+d, 1 )+-----------+(1,1)
          |           |
          |           |
          |           |
          |           |
          |           |
(-1+d,-1+d)+-----------+(1,-1+d)

keeping other things unchanged, I got the leftmost red line drawn on the screen.

The above results I got are really confusing. Why was only the leftmost column drawn while the bottom row was not?

I would like to know how should I draw a quad and set their coordinates so that I can establish a one on one mapping between pixels and texels.

1 Answers
Related