3
votes

I'm new to OpenGL ES 2.0 so please bear with me... I'd like to pass a BOOL flag into my fragment shader so that after a certain touch event has occurred in my app, it renders gl_FragColor differently. I tried using a vec2 attribute for this and just "faking" the .x value as my "BOOL" but it looks like OpenGL is normalizing the value from 0.0 to 1.0 before the shader gets ahold of it. So even though in my app I've set it to 0.0, while the shader is doing its thing, the value will eventually reach 1.0. Any suggestions would be hugely appreciated.

VertexAttrib Code:

// set up context, shaders, use program etc.

[filterProgram addAttribute:@"inputBrushMode"];
inputBrushModeAttribute = [filterProgram attributeIndex:@"inputBrushMode"];

bMode[0] = 0.0;
bMode[1] = 0.0;

glVertexAttribPointer(inputBrushModeAttribute, 2, GL_FLOAT, 0, 0, bMode);

Current Vertex Shader Code:

...
attribute vec2 inputBrushMode;
varying highp float brushMode;

void main()
{
    gl_Position = position;
    ...
    brushMode = inputBrushMode.x;
}

Current Fragment Shader Code:

...
varying highp float brushMode;

void main()
{
    if(brushMode < 0.5) {
        // render the texture
        gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
    } else {
        // cover things in yellow funk
        gl_FragColor = vec4(1,1,0,1);
    }
}

Thanks in advance.

1
"it looks like OpenGL is normalizing the value from 0.0 to 1.0 before the shader gets ahold of it" First, why aren't you using a uniform? Why use a vertex attribute? Second, where is your setup code for providing this attribute? Are you using glVertexAttribPointer correctly?Nicol Bolas
I was under the impression that a uniform's value can't be changed later on, is that not true? I'm pretty sure I'm using glVertexAttribPointer correctly - I'll add that code. Thanks.taber
You can change uniform values at any time (except in the middle of a batch).Tim
@taber: Uniforms don't change within a draw call. But they can be changed between draw calls with glUniform. They are "uniform" compared to "attribute" and "varying". The thing that can't be changed ever is const.Nicol Bolas
Ah nice, thanks, I'll have to give uniforms a shot. Is it possible to pass a single float as a uniform or will it need to also be a vec2 array pointer?taber

1 Answers

9
votes

Create the bool as a glUniform (1.0 or 0.0) instead. Set its value with glUniform1f(GLint location, GLfloat v0). In the shader, check its value like so:

if (my_uniform < 0.5) {
    // FALSE
} else {
    // TRUE
}