2

I am doing some stuff in a glsl fragment shader, and getting some strange results. The code i use for calculations should for now not really be relevant, but for some kind of "debugging" I display colors dependent on the range a variable is in.

The coded for that is: (shadow is a float)

if(shadow == 0.0f)
{
    vFragColor = vec4(1.0f, 0.0f, 0.0f, 0.0f);
}
else if(shadow == 1.0f)
{
    vFragColor = vec4(0.0f, 1.0f, 0.0f, 0.0f);
}
else if(shadow < 1.0f && shadow > 0.0f)
{
    vFragColor = vec4(0.0f, 0.0f, 1.0f, 0.0f);
}
else if(shadow < 0.0f)
{
    vFragColor = vec4(1.0f, 0.0f, 1.0f, 0.0f);
}
else if(shadow > 1.0f)
{
    vFragColor = vec4(0.0f, 1.0f, 1.0f, 0.0f);
}
else
{
    vFragColor = vec4(1.0f, 1.0f, 1.0f, 0.0f);
}

From my understanding, it should not be possible to enter the "else" at the end, since the code before checks for any possible value. But what happens is, that for the areas where I get some strange results, and I wanted to do that kind of "debugging", the code enters the "else" statement.

Does anybody know how this could happen? I do not understand this at all...

7
  • never compare floating point for exact equality, instead see if the distance between the 2 is smaller than some delta Commented Jun 3, 2014 at 8:42
  • @ratchetfreak yes, I know, but the else if statements cover anything from smaller than 0, exactly 0, between 0 and 1, exactly 1 and greater than one... so delta or not, how can the code enter the last else statement?
    – nurgan
    Commented Jun 3, 2014 at 8:44
  • How do you know it reached latest statement if alpha value is 0? It is completely transparent.
    – keltar
    Commented Jun 3, 2014 at 8:52
  • 1
    @nurgan: floats can also be "Not a Number".
    – datenwolf
    Commented Jun 3, 2014 at 9:17
  • 2
    @ratchetfreak Sometimes it is inappropriate to compare floating-point numbers for equality and sometimes it isn't. If you decide “never” to compare them, you choose the path of ignorance and superstition, which may be fine for you, but why should this be the right choice for others? Commented Jun 3, 2014 at 11:41

1 Answer 1

6

floats can also be "Not a Number"

3
  • Do not forget Positive / Negative Infinity as well. There are two missing cases from this comparison, infinity and NaN. You can test both using isinf (...) and isnan (...) respectively in GLSL. Unlike NaN, +/- Infinity will trigger 2 of those cases, but there is no infinity constant in GLSL that you can use to test for equality with infinity, so I figured I would bring up the functions to check for the bit-patterns. Commented Jun 5, 2014 at 4:56
  • @AndonM.Coleman: Shouldn't the infinity cases be covered by the < and > operators?
    – datenwolf
    Commented Jun 5, 2014 at 9:20
  • Yes, that is why I said that it triggers 2 of the cases. But if you want to test for equality with infinity, that needs a special case of its own. Commented Jun 5, 2014 at 16:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.