# Measuring sphere angles to triangle

I'm looking at using multiple importance sampling for path tracing, and doing some lighting calculations relating to spherical lights. I'm trying to decide, given a triangle and a point on that triangle, how the sphere should appear. This should mean calculating how much of the sphere is above the plane (if any) and where in the "sky" it should be so that I can sample and weight rays heading there appropriately.

So far I've done this:

float top = dot(normal, normalize((lightPosition + (normal * size)) - origin));
float bottom = dot(normal, normalize((lightPosition - (normal * size)) - origin));


where normal is the triangle's normal, origin is my point on that triangle, lightPosition is the center of the sphere and size is the radius. My simple strategy was to decide the "highest and lowest" points on the sphere and measure their angles. But I had a surprising result which is that some origins report that the top is above the triangle (i.e. >0) and others report it as below the triangle (i.e. <0). At first I thought this must be some mistake of the data, but employing the debugger, all origins agree on the normal, position, and size, and sampling a few origins and computing the normal gives the expected result.

To be clear in this case, there is no bump mapping or other shenanigans with the normal, it is computed with the standard normalize(cross(b - a, c - a)) method from the vertices of the triangle so all origins always see exactly the same normal.

Any thoughts on how to improve this measurement so that all origins agree on how much of the sphere is above or below the triangle?

## 1 Answer

The issue was that I'm computing the top and bottom of the sphere relative to the plane. But the point "views" the sphere from a certain perspective, so the apparent top and bottom aren't necessarily at the same place.