Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 2
We know how to rasterze - Gven a 3D trangle and a 3D vewpont, we know whch pxels represent the trangle But what color should those pxels be? 3
Adds realsm to our objects Suppose we create a model of a sphere usng many polygons and color t wth glcolor. But our objectve s 4
n ths verson of OpenGL, the sphere s drawn from the front to the back. So f the depth test s cancelled, what we wll see s the back of the sphere. n another verson of OpenGL the results can be dfferent. 5
Lght-materal nteractons affect vertces appearance Due to ths nteracton, each vertex would have dfferent color and shade Need to consder - Lght sources - Materal propertes - Locaton of vewer - Surface orentaton 6
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 7
glcolor3f(red, green, blue) glcolor4f(red, green, blue, alpha) 8
s OpenGL lghtng enabled? NO YES Color determned by glcolor3f( ) gnorng: normals lghts materal propertes Color determned by shades Usng: normals lghts materal propertes 9
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 10
Shadng s defned as a smooth transton from one color to another Curved surfaces are approxmated by polygons Types of shadng are: - Flat shadng - Smooth Shadng 11
Enabled wth glshademodel(gl_flat) Shadng s constant across polygon Color of last vertex determnes nteror color Only sutable for very small polygons v2 v0 v1 12
Enable wth glshademodel(gl_smooth) nterpolate color n nteror Computed durng rasterzaton Much better than flat shadng More expensve to calculate v2 v0 v1 13
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 14
Lghtng n general must be enabled glenable(gl_lghtng) Each ndvdual lght must be enabled glenable(gl_lght0) OpenGL supports at least 8 lght sources There are 3 types of effects of lght source: Ambent Dffuse Specular 15
Ambent lght : Drectonless Objects are evenly lt on all surfaces n all drectons Objects are evenly shaded regardless of ther vewng angle Dffuse lght: Comes from a partcular drecton Reflected evenly off a surface Specular lght: Comes from a partcular drecton Reflected sharply and n a partcular drecton To make objects specular, both lght source and object must have specular property set 16
17
Use vectors (r, g, b, a) for lght propertes Beware: lght postons wll be transformed by the modelvew matrx GLfloat lght_ambent[] = {0.2, 0.2, 0.2, 1.0}; GLfloat lght_dffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat lght_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat lght_poston[] = {-1.0, 1.0, -1.0, 0.0}; gllghtfv(gl_lght0, GL_AMBENT, lght_ambent); gllghtfv(gl_lght0, GL_DFFUSE, lght_dffuse); gllghtfv(gl_lght0, GL_SPECULAR, lght_specular); gllghtfv(gl_lght0, GL_POSTON, lght_poston); 18
Drectonal lght gven by poston vector (parallel rays) GLfloat lght_poston[] = {-1.0, 1.0, -1.0, 0.0}; gllghtfv(gl_lght0, GL_POSTON, lght_poston); Pont source gven by poston pont (spotlght) GLfloat lght_poston[] = {-1.0, 1.0, -1.0, 1.0}; gllghtfv(gl_lght0, GL_POSTON, lght_poston); 19
A lght source whch located nsde the scene. Lght spreads n a cone shape Can be drected CUTOFF angle s between 0 to 90 unless t s 180. (ths s the defualt value) GLfloat lght_drecton[]={1,-1,-1,1}; gllghtfv(gl_lght0, GL_SPOT_DRECTON, lght_drecton); gllghtf(gl_lght0, GL_SPOT_CUTOFF, 45); 20
Lght sources are geometrc objects whose postons or drectons are affected by OpenGL matrces t s possble to: Move the lght source(s) wth the object(s) Move the lght source(s) and object(s) ndependently Fx the object(s) and move the lght source(s) Fx the lght source(s) and move the object(s) 21
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 22
Materal have certan reflectve color propertes Put a blue ball n a dark room wth only yellow lght, how would t looks lke? Materal propertes stay n effect untl changed GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0}; GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0}; GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0}; GLfloat low_sh[] = {5.0}; glmateralfv(gl_front, GL_AMBENT, mat_a); glmateralfv(gl_front, GL_DFFUSE, mat_d); glmateralfv(gl_front, GL_SPECULAR, mat_s); glmateralfv(gl_front, GL_SHNNESS, low_sh); glenable(gl_color_materal); 23
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 24
Defne unt normal before each vertex glnormal3f(nx, ny, nz); glvertex3f(x1, y1, z1); glvertex3f(x2, y2, z2); glvertex3f(x3, y3, z3); same normal for all vertces glnormal3f(nx1, ny1, nz1); glvertex3f(x1, y1, z1); glnormal3f(nx2, ny2, nz2); glvertex3f(x2, y2, z2); glnormal3f(nx3, ny3, nz3); glvertex3f(x3, y3, z3); dfferent normals 25
Length of normals changes under some modelvew transformatons (but not under translatons and rotatons) Ask OpenGL to automatcally re-normalze glenable(gl_normalze); 26
Color n OpenGL Polygonal Shadng Lght Source n OpenGL Materal Propertes Normal Vectors Phong model 27
cosne law (dot product) N L N L cos Nˆ Lˆ cos K ( Nˆ Lˆ) D D L N q L Surface
Phong Model Vˆ Rˆ cos(a) n max(0, Vˆ ˆ) R Phong exponent: apparent smoothness of the surface Vewer R a q N q L V K ( Vˆ Rˆ ) S S n L
Sngle lght source: Vewer R a q N q L V E K A A K D ( N L) K ( V L S R) n L
Multple lght sources: Vewer L 1 N L 2 V E K A A ( K D ( N L ) K S ( V R ) n )
Vsual llustraton of the Phong equaton: here the lght s whte, the ambent and dffuse colors are both blue, and the specular color s whte, reflectng a small part of the lght httng the surface. The ntensty of the dffuse component vares wth the drecton of the surface.
Shadow terms tell whch lght sources are blocked Cast ray towards each lght source L S = 0 f ray s blocked, S = 1 otherwse n S D A A E S R V K L N K K ) ) ( ) ( ( Shadow Term
R R n S D A A E K S R V K L N K K ) ) ( ) ( ( Also trace secondary rays from ht surfaces Global llumnaton from mrror reflecton and transparency
Trace secondary ray n drecton of mrror reflecton Evaluate radance along secondary ray and nclude t nto llumnaton model R R n S D A A E K S R V K L N K K ) ) ( ) ( ( Radance for mrror reflecton ray
E Materal Emsson A Global ambent Lght Source ntensty R Reflecton ntensty K A Materal Ambent K D Materal Dffuse K S Materal Specular K R Materal Reflecton S Lght Shadowed? n Materal Shnness Normalzed vectors: L ntersecton to lght N ntersecton normal V ntersecton to Eye R ntersecton to reflected lght n E K A A ( K D ( N L ) K S ( V R ) ) S K R R