Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glsl infinity constant

Tags:

infinity

glsl

Does GLSL have any pre-defined constants for +/-infinity or NaN? I'm doing this as a workaround but I wonder if there is a cleaner way:

// GLSL FRAGMENT SHADER
#version 410

<snip>

const float infinity = 1. / 0.;

void main ()
{
    <snip>
}

I am aware of the isinf function but I need to assign infinity to a variable so that does not help me.

like image 412
atb Avatar asked May 03 '12 16:05

atb


1 Answers

Like Nicol mentioned, there are no pre-defined constants.

However, from OpenGL 4.1 on, your solution is at least guaranteed to work and correctly generate an infinite value.

See for example in glsl 4.4:

4.7.1 Range and Precision

...

However, dividing a non-zero by 0 results in the appropriately signed IEEE Inf: If both positive and negative zeros are implemented, the correctly signed Inf will be generated, otherwise positive Inf is generated.

Be careful when you use an older version of OpenGL though:

For example in glsl 4.0 it says:

4.1.4 Floats

...

Similarly, treatment of conditions such as divide by 0 may lead to an unspecified result, but in no case should such a condition lead to the interruption or termination of processing.

like image 111
Jan Rüegg Avatar answered Sep 30 '22 19:09

Jan Rüegg