Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glsl cast bool to float

Tags:

glsl

I want to set a float value to 1.0 if one vector equals another, and 0.0 if the vectors are not equal

if( v1 == v2 )  floatVal = 1.0 ;
else  floatVal = 0.0 ;

But wouldn't it be "faster" or an optimization just to set

floatVal = (v1 == v2) ;

But it doesn't work. You can't implicitly (or explicitly) convert float to bool? Is there a way to do this or do I have to use the if statement branch?

like image 578
bobobobo Avatar asked Dec 02 '12 22:12

bobobobo


2 Answers

Didn't you try "float(bool)" function?

GLSLangSpec.Full.1.20.8.pdf section 5.4.1 says you can do all those conversions.

like image 187
CuriousChettai Avatar answered Oct 13 '22 19:10

CuriousChettai


CuriousChettai's right. Just write:

floatVal = float(v1 == v2);

GLSL gives you a compile-error if you assign values with possible loss of precision. So you can do things like:

float f = 3;       // works
int i = 3.0;       // compiler-error
int j = int(3.0);  // works
like image 22
euphrat Avatar answered Oct 13 '22 17:10

euphrat