我想知道为什么 OpenGL 在其函数中使用 float 而不是 double 。Double 应该比 float 准确得多。
2 回答
In the past, many OpenGL functions did have a double
variant. glMultMatrix
for example has f
and d
variations. Most of these don't exist anymore, but that has nothing to do with float
vs. double
. glMultMatrixd
and glMultMatrixf
are gone in core GL 3.1 and above.
In core OpenGL, there are still functions that have double
variants. glDepthRange
takes double
, though there is a float
version (introduced mainly for GL ES compatibility). There are some functions that don't have double
variants, like glBlendColor
.
Sometimes, OpenGL is just being inconsistent. Other times, it is simply following a reasonable principle: not lying to the user.
Take glBlendColor
. If you could pass it double-precision values, that would imply that floating-point blending took place with double-precision accuracy. Since it most certainly does not (on any hardware that exists), providing an API that offers that accuracy is a tacit lie to the user. You're taking high-precision values to a low-precision operation. Though the same logic is true of glDepthRange
(no double-precision depth buffers are not available), yet it takes doubles
. So again, inconsistency.
The glUniform*
suite of functions is a much better example. They set state into the current program object. Until GL 4.0, the double
versions did not exist. Why? Because that would have been a lie. GLSL pre-4.0 did not allow you to declare a double
, for the simple and obvious reason that no pre-4.0 hardware could implement it. There's no point in allowing the user to create a double
if the hardware couldn't handle it.
因为大多数时候你不需要精度,而双精度数是两倍。