f



Barycentric coordinates in fragment shader

Hi,

just curious if there's a neat way to get the barycentric coordinates
(or similar) of the current fragment in a GLSL shader?

Only way I could think of it was to render each triangle individually,
but that's not too good for performance...

- Asbj�rn
0
Lord
10/20/2006 4:24:46 AM
comp.graphics.api.opengl 7074 articles. 1 followers. Post Follow

2 Replies
1450 Views

Similar Articles

[PageSpeed] 51

Lord Crc wrote:
> just curious if there's a neat way to get the barycentric coordinates
> (or similar) of the current fragment in a GLSL shader?

NO, but you can get the same result if you can afford to choose the
primitive type to be GL_TRIANGLES and setup the barycentric to each
vertex as:

A = vec3(1.0, 0.0, 0.0);
B = vec3(0.0, 1.0, 0.0);
C = vec3(0.0, 0.0, 1.0);

Write those into the varyings, then you can access the per fragment
value as if it were the barycentric coordinate. It depends on the
implementation what it costs if anything at all (thinking of a driver
where primitive type is part of the shader specializaton.. not likely
but possible :)

Another way, which won't consume a varying slot is to use gl_FragCoord
to compute the barycentric.. for that you need to store the
barycentric's cofactors as uniforms and do something like scalar
product per component to transform the gl_Fragcoord.xy into barycentric
u and v, probably cheaper to just compute the baryw as (1.0 - u - v) ..
but this is much more complicated & expensive than the obvious
use-varying-slot approach.

What's this for anyway? There might be more elegant solution possible
if know the problem..?

0
jukka
10/20/2006 10:19:34 PM
On 20 Oct 2006 15:19:34 -0700, "jukka@liimatta.org"
<jukka@liimatta.org> wrote:


Thanks for the input, yeah I thought about rendering the triangles
individually, but not about "reverse engineering" it.

>What's this for anyway? There might be more elegant solution possible
>if know the problem..?

I just wanted to toy with some more advanced interpolation schemes,
and also be able to compute the distance to corners/edges in the
triangle.

Cheers
- Asbj�rn
0
Lord
10/21/2006 4:48:43 AM
Reply: