Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I have a very simple vertex shader as follows

#version 120
attribute vec3 position;
attribute vec3 inColor;
uniform mat4 mvp;
varying vec3 fragColor;

void main(void){
    fragColor = inColor;
    gl_Position = mvp * vec4(position, 1.0);
}

Which I load, as well as the fragment shader:

#version 120
varying vec3 fragColor;

void main(void) {
    gl_FragColor = vec4(fragColor,1.0);
}

Which I then load, compile, and link to my shader program. I check for link status using

glGetProgramiv(shaderProgram, GL_LINK_STATUS, &shaderSuccess);

which returns GL_TRUE so I think its ok. However, when I query the active attributes and uniforms using

#ifdef DEBUG
        int totalAttributes = -1;
        glGetProgramiv(shaderProgram, GL_ACTIVE_ATTRIBUTES, &totalAttributes);
        for(int i=0; i<totalAttributes; ++i)  {
            int name_len=-1, num=-1;
            GLenum type = GL_ZERO;
            char name[100];
            glGetActiveAttrib(shaderProgram, GLuint(i), sizeof(name)-1,
                               &name_len, &num, &type, name );
            name[name_len] = 0;
            GLuint location = glGetAttribLocation(shaderProgram, name);
            fprintf(stderr, "Attribute %s is bound at %d\n", name, location);
        }
        int totalUniforms = -1;
        glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &totalUniforms);
        for(int i=0; i<totalUniforms; ++i)  {
            int name_len=-1, num=-1;
            GLenum type = GL_ZERO;
            char name[100];
            glGetActiveUniform(shaderProgram, GLuint(i), sizeof(name)-1,
                              &name_len, &num, &type, name );
            name[name_len] = 0;
            GLuint location = glGetUniformLocation(shaderProgram, name);
            fprintf(stderr, "Uniform %s is bound at %d\n", name, location);
        }
#endif

I get:

Attribute inColor is bound at 0

Attribute position is bound at 1

Uniform mvp is bound at 0

Which leads to failure when trying to use the shader to render the objects. I have tried switching the order of declaration of position & inColor, but still, only position is bound with the other two giving 0

Can someone please explain why this is happening? Thanks

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

0 is a valid location index, with -1 being the signifier of an error; see: http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml and http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml.

If you support GL_ARB_explicit_attrib_location or GLSL 330+ I'd strongly recommend that you switch over to using "layout(location=" syntax for your VS input; this resolves a lot of confusion surrounding attrib locations. Unfortunately explicit uniform locations are GL4.3 only so they're not going to be a realistic option just yet.

Not sure how much of that is available on ES though.

If you're still getting rendering errors despite this then I'd suggest that the cause of your errors is most likely elsewhere. Some description of the errors as well as some more code (your glVertexAttribPointer calls would be particularly useful) would be needed to further diagnose.

share|improve this answer
    
Ok, thanks for the info. I have now corrected my shader attribute binding logic to return failure on location<0, and accept 0 as location. However, I still have nothing rendered on the screen, except the clearing code. Now that it is not a shader validation problem, should I close this question and ask a new one? –  codemonkey Nov 8 '12 at 14:21
    
I'd suggest so, yes; it's clearly a different problem now so a new question focussing on that seems most appropriate. –  Jimmy Shelter Nov 8 '12 at 14:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.