GLSL Tutorial 9 Shader Introduction
GLSL Tutorial 9 Shader Introduction
Examples
Per-pixel lighting
Animation
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
Shader processing
Parallel processing
vertices: properties
vertex scheduler
vertex processors
vertices: positions, properties
rasterizer
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;
10
Vertex shader
varying vec3 normal, color, pos;
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;
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