Tell me about vertex shader

satya - Sat Jul 28 2012 16:57:00 GMT-0400 (Eastern Daylight Time)

a vertex shader is a program that gets run for every vertex position

So it is executed multiple times for each vertex

satya - Sat Jul 28 2012 16:58:22 GMT-0400 (Eastern Daylight Time)

what are Vertex attributes?

each vertex in opengl has things like position, color, normal etc. These are called the "attributes" of that vertex. These attributes are passed to a vertex shader program as inputs.

satya - Sat Jul 28 2012 17:00:31 GMT-0400 (Eastern Daylight Time)

what are its responsibilities?

Calculate the final position of the vertex in a rendered scene. this value is given out as a built in variable called "gl_position".

satya - Sat Jul 28 2012 17:02:58 GMT-0400 (Eastern Daylight Time)

Primitive Assembly

vertices are transformed into geometric primitives such as triangles and lines by taking into consideration eye point, and frustum.

satya - Sat Jul 28 2012 17:04:46 GMT-0400 (Eastern Daylight Time)

Rasterization

A process that converts primitives into 2 dimensional "fragments' which are processed by fragment shader to convert them to pixels. Fragment reader only gets the (x,y) coordinates.

satya - Sat Jul 28 2012 17:07:42 GMT-0400 (Eastern Daylight Time)

values that goes into a fragment shader must be defined by the vertex shader


//Previously
varying vec4 v_color;

//or
out vec4 v_color;

satya - Sat Jul 28 2012 17:11:02 GMT-0400 (Eastern Daylight Time)

outputs of vertex shader


All declared out variables
These are varying variables

//built in variables
//these are not varying
gl_position
gl_FrontFacing
gl_PointSize

satya - Sat Jul 28 2012 17:11:21 GMT-0400 (Eastern Daylight Time)

Same are the inputs to a fragment shader

Same are the inputs to a fragment shader

satya - Sat Jul 28 2012 17:12:18 GMT-0400 (Eastern Daylight Time)

Output from a fragment shader


gl_FragColor

Some literature seem to indicate any single output from this is considered color as long as it is declared as an out variable.

satya - Sat Jul 28 2012 17:14:04 GMT-0400 (Eastern Daylight Time)

More on fragment shader

A fragment produced by rasterization with (x,y) screen coordinates can only modify the pixel at (x,y)

From Open GL ES 2.0 Programming Guide

satya - Sun Sep 02 2012 20:50:36 GMT-0400 (Eastern Daylight Time)

A simple vertex shader


uniform mat4 uMVPMatrix;
attribute vec4 aPosition;
void main() 
{
   gl_Position = uMVPMatrix * aPosition;
}

satya - Sun Sep 02 2012 20:51:08 GMT-0400 (Eastern Daylight Time)

a fragment shader


void main() 
{
   gl_FragColor = vec4(0.5, 0.25, 0.5, 1.0);
}

satya - Sun Sep 02 2012 20:51:44 GMT-0400 (Eastern Daylight Time)

a texture based vertex shader


uniform mat4 uMVPMatrix;
attribute vec4 aPosition;
attribute vec2 aTextureCoordinate;
varying vec2 v_TextureCoordinate;
void main() 
{
   gl_Position = uMVPMatrix * aPosition;
   v_TextureCoordinate = aTextureCoordinate;
}

satya - Sun Sep 02 2012 20:52:34 GMT-0400 (Eastern Daylight Time)

Corresponding fragment shader


precision mediump float; //Appears Mandatory for this version of GLSL 
varying vec2 v_TextureCoordinate;
uniform sampler2D s_2DtextureSampler;
void main() 
{
   gl_FragColor = texture2D(s_2DtextureSampler, v_TextureCoordinate);
}