2

目前,我在光照通过后使用以下公式对颜色进行伽玛校正(将它们从 RGB 转换为 sRGB 颜色空间):

output = pow(color, vec3(1.0/2.2));

这个公式是伽马校正的正确公式吗?我问是因为我遇到一些人说不是,而且正确的公式更复杂,与幂2.4而不是2.2有关。我还听说 R、G 和 B 这三种颜色应该有不同的权重(比如 0.2126、0.7152、0.0722)。

我也很好奇 OpenGL 在启用时GL_FRAMEBUFFER_SRGB会使用哪个功能。

编辑:这是盖伊戴维森的演讲“你所知道的关于颜色的一切都是错误的”中涵盖的众多主题之一。伽马校正功能在这里介绍,但整个讨论与色彩空间有关,包括 sRGB 和伽马校正。

4

1 回答 1

4

Gamma 校正可能有任何值,但考虑到线性 RGB/非线性 sRGB 转换,2.2 是一个近似值,因此您的公式可能被认为是错误和正确的: https ://en.wikipedia.org/wiki/SRGB#Theory_of_the_transformation

真正的 sRGB 传递函数基于 2.4 伽马系数,并且在暗值处具有不连续性,如下所示:

float Convert_sRGB_FromLinear (float theLinearValue) {
  return theLinearValue <= 0.0031308f
       ? theLinearValue * 12.92f
       : powf (theLinearValue, 1.0f/2.4f) * 1.055f - 0.055f;
}
float Convert_sRGB_ToLinear (float thesRGBValue) {
  return thesRGBValue <= 0.04045f
       ? thesRGBValue / 12.92f
       : powf ((thesRGBValue + 0.055f) / 1.055f, 2.4f);
}

实际上,您可能会在某些 GLSL 代码中发现更粗略的近似值,使用 2.0 系数而不是 2.2 和 2.4,以避免使用昂贵的pow()( x*xand sqrt()are used instead)。这是为了实现最佳性能(在旧图形硬件的情况下)和代码简单性,同时牺牲色彩再现。实际上,牺牲并不那么明显,而且大多数游戏都应用了额外的色调映射和用户管理的伽马校正系数,因此结果与 sRGB 标准没有直接关系。

GL_FRAMEBUFFER_SRGB并且从GL_SRGB8纹理中采样预计将使用更正确的公式(在纹理采样的情况下,它更有可能在 GPU 上预先计算查找表,而不是真正的公式,因为只有 256 个值要转换)。例如,请参阅对GL_ARB_framebuffer_sRGB 扩展的注释:

 Given a linear RGB component, cl, convert it to an sRGB component, cs, in the range [0,1], with this pseudo-code:

     if (isnan(cl)) {
         /* Map IEEE-754 Not-a-number to zero. */
         cs = 0.0;
     } else if (cl > 1.0) {
         cs = 1.0;
     } else if (cl < 0.0) {
         cs = 0.0;
     } else if (cl < 0.0031308) {
         cs = 12.92 * cl;
     } else {
         cs = 1.055 * pow(cl, 0.41666) - 0.055;
     }

  The NaN behavior in the pseudo-code is recommended but not specified in the actual specification language.
  sRGB components are typically stored as unsigned 8-bit fixed-point values.
  If cs is computed with the above pseudo-code, cs can be converted to a [0,255] integer with this formula:

     csi = floor(255.0 * cs + 0.5)

这是另一篇描述 OpenGL 应用程序中 sRGB 使用的文章,您可能会发现它很有用:https ://unlimited3d.wordpress.com/2020/01/08/srgb-color-space-in-opengl/

于 2020-04-10T10:41:18.267 回答