2

Suppose I have a triangle say(PC0,PC1,PC2), and its barycentric coordinates is((1,0),(0,1),(0,0)), they are all in clip space.

And now I want calculate the interpolated barycentric coordinates in screen space, how can I do that so it could be correct? I have see something like perspective correct interpolation, but I cant find a good mapping relationship bewteen them.

Mirocos
  • 43
  • 6
  • 1
    Barycentric coordinates on a triangle are three numbers (not three groups of two numbers). And they sum up to 1 if the described point is inside the triangle. – BDL Nov 23 '22 at 08:48
  • I think you just interpolate them (perspective-correctly). In fact the GPU may already be doing this and basing all other interpolation on it. – user253751 Nov 23 '22 at 10:17
  • @user253751 Yes, actually I am working on a problem that I need to solve the derivatives of barycentric coordinates in clip space w.r.t the one in NDC space. so I want know the actual relationship between them. – Mirocos Nov 23 '22 at 13:07
  • @BDL Sorry I didnt explain my problem well, I mean in clip space, for PC0, its barycentric coordinates (u,v) = (1,0) and same for other vertices. And I think two numbers is good for denote barycentric coordinates cuz the third one is "1-u-v" in my situation. – Mirocos Nov 23 '22 at 13:11
  • @Mirocos you might be interested in the dFdx and dFdy functions in GLSL - you can directly ask for the derivatives per pixel, which should be easy enough to convert to NDC space. The GPU does this by actually calculating them for each pixel and then calculating finite differences between adjacent pixels, so accuracy is "good enough" – user253751 Nov 23 '22 at 13:22
  • @Mirocos: Can't you just pass the coordinate to the fragment shader as an input and let the regular machinery handle it? – Nicol Bolas Nov 23 '22 at 14:36
  • @user253751 As far as I know, the function dFdx and dFdy in GLSL only calculate the derivatives of target variable w.r.t X or Y in screen space, right? But it still left the gap between clip space and screen space. – Mirocos Nov 24 '22 at 14:38
  • @user253751 To bridge this gap, we need to calculate the jacobian matrix (ducdus, ducdvs, dvcdus, dvcdvs). Actually, I find some code in "nvdiffrast" which give an example to calculate, but I cant undestand all of its code. And here is the question which include the example code, if you are interested in that. https://stackoverflow.com/questions/74529717/abound-image-space-derivatives-of-the-barycentrics – Mirocos Nov 24 '22 at 14:38
  • @Mirocos: "*I mean in clip space, for PC0, its barycentric coordinates (u,v) = (1,0) and same for other vertices.*" As previously stated, a barycentric coordinate has 3 values. If it only has 2 coordinates, it isn't barycentric. You seem to be talking about a texture coordinate or something. Please edit your question to explain what exactly you mean by these terms. – Nicol Bolas Nov 30 '22 at 16:04

1 Answers1

2

The conversion from screen-space barycentric (b0,b1,b2) to clip-space barycentric (B0,B1,B2) is given by:

                (b0/w0, b1/w1, b2/w2)
(B0, B1, B2) = -----------------------
                b0/w0 + b1/w1 + b2/w2

Where (w0,w1,w2) are the w-coordinates of the three vertices of the triangle, as set by the vertex shader. (See How exactly does OpenGL do perspectively correct linear interpolation?).

The inverse transformation is given by:

                (B0*w0, B1*w1, B2*w2)
(b0, b1, b2) = -----------------------
                B0*w0 + B1*w1 + B2*w2
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • thanks for your time, sorry for reply so late cuz I'm busy for my graduation paper. I'll try to do the derivates as soon as possible! – Mirocos Jan 03 '23 at 13:17