# Perspective correct interpolation of vertex attr

Hey,

I basically want to write a vertex attribute into a G-Buffer (texture). While doing this, the interpolation seems to be broken, but I don’t know why. I’m using OpenGl 3.0 with GLSL 1.5.

.

Here is an image, which should illustrate the situation. It shows an velocity buffer of a box. The box itself is stretched. The problem with the interpolation can be seen at both stretched boxes. On the right side the black line just emphasizes the edge, at which the interpolation fails and a discontinuity is visible.

This interpolation is done by the shader itself. I tried the qualifiers “smooth” and “noperspective”. There is no difference in the resulting image. Does anyone has a suggestion?

Merry Christmas Eckhard

On the right side the black line just emphasizes the edge, at which the interpolation fails and a discontinuity is visible.

I don’t see a black line in the picture. I do see a red line, if that’s what you’re talking about.

As for why that exists, that’s pretty standard. It’s the nature of interpolating values across triangles.

Along the main diagonal of the quad, you are only interpolating between the two coordinates at either ends of the diagonal line.

If you need different interpolation that avoids this problem, you will need to code it yourself. You can do this by passing values from the vertex shader to the fragment shader that, when interpolated, represent the location on the quad’s surface in both X and Y. Then you can use those two values to do bilinear interpolation. So the upper left vertex gets the vec2 of (0, 0), the upper right gets (1, 0), the lower left gets (0, 1) and the lower right gets (1, 1).

You then use the value to do bilinear interpolation for the quad.

This interpolation is done by the shader itself.

You should probably show us the shader that does the interpolation, then.

The preview function of this site showed an old version of the image.

So is there no way to use bilinear interpolation with triangles, so that it could solve my problem?

You mean “there is no way to use bilinear interpolation with quads or ngons with n>=4”.
By default, this is the case. But as said Alfonse, it is possible to code your own interpolation within shader.

This would be the case for values I can look up in a texture, but the values I need are vertex attributes. In my case a velocity vector as vec3.

The velocity is calculated in a vertex program and passed on to the fragment program.

Hey,

two days ago I found the answer for my problem. First I wanted to show you two picture, what I intended.  It shows a scene and a velocity buffer, where the camera moves towards the Cornel Box. I tried to align stroke texture according to the velocities, but were unsuccessful. I simply passed the velocity into the fragment shader and stored it in a texture.

``````
in vec3 velocity;
out vec4 fragColor;

void main (){
fragColor = vec4(velocity, 1.0);
}

``````

Instead of passing the velocity, I tried to pass the previous and current position into the program and calculated the velocity there.

``````
in vec4 currentPosition;
in vec4 previousPosition;
out vec4 fragColor;

void main (){
vec3 velocity = ((previousPosition / previousPosition.w) - (currentPosition / currentPosition.w)).xyz;
fragColor = vec4(velocity, 1.0);
}

``````

Note, the passed positions are already projected.
It worked and the resulting image looks like this: This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.