Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Blended lines do not look as expected

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    21

    Blended lines do not look as expected

    I use the following fragment shader, which uses the fog effect, to draw my scene:

    Code :
    precision mediump float;
     
    uniform int EnableFog;
    uniform float FogMinDist;
    uniform float FogMaxDist;
     
    varying lowp vec4 DestinationColor;
    varying float EyeToVertexDist;
     
    float computeFogFactor()
    {
        float fogFactor = 1.0;
     
        if (EnableFog != 0)
        {
            //Use a bit lower vlaue of FogMaxDist to get a better fog effect - it will make the far end disappear quicker. 
            float fogMaxDistABitCloser = FogMaxDist * 0.98;
     
            fogFactor = (fogMaxDistABitCloser - EyeToVertexDist) / (fogMaxDistABitCloser - FogMinDist);
     
            fogFactor = clamp(fogFactor, 0.0, 1.0);
        }
     
        return fogFactor;
    }
     
    void main(void)
    {
        float fogFactor = computeFogFactor();
     
        gl_FragColor = DestinationColor * fogFactor;
    }

    The following is a screenshot of the result:
    Click image for larger version. 

Name:	Screen Shot 2014-03-31 at 5.08.41 PM.jpg 
Views:	35 
Size:	14.1 KB 
ID:	1254

    My problem is where the lines are connect at the far ends:

    Click image for larger version. 

Name:	Screen Shot 2014-03-31 at 5.05.40 PM.png 
Views:	27 
Size:	6.4 KB 
ID:	1255

    It seems that the colors of both lines are added to receive the result color. This is not the result i want - i would like the transition to look smooth. How can i get that result?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    The problem is with blending, not with fog.
    If you blend multiple primitives on top of each other, then in most cases they will result in such undesired effects. I assume you use here multiplicative blending here, that's why pixels where two primitives overlap you get darker results.

    I don't think it's a good idea to implement fog using blending. Just mix the fog color with the fragment color (using the fog factor) in the fragment shader and simply output the result, and, of course, disable blending.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

  3. #3
    Intern Contributor Brokenmind's Avatar
    Join Date
    Feb 2014
    Location
    Aachen / Germany
    Posts
    71
    A bit of search resulted in this and this posting. Although they're both for iphone, the functions they use also exist in the normal openGL, namely glBlendEquation and glBlendEquationSeparate. You could try tweaking that, and the snipped above is missing how you managed transparency in your non-shader code, namely glBlendFunc

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    21
    Quote Originally Posted by aqnuep View Post
    The problem is with blending, not with fog.
    If you blend multiple primitives on top of each other, then in most cases they will result in such undesired effects. I assume you use here multiplicative blending here, that's why pixels where two primitives overlap you get darker results.

    I don't think it's a good idea to implement fog using blending. Just mix the fog color with the fragment color (using the fog factor) in the fragment shader and simply output the result, and, of course, disable blending.

    It am not sure how to make it work when i have a gradient background - the fog color changes from pixel to pixel.
    How i can i achieve the fog effect without using alpha blending?

  5. #5
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    Quote Originally Posted by esapir View Post
    It am not sure how to make it work when i have a gradient background - the fog color changes from pixel to pixel.
    How i can i achieve the fog effect without using alpha blending?
    The approach aqnuep suggests should still work. When you mix the fog color with the fragment color in your shader, you can calculate the coefficient based on your desired fog density. This calculation might typically be based on the depth, but there's nothing stopping you from e.g. also using the y coordinate in the density calculation if you want denser fog towards the bottom.

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    21
    Quote Originally Posted by reto.koradi View Post
    The approach aqnuep suggests should still work. When you mix the fog color with the fragment color in your shader, you can calculate the coefficient based on your desired fog density. This calculation might typically be based on the depth, but there's nothing stopping you from e.g. also using the y coordinate in the density calculation if you want denser fog towards the bottom.
    My background is a gradient drawn using two rectangles (colors of the vertices with y=0 are not equals to the colors of the vertices with y=height). If i understand correctly, the fog color i use must match the color of the background in the exact pixel. However, i don't have a way to know that is the color of the background when in the shader when i draw my lines - correct?

Posting Permissions

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