4
$\begingroup$

Why do I need to specify the same name for color input in fragment shader and output color from vertex shader?

//Vertex shader
out vec3 vertex_color;

void main()
{
    vertex_color=vec3(1.0,0.0,0.0);
}
//Fragment shader 
in vec3 vertex_color;
out vec4 frag_color;

void main()
{
    frag_color=vec4(vertex_color,1.0);
}

Since we already hand the color value from vertex shader to fragment shader, why do we need same names?

$\endgroup$

2 Answers 2

4
$\begingroup$

Using the same name is exactly how you tell OpenGL that you want the value passed through from vertex to fragment.

You say "we already hand the color value from vertex shader to fragment shader", but that's not correct. Usually, the only value that's passed between shaders automatically is position, and that's only because it feeds into the GPU's rasterization hardware to draw the triangle on the screen.

Any other values such as color, normal, texture coordinates, etc. that you want passed between shader stages have to be explicitly hooked up by the shader author. And the way you do that in GLSL is to create an out variable in one stage, and an in variable in the next stage, with the same name.

$\endgroup$
3
$\begingroup$

Because you may want to pass more than one attribute through to the fragment shader. 2 which are essential are normal vector and the texture coordinates once you start doing lighting and textured meshes.

You can in newer openGL versions give a numbered location to the attributes you pass through using layout(location=1). Then the names don't have to match.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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