In a GLSL fragment shader I am trying to cast a float into an int. The compiler raises an error:

ERROR: 0:60: '=' :  cannot convert from 'mediump float' to 'highp int'

I tried raising the precision of the int:

mediump float indexf = floor(2.0 * mixer);
highp int index = indexf;

but to no avail.

How do I cast the int properly?

  • 1
    \$\begingroup\$ For future reference, you will get better answers if you ask general programming questions like this one at Stack Overflow. stackoverflow.com - depending on community feedback this question might be migrated there anyway. \$\endgroup\$ – Ricket Feb 17 '11 at 18:48
  • \$\begingroup\$ For reinterpret casts, there are now functions such as floatBitsToInt. \$\endgroup\$ – jozxyqk Oct 10 '14 at 9:35

Try this:

highp int index = int(indexf);

I found it here.

  • \$\begingroup\$ Also note that casting a float to an int automatically floors it (at least in any implementation I've ever seen) so your call to floor should be unnecessary. \$\endgroup\$ – Ricket Feb 17 '11 at 20:03
  • 9
    \$\begingroup\$ In most languages the rounding mode is truncate / round-to-zero, which is equivalent to floor for positive numbers but not negative numbers. I don't remember if this applies to GLSL, but I'd be surprised if it doesn't. \$\endgroup\$ – user744 Feb 17 '11 at 20:19
  • \$\begingroup\$ @Joe That's an interesting fact that I never thought about, and I just confirmed it with a Java test case. \$\endgroup\$ – Ricket Feb 18 '11 at 3:05
  • 4
    \$\begingroup\$ Is highp important here? (On an Integer) \$\endgroup\$ – Léon Pelletier Jul 5 '15 at 6:11
  • 1
    \$\begingroup\$ How cna I convert this to const so that I can sample an array? \$\endgroup\$ – lesolorzanov Feb 7 '19 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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