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

2

6

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.

Keister answered 25/6, 2013 at 6:44 Comment(5)
Have you tried explicitly specifying precision (lowp, mediump, etc.)? Also, what error message do you get on shader compilation?Hesperian
Post the error messageKarlakarlan
I didn't know you needed to specify one for a uniform. one isn't specified here: opengl.org/sdk/docs/tutorials/ClockworkCoders/uniform.php And the call to GetShader doesn't return a true status.Keister
Yep, it's just my guess. Detailed compilation error message may be helpful.Hesperian
how can i get a detailed compilation error from ios? I'm actually compiling the code on monotouch/xamarin by tweaking their OpenGLES20Example.Keister
E
4

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;
Effeminacy answered 25/6, 2013 at 22:25 Comment(0)
F
3

I had the same issue and it was indeed related to the missing precision, but the issue is that if a uniform is shared between the vertex and fragment shaders, it must be of the same precision (at least in ES 2.0). This makes sense I suppose, because they are shared between the shaders.

And since the default precision for vertex shaders is highp, the precision for this uniform in the fragment shader needs to be highp too, as in:

precision mediump float;
uniform highp vec4 uniformColor;

Alternatively you can change the precision to mediump in the vertex shader.

The error message was very informative, when I finally noticed it: Symbol uEcLightPos defined with different precision in vertex and fragment shaders.

Florettaflorette answered 12/10, 2013 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.