Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 443
a paid nerd Avatar asked Jan 31 '13 18:01

a paid nerd


People also ask

How does a fragment shader work?

A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value. The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated.

Is fragment shader a pixel shader?

A fragment shader is the same as pixel shader.

How do textures work in OpenGL?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.

How do shaders work in OpenGL?

A Shader is a user-defined program designed to run on some stage of a graphics processor. Shaders provide the code for certain programmable stages of the rendering pipeline. They can also be used in a slightly more limited form for general, on-GPU computation.


1 Answers

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.

like image 191
genpfault Avatar answered Sep 19 '22 16:09

genpfault