Finding pixel corners' texture coordinates using dFdx and dFdy

Inside the fragment shader, I’m trying to estimate each pixel’s corner’s texture coordinates using the derivative functions:

    vec2 dxHalf = dFdx(gl_TexCoord[0])*0.5;
    vec2 dyHalf = dFdy(gl_TexCoord[0])*0.5;

    vec2 vec2BotLeft =
        vec2(gl_TexCoord[0] - dxHalf - dyHalf);
    vec2 vec2BotRight =
        vec2(gl_TexCoord[0] + dxHalf - dyHalf);
    vec2 vec2TopLeft =
        vec2(gl_TexCoord[0] - dxHalf + dyHalf);
    vec2 vec2TopRight =
        vec2(gl_TexCoord[0] + dxHalf + dyHalf);

Which seems to work great, except for cases where the triangle is almost perpendicular to the near plane. In which case, the dFdx() and dFdy() functions (correctly) go to positive or negative infinity.

I would like to somehow clamp those values to stay within the pixel’s parent triangle (or at least the triangle’s bounding rectangle). (Clamping the values to [0, 1] was not enough, since a texture could have hundreds of triangles inside.)

I thought of using barycentric coordinates, but that would require me to pass in a barycentric coordinate for every vertex. Since I’m using VBO’s with indexed arrays, this would be a hassle.

Any ideas?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.