Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples 1
Per-pixel lighting Texture convolution filtering 2
Post-processing, animated procedural textures Vertex displacement mapping 3
Fragment shader Mandelbrot set Animation 4
Direct volume rendering The OpenGL pipeline 5
OpenGL 1.4 connectivity vertex transform & lighting primitive assembly rasterization texture fog, texturing framebuffer blending, depth, stencil OpenGL 2.1 connectivity vertex shader primitive assembly rasterization VBO texture framebuffer fragment shader blending, depth, stencil 6
Shader processing Parallel processing vertices: properties vertex scheduler vertex processors vertices: positions, properties rasterizer fragments: positions, properties fragment processors framebuffer fragments: colors 7
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 8
Simple example Per-pixel lighting 9
Vertex shader color = gl_color.rgb; normal = normalize(gl_normalmatrix * gl_normal); pos = vec3(gl_modelviewmatrix * gl_vertex); gl_position = gl_modelviewprojectionmatrix * gl_vertex; Vertex shader color = gl_color.rgb; normal = normalize(gl_normalmatrix * gl_normal); pos = vec3(gl_modelviewmatrix * gl_vertex); uniform (prog. to vertex, per primitive) attribute (prog. to vertex, per vertex) varying (vertex to fragment) gl_position = gl_modelviewprojectionmatrix * gl_vertex; 10
Vertex shader color = gl_color.rgb; normal = normalize(gl_normalmatrix * gl_normal); pos = vec3(gl_modelviewmatrix * gl_vertex); int, float, bool (basic types) vec2, vec3, vec4 (2,3,4d vector) mat2, mat3, mat4 (2,3,4d float matrix) ivec3, bvec4, etc. (vectors only) gl_position = gl_modelviewprojectionmatrix * gl_vertex; Vertex shader 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 vertex shader built-in attributes writing to gl_position is mandatory color = gl_color.rgb; normal = normalize(gl_normalmatrix * gl_normal); pos = vec3(gl_modelviewmatrix * gl_vertex); gl_position = gl_modelviewprojectionmatrix * gl_vertex; Vertex shader built-in uniforms 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; 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; built-in functions 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; writing to gl_fragcolor or gl_fragdata[] is mandatory 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