4. shading - uni-weimar.de file4 – shading computer graphics exercise – jakob wagner shader...
TRANSCRIPT
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Computer Graphics Exercise4. Shading
Jakob Wagner for internal use only
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Fragment Shader
Vertex Shader
Shaders
Vertex Specificationdefine vertex format & data in model space
Vertex Processingtransform to clip space
Vertex Post-processingclipping, perspective divide, viewport transform
Primitive Assemblybuild points/lines/triangles from vertices
Rasterisationscan-convert primitives to fragments, property transfer
Fragment Processingcomputer fragment properties
Fragment Submissiontesting and blendingwrite to buffer
https://glumpy.github.io/modern-gl.html2
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Shaders
- contain instructions for the Vertex/Fragment processing stages
- use C-like syntax (GLSL)
- two types of variables:
- Uniforms: explicitly uploaded from CPU, constant value until new value is uploaded
- Varyings: shader inputs/outputs, varying between each shader instance
- built-in variables: minimal Vertex shader outputs/ Fragment Shader inputs necessary for pipeline processing
e.g. Vertex position output in Vertex Shader and Fragment position input in Fragment Shader
3
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Vertex Shader
Vertex Attributes (varying input)- 1st is position in Model Space- 2nd is normal in Model Space
Uniforms (constant input)- transformation matrices uploaded with
Values passed to Fragment Shader (varying output)- normal in View Space
Vertex Processing- position (predefined output) is transformed from
Model- to Projection-Space
- normal is transformed from Model- to View Space
4
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Fragment Shader
Values interpolated from Vertices (varying input)- normal in View Space
Values passed to Sample Processing (varying output)- fragment color, 4th component is opacity
Fragment Processing- assign fragment RGB components value of the
normal XYZ components and make it opaque
5
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Shader Uniforms
- are part of ShaderProgram state
- value does not change until a new value is uploaded
- are shared between all stages of a ShaderProgram
- upload uniform value to programa. query uniform location with
- program handle- uniform name in Shader
b. binding shader programc. upload value to uniform location in bound shader
- querying a location is expensive -> store it and only update when the shader is reloaded
- glUniform* always uploads to the ShaderProgram that is currently bound
- location of uniform of the same name varies between ShaderPrograms
6
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Assignment 2 – Star Shader
- different vertex attribute layout -> new vertex shader required- 2 inputs: position (vec3) and color (vec3)- color needs to be passed to fragment shader -> vertex shader needs color output variable- fragment shader assigns input color to fragment
- Model matrix not necessary because no transformation happens
- in the star shader needs to be loaded and the uniforms requested
- shader needs View and Projection matrices- uniform locations between shaders vary -> matrix locations in star shader differ from planet shader- when modified in and , the matrices need to be uploaded to the star shader at
their respective locations
7
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Fragment Shader
Vertex Shader
Rasterisation
Vertex Specificationdefine vertex format & data in model space
Vertex Processingtransform to clip space
Vertex Post-processingclipping, perspective divide, viewport transform
Primitive Assemblybuild points/lines/triangles from vertices
Rasterisationscan-convert primitives to fragments, property transfer
Fragment Processingcomputer fragment properties
Fragment Submissiontesting and blendingwrite to buffer
https://glumpy.github.io/modern-gl.html8
4 – Shading
Computer Graphics Exercise – Jakob Wagner
variable definition
variable interpolation
Property Transfer
Vertex Shader
d2
d
3
d
1
v1
v2v3
pass_Color = vec3(d1∙ pass_Color1 + d2∙ pass_Color2 + d3∙ pass_Color3)
gl_FragCoord = vec4(d1∙ gl_Position1 + d2∙ gl_Position2 + d3∙ gl_Position3)
pass_color3 = vec3(0.0f, 1.0f,0.0f)
gl_Position3 = vec4(20.0f, 0.0f, 0.0f)
pass_color2 = vec3(1.0f, 0.0f, 0.0f)
gl_Position2 = vec4(0.0f, 0.0f, 0.0f)
pass_color1 = vec3(0.0f, 0.0f, 1.0f)
gl_Position1 = vec4(10.0f, 20.0f, 0.0f)
Fragment Shader
Interpolation
9
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Perspective-correct Interpolation
z-coordinates (depth) are not linear after projective transformation- very good depth accuracy close to camera- low depth accuracy close to far-plane- output distance between points dependent on distance to camera
linear depth: (v2 - v1) / (v4 - v3) = d1-2 / d3-4
non-linear depth: (v2 ‘- v1’) / (v4’ - v3’) ≠ d1-2’ / d3-4’
fragment property interpolation takes place after perspective projection- linear interpolation leads to wrong results- “Perspective-correct Interpolation” necessary- explanation e.g. in Low, Perspective-Correct Interpolation, 2002
http://learnopengl.com/#!Advanced-OpenGL/Depth-testing
input depth
output depth
v1
v2
v1’
v2’
dz
dz’ linear depth
actual depth
v1
v2
d1-2
v1’
v2’
d1-2’v3
v4
d3-4
v3’v4’
d3-4’
actual depth
linear depth
10
4 – Shading
Computer Graphics Exercise – Jakob Wagner
- interpolation can be specified in GLSL:- flat: no interpolation, values from first vertex used -> Flat Shading
- smooth: default, perspective-correct interpolation
- nonperspective: linear interpolation
- qualifier for variable must match between shaders
Interpolation Qualifiers
https://commons.wikimedia.org/wiki/File:Phong-shading-sample.jpghttp://s19.photobucket.com/user/Coincoin/media/perspective-correction.png.html
smooth (perspective-correct)
nonperspective (linear)
texture coordinate interpolation
flat (1 normal per triangle)
normal vector interpolation
non-flat(1 normal per vertex)
11
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Phong Reflection Model
Components:- ambient: indirect light incoming from general surroundings
->constant
- diffuse: “Lambertian reflectance”, diffusely reflected light from surface microfacets-> dependent on angle α between surface n and light direction l
- specular: reflection of light directly to viewer-> dependent on angle ω between viewer v and light direction reflected from surface l’-> specular highlight decay (glossiness) controlled by a
Formula: ka,kd,ks,a - material parameters, ia,id,is - light parameterswhen v1 and v2 unit vectors, then: cos(∡(v1, v2)) = <v1, v2> (dot product)
I = ka∙ ia + kd∙ id ∙ cos(ω) + ks∙ is ∙ cos(α)a
= ka∙ ia + kd∙ id ∙ <l, n> + ks∙ is ∙ <v, l’>a
https://commons.wikimedia.org/wiki/File:Phong_components_version_4.png
nv
ll’
αω
12
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Blinn-Phong Reflection Model
reflection operation computation expensive -> approximation by Blinn does not require a reflection
Blinn-Phong model: instead of angle between viewer and reflected light diruse angle ρ between normal and halfway vector h between viewer and light
h = (l + v) / (ǁl + vǁ) = normalize(v + l)
I = ka∙ ia + kd∙ id ∙ cos(ω) + ks∙ is ∙ cos(ρ)b
= ka∙ ia + kd∙ id ∙ <l, n> + ks∙ is ∙ <n, h>b
- with same exponent as Phong model a is too small -> b = 4 ∙ a
- Blinn approximation is actually empirically more accurate than Phong
n
v
l
h
ορο
https://commons.wikimedia.org/wiki/File:Blinn_phong_comparison.png
13
4 – Shading
Computer Graphics Exercise – Jakob Wagner
Assignment 3 – Blinn-Phong implementation
- shading should be computed in View Space -> requires light and fragment position in View Space-> in Vertex Shader calculate vertex position in View Space and pass to Fragment Shader-> upload uniform of sun position (which is in World Space) in View Space to shader using glUniform3f() or update value when when the View Matrix changes
- light color properties can be hardcoded in fragment Shader
- planet diffuse color can be assigned through a single vec3 uniform that is changed to the respective color before each planet is drawn
- planet ambient color can be assumed as being the same as the diffuse color
- planet specular color can be assumed as being white
- before calculating angles with the dot product, both vectors need to be normalized
14