Reputation: 353
I want to utilize shaders to not only discard fragments if they are on one side of a predefined plane but also render a contour along the intersection.
My fragment shader currently does something along the lines of:
float dot = dot(world_coordinate, normalize(clipping_normal.xyz)) - clipping_normal.w;
if (dot > 0.0f)
discard;
this works but without the desired contour. I tried comparing the dot product against values close to 0.0 but this results in a contour with varying width depending on view etc...
This is what I am trying to achieve. Notice that the white contour/edge of where the plane intersects the sphere is of consistent width:
So below is what the result I currently see:
With the fragment shader:
in vec4 color;
in vec3 world_position;
out vec4 frag_color;
void main()
{
float dist = (dot(clipping_plane.xyz, world_position) - clipping_plane.w) /
dot(clipping_plane.xyz, clipping_plane.xyz);
if(dist >= 0.0f && dist < 0.05f)
frag_color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
else if(dist < 0.0f)
discard;
else
frag_color = ComputePhong(color);
}
Upvotes: 2
Views: 504
Reputation: 7198
The contour of the intersection also belongs to the clipping plane, so the distance to this plane is zero.
Using dot(point, normal)
is not enough. You need d= A·x + B·y + C·z + D
which is the numerator (without "modulus") of the full distance point-to-plane formula. See plane geometry.
This calculated d
gives you not only distance (add the square in the denominator if normal A,B,C is not unitary), but also its sign tells you which side of the plane the point is.
Working inside fragment shader likely you work with NDC coordinates. So transform A,B,C,D
into NDC too.
Upvotes: 1