2016-11-13 409 views
1

我正在尝试使GLSL中的某个颜色具有某个位置的功能。在GLSL ES 3.00及以上版本中支持按位运算符

precision highp float; 
uniform sampler2D uTexture0; 
varying vec2 vTextureCoords; 

int getBit(float color, int bit){ 
     highp int colorInt = int(color); 
     return (colorInt >> bit) & 1; 
} 

void main(void) { 
    highp vec4 texelColour = texture2D(uTexture0, vec2(vTextureCoords.s, vTextureCoords.t)); 

if(getBit(texelColour.r * 255.0, 7) == 1){ 
     gl_FragColor = vec4(0.5, 0.8, 0.5, 0.8); 
}else{ 
     gl_FragColor = vec4(0.4, 0.1, 0.0, 0.0); 
} 

}

Chrome和Firefox返回该错误

ERROR: 0:35: '>>' : bit-wise operator supported in GLSL ES 3.00 and above only 
ERROR: 0:35: '&' : bit-wise operator supported in GLSL ES 3.00 and above only 

我试图迫使版本在我的着色器的第一线,像这样:

#version 130 

但控制台返回版本不受支持。

有没有办法为getBit函数创建子例程? 或者什么设置启用在sharder片段中实现bitwize运算符?

感谢您的回复。

纪尧姆

回答

0

我发现在写我自己和移位功能

int getBit(float num, float b){ 
     num = num * 255.0; 
     int bit = 0; 
     for(int i = 7; i >= 0; i--){ 
      if(num >= pow(2.0,float(i))){ 
       num = num - pow(2.0,float(i)); 
       if(b == float(i)){ 
        bit = 1; 
       } 
      } 
     } 
     return bit; 
    } 


if(getBit(texelColour.r , 3.0) != 0 && getBit(texelColour.g * 255.0, 0.0) == 0 && getBit(texelColour.b * 255.0, 0.0) == 0){ 
    gl_FragColor = vec4(0.5, 0.8, 0.5, 0.8); 
}else{ 
    gl_FragColor = vec4(0.4, 0.1, 0.0, 0.0); 
} 

我在GLSL福利局,因此可以肯定是更好,但它有效的解决方案。

PS:我需要的是只有1个字节小数

纪尧姆