6
\$\begingroup\$

When using GLboolean, should I just use true and false or should I use GL_TRUE and GL_FALSE? When should I prefer using GL_TRUE and GL_FALSE? or maybe I shouldn't care at all (Because both works)?

Here It is said that both are actually a different type.

But in some tutorial websites I've read, they use GL_TRUE or GL_FALSE on functions that requires GLboolean. Example:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*) 0);

Well, it works too if we substitute GL_FALSE with false in the code above.

\$\endgroup\$
  • \$\begingroup\$ If you're to submit a value to the OpenGL API, use GL_TRUE or GL_FALSE, otherwise use true or false. \$\endgroup\$ – Alexandre Vaillancourt May 22 '16 at 13:00
  • \$\begingroup\$ @AlexandreVaillancourt Is there any more cases? or that's just it? \$\endgroup\$ – Greffin28 May 22 '16 at 13:08
  • \$\begingroup\$ Generally you'll try to use the API as much as you can, because it's been designed like this. Imagine this: on your main target platform, true, false, GL_TRUE and GL_FALSE are represented as short ints. All is fine... until you decide to port your code to a second platform. Unfortunately, on that new platform, true and false are still represented as short ints, but GL_TRUE and GL_FALSE use ints as their underlying types. You could get a lot of hard to find bugs. That aside, it's a good practice to correctly use a supplied API; you get less surprises this way. \$\endgroup\$ – Alexandre Vaillancourt May 22 '16 at 13:20
  • 1
    \$\begingroup\$ And your colleagues think better of you :) \$\endgroup\$ – Alexandre Vaillancourt May 22 '16 at 13:21
  • \$\begingroup\$ @AlexandreVaillancourt Can you provide an example of such bug that can be caused by different sizes of GL_TRUE/FALSE and true/false? They are usually used as function arguments, thus they will be converted to a proper type anyway. \$\endgroup\$ – HolyBlackCat May 22 '16 at 13:22
6
\$\begingroup\$

It's only a matter of opinion and code style. You can use:

  • GL_TRUE and GL_FALSE
  • true and false
  • 1 and 0
  • Or anything that can be converted to 1 and 0.

They all may have different types, but usually it does not matter because they all will be implicitly converted to GLboolean upon use.

Use whatever you want, any of them will work.

My only advice would be to choose one style and maintain the consistent usage of it across your code.

\$\endgroup\$
  • \$\begingroup\$ Hmm, stick with one side is a good idea indeed.. I have a question, because it can differs, is it okay to let's say use GLboolean like: if (glBooleanVariable)? is there going to be "some rare/special case" to make it not function properly? (so i have to type it like: if (glBooleanVariable == GL_TRUE)) \$\endgroup\$ – Greffin28 May 22 '16 at 14:51
  • 2
    \$\begingroup\$ @Greffin28 That's fine; explicit comparisons to true (or GL_TRUE in this case) are not necessary. \$\endgroup\$ – Josh May 22 '16 at 16:03
2
\$\begingroup\$

Those macros were created because C didn't have a true/false keyword like C++ does.

In C using GL_TRUE and GL_FALSE made the code easier to read compared to using just 0 and 1 so it was provided by the OpenGL headers.

In C++ you may as well use the builtin keywords true and false and enjoy your favorite IDE's highlighting feature. Or you may use OpenGL's macro to be consistent with OpenGL code.

  • GL_TRUE is 1 and GL_FALSE is 0.
  • C++'s true is also 1 and false is also 0.

It's guaranteed to work.

For portability however you must still use GLboolean and not bool for any GL functions demanding a pointer to GLboolean (such as glGetBooleanv) even if they happen to be the same size on current platforms. That may not be the case in the future.

But assigning GLboolean b = true; is perfectly safe.


To answer the comment on explicitly comparing to GL_TRUE/GL_FALSE:

It's generally a bad idea to compare to GL_TRUE or true (eg: if(x == true)) as this may introduce hard to find bugs (eg: where x is 2). You should use simply if(x) as anything other than zero is considered true but may not be equal to GL_TRUE or true and this may create an inconsistency between what your code thinks is true and OpenGL's interpretation.

GLboolean are not bool and may (accidentally) contain a value other than 1 or 0. This can cause some confusion and the comparison failing can result in a red herring and many hours of debugging down the wrong path when tracing code.

Comparing to GL_FALSE or false is safe (both == and !=) as only 0 can be false.

So if you wish to use an explicit comparison use if(x != GL_FALSE) to consistently compare x to "true".

\$\endgroup\$

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.