Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Distance to lines results in inlets at line crossings

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2014

    Distance to lines results in inlets at line crossings


    I have a problem with the result that I get when calculating distances to lines in a shader program, specifically distances to multiple lines.

    Click image for larger version. 

Name:	cross_artifact.jpg 
Views:	203 
Size:	12.3 KB 
ID:	1325

    As you can see from the screenshots there is no problem with single lines, but when combined in the calculation inlets are visible at the line crossing that I do not understand and would like to get rid of. If this is some sort of optical illusion, then I still would like to know how to avoid this.
    (background: I want to create a soft shadow effect from a polygon silhouette that gets passed to the fragment shader)

    Part of the fragment shader code that is used to calculate the combined distance:

    Code :
    void main(void) {
        outputColor = vec4(1f, 1f, 0f, 1f);
        bool bands = false;
        float len = 0.2;        
        float distHorizontalLine = getDistanceToLine(vec3(0f, 0f, 0f), vec3(len, 0f, 0f), currentPoint);    
        float distVerticalLine =   getDistanceToLine(vec3(0f, 0f, 0f), vec3(0f, len, 0f), currentPoint);   
        float dist = distHorizontalLine;                   // [A] distance to horizontal line
        dist = distVerticalLine;                           // [B] distance to vertical line
        // dist = min(distVerticalLine, distHorizontalLine);  // [C] distance to cross
        float radius = 0.25f;
        if (dist<radius) {
            float ratio = dist/radius;        
            vec4 distColor = vec4(0f, 0f, 1f, 1f);        
            outputColor = mix(distColor, outputColor, ratio);
            if (bands) {
                // debug: display bands
                float band = (ceil(fract(ratio)*10f))/10f;
                outputColor = vec4(band, band, 1-band, 1f);

    The complete project (java (lwjgl) + shaders sources):
    Attached Files Attached Files
    Last edited by nospoon2014; 06-01-2014 at 07:07 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Replace min() with something else, e.g.
    Code :
        dist = min(min(distVerticalLine, distHorizontalLine), distVerticalLine*distHorizontalLine/radius);

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2014
    GClements thanx for the hint, I got the asked result, however I have tried to come up with a proper result (without unwanted markings), but so far I have had not much luck. I think this problem actually consist of 2 problems:
    1] the calculated distances are not exactly the same for both lines, so the shader outputs a slightly different color which is visible as a discontinuous region at the 'shadow' intersection
    Click image for larger version. 

Name:	zoomed.jpg 
Views:	167 
Size:	10.6 KB 
ID:	1342

    2] the optical illusion thing. Even when solving [1] the inlets are disturbingly visible. It just doesn't make sense though, they should not appear in the first place.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts