1(84) Information Coding / Computer Graphics, ISY, LiTH TSBK 07 Computer Graphics Ingemar Ragnemalm, ISY 1(84)
Lecture 5 3D graphics part 3 Illumination Illumination applied: Shading Surface detail: Mappings Texture mapping... 2(84)2(84)
3D models Bunny Utah Teapot Teddy 3(84)3(84)
The OpenGL pipeline Vertices Vertex processing: Transformations Primitive assembly Primitives Clip & cull Frame buffer operations: Z-buffer & stencil test, write pixel Fragment processing: shading & texture Fragments Rasterization Pixels Let's do something here 4(84)4(84)
Varying Values sent from vertex shaders are interpolated and sent to fragments Key component for all fragment shaders 5(84)5(84)
Illumination We know where to put a polygon. Now, what should we fill it with? What pixel value should we choose? Several factors to take into account. The most important ones: Shading, illumination. Texture mapping. Shading is determined according to an illumination model. 6(84)6(84)
Light sources Reflecting surface Light source Resulting light intensity 7(84)7(84)
Light sources Small sources can be modelled as point light source Others can be modelled as distributed light sources 8(84)8(84)
Reflections Diffuse reflection Specular reflection Paper Plastic Metal Mirror 9(84)9(84)
3-component illumination model A common simple illumination model is built from three components: Ambient light Diffuse reflections Specular reflections 10(84) 10(84)
Ambient light Same everywhere I amb = k d * I a Light emitted from surface Diffuse reflectivity Ambient light level of scene 11(84)11(84)
Diffuse reflections Ideal diffuse reflector = Lambertian surface Incoming light produces same intensity in all directions 12(84)12(84)
Lambert s cosine law I diff = k d *I l *cos θ θ Light emitted Light level of light source from surface Diffuse reflectivity 13(84)13(84)
Example: Diffuse sphere k d = 1 I l = 0.9 I a = 0.1 14(84)14(84)
Dot product is nicer than angles cos θ = s n s θ n I diff = k d *I l *s n 15(84)15(84)
Specular reflections Incoming light produces higher intensity around the mirroring angle The width of the highlight varies with surface types 16(84)16(84)
The Phong model I spec = W(θ)*I l *cos α φ s n r θ θ φ v or I spec = k s *I l *cos α φ cos φ = r v 17(84)17(84)
Calculation of R Mirror s by n s n r θ θ φ v 18(84)18(84)
Total contribution from one light source I amb = k d * I a s n r θ θ φ v I diff = k d *I l *s n I spec = k s *I l *(r v)α I total = I amb + I diff + I spec 19(84)19(84)
Clamping shading Note that the three-component light model will produce negative light This will cause problems in scenes with several light sources + - To avoid problems, clamp the resulting value, e.g. max(0, light) 20(84)20(84)
Complete formula I = I amb + I diff + I spec = = k d * I a + ( k d *I l *max(0, s n) + k s *I l *max(0, r v) α ) ) where sums over all light sources 21(84)21(84)
Examples Diffuse surface, k d = 0.9 Specular surface, n = 1 Specular surface, n = 5 Specular surface, n = 25 k d = 0.45 k s = 0.5 I l = 1.0 I a = 0.1 Specular surface, n = 125 22(84)22(84)
Examples Specular surface, n = 5 Specular surface, n = 25 k d = 0.45 k s = 0.5 I l = 1.0 I a = 0.1 Specular surface, n = 125 23(84)23(84)
Alternative formulation Blinn-Phong s n θ α h θ+α v Halfway vector h = (s+v) / s + v I spec = k s *I l *cos n α = = I spec = k s *I l *(n h) 24(84)24(84)
Advanced illumination models Make ks a function of the viewing angle - better modelling of glass and paper BRDF - highly general multi-dimensional function 25(84)25(84)
Global illumination models Radiosity: Models light exchange recusively Ray-tracing, trace viewing rays. Photon mapping, backwards ray-tracing, trace lighting rays. 26(84)26(84)
Polygon shading Using the illumination models in high-speed polygon rendering 27(84)27(84)
Three ways to render a shaded polygon: Flat shading Gouraud shading Phong shading 28(84)28(84)
Flat shading Intensity calculated once and for all for the whole polygon E.g. Ip = k d N L 29(84)29(84)
Flat shading is correct when: 1) The surfaces should be flat, not approximating a curved surface 2) Distance to light source high => N L constant 3) Distance to camera high => V R constant and in particular 4) When the problem is not lighting, but something else (Rendering surface identifications) 30(84)30(84)
Gouraud shading L N Intensity calculated once per vertex Each vertex has its own surface normal L L N Interpolate intensities N 31(84)31(84)
Gouraud shading can simulate curved surfaces fairly well, but many polygons may be needed, and edges remain visible Calculations in vertex shader - extremely fast 32(84)32(84)
Phong shading L N Each vertex has its own surface normal Normal vectors are interpolated L L N N 33(84)33(84)
Phong shading can simulate curved surfaces very well, even with low polygon counts Calculate the light in the fragment shader Computationally heavier 34(84)34(84)
Phong shading The Phong model Phong Shading doesn t necessarily use specular reflections. Phong Shading = normal-vector interpolation shading 35(84)35(84)
Example: Gouraud shader Transform normal vectors Calculate shading value per vertex, (here using diffuse only), by dot product with light direction Interpolate between vertices 36(84)36(84)
Gouraud shader - vertex shader #version 150 in vec3 inposition; in vec3 innormal; out vec3 excolor; void main(void) { const vec3 light = vec3(0.58, 0.58, 0.58); float shade; shade = dot(normalize(innormal), light); shade = clamp(shade, 0, 1); excolor = vec3(shade); gl_position = vec4(inposition, 1.0); } 37(84)37(84)
Gouraud shader - fragment shader #version 150 in vec3 excolor; out vec4 outcolor; void main(void) { outcolor = vec4(excolor,1.0); } 38(84)38(84)
Gouraud shader Note: The variable excolor is interpolated between vertices dot() och normalize() do what you expect. innormal is the normal vector in model coordinates (Should be transformed in a real program) The constant vector light is here hard coded 39(84)39(84)
Typical Gouraud shaded bunny 40(84)40(84)
Version 2: Add specular lighting to vertex shader // Specular vec3 reflectedlightdirection = reflect(-light, norm); vec3 eyedirection = vec3(normalize(-inposition)); float specularstrength = 0.0; specularstrength = dot(reflectedlightdirection, eyedirection); float exponent = 8.0; specularstrength = max(specularstrength, 0.01); specularstrength = pow(specularstrength, exponent); shade = (0.3*diffuseshade + 0.9*specularStrength); } (Again some transformations skipped.) 41(84)41(84)
Specular Gouraud shaded bunny A bit polygonal... 42(84)42(84)
Example: Phong shader Better shading Interpolate normal vectors between vertices Calculate shading value per fragment Practically the same operations, but the light calculation are done in the fragment shader 43(84)43(84)
Phong shader Vertex shader #version 150 in vec3 inposition; in vec3 innormal; out vec3 exnormal; out vec3 surf; void main(void) { exnormal = innormal; surf = inposition; // For specular gl_position = vec4(inposition, 1.0); } 44(84)44(84)
Phong shader Fragment shader #version 150 out vec4 outcolor; in vec3 exnormal; in vec3 surf; void main(void) { const vec3 light = vec3(0.58, 0.58, 0.58); float shade; shade = dot(normalize(exnormal), light); shade = clamp(shade, 0, 1); outcolor = vec4(shade, shade, shade, 1.0); } 45(84)45(84)
..and add specular part // Specular vec3 reflectedlightdirection = reflect(-lightdirection, n); vec3 eyedirection = normalize(-surf); float specularstrength = 0.0; if (dot(lightdirection, n) > 0.0) { specularstrength = dot(reflectedlightdirection, eyedirection); float exponent = 200.0; specularstrength = max(specularstrength, 0.01); specularstrength = pow(specularstrength, exponent); } outcolor = vec4(diffusestrength*0.5 + specularstrength*0.5); } 46(84)46(84)
Specular Phong shaded bunny Now we re talking 47(84)47(84)
Use the shading you need, balance computing and quality Gouraud Phong 48(84)48(84)
Same for Stanford bunny Gouraud Phong 49(84)49(84)