Shading To determine the correct shades of color on the surface of graphical objects.
Why Shading ?
Light and surfaces The color we see is determined by interactions among light sources and surfaces.
Lights, Surfaces, and Imaging Proj. Plane COP Light Source Objects
Modes of Interaction of Light with Materials Specular reflection Diffuse reflection Ambient reflection Note: Any given surface can have some of all three properties.
Phong Model
Phong Model Diffuse Specular Ambient To source To viewer Normal A simple model that can be computed rapidly Has three components Diffuse Specular Ambient Uses four vectors To source To viewer Normal Perfect reflector
The Phong Model
The Combined Effect
Adding up the Components For each light source and each color component, the Phong model can be written (without the distance terms) as I =kd Id l · n + ks Is (v · r )a + ka Ia For each color component we add contributions from all sources
Elements of the Phong Lighting Model: Ambient Reflection The intensity of ambient light is the same at every point on the surface. The percentage of light reflected is given by: Note that the ambient reflection terms can be different for red, green and blue.
Elements of the Phong Lighting Model: Diffuse Reflection Intensity of illumination is dependent upon the incidence angle of light from the source (Lambert’s law): n l q Adding a term for attenuation with distance from source:
Elements of the Phong Lighting Model: Specular Reflection For a shiny surface, most light is reflected around vector r, corresponding to a reflected angle equal to the angle of incidence. If f is the angle between v and r, The exponent is a shininess coefficient Large Small
Light Sources In the Phong Model, we add the results from each light source Each light source has separate diffuse, specular, and ambient terms to allow for maximum flexibility even though this form does not have a physical justification Separate red, green and blue components Hence, 9 coefficients for each point source Idr, Idg, Idb, Isr, Isg, Isb, Iar, Iag, Iab Example: ambient light Ia ,
Material Properties kdr, kdg, kdb, ksr, ksg, ksb, kar, kag, kab Material properties match light source properties Nine absorbtion coefficients kdr, kdg, kdb, ksr, ksg, ksb, kar, kag, kab Shininess coefficient a
Steps in OpenGL shading
Steps in OpenGL shading Enable shading and select model Specify normals Specify material properties Specify lights
Normals Length can be affected by transformations In OpenGL the normal vector is part of the state Set by glNormal*() glNormal3f(x, y, z); glNormal3fv(p); Usually we want to set the normal to have unit length so cosine calculations are correct Length can be affected by transformations Note that scaling does not preserved length glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty
Enabling Shading Once lighting is enabled, glColor() ignored Shading calculations are enabled by glEnable(GL_LIGHTING) Once lighting is enabled, glColor() ignored Must enable each light source individually glEnable(GL_LIGHTi) i=0,1…..
The Complete Phong Model Distance term Ambient term Diffuse term Specular term
Defining a Point Light Source For each light source, we can set an RGB for the diffuse, specular, and ambient parts, and the position GLfloat diffuse0[]={1.0, 0.0, 0.0, 1.0}; GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0}; GLfloat specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos); glLightv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightv(GL_LIGHT0, GL_SPECULAR, specular0);
Distance and Direction The source colors are specified in RGBA The position is given in homogeneous coordinates If w =1.0, we are specifying a finite location If w =0.0, we are specifying a parallel source with the given direction vector The coefficients in the distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms). Change by a= 0.80; glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);
Global Ambient Light Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that disappears when the light is turned off OpenGL allows a global ambient term that is often helpful glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)
Moving Light Sources Light sources are geometric objects whose positions or directions are affected by the model-view matrix Depending on where we place the position (direction) setting function, we can Move the light source(s) with the object(s) Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s) Move the light source(s) and object(s) independently
Material Properties GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; Material properties are also part of the OpenGL state and match the terms in the Phong model Set by glMaterialv() GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shine = 100.0 glMaterialf(GL_FRONT, GL_AMBIENT, ambient); glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialf(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shine);
Front and Back Faces back faces not visible back faces visible The default is shade only front faces which works correctly for convex objects If we set two sided lighting, OpenGL will shade both sides of a surface Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf back faces not visible back faces visible
Shading Model
Polygonal Shading: Flat Shading Each point on the polygon is assigned the same shade. Small differences in shading between adjacent polygons are visible.
Shading in OpenGL: Flat Shading OpenGL uses the normal of the first vertex of a single polygon to determine the color. Requested by: glShadeModel(GL_FLAT);
Shading in OpenGL: Smooth Shading Requested by: glShadeModel(GL_SMOOTH); Lighting calculations will be done at each vertex using the material properties, and the vectors v and l calculated for that vertex. Bilinear interpolation is used to determine color values in the interior of the polygon.
Bilinear Interpolation
Flat vs Smooth Shading
Polygonal Shading: Phong Shading Normals are interpolated across each polygon instead of color values.
Global Rendering Local lighting model Each object is shaded independently. Does not consider the effect of other objects on the final shaded color value. Cannot produce shadows. Global lighting model Ray tracing Radiosity
Ray Tracing : Initial Stage Divide the image plane into pixel-sized areas. Generate a cast ray through each pixel. If the ray goes off to infinity without striking anything Assign background color to the pixel. If the ray strikes a surface Calculate a shade for the point of intersection
Ray-casting model