# How can I find a position between 4 vertices in a fragment shader?

I'm creating a shader with SharpDX (DirectX11 in C#) that takes a segment (2 points) from the output of a Vertex Shader and then passes them to a Geometry Shader, which converts this line into a rectangle (4 points) and assigns the four corners a texture coordinate.

After that I want a Fragment Shader (which recieves the interpolated position and the interpolated texture coordinates) that checks the depth at the "spine of the rectangle" (that is, in the line that passes through the middle of the rectangle.

The problem is I don't know how to extract the position of the corresponding fragment at the spine of the rectangle. This happens because I have the texture coordinates interpolated, but I don't know how to use them to get the fragment I want, because the coordinate system of a) the texture and b) the position of my fragment in screen space are not the same.

• the fragment shader interpolates the texture coods automatically IIRC – ratchet freak Nov 12 '13 at 16:08
• Yes, they are automatically interpolated, but what I want is to get the position of the line that goes through the middle of the rectangle at it's closest point to my fragment. – c4sh Nov 12 '13 at 17:09

One way to do this is to create another output from the geometry shader, called spinePos for example. In the geometry shader, calculate the spine position at each end of the rectangle, and assign it to spinePos for the two vertices at that end.
The rasterizer will interpolate this value across pixels as it normally does, but since the two vertices at each end have the same spinePos value, there will be no change in the value from interpolating across the thickness of the rectangle. It will only change as you move along the length of the rectangle.