I wrote this fragment program to highlight part of a 3D model that is in SectPlane plane and falls completely into the triangles (in this plane) defined as (P1, P2, P3) and (P4, P2, P3)
varying vec4 Position; // position of the fragment (initialized as Position = gl_Vertex; in vertex program)
uniform vec4 SectPlane; // equation of the plane (in the same coord. system as Position)
uniform vec3 P1; // four points in this plane to form two triangles (P1, P2, P3) and (P4, P2, P3)
uniform vec3 P2;
uniform vec3 P3;
uniform vec3 P4;
bool SameSide( vec3 p1, vec3 p2, vec3 a, vec3 )
{
vec3 cp1 = cross( b-a, p1-a );
vec3 cp2 = cross( b-a, p2-a );
return dot(cp1, cp2) >= 0;
}
bool PointInTriangle( vec3 p, vec3 a, vec3 b, vec3 c )
{
return SameSide(p, a, b, c) && SameSide(p, b, a, c) && SameSide(p, c, a, b);
}
void main()
{
float Threshold = 0.01 + 10.0 * gl_FragCoord.z - 1.0;
float Dist = abs( dot( Position, SectPlane ) );
if ( Dist > Threshold ) { discard; }
if ( !( PointInTriangle( vec3( Position ), P1, P2, P3 ) || PointInTriangle( vec3( Position ), P4, P2, P3 ) ) ) { discard; }
gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.0 );
}
The problem is i have to make the thickness of this section independant from the distance to the camera. I tried to use Threshold = 0.01 + 10.0 * gl_FragCoord.z - 1.0; as a criterium, but this brings no success at all.
Please, any suggestions and improvements are welcome!