Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a uniform color to a fragment shader (openGL ES 2.0)

This is a strange problem. If I try to pass a uniform color to the fragment shader, i get a compile error

uniform vec4 uniformColor;

void main(){
   gl_FragColor = uniformColor;
}

But if I pass the same uniform color to the vertex shader then pass it to the fragment shader via a varying, then it works fine..

    attribute vec4 position;
    uniform mat4 matrix;
    uniform vec4 uniformColor;
    varying vec4 fragmentColor; 
    void main()
    {
        gl_Position = matrix * position;
        fragmentColor = uniformColor;
    }

and

varying lowp vec4 fragmentColor;
void main()
{
  gl_FragColor = fragmentColor;
}

this is on an iOS.

I'm a little confused as copying and pasting examples from online gives me errors.

like image 348
reza Avatar asked Jun 25 '13 06:06

reza


1 Answers

There is no default float precision qualifier for fragment shaders in OpenGL ES. A precision qualifier is required to use any floating pointer variable - it doesn't matter if it's a uniform, varying, or just a local variable. There is a default precision for vertex shaders, so you don't need to add any qualifiers there.

In OpenGL the precision qualifier is not required, which is why the example you're pointing to does not include them.

Try adding this to the top of your shader and you should be fine:

precision mediump float;
like image 148
Jan-Harald Avatar answered Sep 20 '22 01:09

Jan-Harald