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;
}
}
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 < textured < 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.