a paid nerd
a paid nerd

Reputation: 31512

How do OpenGL fragment shaders know what pixel to sample in a texture?

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?

Upvotes: 5

Views: 1394

Answers (1)

genpfault
genpfault

Reputation: 52083

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.

Upvotes: 7

Related Questions