Ios – Output of vertex shader ‘colorVarying’ not read by fragment shader

glsliosopengl-esopengl-es-2.0shader

As is shown below, the error is very strange. I use OpenGLES 2.0 and shader in my iPad program, but it seems something goes wrong with the code or project configuration. The model is drawn with no color at all (black color).

2012-12-01 14:21:56.707 medicare[6414:14303] Program link log:
WARNING: Could not find vertex shader attribute 'color' to match BindAttributeLocation request.
WARNING: Output of vertex shader 'colorVarying' not read by fragment shader
[Switching to process 6414 thread 0x1ad0f]

And I use glBindAttibLocation to pass position and normal data like this:

// This needs to be done prior to linking.
glBindAttribLocation(_program, INDEX_POSITION, "position");
glBindAttribLocation(_program, INDEX_NORMAL, "normal");

glBindAttribLocation(_program, INDEX_COLOR, "color"); //pass color to shader

There are two shaders in my project. So any good solutions to this odd error? Thanks a lot!

My vertex shader:

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

attribute vec4 position;
attribute vec3 normal;
attribute vec4 color;

varying lowp vec4 DestinationColor;
void main()
{
    //vec4 a_Color = vec4(0.9, 0.4, 0.4, 1.0);
    vec4 a_Color = color;
    vec3 u_LightPos = vec3(1.0, 1.0, 2.0);

    float distance = 2.4;
    vec3 eyeNormal=normalize(normalMatrix * normal);

    float diffuse = max(dot(eyeNormal, u_LightPos), 0.0); // remove approx ambient light
    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
    DestinationColor = a_Color * diffuse; // average between ambient and diffuse   a_Color * (diffuse + 0.3)/2.0;

    gl_Position = modelViewProjectionMatrix * position;
}

And my fragment shader is:

varying lowp vec4 DestinationColor;

void main()
{
  gl_FragColor = DestinationColor;
}

Very simple. Thanks a lot!

Best Answer

I think there are a few things wrong here. First your use of attribute might not be right. An attribute is like an element that changes for each vertex.. do you have the color as an element in your data structure? Cause if not, the shader isn't going to work right.

And I use glBindAttibLocation to pass position and normal data like this:

no you don't. glBindAttribLocation "Associates a generic vertex attribute index with a named attribute variable". It doesn't pass data. It associates an index (an glint) with the variable. You pass things in later with: glVertexAttribPointer.

I don't even use the bind.. I do it this way - set up the attribute:

glAttributes[PROGNAME][A_vec3_vertexPosition] = glGetAttribLocation(glPrograms[PROGNAME],   "a_vertexPosition");
glEnableVertexAttribArray(glAttributes[PROGNAME][A_vec3_vertexPosition]);

and then later before calling glDrawElemetns pass your pointer to it so it can get the data:

glVertexAttribPointer(glAttributes[PROGNAME][A_vec3_vertexPosition], 3, GL_FLOAT, GL_FALSE, stride, (void *) 0);

There I'm using a 2 dimensional array of ints called glAttributes to hold all of my attribute indexes. But you can use glints like you are now.

The error message tells you what's wrong. In your vertex shader you say:

attribute vec4 color;

But then down below you also have an a_Color:

DestinationColor = a_Color * diffuse;

Be consistent with your variable names. I put a_ v_ and u_ in front of all mine now to try to keep straight what kind of variable it is. What you're calling an a_ there is really a varying.

I also suspect that the error message was not from the same version of the shader and code that you posted because of the error:

WARNING: Output of vertex shader 'colorVarying' not read by fragment shader

And the error about colorVarying is confusing when it isn't even in this version of your vertex shader. Repost the current version of the shaders and the error messages you get from those and it will be easier to help you.

Related Topic