IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, [email protected] Stefan...

14
1 Shader Programming Daniel Wesslén, [email protected] Stefan Seipel, [email protected] Examples

Transcript of IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, [email protected] Stefan...

Page 1: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

1

Shader Programming

Daniel Wesslén, [email protected] Seipel, [email protected]

Examples

Page 2: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

2

Per-pixel lighting

Texture convolution filtering

Page 3: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

3

Post-processing, animated procedural textures

Vertex displacement mapping

Page 4: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

4

Fragment shader Mandelbrot set

Animation

Page 5: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

5

Direct volume rendering

The OpenGL pipeline

Page 6: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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

Page 7: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

7

Shader processing

Parallel processing

rasterizer

vertex scheduler

vertex processors

fragment processors

framebuffer

vertices: properties

vertices: positions, properties

fragments: positions, properties

fragments: colors

Page 8: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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

Page 9: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

9

Simple example

Per-pixel lighting

Page 10: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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)

Page 11: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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

Page 12: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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

Page 13: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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

Page 14: IGS Shader QuickIntro - Uppsala University · Shader Programming Daniel Wesslén, dwn@hig.se Stefan Seipel, ssl@hig.se Examples. 2 Per-pixel lighting Texture convolution filtering.

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