PDA

View Full Version : why glVertexAttrib does not work?



apapaxionga
04-02-2011, 08:42 PM
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);
}

Dark Photon
04-03-2011, 04:01 PM
glGetAttribLocation (http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml) 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 (http://www.opengl.org/sdk/docs/man/xhtml/glBindAttribLocation.xml) 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

apapaxionga
04-03-2011, 08:21 PM
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....

apapaxionga
04-03-2011, 08:48 PM
it works!!!!!!!!!!!!thanks a lot