INF3320 Computer Graphics and Discrete Geometry Visual appearance Christopher Dyken and Martin Reimers 23.09.2009 Page 1
Visual appearance Real Time Rendering: Chapter 5 Light Sources and materials Shading Transparency, alpha and compositing Gamma correction The Red Book: Lighting (Chapter 5) Page 2
The OpenGL Pipeline revisited Page 3
Recap and preview Previously: modeling vertex transformations triangles transformed into view / device coordinates The next step in the pipeline is to rasterize primitives Shading amounts to colouring the pixels For that we can use lighting models, texturing, shaders,... Page 4
The shading determines the visual appearance Here, the same set of triangles are subjected to different shaders: Hidden line Plain Phong Shiny Phong Parallax GS particle occlusion map fire Page 5
Lighting Page 6
To set up lighting in OpenGL OpenGL has a built in simple lighting model: 1. Determine one normal per vertex 2. Set up a number of light sources 3. Set up lighting model 4. Determine material properties Lighting calculated per vertex, pixels shaded by linear interpolation Geometry Lighting Shading Page 7
To set up lighting in OpenGL - example Render a lit sphere: GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (GL_SMOOTH); glmaterialfv(gl_front, GL_SPECULAR, mat_specular); glmaterialfv(gl_front, GL_SHININESS, mat_shininess); gllightfv(gl_light0, GL_POSITION, light_position); glenable(gl_lighting); glenable(gl_light0);... glutsolidsphere(1,0,20,0,16.0); Check out Nate Robins lighting tutorial Page 8
Light sources Directional light Point light Spot light cut off angle light direction Emit light The light vector points toward the source Light color is the intensity in RGB components Types of light: (local) ambient, diffusive and specular Attenuation: the intensity decays with distance In addition there is (global) ambient light Page 9
Light sources II A number of lights can be used (8 required in OpenGL) Use GL_LIGHT0, GL_LIGHT1 etc. Position light in world coordinates Modelview transforms affect light position and direction Check out Nate Robins lightposition tutorial Page 10
Light and matter Page 11
Materials Materials interact with light in essentially two ways Absorbtion: light energy changes form Scattering: light changes direction, e.g. reflection and refraction (transmission) Materials have mixed properties wrt. these interactions, no materials are perfect: there will always be some absorption, reflection and refraction. There are no perfect mirrors. Page 12
Diffuse reflection A perfectly diffuse surface that reflects light isotropically (equally in all directions) is called Lambertian (e.g. chalk) l θ n Lambert s cosine law The fraction of outgoing light intensity I (exitance) for a Lambertian surface with normal n and light vector l equals I = n l n l = cos(θ) Note: no dependency on view direction! Page 13
Mirror Reflection Reflection law for perfect mirrors the incoming light direction is mirrored about the surface normal ( (l, n) = (r, n)) and all three vectors lie in the same plane Example: reflection vector Let n = 1. Then l projected onto n is halfway between l and r. l θ n θ r which gives l+r 2 = (l n)n r = 2(l n)n l. Page 14
Specular Reflection Specular Reflection model the fraction of outgoing specular intensity (=exitance) along v depends on the angle between the half vector h = (v + l)/ v + l and the surface normal: (n h) s = (cos(θ)) s where s is a shininess factor and θ = (n, h) The specular exitance depends on l n θ h v n, v, l and s, as well as irradiance (incoming intensity) Note: depends on view direction v! Page 15
Refraction Light is bent when it passes the boundary of two different media. The index of refraction η is the speed of light in vacuum c divided by the speed of light in the material v, i.e. η = c v. Let η i and η r be the refractive indices of material i and r, and let θ i and θ r be the angle between the light ray and surface normal for the incident and the refracted ray. i η i θ i n Snell s law says η r η i sin θ i = η r sin θ r. θ r r Page 16
If η i is larger than η r, e.g. from glass to air: We get total reflection when the incident angle is larger than the critical angle. In this case, the light is not refracted, but reflected back into the material. The critical angle θ c is given by Material η Vacuum 1.00000 Air 1.00029 CO 2 1.000449 H 2 O @20 C 1.33335 H 2 O @100 C 1.31819 Alcohol 1.329 Ice 1.31 Sapphire 1.77 Diamond 2.417 sin θ c = η r η i. Page 17
Material color Specifies the fraction of incoming light that is reflected. A red object is red because it absorbs all colors but read when lit by white light (what about blue light?) Page 18
Material color Specifies the fraction of incoming light that is reflected. A red object is red because it absorbs all colors but read when lit by white light (what about blue light?) Materials have ambient, diffuse and specular colors (i.e. they reflect the corresponding light in different ways) Diffuse color: THE color Ambient color: reflected off of other surfaces Specular color: the color of reflected light (often white-ish) In addition, materials can have an emissive color Page 19
Light and Material interaction The different types of light color and material color interact:... material color scales the light color Suppose the ambient light color (intensity) of a light source is c = (LR, LG, LB) and the ambient material color (reflected proportion) of the material is m = (MR, MG, MB), then the resulting reflected light is m c = (LR MR, LG MG, LB MB) Light is scaled according to reflectance model, e.g. diffusive m c n l n l Outgoing light intensity add up (clamped to 1) Page 20
Lighting and Shading Page 21
What is the difference between lighting and shading? Lighting The interaction between material and light sources, yields the amount of light in a point in space. Shading The coloring of a fragment/pixel, e.g. based on a lighting model Phong lighting and Phong shading The Phong lighting models refers to a method to light a point not to be confused with Phong shading, which is to interpolate the normal vector over a surface. Page 22
Geometry Flat shading Gouraud shading Phong shading shading interpolation Flat shading evaluates the lighting model once for each triangle, and fills the triangle with that colour. Gouraud shading evaluates the lighting model at each of the three vertices and interpolates the colour to determine the fragment colours inside the triangle. Phong shading evaluates the lighting model per fragment. The normal vector is interpolated (and normalized) over the triangle. Page 23
Phong Lighting Model Page 24
The Phong lighting model is composed of three components: ambient component, diffuse component, specular component. Remember: Looking good with a cheap evaluation is more important than physical correctness. Vectors used by the Phong lighting model n n is the surface normal at p. v l is light source direction. l r r is l reflected about n. p v points towards the viewer. Page 25
Ambient component The ambient term simulates indirect lighting, light bouncing off surfaces onto other surfaces It is the same regardless of orientation, and is modeled with a constant. Diffuse component Diffuse component models Lambert s law, i = n l If negative, surface is facing away. To avoid negative intensity, i diff = max(n l, 0) Page 26
Specular component Specular component models light reflected directly by the surface. Half-vector: h = l+v l+v, so i spec = max(h n, 0) s, where s models the shininess, Alternative approach From law of reflection, r = 2(l n)n l i spec = max(r v, 0) s (this is less realistic, see RTR 7.5.7 for an explanation) Page 27
Light attenuation Light intensity is approximately proportional to the inverse of the square of the distance d from p to the light source: f (d) = 1 a+bd+cd 2, We can manipulate the attenuation model by a, b, and c. Only the diffuse and the specular component is attenuated. Material and light properties are modeled through ambient, diffuse, and specular colours. Thus, the whole equation is z } { z } { c = m a l a + f (d) max(n l, 0)m d l d + max(h n, 0) s m s l s where denotes element-by-element multiplication Page 28
The OpenGL fixed-function lighting model Page 29
To set up lighting in OpenGL 1. Set up one normal pr vertex 2. Set up lights 3. Set up lighting model 4. Determine material properties Example: http://glprogramming.com/red/chapter05.html#name3 Page 30
The OpenGL fixed-function lighting model OpenGL carries out lighting calculations in eye-coords (after modelview transform, but before perspective transform). OpenGL fixed function lighting API Lighting calculations enabled with glenable(gl_lighting). Global light model properties like ambient colors and if the viewer is infinitely far away is specified with gllightmodel. Per light properties like light colours, type, position, direction, and attenuation coefficients are specified with gllight. Material properties like colours and shininess with glmaterial The Phong Lighting equation is calculated for each vertex using the normal vector specified by glnormal. Page 31
Which values are used in the calculation? vertex position in eye coords normal vector in eye coords eye position in eye coords light position in eye coords glcolor*() current colour glmaterial*(*, AMBIENT) glmaterial*(*, DIFFUSE) lighting vertex color out glmaterial*(*, SPECULAR) glenable(gl_lighting) glcolormaterial() glmaterial*(*, EMISSION) Note: light properties are missing! Shading mode glshademodel specifies if colors are constant over each primitive (GL_FLAT) or Gouraud shaded (GL_SMOOTH). Page 32
Normals are specified per vertex by glnormal*( normal ) or in arrays with glnormalpointer Normals should be normalized Some transformations changes length use glenable(gl_normalize) or glenable(gl_rescale) Page 33
Light model is specified by gllightmodel*(pname, *params) where pname controls the following parameters: GL_LIGHT_MODEL_AMBIENT specifies the ambient RGBA-color. GL_LIGHT_MODEL_COLOR_CONTROL specifies if the specular part is applied after texturing GL_LIGHT_MODEL_LOCAL_VIEWER specifies if the viewer is infinitely far away or not in specular computations (local viewer is more expensive) GL_LIGHT_MODEL_TWO_SIDE controls two-sided lighting Page 34
Lights are specified by gllight*(gl_lighti, pname, param) where GL_LIGHTi with 0 i <GL_MAX_LIGHTS (min 8) the parameters pname GL_AMBIENT, GL_DIFFUSE, or GL_SPECULAR, then param specifies the RGBA ambient, diffuse, or specular color of light. GL_POSITION specifies the homogeneous position of source 1 GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, or GL_QUADRATIC_ATTENUATION specifies a,b,c GL_SPOT_DIRECTION, GL_SPOT_EXPONENT, or GL_SPOT_CUTOFF specifies spotlight parameters 1 w = 0 implies directional light Page 35
Material properties are specified by glmaterial*(face, pname, param) where face is GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK, and specifies the material properties: GL_AMBIENT, GL_DIFFUSE, or GL_SPECULAR, specifies the material RGBA ambient, diffuse, or specular color. GL_SHININESS specifies the specular exponent m shi Page 36
glcolormaterial When lighting is enabled, the color of glcolor is ignored unless you override with glcolormaterial( face, mode) Let you override the color specified by glmaterial with the color specified by glcolor. The following modes can be used: GL_EMISSION, GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, and GL_AMBIENT_AND_DIFFUSE. Page 37
Transparency, Alpha and compositing Page 38
Transparency and Alpha Semi-transparent objects can be rendered by view based or light based methods Screen-door transparency: render checkerboard pattern, every other pixel is foreground. Best at 50% transparency and one object Alpha Blending: foreground (transparent) color blenden with background color, using the over operator and the (fourth) alpha channel in RGBA: c = αc s + (1 α)c d where c s and c d are source and destination colors. Need to render back to front, i.e. need to sort the geometry (nontrivial) There are many applications, blending modes etc... later Page 39
Gamma Correction Screen intensity is usually not a linear function of input voltage. To represent color correctly we modify input signal by V out = V γ in Page 40
The last slide More about transparency, blending etc. later Next time: more appearance, antialiasing, texturing Page 41