Accuracy issues with vertex attribues interpolation

Hello,

I’m facing a problem with attribute values received in shaders not corresponding to what I’m giving them as input. I’ve been pulling my hair over this for two days so I hope somebody can help me figure this one out.
It’s kind of hard to explain so I’ll just show you what I do.
Consider the following vertex shader:

out vec4 intensityInfo;
in vec4 intensity;

void main()
{
    intensityInfo = intensity;
    gl_Position = ftransform();
}

and fragment shader:

in vec4 intensityInfo;

void main()
{
    if (intensityInfo.g > .9)
        gl_FragColor = vec4(1,1,1,1);
    else
        gl_FragColor = intensityInfo;
}

and a series of triangles being displayed with the following code:

// Assume 1 is the index of the attribute named intensity
gl.glVertexAttrib4f(1, 0, 0, 0, 1);
[snip: calculate vertex coordinates]
gl.glVertex3f(x, y, z);
gl.glVertexAttrib4f(1, 0, .8f, 0, 1);
[snip: calculate vertex coordinates]
gl.glVertex3f(x, y, z);
gl.glVertexAttrib4f(1, 0, .8f, 0, 1);
[snip: calculate vertex coordinates]
gl.glVertex3f(x, y, z);

Now, in theory, the condition intensityInfo.g > .9 in the fragment shader should never be true, since the values I pass into the vertex attributes for the green component range from 0 to 0.8. However, sometimes they do! I’d like to know why, and how to prevent it because it’s keeping my shader from giving the result I’m going for.

The interresting thing is that this happens only when the triangle is close to being perpendicular to the view, as illustrated by these two pictures of a series of triangles:


As you can see, the polygons that are far away and almost perpendicular to the view contain white/grey pixels, when in theory there shouldn’t be anything else than green-black gradient.

Any help would be appreciated!

(Not sure if it matters, but I’m developing in Java with JOGL)

Attributes may be extrapolated (not interpolated) outside the triangle when:

  • you use multisampling without centroid interpolation (from your screenshots, this looks like the issue…)
  • “helper” pixels in a 2x2 quad fragment are evaluated outside the triangle (doesn’t affect your simple example…)

Excellent! Thank you!