加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – OpenGL:GLSL浮点数精度低

发布时间:2020-12-16 07:34:32 所属栏目:百科 来源:网络整理
导读:我有浮动alpha纹理,包含幅度值.它被转换为分贝并以灰度显示. 这是会话代码(C): const float db_min = -100,db_max = 0;float image[height][width];for (int y = 0; yheight; ++y) { for (int x = 0; xwidth; ++x) { image[y][x]= 20.f * log(a[i])/log(10.
我有浮动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,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,0);
}

这是一个截图:

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读