Shading Sides by Uf Assarsson and Tomas Akenine-Möer Department of Computer Engineering Chamers University of Technoogy
Overview of today s ecture A simpe most basic rea-time ighting mode It is aso OpenGL s od fixed pipeine ighting mode Fog Gamma correction Transparency and apha
Compute ighting at vertices, then interpoate over triange ight bue Rasterizer Geometry red green How compute ighting? We coud set coors per vertex manuay For a itte more reaism, compute ighting from Light sources Materia properties Geometrica reationships Tomas Akenine-Mőer 2002
The ambient/diffuse/specuar/emission mode Aso the most basic rea-time mode: Light interacts with materia and change coors at bounces: outcoor rgb ~ materia rgb ightcoor rgb Diffuse ight: the part that spreads in a direction (view independent) due to that the surface is rough on microscopic eve n
The ambient/diffuse/specuar/emission mode Aso the most basic rea-time mode: Light interacts with materia and change coors at bounces: outcoor rgb ~ materia rgb ightcoor rgb Diffuse ight: the part that spreads in a direction (view independent) Specuar ight: the part that spreads mosty in the refection direction (often same coor as ight source) n
The ambient/diffuse/specuar/emission mode Aso the most basic rea-time mode: Light interacts with materia and change coors at bounces: outcoor rgb ~ materia rgb ightcoor rgb Diffuse ight: the part that spreads in a direction (view independent) Specuar ight: the part that spreads mosty in the refection direction (often same coor as ight source) Ambient ight: incoming background ight from a directions and spreads in a directions (view-independent and ight-position independent coor) n
The ambient/diffuse/specuar/emission mode Aso the most basic rea-time mode: Light interacts with materia and change coors at bounces: outcoor rgb ~ materia rgb ightcoor rgb Diffuse ight: the part that spreads in a direction (view independent) Specuar ight: the part that spreads mosty in the refection direction (often same coor as ight source) Ambient ight: incoming background ight from a directions and spreads in a directions (view-independent and ight-position independent coor) Emission: sef-gowing surface n
A basic ighting mode Materia: Ambient (r,g,b,a) Diffuse (r,g,b,a) Specuar (r,g,b,a) Light: Ambient (r,g,b,a) Diffuse (r,g,b,a) Specuar (r,g,b,a) DIFFUSE Base coor SPECULAR Highight Coor AMBIENT Low-ight Coor EMISSION Gow Coor SHININESS Emission (r,g,b,a) = sjävysande färg Surface Smoothness Tomas Added Akenine-Mőer by Uf Assarsson, 2002 2004
Ambient component: iamb Ad-hoc tries to account for ight coming from other surfaces Just add a constant coor: i amb = m amb Äs amb i.e., (i r, i g, i b, i a ) = (m r, m g, m b, m a ) ( r, g, b, a )
Diffuse component : idiff i=iamb+idiff+ispec Diffuse (Lambert s aw): i diff = n Photons are scattered equay in a directions i diff = ( n ) m diff Äs diff = cosf n and are assumed being unit vectors Tomas Akenine-Mőer 2002
Lambertian Surfaces Perfecty diffuse refector Light scattered equay in a directions Highy refective surface (specuar) Fuy diffuse surface (Lambertian)
Lighting Specuar component : ispec Diffuse is du (eft) Specuar: simuates a highight Tomas Akenine-Mőer 2002
Specuar component: Phong Phong specuar highight mode Refect around n: r r = - + 2(n )n i = ( r v) = (cos r) spec m shi m shi ( n ) n - n n must be unit vector n i spec = ((n ) < 0)? 0 : max(0,(r v)) m shi m spec s spec Next: Binns highight formua: (n. h) m Tomas Akenine-Mőer 2002
Hafway Vector Binn proposed repacing v r by n h where h = (+v)/ + v (+v)/2 is hafway between and v If n,, and v are copanar: y = f/2 Must then adjust exponent so that (n h) e (r v) e (e 4e)
Efficiency The Binn rendering mode is ess efficient than pure Phong shading in most cases, since it contains a square root cacuation. Whie the origina Phong mode ony needs a simpe vector refection, this modified form takes more into consideration. However, as many CPUs and GPUs contain singe and doube precision square root functions (as standard features) and other instructions that can be used to speed up rendering -- the time penaty for this kind of shader wi not be noticed in most impementations. However, Binn-Phong wi be faster in the case where the viewer and ight are treated to be at infinity. This is the case for directiona ights. In this case, the haf-ange vector is independent of position and surface curvature. It can be computed once for each ight and then used for the entire frame, or indeed whie ight and viewpoint remain in the same reative position. The same is not true with Phong's origina refected ight vector which depends on the surface curvature and must be recacuated for each pixe of the image (or for each vertex of the mode in the case of vertex ighting). In most cases where ights are not treated to be at infinity, for instance when using point ights, the origina Phong mode wi be faster.
Lighting i=iamb+idiff+ispec + + = This is just a hack! Has itte to do with how reaity works! Tomas Akenine-Mőer 2002
Additions to the ighting equation Accounting for distance: 1/(a+bt+ct 2 ) Severa ights: just sum their respective contributions Different ight types: Tomas Akenine-Mőer 2002
Carifications Energy is emitted at equa proportions in a directions from a spherica radiator. Due to energy conservation, the intensity is proportiona to the spherica area at distance r from the ight center. A = 4πr 2 Thus, the intensity scaes ~ 1/r 2 r
Shading Shading: do ighting (at e.g. vertices) and determine pixe s coors from these Three common types of shading: Fat, Goraud, and Phong Tomas Akenine-Mőer 2002
Shading Three common types of shading: Fat, Goraud, and Phong In standard Gouraud shading the ighting is computed per triange vertex and for each pixe, the coor is interpoated from the coors at the vertices. In Phong Shading the ighting is not computed per vertex. Instead the norma is interpoated per pixe from the normas defined at the vertices and fu ighting is computed per pixe using this norma. This is of course more expensive but ooks better. Fat Gouraud Phong Tomas Akenine-Mőer 2002
// Vertex Shader #version 130 Gouraud Shading Code in vec3 vertex; in vec3 norma; uniform vec4 mtramb, mtrdiffuse, mtrspec, mtremission; uniform vec4 ightamb, ightdiffuse, ightspec, ightemission; uniform foat shininess; uniform mat4 modeviewprojectionmatrix, normamatrix, modeviewmatrix; uniform vec4 ightpos; // in view space out vec3 outcoor; For one ight source // Fragment Shader: #version 130 in vec3 outcoor; out vec4 fragcoor; void main() { g_position = modeviewprojectionmatrix*vec4(vertex,1); // ambient outcoor = ightamb * mtramb; // diffuse vertex = vec3(modeviewmatrix * vec4(vertex,1)); norma = normaize(normamatrix * norma); vec3 ightdirection = normaize(ightpos vertex.xyz); foat intensity=max(0, dot(norma, ightdirection) outcoor += ightdiffuse*mtrdiffuse*intensity; void main() { } fragcoor = vec4(outcoor,1); } // specuar vec3 viewvec = -vertex.xyz; // because we are in view space vec3 refvec = -ightdirection + norma*(2*dot(norma*ightdirection)) r = - + 2(n )n intensity=pow(max(0,(dot(refvec, viewvec)), shininess)); outcoor += ightspec * mtrspec * max(0,intensity); i spec = ((n ) < 0)? 0 : max(0,(r v)) m shi m spec s spec
Phong Shading Code For one ight source // Vertex Shader #version 130 in vec3 vertex; in vec3 norma; uniform vec3 mtramb; uniform vec3 ightamb; uniform vec4 ightpos; uniform mat4 modeviewprojectionmatrix; uniform mat4 normamatrix; uniform mat4 modeviewmatrix; out vec3 outcoor; out vec3 N; out vec3 viewvec; out vec3 ightdirection; void main() { g_position = modeviewprojectionmatrix* vec4(vertex,1); } // ambient outcoor = ightamb * mtramb; N= normaize(normamatrix * norma); ightdirection = normaize(ightpos vertex.xyz); viewvec=-vec3(modeviewmatrix*vec4(vertex,1)); // Fragment Shader: #version 130 in vec3 outcoor, ightdirection, N, pos; in vec3 viewvec; uniform vec3 mtrdiffuse, mtrspec, mtremission; uniform vec3 ightdiffuse, ightspec, ightemission; uniform foat shininess; out vec4 fragcoor; void main() { } N = normaize(n); // renormaize due to the interpoation // diffuse foat intensity=max(0, dot(n, ightdirection) outcoor += ightdiffuse*mtrdiffuse*intensity; // specuar vec3 refvec = -ightdirection + N*(2*dot(N*ightDirection)); intensity=pow(max(0,(dot(refvec, viewvec)), shininess)); outcoor += ightspec * mtrspec * max(0,intensity); fragcoor = vec4(outcoor,1);
Transparency and apha Transparency Very simpe in rea-time contexts The too: apha bending (mix two coors) Apha (a) is the forth coor component (r,g,b,a) e.g., of the materia for a triange Represents the opacity 1.0 is totay opaque 0.0 is totay transparent The over operator: c = ac + ( 1-a) c o Rendered object s d
Transparency Need to sort the transparent objects Render back to front (bending is order dep.) See next side c = ac + ( 1-a) c Lots of different other bending modes Can store RGBa in textures as we o Rendered fragment s Background d So the texes with a=0.0 do not not hide the objects behind Uf Assarsson 2007
Transparency Need to sort the transparent objects First, render a non-transparent trianges as usua. Then, sort a transparent trianges and render them back-to-front with bending enabed. The reason for sorting is that the bending operation (i.e., over operator) is order dependent. Tomas Akenine-Mőer 2002
Bending Used for Transparency c = ac + ( 1-a) c o s d gbendfunc(gl_src_alpha, GL_ONE_MINUS_SRC_ALPHA) Effects (shadows, refections) (Compex materias) Quake3 used up to 10 rendering passes, bending toghether contributions such as: Diffuse ighting (for hard shadows) Bump maps Base texture Specuar and emissive ighting Voumetric/atmospheric effects Enabe with genabe(gl_blend) 26 Uf Assarsson 2003
Fog Simpe atmospheric effect A itte better reaism Hep in determining distances Tomas Akenine-Mőer 2002
Coor of fog: c c p = fc s + (1 f )c f How to compute f? f coor of surface: f [0,1] 3 ways: inear, exponentia, exponentia-squared Linear: f = z z end end - - z z p start cs Tomas Akenine-Mőer 2002
Fog exampe Often just a matter of Choosing fog coor Choosing fog mode Od OpenGL just turn it on. New OpenGL program it yoursef in the fragment shader Tomas Akenine-Mőer 2002
Fog in up-direction Tomas Akenine-Mőer 2002
rgb_out Gamma correction Lighting computes rgb coor intensities in inear space from [0,1] x γ γ = 2.2 darker brighter darker brighter rgb_in screen = rgb_in x γ : perceived in. intensity: inear intensity: However, CRT-monitor output is exponentia. Has more precision for darker regions. Very Good! But we need to adapt the input to utiize this. Ese, our images wi be too dark. Expon. distribution better for humans. Our eyes have noninear sensitivity and monitors have imited brightness Intensities: x γ vs inear Textures: store in gamma space for better ditributed precision. So, store coor intensities with more precision for darker coors: i.e., convert coor to x (1 / γ) before storing in 8- bits in the frame buffer. Conversion to x (1 / γ) is caed gamma correction. x (1 / γ) x γ (x (1 / γ) ) γ x (1 / γ) intensity Shader rgb coors Frame buffer rgb coors. Dark pixes are made brighter Dispayed by CRT Framebuf. rgb: 0 0.5 0.66 0.8 0.9 1 Shader rgb: 0 0.2 0.4 0.6 0.8 1 Linear output again, but redistributed precision.
Gamma correction If input to gun is 0.5, then you don t get 0.5 as output in intensity Instead, gamma correct that signa: gives inear reationship Tomas Akenine-Mőer 2002
Gamma correction I = a( V +e) g I=intensity on screen V=input votage (eectron gun) a,e, and g are constants for each system Common gamma vaues: 2.2-2.6 Assuming e=0, gamma correction is: c = c i ( 1/ g ) Tomas Akenine-Mőer 2002
Why is it important to care about gamma correction? Portabiity across patforms Image quaity Texturing Anti-aiasing One soution is to put gamma correction in hardware srgb asumes gamma=2.2 Can use EXT_framebuffer_sRGB to render with gamma correction directy to frame buffer
Gamma correction today Standard is 2.2 Happens to give more efficient coor space when compressing intensity from 32-bit foats to 8-bits. Thus, sti motivated. better distribution for humans Gamma of 2.2 Truth On most dispays (those with gamma of about 2.2), one can observe that the inear intensity output (bottom) has a arge jump in perceived brightness between the intensity vaues 0.0 and 0.1, whie the steps at the higher end of the scae are hardy perceptibe. A inear input that has a nonineary-increasing intensity (upper), wi show much more even steps in perceived brightness.
What is important Amb-, diff-, spec-, emission mode + formuas Phong s + Binn s highight mode Fat-, Gouraud- and Phong shading Transparency Fog Tomas Akenine-Mőer 2002
THE END