You are on page 1of 14

Shader Programming

Daniel Wessln, dwn@hig.se


Stefan Seipel, ssl@hig.se

Examples

Per-pixel lighting

Texture convolution filtering

Post-processing, animated procedural textures

Vertex displacement mapping

Fragment shader Mandelbrot set

Animation

Direct volume rendering

The OpenGL pipeline

OpenGL 1.4
connec
tivity

vertex
transform &
lighting

primitive
assembly

texture

rasterization

fog,
texturing
blending, depth, stencil
framebuffer

OpenGL 2.1
connec
tivity

vertex
shader

primitive
assembly

VBO

rasterization

fragment
shader

texture
framebuffer

blending, depth, stencil

Shader processing

Parallel processing
vertices: properties
vertex scheduler

vertex processors
vertices: positions, properties
rasterizer

fragments: positions, properties

fragment processors
fragments: colors
framebuffer

Vertex shader
vertices: properties
vertex processors
vertices: positions, properties

Transform
Animation
Per-vertex lighting
Displacement
Mostly: Parameter setup for fragments
Can not modify topology
No access to other vertices

Fragment shader
fragments: positions, properties
fragment processors
fragments: colors

Per-pixel lighting
Texturing
Compositing
Filtering
Fog
Can not read or write pixels
No access to other fragments

Simple example
Per-pixel lighting

Vertex shader
varying vec3 normal, color, pos;
void main()
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Vertex shader
varying vec3 normal, color, pos;

uniform (prog. to vertex, per primitive)


attribute (prog. to vertex, per vertex)

varying (vertex to fragment)


void main()
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);

pos = vec3(gl_ModelViewMatrix * gl_Vertex);


gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

10

Vertex shader
varying vec3 normal, color, pos;

int, float, bool (basic types)


vec2, vec3, vec4 (2,3,4d vector)

mat2, mat3, mat4 (2,3,4d float matrix)


void main()
ivec3, bvec4, etc. (vectors only)
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);

pos = vec3(gl_ModelViewMatrix * gl_Vertex);


gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Vertex shader
varying vec3 normal, color, pos;
void main()
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

11

Vertex shader
varying vec3 normal, color, pos;

vertex shader built-in attributes


writing to gl_Position is mandatory

void main()
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Vertex shader
varying vec3 normal, color, pos;

built-in uniforms

void main()
{
color = gl_Color.rgb;
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

12

Fragment shader
uniform float shininess;
varying vec3 normal, color, pos;
void main()
{
vec3 nlight = normalize(gl_LightSource[0].position.xyz - pos);
vec3 neye = normalize(-pos);
vec3 nnormal = normalize(normal);
vec3 nhalf = normalize(neye + nlight);
float diff = max(0.0, dot(nlight, nnormal));
float spec = diff > 0.0 ? pow(dot(nhalf, nnormal), shininess) : 0.0;
gl_FragColor = vec4(color * diff + spec, 1);
}

Fragment shader
uniform float shininess;
varying vec3 normal, color, pos;

built-in functions

void main()
{
vec3 nlight = normalize(gl_LightSource[0].position.xyz - pos);
vec3 neye = normalize(-pos);
vec3 nnormal = normalize(normal);
vec3 nhalf = normalize(neye + nlight);
float diff = max(0.0, dot(nlight, nnormal));
float spec = diff > 0.0 ? pow(dot(nhalf, nnormal), shininess) : 0.0;
gl_FragColor = vec4(color * diff + spec, 1);
}

13

Fragment shader
uniform float shininess;
varying vec3 normal, color, pos;

writing to gl_FragColor or
gl_FragData[] is mandatory

void main()
{
vec3 nlight = normalize(gl_LightSource[0].position.xyz - pos);
vec3 neye = normalize(-pos);
vec3 nnormal = normalize(normal);
vec3 nhalf = normalize(neye + nlight);
float diff = max(0.0, dot(nlight, nnormal));
float spec = diff > 0.0 ? pow(dot(nhalf, nnormal), shininess) : 0.0;
gl_FragColor = vec4(color * diff + spec, 1);
}

14