PDA

View Full Version : Basic Vertex Shader / Fragment Shader interaction



endlosschleife
04-19-2011, 03:35 AM
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

Alfonse Reinheart
04-19-2011, 03:46 AM
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 (http://www.arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html#d0e2460).

endlosschleife
04-19-2011, 04:53 AM
That helped clearing things up. Thank you!