01076536 Computer Graphics Chapter 10 llumination Models and Surface-Rendering Methods Somsak Walairacht, Computer Engineering, KMTL 1
Outline Light Sources Surface Lighting Effects Basic llumination Models Polygon Rendering Methods Texture Mapping Ray-Tracing Radiosity 2
llumination Model Lighting model or Shading model Calculate the color of an illuminated positions on the surface of an object Ray-tracing Topics Light sources Basic models Lighting-surface effects 3
Surface-Rendering Method Shading Method Determine the pixel color using color calculations from an illumination model Scan-line, mage-space algorithms Topics Polygon rendering methods Ray-tracing Radiosity Texture Mapping 4
Why Lighting? f we don t have lighting effects nothing looks three dimensional 5
Photorealism in Computer Graphics Accurate representation of surface properties Good physical descriptions of light effects in the scene Reflections Refraction Transparency Surface texture Shadows 6
Light Sources 1) Point light Simplest, position and color 2) nfinite distant light Large source, the sun Constant distance to any positions Directional vector and color Radial intensity attenuation 3) Directional light Spotlight, cone beam Directional vector, angular limit, position, and color 4) Extended light Large source closed to object Light emitting surface 8
Point Light Sources A point source is the simplest model we can use for a light source Simply define The position of the light The RGB values for the colour of the light Light is emitted in all directions Useful for small light sources 9
Radial ntensity Attenuation As light moves from a light source its intensity diminished At any distance d l away from the light source the intensity diminishes by a 1 factor of 2 d l However, using this factor does not produce very good results so we use something different 10
Radial ntensity Attenuation An inverse quadratic function is used instead f radatten ( d l ) k c k l d 1 l k q d 2 l where d l = distance between the light and the surface being shaded k c = constant attenuation factor k l = linear attenuation factor k q = quadratic attenuation factor 11
Example f radatten ( d l ) 1 2 r d l 1 1 r 2 d 2 l 12
nfinite Distant Light Sources A large light source, like the sun, can be modelled as a point light source However, it will have very little directional effect Radial intensity attenuation is not used 13
Directional Light Sources To turn a point light source into a spotlight, simply add a vector direction and an angular limit θ l 14
Directional Light Sources & Spotlight Effects Denote V light as the unit vector in the direction of the light V obj as the unit vector from the light source to an object The dot-product of these two vectors gives us the angle between them V obj Vlight cos f this angle is inside the light s angular limit then the object is within the spotlight 15
Angular ntensity Attenuation As well as light intensity decreasing as we move away from a light source, it also decreases angularly A commonly used function for calculating angular attenuation is: fangatten ( ) cos where a l the attenuation exponent, is assigned some positive value and angle is measured from the cone axis a l 0 16
Extended Light Sources Approximate it as a light-emitting surface We can set the direction for the point sources Behind the lightemitting surface are not illuminated 17
Reflected Light The colors that we perceived are determined by the nature of the light reflected from an object For example, if white light is shone onto a green object most wavelengths are absorbed, while green light is reflected from the object Colors Absorbed 18
Surface Lighting Effects The amount of incident light reflected by a surface depends on the type of material Shiny materials reflect more of the incident light Dull surfaces absorb more of the incident light For transparent surfaces, some of the light is also transmitted through the material 19
Diffuse Reflection Surfaces that are rough or grainy tend to reflect light in all directions กระจาย This scattered light is called Diffuse Reflection 20
Specular Reflection Additionally to diffuse reflection some of the reflected light is concentrated into a highlight or bright spot เก ยวก บกระจกเงา This is called Specular Reflection 21
ความเข มของแสง Basic llumination Model We will consider a basic illumination model which gives reasonably good results and is used in most graphics systems การท าให ส องสว าง The important components are: Ambient light Diffuse reflection Specular reflection 22
Basic llumination Model Ambient Diffuse Specular Final mage 23
Ambient Light To incorporate background light we simply set a general brightness level for a scene ndependent of viewing direction and the spatial orientation of a surface Assuming only monochromatic light effects, we designate ambient light with the value a 24
Diffuse Reflection Assume that surfaces reflect incident light with equal intensity in all directions Such surfaces are referred to as deal Diffuse Reflectors or Lambertian Reflectors 25
Diffuse Reflection A parameter k d is set for each surface that determines the fraction of incident light that is to be scattered as diffuse reflections from that surface This parameter is known as the Diffuse- Reflection Coefficient or the Diffuse Reflectivity k d is assigned a value between 0.0 and 1.0 0.0: dull surface that absorbs almost all light 1.0: shiny surface that reflects almost all light 26
Diffuse Reflection Ambient Light For background lighting effects we can assume that every surface is fully illuminated by the ambient light a Therefore the ambient contribution to the diffuse reflection is given as: k ambdiff d a Ambient light alone is very uninteresting so we need some other lights in a scene as well 27
Diffuse Reflection Brightness depends on the orientation of a surface The amount of incident light on a surface is given as: cos l, incident l So we can model the diffuse reflections as: k l, diff k d d l, incident l cos 28
Diffuse Reflection Assuming we denote the normal for a surface as N and the unit direction vector to the light source as L then: So, N L cos l, diff k d l ( N 0 L) if if N N L L 0 0 29
Combining Ambient And ncident Diffuse Reflections Most graphics packages use two separate diffuse-reflection coefficients: k a for ambient light k d for incident light The total diffuse reflection equation for a single point source can then be given as: diff k a a k k d a l a ( N L) if if N L N L 0 0 30
Combining Ambient And ncident Diffuse Reflections 31
Ambient Light k a a 32
Ambient and Diffuse Reflection k a a k d l ( N L) 33
Specular Reflection The bright spot on a shiny surface is the result of near total of the incident light in a concentrated region around the specular reflection angle The specular reflection angle equals the angle of the incident light 34
Specular Reflection A perfect mirror reflects light only in the specular-reflection direction Other objects exhibit specular reflections over a finite range of viewing positions around vector R 35
Effect of n shiny The effect of n s on the angular range in which we can expect to see specular reflections 36
The Phong Specular Reflection Model An empirical model for calculating specular reflection range Developed in 1973 by Phong Bui Tuong The intensity of specular reflection is set proportional to the angle between the viewing vector and the specular reflection vector 37
Phong Model The specular reflection intensity is proportional to cos n s The angle can be varied between 0 and 90 so that cos varies from 1.0 to 0.0 The Specular-Reflection Exponent, n s is determined by the type of surface we want to display Shiny surfaces have a very large value (>100) Rough surfaces would have a value near 1 38
Phong Model The specular reflection intensity is given as: n k cos s l, spec s l Since, V R cos l, spec k s l ( V 0.0 R) n s if if V V R R 0 and 0 or N N L L 0 0 39
Phong Model Calculate of vector R R+L = (2LN)N R = (2LN)N L More efficient, use Halfway vector H 40
The Halfway Vector Blinn proposed an alternative to Phong's model that uses the halfway vector The halfway vector is the direction of maximum highlights f the surface is oriented so that the normal is aligned along, the viewer sees the brightest specular highlights f light source and viewer are far away, H ±constant l, spec H k ( N H ) s L V L V l n s 41
Blinn Phong Reflection Model Using halfway angle Set specular reflection exponent, n s close to the formal expression 42
Diffuse and Specular Constants k s The diffuse constant determines the intensity of the diffuse component Notice that the diffuse component gets brighter across the rows The specular constant determines the intensity of the specular component Notice that the specular component gets brighter up the columns k d 43
44 Combining Diffuse & Specular Reflections Effects of diffuse and specular reflections can be combined as For any number of light sources in a scene n s l s l d a a spec diff amb H N k L N k k ) ( ) ( n l n s d l a a n l spec l diff l amb s H N k L N k k 1 1,,
RGB Color Considerations For an RGB color description each intensity specification is a three element vector So, for each light source: l,, lr lg Similarly all parameters are given as vectors: a lb k, k k k k, k, k k, ar ag ab k k, k, k s sr d sg sb dr dg db 45
Total llumination k a l a k d l ( L N) 46
Total llumination k a a l k d ( L N) k ( H s N) n s n s 5 47
Total llumination k a a l k d ( L N) k ( H s N) n s n s 50 48
Total llumination k a a l k d ( L N) k ( H s N) n s n s 500 49
Single Light Source k a a l k d ( L N) k ( H s N) n s 50
Multiple Light Sources k a a i i k d ( L i N) k s ( H i N) n s 51
52 Attenuation Decrease intensity with distance from light d = distance to light r = radius of attenuation for light 2 2 2 2 2 2 ), ( ) ) max(0,(1 ), ( ) max(0,1 ), ( ) max(0,1 ), ( 2 r d r d r d r d e r d att r d att r d att r d att
Attenuation k a a i i n s k L N ) k ( H N ) att( d, r ) d ( i s i i 53
Attenuation k a a i i k L N) k ( H N) ns att( d, r ) ( d i s i i 54
Spot Lights Eliminate light contribution outside of a cone A spotcoeff L L A A cos( ), cos( ), 0 L A L Surface 55
Spot Lights k a a i i k ( L N) k ( H N) ns spotcoeff d i s i i 56
Spot Lights k a a i i k ( L N) k ( H N) ns spotcoeff d i s i i 57
Spot Lights k a a i i k ( L N) k ( H N) ns spotcoeff d i s i i 58
Transparency Snell s law of refraction: L N i i r R T sin T r i sini, :index of refraction r i i cosi cosr N L r r Derivation : Use Snell's law, T. T 1, T N cosr, T N L, and solve for and
No Surface Rendering VS Flat Surface Rendering No Surface Rendering Flat Surface Rendering 60
Flat Surface Rendering VS Gouraud Surface Rendering Flat Surface Rendering Gouraud Surface Rendering 61
Gouraud Surface Rendering VS Phong Surface Rendering Gouraud Surface Rendering Phong Surface Rendering 62
Flat Surface Rendering The simplest method for rendering a polygon surface The same colour is assigned to all surface positions The illumination at a single point on the surface is calculated and used for the entire surface Flat surface rendering is extremely fast, but can be unrealistic 63
Gouraud Surface Rendering Gouraud surface shading was developed in the 1970s by Henri Gouraud Worked at the University of Utah along with van Sutherland and David Evans Often also called intensityinterpolation surface rendering ntensity levels are calculated at each vertex and interpolated across the surface 64
Gouraud Surface Rendering (cont ) To render a polygon, Gouraud surface rendering proceeds as follows: 1. Determine the average unit normal vector at each vertex of the polygon 2. Apply an illumination model at each polygon vertex to obtain the light intensity at that position 3. Linearly interpolate the vertex intensities over the projected area of the polygon 65
Gouraud Surface Rendering (cont ) N 1 The average unit normal vector at v is given as: N 4 N v v N 2 N v N N 1 1 N N or more generally: 2 2 N N 3 3 N N 4 4 N 3 N v n i1 n i1 N N i i 66
67 Gouraud Surface Rendering (cont ) 2 2 1 4 1 1 2 1 2 4 4 y y y y y y y y 1 2 3 4 5 x y Scan-line p 2 2 3 5 3 3 2 3 2 5 5 y y y y y y y y 5 4 5 4 4 4 5 5 x x x x x x x x p p p llumination values are linearly interpolated across each scan-line
Advantages of Gouraud Surface Rendering 68
Gouraud Surface Rendering mplementation Gouraud surfacing rendering can be implemented relatively efficiently using an iterative approach Typically Grouaud shading is implemented as part of a visible surface detection technique 69
Problems with Gouraud Shading Gouraud shading tends to miss certain highlighting n particular Gouraud shading has a problem with specular reflections Also, Gouraud shading can introduce anomalies known as Machbands 70
Problems with Gouraud Shading (cont ) Gouraud shading Phong shading The major problem with Gouraud shading is in handling specular reflections 71
The Machband Effect A psychological phenomenon whereby we see bright bands where two blocks of solid color meet High light can get lost or grow Corners on silhouette remain Machband effect is visible at some edges A good demo is available to experiment with this at: http://www.nbb.cornell.edu/neurobio/land/oldstudentprojects/c s490-96to97/anson/machbandingapplet/ 72
Phong Shading ncremental normal vector update along and between scan lines Comparison to Gouraud shading Better highlights Less Machbanding More costly 73
Phong Surface Rendering A more accurate interpolation based approach for rendering a polygon was developed by Phong Bui Tuong Basically the Phong surface rendering model (or normal-vector interpolation rendering) interpolates normal vectors instead of intensity values 74
Phong Surface Rendering (cont ) To render a polygon, Phong surface rendering proceeds as follows: 1. Determine the average unit normal vector at each vertex of the polygon 2. Linearly interpolate the vertex normals over the projected area of the polygon 3. Apply an illumination model at positions along scan lines to calculate pixel intensities using the interpolated normal vectors 75
76 Phong Surface Rendering (cont ) N 4 N 1 N 2 N 3 Scan line 2 2 1 4 1 1 2 1 2 4 4 N y y y y N y y y y N N 5 2 2 3 5 3 3 2 3 2 5 5 N y y y y N y y y y N N p p 5 5 4 4 4 5 4 5 N y y y y N y y y y N p p p
Phong Surface Rendering mplementation Phong shading is much slower than Gouraud shading as the lighting model is revaluated so many times Typically, Phong shading is implemented as part of a visible surface detection technique 77
Texture Mapping A way of adding surface details Two ways can achieve the goal: Surface detail polygons: create extra polygons to model object details Add scene complexity and thus slow down the graphics rendering speed Some fine features are hard to model! Map a texture to the surface (a more popular approach) Complexity of images does Not affect the complexity Of geometry processing (transformation, clipping )
Texture Mapping 1. projection 3. patch texel 3D geometry 2. texture lookup t 2D projection of 3D geometry 2D image S
Texture Representation Bitmap (pixel map) textures (supported by OpenGL) Procedural textures (used in advanced rendering programs) t (0,0) (1,1) s Bitmap texture: A 2D image - represented by 2D array texture[height][width] Each pixel (or called texel ) by a unique pair texture coordinate (s, t) The s and t are usually normalized to a [0,1] range For any given (s,t) in the normalized rang there is also a unique image value (i.e., a unique [red, green, blue] set )
Texture Value Lookup For given texture coordinates (s,t), we can find a unique image value from the texture map (1,1) How about coordinates that are not exactly at the intersection (pixel) position A) Nearest neighbor B) Linear nterpolation C) Other filters (0,0) (0.25,0)(0.5,0)(0.75,0) (1,0)
Map textures to surfaces Establish mapping from texture to surfaces (polygons): - Application program needs to specify texture coordinates for each corner of the polygon The polygon can be in an arbitrary size (1,0) (1,1) (0,0) (1,0)
Map textures to surfaces Texture mapping is performed in rasterization (0,1) (1,1) For each pixel that is to be painted, its texture coordinates (s, t) are determined (interpolated) based on the corners texture coordinates (why not just interpolate the color?) (0,0) (1,0) The interpolated texture coordinates are then used to perform texture lookup
OpenGL texture mapping Texturing steps in your program 1) Specify texture - read or generate image - Assign to texture 2) Specify texture mapping parameters - Wrapping, filtering, etc. 3) Enable GL texture mapping (GL_TEXTURE_2D) 4) Assign texture coordinates to vertices 5) Disable GL texture mapping (if you don t need to perform texture mapping any more)
Specify textures Load the texture map from main memory to texture memory gltexmage2d(glenum target, Glint level, Glint iformat, int width, int height, Glenum format, Glenum type, Glvoid* img) Example: glteximage2d(gl_texture_2d, 0, GL_RGB, 64, 64, GL_RGB, GL_UNSGNED_BYTE, mymage); (mymage is a 2D array: GLuByte mymage[64][64][3]; ) The dimensions of texture images must be powers of 2
Fix texture size f the dimensions of the texture map are not power of 2, you can 1) Pad zeros 2) use gluscalemage() 100 60 64 128 Ask OpenGL to filter the data for you to the right size you can specify the output resolution that you want Remember to adjust the texture coordinates for your polygon corners you don t want to nclude black texels in your final picture
Texture mapping parameters What happen if the given texture coordinates (s,t) are outside [0,1] range? (1,1) (2,2) (2,2) (0,0) texture (0,0) GL_Repeat E.g: gltexparameteri(gl_textaure_2d, GL_TEXTURE_WRAP_S, GL_CLAMP) (0,0) GL_Clamp f (s >1) s = 1 f (t >1) t = 1
Texture mapping parameters Since a polygon can get transformed to arbitrary screen size, texels in the texture map can get magnified or minified. texture polygon projection texture polygon projection Magnification Minification Filtering: interpolate a texel value from its neighbors or combine multiple texel values into a single one
Texture mapping parameters OpenGL texture filtering: 1) Nearest Neighbor (lower image quality) 2) Linear interpolate the neighbors (better quality, slower) gltexparameteri(gl_texture_2d, GL_TEXTURE_MN_FLTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MN_FLTER, GL_LNEAR) Or GL_TEXTURE_MAX_FLTER
Texture color blending Determine how to combine the texel color and the object color GL_MODULATE multiply texture and object color GL_BLEND linear combination of texture and object color GL_REPLACE use texture color to replace object color E.g: gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Enable (Disable) Textures Enable texture glenable(gl_texture_2d) Disable texture gldisable(gl_texture_2d) Remember to disable texture mapping when you draw non-textured polygons
Specify texture coordinates Give texture coordinates before defining each vertex glbegin(gl_quads); gltexcoord2f(0.0f, 0.0f); glvertex3f(-1.0f, -1.0f, 0.0f); gltexcoord2f(0.0f, 1.0f); glvertex3f(-1.0f, 1.0f, 0.0f); gltexcoord2f(1.0f, 0.0f); glvertex3f( 1.0f, 1.0f, 0.0f); gltexcoord2f(1.0f, 1.0f); glvertex3f( 1.0f, -1.0f, 0.0f); glend();
Transform texture coordinates All the texture coordinates are multiplied by GL_TEXTURE matrix before in use To transform texture coordinates, you do: glmatrixmode(gl_texture); Apply regular transformation functions Then you can draw the textured objects
Put it all together gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT); gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT); gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FLTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MN_FLTER, GL_NEAREST); gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_REPLACE); glenable(gl_texture_2d); gltexmage2d(gl_texture_2d, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSGNED_BYTE, mytexture); Draw_picture1(); // define texture coordinates and vertices in the function
Other Stuff Wrapping texture onto curved surfaces. E.g. cylinder, can, etc Wrapping texture onto sphere Bump mapping: perturb surface normal by a quantity proportional to texture a b a s a b a z z z z t a b a s a b a t
Planar Map Shape Determine which component was projected by looking for color changes in coordinate directions When moving parallel to the x-axis, an object s color changes. When moving up and down along the y-axis, the object s color also changes. However, movement along the z-axis does not produce a change in color. 96
Cylinder Map Shape An (x,y,z) value is converted to cylindrical coordinates of ( r, theta, height ) theta is converted into x-coordinate height is converted into y-coordinate 97
Sphere Map Shape The (x,y,z) value of a point is converted into spherical coordinates The latitude is converted into an x-coordinate The longitude is converted into a y-coordinate 98
Box Map Shape Enclosing box is usually axis-parallel bounding box of object Six rectangles onto which the texture is mapped Similar to planar mapping 99
Bump Mapping Bump mapping affects object surfaces, making them appear rough, wrinkled, or dented Bump mapping alters the surface normals before the shading calculation takes place 100
Bump VS Displacement Mapping Bump Mapping Variation of surface normals Apply normal perturbation without updating positions Displacement Mapping Variation of surface positions, thus normal Requires fine tessellation of object geometry 101
102
Environment Mapping A two-dimensional texture mapping technique that uses a map shape of a box and a map parameter of a reflection ray it s easy to create reflections with a ray tracer, ray tracing is still too expensive for long animations 103
Ray-Tracing Provides rendering method with Refraction/Transparent surfaces Reflective surfaces Shadows mage taken from http://www.tjhsst.edu/~dhyatt/superap/samplex.jpg 104
Ray-Tracing: Principle View point Screen Primary ray Normal Reflected ray Shadow ray Refracted ray
Ray-Tracing: Principle Trace a primary ray passing through a pixel P : intersection point Compute the contribution of the sources to P by tracing shadow rays toward the light sources f a shadow ray intersects an opaque object between P and the light source then P is shadowed Compute the contribution to P of other points within the scene by tracing secondary rays: reflected and refracted A reflected ray is traced only if the material is specular A refracted ray is traced only if the material is transparent A secondary ray intersects the scene at a point P Again compute the contribution of the sources to P by tracing shadow rays toward the light sources. Repeat the process Each ray brings its contribution to the luminance of a point 106
Ray-Tracing: Pseudo code For each ray r from eye to pixel, color the pixel with the value returned by ray_cast(r, scene, depth) ray_cast(r, scene, depth) { f(depth > Max_Depth) { color black } else { f (intersection(r, scene)) { p point_of_intersection(r, scene); u reflect(r, p); v refract(r, p); color phong_direct(p, r) + ks ray_cast(u, scene, depth+1) + kt ray_cast(v, scene, depth+1); } else color background_color ; } return(color); } 107
Ray-Tracing: Pseudo code Explained p point_of_intersection(r, scene); Compute p, the point of intersection of ray r with the scene u reflect(r, p); v refract(r, p); Compute the reflected ray u and the refracted ray v using Snell s Laws phong(p, r) Evaluate the Phong reflection model for the ray r at point p on surface s, taking shadowing into account ks ray_cast(u,scene,depth) Multiply the contribution from the reflected ray u by the specular color ks for surface s containing p. Only (specular-to-specular) * light transport is handled. deal specular (mirror) reflection kt ray_cast(v,scene,depth) Multiply the contribution from the refracted ray v by the specular-refraction coefficient kt for surface s. Only (specular-refraction) * light transport is handled 108
Rays & Ray Tree Corresponding binary ray-tracing tree Primary-, Secondary-, Reflected-, Transmitted- Rays 109
Radiosity Light can be reflected between two objects that have diffuse surfaces This is commonly called color bleeding Radiosity treats light as energy llumination of the scene is the transfer of energy mage created by Erik Svanholm 2002 Zumtobel Staff 110
Radiosity Method Describe the physical process of light distribution in a diffuse reflecting environment Areas that are not illuminated directly are also not completely dark Every objects act as a secondary light source 111
Ray-Tracing VS Radiosity Ray-Tracing mage-space From Eye to Light Specular Reflection Radiosity Object-Space From Light to Surface Diffuse Reflection 112
End of Chapter 10 115