Why glVertexAttrib does not work?

i wrote a simple shader, in the vertex shader ,i declare a variable: in vect4 color;
i use it like that gl_FrontColor = color;

However , i want to change the value of color, so i wrote
the following code ,but it does not change the color to red.
why???can someone explain this to me ?
void Shader::init(const char *vsFile, const char *fsFile)
{
…;
glShaderSource(…);
glCompileShader(…);
glAttachShader(…);

 .....;
GLfloat c[]={1.0,0.0,0.0,1.0};
GLint loc;
loc=glGetAttribLocation(shader_id,"color");
glVertexAttrib3fv(loc,c);
glBindAttribLocation(shader_id,loc,"color"); 


    glLinkProgram(shader_id);

}

glGetAttribLocation queries the location assigned when you link the program. You haven’t linked it yet.

If you’d rather assign the location yourself instead of letting OpenGL assign it, use glBindAttribLocation before link.

You should only call one or the other, not both. Take your pick – whichever way you want to do it is fine. But make the call at the appropriate point relative to LinkProgram.

There’s also GLSL syntax to bind an attribute to a specific location in the shader, if you prefer. If you use that, you don’t have to call either one of the above APIs.

Your choice.

See this for details:

http://www.opengl.org/wiki/glVertexAttribPointer

could you write an examle of the correct order for me?

i change the order like that:
glBindAttribLocation(shader_id,0,“color”);
glVertexAttrib3fv(0,c);
glLinkProgram(shader_id);

but it still does not work…

it works!!!thanks a lot

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.