PDA

View Full Version : How to give value to in , out or uniform variables



apapaxionga
04-03-2011, 09:55 AM
i try to write a simple shader using glVertexAttrib(...) ,but it does not work.

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

in the fragment shader:
gl_FragColor = color* diffuse_value;

if i use gl_Color instead of color ,it works well,
However , i want to change the value of color by using glVertexAttrib(...), 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);
}

Alfonse Reinheart
04-03-2011, 02:18 PM
The fragment shader's inputs are the vertex shader's outputs. The fragment shader does not have access to the vertex shader's inputs, which are vertex attributes.

gl_FrontColor is a vertex shader output that maps to the fragment shader input gl_Color. That's why it works.