Interpolators

Started by
4 comments, last by Danny02 13 years, 1 month ago
Suppose I drew a quad onto the screen with a custom value being passed from the vertex shader to the pixel shader, as shown in the attachment. (The 0s and 1s correspond to the value being assigned in the vertex shader).

In the pixel shader, what value would be read for the top left pixel? Would it be 0.25 or 0 (assuming the same z for all 4 vertices)? i.e. is the interpolated value obtained from the center of the pixel or from the corner?

Finally, is this behavior the same in OpenGL and DirectX (or between shading langues)? And does it depend on which interpolator is used (e.g. TEXCOORD0 vs NORMAL vs POSITION in HLSL)?

Thanks!
Advertisement
just take a look at the OpenGL specs.

http://www.opengl.org/registry/doc/GLSLangSpec.4.10.6.clean.pdf: 4.3.9 Interpolation
http://www.opengl.org/registry/doc/glspec41.core.20100725.pdf:3.5 Line Segments, 2.19 Flatshading

in a shader you can tell openGL excatly how every single varrying should be interpolated (perspective correct, linear or flat)
you can also decide at which position each fragment should be sampled.

the standart way is that u get perspective interpolation with random samples.
So to answer your question, there is no guarantee which value your fragments would have in your example.
The left top fragment could have any value between 0. and 0.5
Normally, it should be 0.25 for the top left pixel but it can vary between implementations.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thanks for the replies. If it's always 0.25 that's exactly what I need but I'll also look more at the gl spec to see if there's a way to enforce that. (The reason is for computing shadow bias.)
D3D9 interpolates the same, but because it addresses pixels in a different way, you can accidentally get "0.0" instead of "0.25" by specifying slightly incorrect vertex positions.
like the glsl specs point out you can use the centroid keyword, which enforces to sample at the middle of the pixel.

eg. centroid in vec2 texcoord;

This topic is closed to new replies.

Advertisement