c – OpenGL:GLSL浮点数精度低

我有浮动alpha纹理,包含幅度值.它被转换为分贝并以灰度显示.

这是会话代码(C):

const float db_min = -100, db_max = 0;
float image[height][width];
for (int y = 0; y<height; ++y) {
  for (int x = 0; x<width; ++x) {
    image[y][x]= 20.f * log(a[i])/log(10.f);
    image[y][x] = (image[y][x]-db_min)/(db_max-db_min);
  }
}

这是片段着色器(GLSL):

#version 120
precision highp float;
varying vec2 texcoord;
uniform sampler2D texture;
void main() {
  float value = texture2D(texture, texcoord).a;
  gl_FragColor = vec4(value, value, value, 0);
}

这是一个截图:

看起来很完美现在,我想在Fragment Shader中编写对话,而不是C:

#version 120
precision highp float;
varying vec2 texcoord;
uniform sampler2D texture;
const float db_min = -100., db_max = 0.;
void main() {
  float value = texture2D(texture, texcoord).a;
  value = 20. * log(value)/log(10.);
  value = (value-db_min)/(db_max-db_min);
  gl_FragColor = vec4(value, value, value, 0);
}

这是一个截图:

为什么结果不同?我究竟做错了什么?

最佳答案 有限的纹理精度 – 这可能是问题.我可以猜测你在8位每通道纹理(例如RGBA8)中保持你的(我理解的日志非常高的范围)值.然后,您可以使用浮点格式或将高精度/范围值打包为4字节格式(例如,固定点).

点赞