Shader and barycentric coordinates

Hi everyone,

I’m working on a particular way of rendering a triangular mesh, and I’d like to give to any point in a triangle the color of the closest vertex in the mesh. Is it possible to do that using only vertex and fragment shaders ? I have seen that it is possible to disable linear interpolation of data sent to the fragment shader, but I don’t know how to send vertices data to the fragment shader.

Does someone have an idea ?
Thanks by advance.

You send data to the fragment shader by defining the variable as an “out” or “varying” variable (depending on the shader version) in the vertex shader and storing a value in it and redefining it in the fragment shader as an “in” or varying. where you can access its interpolated value. If you use the key word “flat” it will not be interpolated but will have the value from first (or last vertex if you want) in the triangle

Yes, that’s exactly what I tried. By I don’t want to to retrieve ONE value per fragment, by THREE values which are the values to the vertices. That’s what I can’t achieve…

Pack the baycentric coordinates into vertex structure of each vertex like you would for texture coordinates

You’ll probably need to pass unmodified the 3 colors of the vertices to the fragment shader too.

You could generate this data in a geometry shader. Just take your 3 vertices and emit (1,0,0), (0,1,0) and (0,0,1) for barycentric coords + then emit all 3 colors to each generated vertices.

Alternatively you could do this by passing in to the vertex shader the barycentric coordinate + the 3 colors of each vertex on the triangle, and then passing the colors straight on to the fragment shader (using flat would probably give better performance).

Once you have the interpolated barycentric coordinate + the 3 colors in the fragment shader, it’s just a case of calculating which color should be used for the fragment.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.