PDA

View Full Version : Accuracy issues with vertex attribues interpolation



davoring
06-24-2015, 12:45 PM
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:

http://i.imgur.com/051L9J5.png
http://i.imgur.com/7Uzl29S.png


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)

arekkusu
06-24-2015, 02:21 PM
Attributes may be extrapolated (not interpolated) outside the triangle when:
* you use multisampling without centroid interpolation (https://www.opengl.org/pipeline/article/vol003_6/) (from your screenshots, this looks like the issue...)
* "helper" pixels (https://fgiesen.wordpress.com/2011/07/10/a-trip-through-the-graphics-pipeline-2011-part-8/) in a 2x2 quad fragment are evaluated outside the triangle (doesn't affect your simple example...)

davoring
06-25-2015, 10:07 AM
Excellent! Thank you!