Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLfloat or GLdouble by default?

Tags:

c++

opengl

In C++, it is better to use double by default and float or long double only when we really know we need them. For OpenGL though, I see that GLfloat seems to be the default. Is it still pertinent considering that double precision is now common on newest GPUs?

like image 471
Korchkidu Avatar asked Nov 21 '13 12:11

Korchkidu


1 Answers

The opposite is the case. You should be using float by default in C++ and only use double if you are positively sure that you really need those extra digits of precision (for some scientific, presumably astronomic or particle physics, simulation). Often when you think you need double, you should be using fixed-point, or you should get your float values into the correct range.

You do not use double precision for graphics. Although all modern GPUs support it just fine, double precision math is often slower than single precision, and it has no observable advantage. Everything related to "graphics" (or let's say 99.99% since "everything" is a very definite wording) works just fine using float or GLfloat for that matter.
Even some years ago when some GPUs worked with only 24-bit precision, there was no visible difference.

In fact, when people use double because they think they should, then 99% of the time they are doing it wrong.

Tom Forsyth even made "double precision" the first point of his Offend-O-Matic. He relates specifically to "games" but it really applies to almost every software, with some scientific simulations being the exception. Be sure to also read the accompanying article A Matter of Precision to understand his reasoning.
A less offensive must-read on floating point is Ericson's epsilon is not 0.000001 presentation.

It is very similar in OpenGL. If you understand how floating point works and use it correctly, then float will work just fine. The rounding errors when applying a transform matrix or such are not visible.
If you don't understand how it works, using double precision will shift the problems backwards a bit, but it will still bite you when you don't expect it.

Stunningly, even nowadays people still use double precision floating point even for absurd causes like currency or time (because it's twice as much precise, eh?) and are surprised when it blows up in their face and people start yelling at them.

like image 79
Damon Avatar answered Sep 28 '22 08:09

Damon