5

So I've got a triangle:

And I've got a vertex shader:

uniform mat4 uViewProjection;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoords;
varying vec2 vTextureCoords;

void main(void) {
  vTextureCoords = aTextureCoords;
  gl_Position = uViewProjection * vec4(aVertexPosition, 1.0);
}

And I've got a fragment shader:

precision mediump float;
uniform sampler2D uMyTexture;
varying vec2 vTextureCoords;

void main(void) {
  gl_FragColor = texture2D(uMyTexture, vTextureCoords);
}

And I feed in three sets of vertices and UVs, interleaved:

# x,  y,    z,   s,   t
0.0,  1.0,  0.0, 0.5, 1.0
-1.0, -1.0, 0.0, 0.0, 0.0 
1.0, -1.0,  0.0, 1.0, 0.0

How does the fragment shader know to draw pixel A differently from pixel B? What changes?

genpfault
  • 51,148
  • 11
  • 85
  • 139
a paid nerd
  • 30,702
  • 30
  • 134
  • 179
  • You fed in a texture right? Why would you expect them to be the same or are you asking how shaders work on the GPU? – Jesus Ramos Jan 31 '13 at 18:53
  • @JesusRamos I'm asking how shaders work. I don't understand what's changing and who's changing it between shader passes. – a paid nerd Jan 31 '13 at 19:02
  • Check out @genpfault's answer. I was going to say the same thing but he beat me to it :) – Jesus Ramos Jan 31 '13 at 19:08
  • 1
    Check this great answer http://stackoverflow.com/q/14246604/187752, when OpenGL might be sampling values you are not expecting. – Kimi Feb 01 '13 at 08:46

1 Answers1

7

As I understand it the rasterization stage of the GL pipeline interpolates vTextureCoords across the triangle face, running the fragment shader on each pixel with the interpolated value.

genpfault
  • 51,148
  • 11
  • 85
  • 139