180 likes | 498 Views
Chapter 6. Shading. Light and Matter Phong Reflection Model Computation of Vectors Polygonal Shading Approximation of a Sphere by Recursive Subdivision Light Sources in OpenGL Specification of Materials in OpenGL Shading of the Sphere Model Global Rendering. Chapter 6. Overview.
E N D
Chapter 6. Shading • Light and Matter • Phong Reflection Model • Computation of Vectors • Polygonal Shading • Approximation of a Sphere by Recursive Subdivision • Light Sources in OpenGL • Specification of Materials in OpenGL • Shading of the Sphere Model • Global Rendering
Chapter 6. Overview • 3-D appearance on 2-D image • possible by “shades” of color • Light-object interaction • Light • point light source • spotlight • distant light source • Object • shape • material properties (color, reflection, transparency(refraction), specular ….) • Shading model (eg, Phong shading model) • Local vs. global lighting models • local: no interaction between object surfaces • computationally cheap (real-time) • global: considering interactions between object surfaces (high realism) • ray tracing and radiosity technique • computationally expensive
Light and Matter • Rendering Equation • mathematical result of recursive reflection of light between surfaces <- multiple interaction among light sources and reflective surfaces [F0601] • can not be solved in general • approximating approaches: radiosity, ray tracing -> too slow to pass polygons through the modeling projection pipeline • simpler rendering model: Phong reflection model • Interaction between Light and Materials [F0604] • specular surface: scattered in a narrow angle • diffuse surface: scattered in all directions • translucent surface: penetrate the surface
Light Sources (1) • Light Source (光原) • objects that are emitting radiant energy • illumination function I(x,y,z,q,f,l) [F0605] • characterization of a general light source • 4 basic types • ambient, point, spotlight, distant • Color Source • RGB model ->independent calculations for each component • Ambient Light (Background Light) • a uniform illumination <- the combination of light reflections from various surfaces • every point receiving same amount, but reflecting differently
Light Sources (2) • Point Sources • ideal case: emits light in all directions • real world [F0608]: large size of light sources • umbra: full shadow • penumbra: partial shadow • Spotlights [F0609] • emits light in a narrow angle • intensity function I = cos e f f : the angle of the direction of the source and S S: a vector at a point on the surface • Distance Light Source • parallel light source [F0612]
Phong Reflection Model (1) • Phong Model • efficient • a close enough approximation to physical reality • use four vectors [F0613] • n : unit normal vector of surface • v :unit direction vector to the viewer • l : unit direction vector to the light source from a position • r : unit direction vector of a perfect reflector, determined by n and l • three types of material-light interactions • ambient + diffuse + specular • Ambient Reflection Ia • reflected light depends on the surface property • Ia = kaLa 0 £ka£ 1 : ambient reflection coefficient La: the intensity of ambient light
Phong Reflection Model (2) • Diffuse Reflection (亂반사) [F0614] • constant over each surface, independent of the viewing direction • Id = kd cosq Ld = kd (In) Ld • attenuation term • Id = kd(In) Ld / (a + bd+cd2)a, b, d : constants • Specular Reflection [F0618] • shiny surface : highlight or bright spot at certain viewing directions • Is = ks cosaf Ls = ks (rv)a Ls a : shineness coefficient • attenuation term • Is = ks(rv) a Ls / (a + bd+cd2) • Total Sum of Three Reflections • I = (kd (In) Ld+ ks (rv)a Ls ) / (a + bd+cd2 ) + kaLa • example [PLATE25]
Computation of Vectors (1) • Normal Vectors • an equation for a plane ax + by + cz + d = 0 • an implicit equation for a curved surface • a parametric form
Computation of Vectors (2) • Angle of Reflection [F0621] r = 2 (l • n) n - l • Use of the Halfway Vector [F0622] h = (l + v) / |l + v| • Transmitted Light [F0623]
Polygonal Shading (1) • Flat Shading (Constant Shading) • assumptions • distant light source : I is constant • distant viewer : v is constant • surface normal vector: n is constant • a single intensity is calculated for each polygon • Mach band [F0628,F0629] • in OpenGL glShadeModel(GL_FLAT) • Gouraud Shading (Interpolative Shading) • procedure • determine the average unit normal vector at each polygon vertex [F0630] • apply an illumination model to each vertex to calculate the vertex intensity • linearly interpolate the vertex intensities over the surface of the polygon • in OpenGL glShadeModel(GL_SMOOTH)
Polygonal Shading (2) • Phong Shading (Normal Vector Interpolation Shading) • procedure • determine the average unit normal vector at each polygon vertex • linearly interpolate the vertex normals over the surface of the polygon [F0633] • apply an illumination model along each scan line to calculate projected pixel intensities for the surface points • produce rendering smoother than those of Gouraud shading • significantly large amount of computational cost
Approximation of a Sphere by Recursive Subdivision • Procedure for Approximating a Unit Sphere • tetrahedron with 4 equilateral triangle • recursive subdivision of a triangle • normalize the new vertices created by bisection to the unit sphere • Program [PR0601]
Light Sources in OpenGL (1) • Up to 8 Light Sources • GL_LIGHT0, …, GL_LIGHT7 glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); • Specification of Light Sources • the position (or direction) glLightfv(GL_LIGHT0, GL_POSITION, light_0_pos); • positional source : position GLfloat light_0_pos[ ] = { 1.0, 2.0, 3.0, 1.0}; • distance source : direction vector GLfloat light_o_pos[ ] = {1.0, 2.0, 3.0, 0.0}; • the amount of ambient, diffuse, specular light GLfloat ambient_0[ ] = {1.0, 0.0, 0.0, 1.0}; // RGBA rep. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_0); glLightfv(GL_LIGHT0, GL_DIFFUSE, deffuse_0); glLightfv(GL_LIGHT0, GL_SPECULAR, specular_0);
Light Sources in OpenGL (2) • Specification of Light Sources • global ambient term independent of the particular light source glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); • distance attenuation model f(d) = 1 / (a + bd + cd2) glLIghtf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, a); • spot light glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f); GL_SPOT_DIRECTION, GL_SPOT_EXPONENT • viewer’s location • default : infinite distance --> easy computation in illumination model glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); • light calculation • glLightModeli(GL_LIGHT_MODEL_TWO_SIDED, GL_TRUE); • calculate the light intensities for both front and back sides
Specification of Materials in OpenGL • Reflectivity Coefficient • RGBA representation GLfloat ambient[ ] = {0.2, 0.2, 0.2, 1.0} • specification of material properties glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv(GL_BACK, GL_SPECULAR, specular); glMaterialf(GL_BACK, GL_SHINESS, 100.0); • self-luminous surface: unaffected and does not affect GLfloat emission[ ] = {0.0, 0.3, 0.3, 1.0}; glMaterialf(GL_FRONT_AND_BACK, GL_EMISSION, emission);
Shading of the Sphere Model • Normal Vector • compute the cross product of two vectors u = (u1, u2, u3) v = (v1, v2, v3) uxv = (u2v3-u3v2, u3v1-u1v3, u1v2-u2v1) • normalize the computed vector n = uxv/|uxv| • Polygon Shading [PR0602] • flat shading • Gouraud shading • need normal vector at each vertex
Global Rendering (1) • Local Lighting Model and Global Lighting Model [F0641] • local: flat, Gouraud, Phong shading • global: ray tracing, radiosity • Ray Tracing [F0644] • good for highly specular surfaces and translucent objects ex. glass balls • determine a ray that passes through the center of each screen-pixel position • construct a binary ray-tracing tree [F0647,F0648] • left branch : reflected ray path • right branch : transmitted ray path • a path is terminated if it reaches the maximum or it strikes a light source • determine the intensity by accumulating the intensity contributions starting at the bottom of the tree • example [PLATE23,PLATE20]
Global Rendering (2) • Radiosity • good for scenes with perfectly diffuse surfaces ex. interiors of building • diffuse-diffuse interactions • procedures • break up the scene into small flat polygon, or patches, each of which can be assumed to be perfectly diffuse and will render in a constant shade [F0649,F0650] • determine form factors that describe how the light energy leaving one patch affects the other in patches pairwise -> a set of linear equations: O(n2), but computed only one time since they are independent of the location of the view • find the global energy balance to determine a color for each polygon surface by using form factors • example [PLATE24]