Basic Vertex Shader / Fragment Shader interaction

Hi all,

this a beginner’s question. I’m just starting out with shader programming and in both tutorials that I’m going through I’m missing a basic point I think.

Here is my confusion:

In the shader examples that I worked through I see that the vertex program sets “varying” attributes and that these are used in the fragment program. What I’m confused about: if I pass let’s say four vertices from the application (-1.0,-1.0; 1.0, -1.0; -1.0, 1.0; 1.0, 1.0) my assumption now would be that the vertex program is triggered four times now, once for each vertex.

But the vertex program looks like this:

attribute vec2 position;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(0.5) + vec2(0.5);
}

Isn’t this overwriting the texcoord each time?

What I want to do in the fragment shader is to render the pixels of a texture (actually a blending of two textures), so my assumption would be that this fragment program is called textureWidth * textureHeight times, but in my example the fragment program does something with the single vertex program output (texcoord). Isn’t this the texture coordinate of a single point in the one case (vertex program) that is used for each individual points of a texture (fragment program)? How is this possible?

uinform float fade_factor;
uniform sample2D textures[];

varying vec2 texcoord;
void main()
{
gl_FragColor = mix (
texture2D(textures[0], texcoord),
texture2D(textures[1], texcoord);
fade_factor
);
}

I must be missing something really basic about how vertex and fragment programs interact. Any help would be appreciated. Thank you!

Mark

Isn’t this overwriting the texcoord each time?

No, it’s not. For the same reason that the attribute inputs that you get are not the same for the three executions of the vertex shader, the outputs from the vertex shader are not overwriting each other.

Each vertex shader execution gets its own set of input attributes, and it will in turn write its own set of outputs (varyings). They are all independent of each other.

Isn’t this the texture coordinate of a single point in the one case (vertex program) that is used for each individual points of a texture (fragment program)? How is this possible?

That is the responsibility of fragment interpolation.

That helped clearing things up. Thank you!

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