OpenGL ES如何在着色器中设置布尔值。

huangapple go评论75阅读模式
英文:

opengl es how to set boolean in shader

问题

我想在我的片段着色器中设置布尔值,因为我需要通过乘法识别何时对物体上色,但我不知道如何从Java代码访问这个名为textured的布尔变量。项目在Java中为Android创建,使用OpenGL ES 3.0

    precision mediump float;
    uniform vec4 vColor;
    uniform bool textured;
    uniform sampler2D uTexture;
    varying vec2 vTexCoordinate;
    
    void main(){
        if(textured){
            gl_FragColor = texture2D(uTexture, vTexCoordinate);
            gl_FragColor *= vColor;
        } else {
            gl_FragColor = vColor;
        }
    }
英文:

I want to set boolean value in my fragment shader beacuse i need to recognize when to color objects by multiplying. but i dont know how to acess this boolean variable textured from java code. Project is created in java for android, using opengl es 3.0.

precision mediump float;
uniform vec4 vColor;
uniform bool textured;
uniform sampler2D uTexture;
varying vec2 vTexCoordinate;

void main(){
    if(textured){
        gl_FragColor = texture2D(uTexture, vTexCoordinate);
        gl_FragColor *= vColor;
    } else {
        gl_FragColor = vColor;
    }
}

答案1

得分: 2

我建议使用int而不是boolif (textured != 0)

或者您可以传递一个浮点值来加权纹理:

precision mediump float;
uniform vec4 vColor;
uniform float textured;
uniform sampler2D uTexture;
varying vec2 vTexCoordinate;

void main()
{
    gl_FragColor = 
       vColor * mix(vec4(1.0), texture2D(uTexture, vTexCoordinate), textured);
}

mix 在两个值之间线性插值。看表达式:

gl_FragColor = 
    vColor * mix(vec4(1.0), texture2D(uTexture, vTexCoordinate), textured);

如果 textured == 0.0,颜色会乘以 vec4(1.0)

gl_FragColor = vColor * vec4(1.0);

如果 textured == 1.0,颜色会乘以从纹理查找返回的颜色:

gl_FragColor = vColor * texture2D(uTexture, vTexCoordinate);

如果 0.0 < textured < 1.0,则 vec4(1.0) 和纹理颜色会线性插值。

此外,在条件语句中查找纹理时要小心。请参阅OpenGL ES 1.1 完整规范 - 6 纹理访问;第110页

> 在非均匀条件块的主体内访问多级贴图纹理会产生未定义的值。非均匀条件块是指其执行在编译时无法确定的块。

英文:

I recommend to use an int rather than a bool: if (textured != 0)

Alternatively you can pass a floating point value which weights the texture:

precision mediump float;
uniform vec4 vColor;
uniform float textured;
uniform sampler2D uTexture;
varying vec2 vTexCoordinate;

void main()
{
    gl_FragColor = 
       vColor * mix(vec4(1.0), texture2D(uTexture, vTexCoordinate), textured);
}

mix linearly interpolate between two values. Look at the expression:

gl_FragColor = 
    vColor * mix(vec4(1.0), texture2D(uTexture, vTexCoordinate), textured);

If textured == 0.0, the color is multiplied by vec4(1.0):

gl_FragColor = vColor * vec4(1.0);

If textured == 1.0, then the color is multiplied by the color returned from the texture lookup:

gl_FragColor = vColor * texture2D(uTexture, vTexCoordinate);

If 0.0 &lt; textured &lt; 1.0, then vec4(1.0) and the texture color are interpolated linearly.


Furthermore be careful when you look up a texture in a condition statement. See OpenGL ES 1.1 Full Specification - 6 Texture Accesses; page 110:

> Accessing mip-mapped textures within the body of a non-uniform conditional block gives an undefined value. A non-uniform conditional block is a block whose execution cannot be determined at compile time.

huangapple
  • 本文由 发表于 2020年9月27日 03:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64081723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定