IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan...

Post on 22-May-2020

6 views 0 download

Transcript of IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan...

1

Shader Programming

Daniel Wesslén, dwn@hig.seStefan Seipel, ssl@hig.se

Examples

2

Per-pixel lighting

Texture convolution filtering

3

Post-processing, animated procedural textures

Vertex displacement mapping

4

Fragment shader Mandelbrot set

Animation

5

Direct volume rendering

The OpenGL pipeline

6

OpenGL 1.4

vertex transform &

lighting

primitive assembly rasterization

fog, texturing

framebuffer

texture

connectivity

blending, depth, stencil

OpenGL 2.1

primitive assembly rasterization

fragment shader

framebuffertexture

connectivity

blending, depth, stencil

vertex shader

VBO

7

Shader processing

Parallel processing

rasterizer

vertex scheduler

vertex processors

fragment processors

framebuffer

vertices: properties

vertices: positions, properties

fragments: positions, properties

fragments: colors

8

Vertex shader

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

vertex processors

vertices: properties

vertices: positions, properties

Fragment shader

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

fragment processors

fragments: positions, properties

fragments: colors

9

Simple example

Per-pixel lighting

10

Vertex shadervarying 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 shadervarying 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;}

uniform (prog. to vertex, per primitive)

attribute (prog. to vertex, per vertex)

varying (vertex to fragment)

11

Vertex shadervarying 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;}

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)

Vertex shadervarying 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;}

12

Vertex shadervarying 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 built-in attributes

writing to gl_Position is mandatory

Vertex shadervarying 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;}

built-in uniforms

13

Fragment shaderuniform 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 shaderuniform 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);}

built-in functions

14

Fragment shaderuniform 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);}

writing to gl_FragColor or gl_FragData[] is mandatory