i've implemented a shader that produces a simple Radar Ping given a uniform time input. ShaderToy Proof of concept

Now i implemented this into my real Opengl GLFW GLAD C++ Core4.5Profile NVIDIA GTX960 environment and recieve only weird ouput when i use my own function call.

Why do i recieve such a weird output if i use a function call in opengl glsl?

PS: I have a feeling this has something to do with the precision and the call by value standards of glsl.

Thank you very much for any response, or even a hint where to look for help.

EDIT: The problem was as Kevin van der Velden described.

Result Images:result

This is my shader Code:

    #version 330 core

    in vec2 v_TexCoord;
    out vec4 FragColor;

    uniform float u_time;

    // Generates a BnW Ping on normalized 2d coordinate system
    float RadarPing(in vec2 uv, in vec2 center,in  float innerTail, 
                   in float frontierBorder, in float timeResetSeconds, 
                   in float radarPingSpeed, in float fadeDistance,
                   in float currentTime)
        vec2 diff = center-uv;
        float r = length(diff);
        float timeMod = mod(currentTime, timeResetSeconds) * radarPingSpeed;

        float circle;
        circle += smoothstep(timeMod - innerTail, timeMod, r) * smoothstep(timeMod + frontierBorder,timeMod, r);
        return circle;

    void main()
        //normalize coordinates 
        vec2 uv = v_TexCoord; //move coordinates to 0..1
        uv = uv*2.; // translate to the center
        uv += vec2(-1.0, -1.0);
        //uv.x *= iResolution.x/iResolution.y; //correct the aspect ratio

        float color;
        // generate some radar pings
        float fadeDistance = 1.0;
        float resetTimeSec = 5.0;
        float radarPingSpeed = 0.3;
        vec2 greenPing = vec2(0.0, -1.0);

        //vec2 diff = greenPing-uv;
        //float r = length(diff);
        //float moduloTime = mod(u_time, resetTimeSec);
        //color = smoothstep(moduloTime - 0.25, moduloTime, r) * moduloTime + 0.025, moduloTime, r);

        color += RadarPing(uv, greenPing, 0.25, 0.025,  radarPingSpeed, fadeDistance, u_time);

        FragColor = vec4(color, color, color,1.0);

1 Answer 1


You're invoke (what C++ would call) undefined behaviour, in these lines:

    float color;
    color += RadarPing(uv, greenPing, 0.25, 0.025,  radarPingSpeed, fadeDistance, u_time);

You're not setting color to a value, so depending on the drivers it will just use any old value. Then you're adding a value to it.

Notice that in your code without function call you're using color = smoothstep(..., which means you're ignoring whatever was stored in the variable completely, causing it to work as expected.

  • \$\begingroup\$ You are correct. Thank you very much. EDIT: Almost.. the problem lied in the function itself... the same issue you pointed out. Also an undefined behaviour in the circle += smoothstep(...); line. [Thread closed] \$\endgroup\$
    – LordSmith
    Mar 4, 2017 at 19:38
  • \$\begingroup\$ I'd like to add a little to this: When instantiating types in any language, best practice is RAII (Resource Acquisition Is Initialisation), meaning whenever you create a new type, initialise it's data, or it will use whatever the bits are currently set to in physical memory, as requesting memory does not automatically zero it. \$\endgroup\$
    – Ian Young
    Mar 24, 2017 at 5:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .